您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 咨询培训 > 步进电机、直流电机正反转程序
步进电机#includereg51.h#defineucharunsignedchar#defineuintunsignedintucharcodeFFW[]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09};ucharcodeREV[]={0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01};sbitK1=P3^0;sbitK2=P3^1;sbitK3=P3^2;//延时voiddelay_ms(uintx){uchart;while(x--)for(t=0;t120;t++);}//正转voidstep_motor_ffw(ucharn){uchari,j;for(i=0;i5*n;i++){for(j=0;j8;j++){if(K3==0)break;P1=FFW[j];delay_ms(25);}}}//反转voidstep_motor_rev(ucharn){uchari,j;for(i=0;i5*n;i++){for(j=0;j8;j++){if(K3==0)break;P1=REV[j];delay_ms(25);}}}//主函数voidmain(){ucharN=3;while(1){if(K1==0){P0=0xfe;step_motor_ffw(N);if(K3==0)break;}elseif(K2==0){P0=0xfd;step_motor_rev(N);if(K3==0)break;}else{P0=0xfb;P1=0x03;}}}正反转#includereg51.h#includeintrins.h#defineINT8Uunsignedchar#defineINT16Uunsignedint#defineS1_ON()(P3&(15))==0X00#defineS2_ON()(P3&(16))==0X00#defineS3_ON()(P3&(17))==0X00#defineForward0#defineStop1#defineBackward2sbitM_DIR=P1^0;sbitM_PWM=P1^1;sbitKey_INC=P3^0;sbitKey_DEC=P3^1;//延时voiddelay_us(INT16Ux){for(;x0;x--)_nop_();}//主函数voidmain(){intspeed=200;while(1){if(S2_ON()){P0=~(1Stop);M_DIR=0;M_PWM=0;continue;}if(S1_ON()){M_DIR=0;P0=~(1Forward);}if(Key_INC==0)speed=speed400?speed+1:400;if(Key_DEC==0)speed=speed0?speed:0;M_PWM=1;delay_us(speed);M_PWM=0;delay_us(400-speed);}}PWM控制#includereg52.h#includeintrins.h#defineucharunsignedchar#defineuintunsignedintcharflag=1;sbitpwmout=P2^0;unsignedcharcount1=0,count2=0,count;sbitICL7135_POL=P3^5;sbitICL7135_BUSY=P3^2;//接外部中断口sbitICL7135_CLK=P3^4;//接计数器口intICL7135_count;//计数存储单元//uintICL7135_out;//A转D后数据的10000倍,存于此单元bitICL7135_flag;//输出正负的位标志floatICL7135_ref=5.0;//定义基准电压变量,方便修改voidInit()//初始化子程序{TMOD=0x25;//T0工作于计数模式EA=1;TL0=0;TH0=0;EX0=1;IT0=1;//下降沿触发ET1=1;TH1=200;TL1=200;TR1=1;}voidmain(){Init();//初始化ICL7135while(1){while(ICL7135_BUSY==0);TR0=1;}}voidint0()interrupt0{floata;//浮点型转整型中间变量TR0=0;//停止计数ICL7135_count=TH0*256+TL0;//16方式计得的数ICL7135_flag=ICL7135_POL;//判断输出是正是负a=ICL7135_count-10000;//正向积分为10000个脉冲ICL7135输出放大10000倍后的采样值if(a0)//此句是为了避免因计数误差而导致a0而出现的错误显示。(前提ICL7135_count必须为有符号变量)a=0;//ICL7135_out=a;//浮点型转整型。count=(a/10000)*255;TL0=0;//重新转载数据TH0=0;}voidtimer1()interrupt3{if(count1count)count1++;elseif(count1==count)pwmout=0;if(count2255)count2++;elseif(count2==255){count1=0;count2=0;pwmout=1;}}
本文标题:步进电机、直流电机正反转程序
链接地址:https://www.777doc.com/doc-2479580 .html