求助:当串口接收到‘1’时 电机正转,接收到‘2’时,电机反转,再接收到‘1’时 步进电机会卡住不动,再发一次‘1’,步进电机才会转动,是什么原因?有什么解决方法吗?
- #include<reg52.h>
- #define uint unsigned int
- #define uchar unsigned char
- //------8拍-----
- uchar code fz[8]={0x10,0x03,0x02,0x06,0x04,0x0c,0x08,0x09}; //
- uchar code zz[8]={0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x10}; //
- uchar state=0;
- uchar both=0;
- unsigned char receiveData='0';
- void UsartConfiguration(); //初始化串口
- void delay(void)
- {
- unsigned char a,b;
- for(a=15;a>0;a--)
- for(b=152;b>0;b--);
- }
- void motor1_zz() //正转函数
- {
- uint j;
- for(j=0;j<8;j++)
- {
- P1=zz[j];
- delay();
- }
- }
- void motor1_fz() //反转函数
- {
- uint j;
- for(j=0;j<8;j++)
- {
- P1=fz[j];
- delay();
- }
- }
- void main()
- {
- UsartConfiguration();
- while(1)
- {
- while(both==0)
- {
- if(receiveData=='1')
- {
- state=1;
- receiveData='0';
- break;
- }
- if(receiveData=='2')
- {
- state=2;
- receiveData='0';
- break;
- }
- P1=0x0f;
-
- }
- while(state==1)
- {
- if(receiveData=='2')
- {
- state=2;
- receiveData='0';
- break;
- }
- if(receiveData=='1')
- {
- state=1;
- receiveData='0';
- break;
- }
- motor1_zz();
-
- }
- while(state==2)
- {
- if(receiveData=='1')
- {
- state=1;
- receiveData='0';
- break;
- }
- if(receiveData=='2')
- {
- state=2;
- receiveData='0';
- break;
- }
- motor1_fz();
-
-
- }
-
- }
- }
- /************************************************************************
- * 函数名 :UsartConfiguration()
- * 函数功能 :设置串口
- * 输入 : 无
- * 输出 : 无
- *************************************************************************/
- void UsartConfiguration()
- {
- SCON=0X50; //设置为工作方式1
- TMOD=0X21; //设置计数器工作方式2 和定时器0
- PCON=0X80; //波特率加倍
- TH1=0XFA; //计数器初始值设置,注意波特率是9600的
- TL1=0XFA;
- ES=1; //打开接收中断
- EA=1; //打开总中断
- TR1=1; //打开计数器
- }
- void Usart() interrupt 4
- {
- receiveData=SBUF;//fa出去接收到的数据
- while(!RI);
- RI = 0;//清除接收中断标志位
- }
复制代码
|