单片机控制28BYJ-48步进电机驱动
单片机源程序如下:
- #include<reg52.h>
- #include<intrins.h>
-
- unsigned char CCW[8]={0x80,0xC0,0x40,0x60,0x20,0x30,0x10,0x90}; //逆时钟旋转相序表
- unsigned char CW[8]={0x90,0x10,0x30,0x20,0x60,0x40,0xC0,0x80}; //正时钟旋转相序表
- //unsigned char tab[] = {0x0f};
- sbit K1 = P3^0; //反转按键
- sbit K2 = P3^1; //正转按键
- sbit K3 = P3^2; //停止按键
- sbit K4 = P3^3; // 蜂鸣器
- unsigned char K1_Flag=0,K2_Flag=0,K3_Flag=0,K4_Flag=0;
- void delay_ms(unsigned int z) //@11.0592MHz
- {
- unsigned char i, j;
- do
- {
- _nop_();
- _nop_();
- _nop_();
- i = 11;
- j = 190;
- do
- {
- while (--j);
- } while (--i);
- }while (--z);
- }
- void delay_us(unsigned int z) //@11.0592MHz
- {
- do
- {
- _nop_();
- _nop_();
- _nop_();
- }while(--z);
- }
- void main(void)
- {
- unsigned char i;
- while(1)
- {
- if(K1 == 0)
- {
- delay_ms(5);
- {
- if(K1 == 0)
- {
- K1_Flag = ~K1_Flag;
- K2_Flag = 0;
- K3_Flag = 0;
- K4_Flag = 0;
- }
- while(K1 == 0);
- }
- }
- else if(K2 == 0)
- {
- delay_ms(5);
- {
- if(K2 == 0)
- {
- K2_Flag = ~K2_Flag;
- K1_Flag = 0;
- K3_Flag = 0;
- K4_Flag = 0;
- }
- while(K2 == 0);
- }
- }
- else if(K3 == 0)
- {
- delay_ms(5);
- {
- if(K3 == 0)
- {
- K3_Flag = ~K3_Flag;
- K1_Flag = 0;
- K2_Flag = 0;
- K4_Flag = 0;
- }
- while(K3 == 0);
- }
- }
- else if(K4 == 0)
- {
- delay_ms(5);
- {
- if(K4 == 0)
- {
- K4_Flag = ~K4_Flag;
- K1_Flag = 0;
- K2_Flag = 0;
- K3_Flag = 0;
- }
- while(K4 == 0);
- }
- }
- if(K1_Flag)
- {
- for(i=0;i<8;i++) //旋转45度
- {
- P2=CW[i];
- delay_us(80); //调节转速
- P0 = 0XC6;
- P2 &= 0XF0;
- P2 |= 0X02;
- P0 = 0XF9;
- P2 &= 0XF0;
- P2 |= 0X04;
- }
- }
- else if(K2_Flag)
- {
- for(i=0;i<8;i++) //旋转45度
- {
- P2=CW[i];
- delay_us(200); //调节转速
- }
- P0 = 0XC6;
- P2 &= 0XF0;
- P2 |= 0X02;
- P0 = 0Xa4;
- P2 &= 0XF0;
- P2 |= 0X04;
- }
- else if(K3_Flag)
- {
- for(i=0;i<8;i++) //旋转45度
- {
- P2=CW[i];
- delay_ms(1); //调节转速
- }
- P0 = 0XC6;
- P2 &= 0XF0;
- P2 |= 0X01;
- P0 = 0XB0;
- P2 &= 0XF0;
- P2 |= 0X02;
- }
- else if(K4_Flag)
- {
- for(i=0;i<8;i++) //旋转45度
- {
- P2=CW[i];
- delay_ms(5); //调节转速
- }
- P0 = 0XC6;
- P2 &= 0XF0;
- P2 |= 0X01;
- P0 = 0X99;
- P2 &= 0XF0;
- P2 |= 0X02;
- }
- }
- }
复制代码
所有资料51hei提供下载:
28BYJ-48步进电机驱动.rar
(30.14 KB, 下载次数: 77)
|