This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

急!!大神,我想问个问题,如何用按键控制步进电机正反转,我自己编了条程序,还请大神帮忙看看为什么按键控制没用,非常感谢!!!



#ifndef PART_TM4C123GH6PM
#define PART_TM4C123GH6PM
#endif

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <inc\tm4c123gh6pm.h>
#include "driverlib/ssi.h"
#include "driverlib/rom.h"
#include "inc/hw_memmap.h"
#include "driverlib/gpio.h"
#include "driverlib/sysctl.h"

#define data GPIO_PORTB_AHB_DATA_R

unsigned int i,j,k;
unsigned int N=512;
unsigned char eight_pos[8]={0x06,0x07,0x03,0x0b,0x09,0x0d,0x0c,0x0e}; /*八拍驱动正转*/
unsigned char eight_rev[8]={0x0e,0x0c,0x0d,0x09,0x0b,0x03,0x07,0x06}; /*八拍驱动正转*/
void delay(unsigned int z);
void m_eight_pos();
void m_eight_rev();


int main(void)
{

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);

GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1|GPIO_PIN_2 |GPIO_PIN_3 );

GPIOPinTypeGPIOInput(GPIO_PORTA_BASE, GPIO_PIN_2 | GPIO_PIN_3);


while(1)
{

if((GPIOPinRead(GPIO_PORTB_BASE, GPIO_PIN_2 ))==0)
{
m_eight_pos(); //八拍驱动正转360度
}
// delay(200);

else if((GPIOPinRead(GPIO_PORTB_BASE, GPIO_PIN_3))==0)
{
m_eight_rev(); //八拍驱动反转360度
}
// delay(200);

}

}
void delay(unsigned int z) /*延时z毫秒*/
{
unsigned int x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--) ;
}
/******************************八拍驱动正转 (N*22.5/32)*************************************/
void m_eight_pos()
{
for(k=0;k<N;k++)
{

for(i=0;i<8;i++) //八拍一个脉冲转子转动5.625/2度,八拍共 22.5度

{
GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_0 | GPIO_PIN_1|GPIO_PIN_2 |GPIO_PIN_3,(eight_pos[i]&0x01)|(eight_pos[i]&0x02)|(eight_pos[i]&0x04)|(eight_pos[i]&0x08));

delay(20);
}


}
}
/******************************八拍驱动反转 (N*22.5/32)*************************************/
void m_eight_rev()
{
for(k=0;k<N;k++)
{

for(i=0;i<8;i++) //八拍一个脉冲转子转动5.625/2度,八拍共22.5度

{
GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_0 | GPIO_PIN_1|GPIO_PIN_2 |GPIO_PIN_3,(eight_rev[i]&0x01)|(eight_rev[i]&0x02)|(eight_rev[i]&0x04)|(eight_rev[i]&0x08));
delay(20);
}
}
}