Proteus教程:步进电机|消息
Proteus教程:步进电机
2023-06-14 11:59:27
(资料图)
视频教程:https://www.bilibili.com/video/BV1fF411K79x
#include#include #define uchar unsigned char#define uint unsigned int#define LCD1602_DATAPINS P0sbit LCD1602_E=P2^2;sbit LCD1602_RW=P2^1;sbit LCD1602_RS=P2^0;sbit MA_1 = P1^0;sbit MA_2 = P1^1;sbit MA_3 = P1^2;sbit MA_4 = P1^3;sbit MB_1 = P1^4;sbit MB_2 = P1^5;sbit MB_3 = P1^6;sbit MB_4 = P1^7;uchar flag;unsigned char code M_A[8]={0xf1,0xf3,0xf2,0xf6,0xf4,0xfc,0xf8,0xf9}; //X反转顺序unsigned char code M_B[8]={0xf9,0xf8,0xfc,0xf4,0xf6,0xf2,0xf3,0xf1}; //X正转顺序/******************************************************************************** 函 数 名 : Delay* 函数功能 : 延时*******************************************************************************/void DDelay(unsigned int t){ unsigned int k; while(t--) {for(k=0; k<80; k++); }}/******************************************************************************** 函 数 名 : Lcd1602_Delay1ms* 函数功能 : 延时函数,延时1ms* 输 入 : c* 输 出 : 无* 说 名 : 该函数是在12MHZ晶振下,12分频单片机的延时。*******************************************************************************/void Lcd1602_Delay1ms(uint c) //误差 0us{ uchar a,b;for (; c>0; c--){ for (b=199;b>0;b--) { for(a=1;a>0;a--); } } }/******************************************************************************** 函 数 名 : LcdWriteCom* 函数功能 : 向LCD写入一个字节的命令* 输 入 : com* 输 出 : 无*******************************************************************************/void LcdWriteCom(uchar com) //写入命令{LCD1602_E = 0; //使能LCD1602_RS = 0; //选择发送命令LCD1602_RW = 0; //选择写入LCD1602_DATAPINS = com; //放入命令Lcd1602_Delay1ms(1);//等待数据稳定LCD1602_E = 1; //写入时序Lcd1602_Delay1ms(5); //保持时间LCD1602_E = 0;}/******************************************************************************** 函 数 名 : LcdWriteData* 函数功能 : 向LCD写入一个字节的数据* 输 入 : dat* 输 出 : 无*******************************************************************************/ void LcdWriteData(uchar dat)//写入数据{LCD1602_E = 0;//使能清零LCD1602_RS = 1;//选择输入数据LCD1602_RW = 0;//选择写入LCD1602_DATAPINS = dat; //写入数据Lcd1602_Delay1ms(1);LCD1602_E = 1; //写入时序Lcd1602_Delay1ms(5); //保持时间LCD1602_E = 0;}/******************************************************************************** 函 数 名 : LcdInit()* 函数功能 : 初始化LCD屏* 输 入 : 无* 输 出 : 无*******************************************************************************/ void LcdInit() //LCD初始化子程序{ LcdWriteCom(0x38); //开显示LcdWriteCom(0x0c); //开显示不显示光标LcdWriteCom(0x06); //写一个指针加1LcdWriteCom(0x01); //清屏LcdWriteCom(0x80); //设置数据指针起点}//按指定位置显示一个字符void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData){ Y &= 0x1; X &= 0xF; //限制X不能大于15,Y不能大于1 if (Y) X |= 0x40; //当要显示第二行时地址码+0x40; X |= 0x80; // 算出指令码 LcdWriteCom(X); //这里不检测忙信号,发送地址码 LcdWriteData(DData);}//按指定位置显示一串字符void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData){ unsigned char ListLength; ListLength = 0; Y &= 0x1; X &= 0xF; //限制X不能大于15,Y不能大于1 while (DData[ListLength]>=0x20) //若到达字串尾则退出 { if (X <= 0xF) //X坐标应小于0xF { DisplayOneChar(X, Y, DData[ListLength]); //显示单个字符 ListLength++; X++; } }}//unsigned char code M_A[8]={0xf1,0xf3,0xf2,0xf6,0xf4,0xfc,0xf8,0xf9}; //X反转顺序//unsigned char code M_B[8]={0xf9,0xf8,0xfc,0xf4,0xf6,0xf2,0xf3,0xf1}; //X正转顺序void Motor_A1(){DDelay(3);MA_1 = 0;MA_2 = 0;MA_3 = 0;MA_4 = 1;DDelay(3);MA_1 = 0;MA_2 = 0;MA_3 = 1;MA_4 = 1;DDelay(3);MA_1 = 0;MA_2 = 0;MA_3 = 1;MA_4 = 0;DDelay(3);MA_1 = 0;MA_2 = 1;MA_3 = 1;MA_4 = 0;DDelay(3);MA_1 = 0;MA_2 = 1;MA_3 = 0;MA_4 = 0;DDelay(3);MA_1 = 1;MA_2 = 1;MA_3 = 0;MA_4 = 0;DDelay(3);MA_1 = 1;MA_2 = 0;MA_3 = 0;MA_4 = 0;DDelay(3);MA_1 = 1;MA_2 = 0;MA_3 = 0;MA_4 = 1;}//unsigned char code M_B[8]={0xf9,0xf8,0xfc,0xf4,0xf6,0xf2,0xf3,0xf1}; //X正转顺序void Motor_A2(){DDelay(3);MA_1 = 1;MA_2 = 0;MA_3 = 0;MA_4 = 1;DDelay(3);MA_1 = 1;MA_2 = 0;MA_3 = 0;MA_4 = 0;DDelay(3);MA_1 = 1;MA_2 = 1;MA_3 = 0;MA_4 = 0;DDelay(3);MA_1 = 0;MA_2 = 1;MA_3 = 0;MA_4 = 0;DDelay(3);MA_1 = 0;MA_2 = 1;MA_3 = 1;MA_4 = 0;DDelay(3);MA_1 = 0;MA_2 = 0;MA_3 = 1;MA_4 = 0;DDelay(3);MA_1 = 0;MA_2 = 0;MA_3 = 1;MA_4 = 1;DDelay(3);MA_1 = 0;MA_2 = 0;MA_3 = 0;MA_4 = 1;}void Motor_B1(){DDelay(3);MB_1 = 0;MB_2 = 0;MB_3 = 0;MB_4 = 1;DDelay(3);MB_1 = 0;MB_2 = 0;MB_3 = 1;MB_4 = 1;DDelay(3);MB_1 = 0;MB_2 = 0;MB_3 = 1;MB_4 = 0;DDelay(3);MB_1 = 0;MB_2 = 1;MB_3 = 1;MB_4 = 0;DDelay(3);MB_1 = 0;MB_2 = 1;MB_3 = 0;MB_4 = 0;DDelay(3);MB_1 = 1;MB_2 = 1;MB_3 = 0;MB_4 = 0;DDelay(3);MB_1 = 1;MB_2 = 0;MB_3 = 0;MB_4 = 0;DDelay(3);MB_1 = 1;MB_2 = 0;MB_3 = 0;MB_4 = 1;}void Motor_B2(){DDelay(3);MB_1 = 1;MB_2 = 0;MB_3 = 0;MB_4 = 1;DDelay(3);MB_1 = 1;MB_2 = 0;MB_3 = 0;MB_4 = 0;DDelay(3);MB_1 = 1;MB_2 = 1;MB_3 = 0;MB_4 = 0;DDelay(3);MB_1 = 0;MB_2 = 1;MB_3 = 0;MB_4 = 0;DDelay(3);MB_1 = 0;MB_2 = 1;MB_3 = 1;MB_4 = 0;DDelay(3);MB_1 = 0;MB_2 = 0;MB_3 = 1;MB_4 = 0;DDelay(3);MB_1 = 0;MB_2 = 0;MB_3 = 1;MB_4 = 1;DDelay(3);MB_1 = 0;MB_2 = 0;MB_3 = 0;MB_4 = 1;}void M_Con(uchar id,uchar dir,uint step){uchar i;if(id == 0){if(dir == 0){for(i=0;i=step2){num = step1 - step2;for(i=0;i
审核编辑:汤梓红 标签: