51单片机控制的步进电机C语言程序

上传人:痛*** 文档编号:145699753 上传时间:2022-08-29 格式:DOC 页数:9 大小:68KB
返回 下载 相关 举报
51单片机控制的步进电机C语言程序_第1页
第1页 / 共9页
51单片机控制的步进电机C语言程序_第2页
第2页 / 共9页
51单片机控制的步进电机C语言程序_第3页
第3页 / 共9页
点击查看更多>>
资源描述
51单片机控制的步进电机C语言程序用的是L298驱动的 和ULN2003一样,你把它换成2003就行拉#include unsigned char code table=0xf1,0xf3,0xf2,0xf6,0xf4,0xfc,0xf8,0xf9,0x00,0xf1,0xf9,0xf8,0xfc,0xf4,0xf6,0xf2,0xf3,0x00;unsigned char temp,temp_old;unsigned char key;unsigned char i,j,k,m,s;void delay(int i) for(m=i;m0;m-) for(j=250;j0;j-) for(k=10;k0;k-); void saomiao() P3=0xff; P3_4=0; temp=P3; temp=temp&0x0f; if(temp!=0x0f) for(i=50;i0;i-) for(j=200;j0;j-); temp=P3; temp=temp&0x0f; if(temp!=0x0f) temp=P3; temp=temp&0x0f; switch(temp) case 0x0e: key=1; break; case 0x0d: key=2; break; case 0x0b: key=3; break; case 0x07: key=4; break; temp=P3; temp=temp&0x0f; while(temp!=0x0f) temp=P3; temp=temp&0x0f; P3=0xff; P3_5=0; temp=P3; temp=temp&0x0f; if(temp!=0x0f) for(i=50;i0;i-) for(j=200;j0;j-); temp=P3; temp=temp&0x0f; if(temp!=0x0f) temp=P3; temp=temp&0x0f; switch(temp) case 0x0d: key=5; break; case 0x0b: key=6; break; case 0x07: key=7; break; temp=P3; temp=temp&0x0f; while(temp!=0x0f) temp=P3; temp=temp&0x0f; void main(void) while(1) saomiao(); if(key=1) P1=0; P2=0; saomiao(); if(key=2) temp_old=key; for(s=0;s8;s+) P2=tables; P1_4=0; delay(13); saomiao(); if(key!=temp_old) P1_4=1; break; if(key=3) temp_old=key; for(s=0;s8;s+) P2=tables; P1_5=0; delay(5); saomiao(); if(key!=temp_old) P1_5=1; break; if(key=4) temp_old=key; for(s=0;s8;s+) P2=tables; P1_6=0; delay(20); saomiao(); if(key!=temp_old) P1_6=1; break; if(key=5) temp_old=key; for(s=9;s17;s+) P2=tables; P1_7=0; delay(13); saomiao(); if(key!=temp_old) P1_7=1; break; if(key=6) temp_old=key; for(s=9;s17;s+) P2=tables; P1_5=0; delay(5); saomiao(); if(key!=temp_old) P1_5=1; break; if(key=7) temp_old=key; for(s=9;s17;s+) P2=tables; P1_6=0; delay(20); saomiao(); if(key!=temp_old) P1_6=1; break; C语言程序源代码#include / 51寄存器定义#include intrins.h#define control P1 /P1_0:A相,P1_1:B相,P1_2:C相,P1_3:D相#define discode P0 /显示代码控制端口#define uchar unsigned char /定义无符号型变量#define uint unsigned int sbit en_dm=P30; /显示代码锁存控制sbit en_wk=P31; /位控锁存控制uchar code corotation4= 0x03,0x06,0x0c,0x09;/电机正转uchar code rollback4=0x0c,0x06,0x03,0x09; /电机反转uchar code tab10=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f;/显示字段uint code Levspeed5=500,400,300,200,100;/电机速度等级uchar Hscan,speedcount;/Hscan行扫描,speedcount 速度等级计数uint uu; /频率计数uint step,setstep; /step:电机步伐计数,setstep:手动设置电机步伐uint speed=500; /电机初始速度uchar count; uchar flag5;uchar butcount; /按键次数/*/flag0 正转标志/flag1 反转标志/flag2 加速标志/flag3 减速标志/flag4 设置标志/*Delay1mS(unsigned int tt) /延时1ms “Delay1mS”延时子程序,用循环语句延时。 unsigned char i; while(tt-) for(i=113;i0;i-);keyscan() /键盘扫描 “keyscan”键盘扫描子程序,用于寻找是否有按键按下。 P2=0xf0; /把oxfe赋值给P2口 /将按键控制口置于未按键的状态.单片机输出/口假设不是按键按下状态. if(P2!=0xf0) /如果P2口的值不等于0xfe /检查是否有按键按下.如果有,处理内的事 /有按键按下. Delay1mS(150);/调用延时函数/有按键按下,避开无效读码值的时间.,或者是脉冲长度 Hscan=0xfe; /将Hscan赋值,初始遥控值是要置高电平的. P2=Hscan; / while(Hscan&0x10)!=0) /检查X10口是否有键按下。未按下是1, /在这显示出你的按键口是P2_4.即检查P2_4是高电平,无/键按下,低电平有键按下。 P2=Hscan; if(P2&0xf0)!=0xf0) /检查按键脉冲是否是变化, return P2;else Hscan=(Hscan=5) /输入的数字超过5个就置0,无效 butcount=0; setstep=0; break;case 0xde: /按键8 /读取的码值=0xdE,则是按键“8”的代码 /其他同”7” /while(P2=0xde); setstep=setstep*10+8; butcount+; step=setstep; if(butcount=5) butcount=0; setstep=0; break; case 0xbe: /按键9 /同上”7” /while(P2=0xbe); setstep=setstep*10+9; butcount+; step=setstep; if(butcount=5) butcount=0; setstep=0; break; case 0x7e: /按键正转 /正转按键识别 while(P2=0x7e); /等待按键松开/一值按住电机是不转的,放开后才转.有检查/P2口的状态值 flag0=0xff; /开启正转标志,关闭反转/置正转标志. flag1=0x00; /清除反转标志. butcount=0; speedcount=0; speed=500; /置电机的转速. if(!flag4) step=0; TR0=1; break; case 0xed: /按键4 /同上”7” while(P2=0xed); setstep=setstep*10+4; butcount+; step=setstep; if(butcount=5) butcount=0; setstep=0; break; case 0xdd: /按键5 /同上”7” /while(P2=0xdd); setstep=setstep*10+5; step=setstep; butcount+; if(butcount=5) butcount=0; setstep=0; break;
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 成人自考


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!