- #include"reg52.h"
- #include"intrins.h"
-
- typedef unsigned int u16;
- typedef unsigned char u8;
- #define GPIO_MOTOR P1;//定义步进电机的IO口
- u8 code FFW[8]={0xf4,0xf5,0xf1,0xf9,0xf8,0xfa,0xf2,0xf6}; // 电机正转
- u8 code FFZ[8]={0xf2,0xfa,0xf8,0xf9,0xf1,0xf5,0xf4,0xf6}; //电机反转
- u8 Speed=0;
- u8 direction=0;
- u8 receiveData='0';
- void UsartConfiguration(); //函数的声明
- void Motor();
- void delay(u16 i)
- {
- while(i--);
- }
- void main()
- {
- UsartConfiguration();
- Speed=180;
- while(1)
- {
- if(receiveData=='1') //串口接收到'1',电机正转
- {
- direction=1;
- receiveData='0';
- }
- if(receiveData=='2') //串口接收到'2',电机反转
- {
- direction=2;
- receiveData='0';
- }
- if(receiveData=='3') // 串口接收到'3',电机暂停
- {
- P1=0;
- receiveData='0';
- }
- if(receiveData=='4') //串口接收到'4',电机开始
- {
- P1=1;
- receiveData='0';
- }
- if(receiveData=='5') //串口接收到'5',电机加速
- {
- Speed=20;
- receiveData='0';
- Speed=+180;
- }
- if(receiveData=='6') //串口接收到'6',电机减速
- {
- Speed=-20;
- receiveData='0';
- Speed=+180;
-
- }
- }
- Motor();
- direction=0;
- }
- void UsartConfiguration()
- {
- SCON=0X50; //波特率是4800
- TMOD=0X20;
- PCON=0X80;
- TH1=0XF3;
- TL1=0XF3;
- ES=1;
- EA=1;
- TR1=1;
- }
- void Usart() interrupt 4
- {
- receiveData=SBUF;//出去接收到的数据
- RI = 0;
- SBUF=receiveData;
- while(!TI);
- TI=0;//清除接收中断标志位
- }
- void Motor()
- {
- u8 i;
- for(i=0;i<8;i++)
- {
- if(direction==1)
- {
- P1=FFW[i];
- }
- if(direction==2)
- {
- P1=FFZ[i];
- }
- delay(Speed);
- }
- }
复制代码 |