DC Motor Control直流电机控制正反转代码
步进电机、直流电机正反转程序
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar code FFW[]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09};
uchar code REV[]={0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01};
#define S2_ON()(P3&(1<<6))==0X00
#define S3_ON()(P3&(1<<7))==0X00
#define Forward 0
#define Stop 1
#define Backward 2
sbit M_DIR=P1^0;
sbit M_PWM=P1^1;
sbit Key_INC=P3^0;
M_PWM=0;delay_us(400-speed);
}
}
PWM控制
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
char flag=1;
sbit pwmout=P2^0;
if(K3==0)
break;
}
else
{
P0=0xfb;
P1=0x03;
}
}
}
正反转
#include<reg51.h>
#include<intrins.h>
#define INT8U unsigned char
直流电机正反转C程序
直流电机正反转C程序//直流电机正反转C程序#include#define uchar unsigned char#define uint unsigned intsbit dula=P2^6;sbit wela=P2^7;sbit key4=P3^0;sbit key1=P3^1;//sbit set=P3^4;bit flag=0;uchar bai,shi,ge;uint i,count,num;uint disnum;//循环次数uchar code tabledu[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0};void delay_12MHZ_s(uint x){uint j,k,i;for(j=x;j>0;j--)for(k=112;k>0;k--)for(i=1114;i>0;i--);}void delay_ms(uint x){uint j,k;for(j=x;j>0;j--)for(k=112;k>0;k--);}void display_sm()//三位数码管显示循环次数{bai=disnum/100;shi=disnum%100/10;ge=disnum%10;dula=1;if(bai==0)//如果百位是0则不显示百位P0=0xff;elseP0=tabledu[bai];dula=0;P0=0xff;wela=1;P0=0xfe;wela=0;delay_ms(10);dula=1;if(shi==0)//如果十位是0则不显示十位P0=0xff;elseP0=tabledu[shi];dula=0;P0=0xff;wela=1;P0=0xfd;wela=0;delay_ms(10);dula=1;P0=tabledu[ge];P0=0xff;wela=1;P0=0xfb;wela=0;delay_ms(10);}void main(){// set=0;flag=0;TMOD=0x10;TH1=(65536-50000)/256;TL1=(65536-50000)%256;EA=1;ET1=1;EX0=1;IT0=1;while(1){display_sm();if(flag!=0){TR1=1;while(1){key1=0;key4=1;if((flag!=1)||(num>=240))//计时4分钟到或有中断产生结束正转{TR1=0;key4=1;key1=1;break;}}if(flag!=0){TR1=1;while(1){key4=0;key1=1;if((flag!=1)||(num>=60))//计时1分钟到或有中断产生结束反转{num=0;TR1=0;key4=1;key1=1;break;}}}if(flag!=0){delay_12MHZ_s(298);//电机停止约5分钟num=0;disnum++;//1个循环后(正转4分钟+反转1分钟+停止5分钟)显示数据加一if(disnum==999)disnum=0;}}Else//没有按键按下显示电机停止{key1=1;key4=1;}}}void int0 () interrupt 0//外部中断响应一次电机转动方向改变一次{EA=0;delay_ms(2);if(INT0==0){flag=~flag;num=0;disnum=0;}while(!INT0);delay_ms(2);while(!INT0);EA=1;}void time0 () interrupt 3//定时电机转动时间{TH1=(65536-50000)/256;TL1=(65536-50000)%256;count++;if(count==20) {count=0; num++;}}。
C语言实现控制电机加减速正反转(飞思卡尔C代码)
C语言实现控制电机加减速正反转(飞思卡尔C代码)用单片机控制直流电动机的正反转、加减速的程序如何用C语言写参考一下这个例子吧。
#include#define uchar unsigned char#define uint unsigned intsbit PW1=P2^0 ;sbit PW2=P2^1 ; //控制电机的两个输入sbit accelerate=P2^2 ; //调速按键sbit stop=P2^3 ; //停止按键sbit left=P2^4 ; //左转按键sbit right=P2^5 ; //右转按键#define right_turn PW1=0;PW2=1 //顺时针转动#define left_turn PW1=1;PW2=0 //逆向转动#define end_turn PW1=1;PW2=1 //停转uint t0=25000,t1=25000; //初始时占空比为50%uint a=25000; // 设置定时器装载初值25ms 设定频率为20Hz uchar flag=1; //此标志用于选择不同的装载初值uchardflag; //左右转标志uchar count; //用来标志速度档位void keyscan(); //键盘扫描void delay(uchar z);void time_init(); //定时器的初始化void adjust_speed(); //通过调整占空比来调整速度//**********************************//void main(){time_init(); //定时器的初始化while(1){keyscan(); //不断扫描键盘程序,以便及时作出相应的响应}}//*************************************//void timer0() interrupt 1 using 0{if(flag){flag=0;end_turn;a=t0; //t0的大小决定着低电平延续时间TH0=(65536-a)/256;TL0=(65536-a)%256; //重装载初值}else{flag=1; //这个标志起到交替输出高低电平的作用if(dflag==0){right_turn; //右转}else{left_turn; //左转}a=t1; //t1的大小决定着高电平延续时间TH0=(65536-a)/256;TL0=(65536-a)%256; //重装载初值}}voidtime_init(){TMOD=0x01; //工作方式寄存器软件起动定时器定时器功能方式1 定时器0TH0=(65536-a)/256;TL0=(65536-a)%256; //装载初值ET0=1; //开启定时器中断使能EA=1; // 开启总中断TR0=0;}//****************************************//void delay(uchar z) //在12M下延时z毫秒{uintx,y;for(x=z;x>0;x--)for(y=110;y>0;y--);}//******************************// voidkeyscan(){if(stop==0){TR0=0; //关闭定时器0 即可停止转动end_turn;}if(left==0){TR0=1;dflag=1; //转向标志置位则左转}if(right==0){TR0=1;dflag=0; //转向标志复位则右转}if(accelerate==0){delay(5) ; //延时消抖if(accelerate==0){while(accelerate==0) ; //等待松手count++;if(count==1){t0=20000;t1=30000; //占空比为百分之60 }if(count==2){t0=15000;t1=35000; //占空比为百分之70 }if(count==3){t0=10000;t1=40000; //占空比为百分之80 }if(count==4){t0=5000;t1=45000; //占空比为百分之90}if(count==5){count=0;}}}}功能特点:1)总线速度高达40 M Hz,CAN总线:3个1Mbps的CAN总线,兼容CAN2.0 A/B;2)128 KB程序Flash和8 KB DataFlash,用于实现程序和数据存储,均带有错误校正码(E CC);3)可配置A/D:16通道模数转换器;可选8位10位和12位精度,3μs的转换时间4)内嵌MS CAN模块用于CAN节点应用,内嵌支持LIN协议的增强型SIC模块和SPI模块;5)4通道16位计数器,CRG时钟和复位发生器:锁相环、看门狗、实时中断;增强型捕捉定时器;6)出色的低功耗特性,带有中断唤醒功能的10,实现唤醒休眠系统的功能;7)通道PWM:8位8通道或16位4通道PWM,易于实现电机控制。
基于单片机的直流电机控制系统程序代码
基于单片机的直流电机控制系统程序代码#include#define unchar unsigned char#define unint unsigned int#define SW1 0x2e#define SW2 0x2d#define SW3 0x2b#define SW4 0x27#define SW5 0x0f#define ZHENGZHUAN 0#define FANZHUAN 1#define TINGZHI 2#define JIASU 3#define JIANSU 4unsigned char code dispcode[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x84,0xff,0xf7};//显示代码unsigned char dispbitcode[]={0x7f,0xbf,0xdf,0xef};//位选口unsigned char dispbuf[4]={0,0,0,0};unsigned char dispbitcnt;unint i;unchar tp=0;unint count=0;unint n=0;unint xs;unint m;unchar keyvalue;sbit p07=P0^7;sbit P30=P3^0;sbit P31=P3^1;sbit P37=P3^7;sbit P10=P1^0;sbit P11=P1^1;sbit P12=P1^2;sbit P13=P1^3;sbit P15=P1^5;void ledshow();unsigned char keyscan();void delay();void zhengzhuan();void fanzhuan();void tingzhi();void jiasu();void jiansu();void t0(void);void INT_0(void);void Timer_1(void);//主程序void main(void){P30=1;P31=0;TMOD=0x12;//内部中断定时器选择TH0=0x66;//50ms定时TL0=0x50;EA=1;//开中断总开关ET0=1;//开内部中断0ET1=1;//开内部中断1TR0=1;//定时器开始工作TR1=1;//计时器开始工作IT0=1;//外部中断0为下降沿触发EX0=1;//开外部中断0while(1){ledshow();//数码管显示keyvalue=keyscan();//键盘扫描switch(keyvalue){case ZHENGZHUAN://正转模块zhengzhuan();break;case FANZHUAN://反转模块fanzhuan();break;case TINGZHI://停止模块tingzhi();break;case JIASU://加速模块jiasu();break;case JIANSU://减速模块jiansu();break;default: break;}}}//延时20ms程序void delay(){unsigned char i,j;for(i=1;i<=125;i++)for(j=1;j<=50;j++) {}}//键盘扫描程序unsigned char keyscan() {unchar temp=0;P1=0xff;if((P1&0x2f)!=0x2f) {delay();if((P1&0x2f)!=0x2f) { temp=P1&0x2f; switch(temp){case SW1:return 0;break;case SW2:return 1;break;case SW3:return 2;break;case SW4:return 3;break;case SW5:return 4;break;default:break; }}}while((P1&0x2f)!=0x2f);}//数码管显示程序void ledshow(){P2|=0xf0;P0=dispcode[dispbuf[0]]; P2=0xef;for(i=0;i<700;i++);P2|=0xf0;P0=dispcode[dispbuf[1]]; P2=0xdf;p07=0;for(i=0;i<700;i++);P2|=0xf0;P0=dispcode[dispbuf[2]]; P2=0xbf;for(i=0;i<700;i++);P2|=0xf0;P0=dispcode[dispbuf[3]]; P2=0x7f;for(i=0;i<700;i++); dispbuf[3]=xs/1000; dispbuf[2]=(xs%1000)/100;dispbuf[1]=(xs%100)/10; dispbuf[0]=xs%10;}//中断服务程序//pWM调速脉冲中断程序void t0(void)interrupt 1 using 0 { if(count>100)count=0;if(count>tp)P37=0;else P37=1;count++;}//外部中断0计数程序void INT_0(void) interrupt 0 { EX1=0;//关外部中断0n++;//转圈计数加1EX1=1;//开外部中断0}//内部中断1计时计数程序void Timer_1(void) interrupt 3 { TH1=0x65;//50ms定时TL1=0xF3;m++;if(m==20)//50*20=1S{xs=(n*10)/4;m=0;n=0;}}//模块程序void zhengzhuan() {P30=1;P31=0;}void fanzhuan() {P30=0;P31=1;}void tingzhi() {P30=0;P31=0;}void jiasu(){if(tp>99)tp=100;else tp=tp+2;}void jiansu(){if(tp<1)tp=0;else tp=tp-2;}。
bldc电机控制 正弦波 换相 代码案例
bldc电机控制正弦波换相代码案例摘要:一、引言二、BLDC 电机的工作原理三、正弦波换相在BLDC 电机控制中的应用四、BLDC 电机控制代码案例五、总结正文:一、引言BLDC 电机,即无刷直流电机,以其高效、低噪音、长寿命等优点,在众多领域得到了广泛的应用。
为了实现对BLDC 电机的精确控制,正弦波换相技术成为了研究的热点。
本文将详细介绍BLDC 电机控制中正弦波换相的应用,并通过代码案例进行具体阐述。
二、BLDC 电机的工作原理BLDC 电机主要由定子和转子两部分组成。
定子上装有永磁体或绕组,转子则由永磁体或绕组组成。
通过改变定子上的电流方向和大小,可以实现对BLDC 电机的速度、转向和转矩的控制。
三、正弦波换相在BLDC 电机控制中的应用正弦波换相技术是利用正弦波电流来驱动BLDC 电机,通过改变正弦波电流的相位,实现对BLDC 电机的控制。
相较于方波换相,正弦波换相具有更好的控制性能,可以减小电机的震动和噪音,提高电机的效率。
四、BLDC 电机控制代码案例以下是一个简单的BLDC 电机控制代码案例:```c#include <avr/io.h>#include <util/delay.h>// 设定PWM 频率#define PWM_FREQ 1000// 设定电机引脚#define MOTOR_PIN PD6void setup_pwm(void) {// 设置引脚为输出DDRD |= (1 << MOTOR_PIN);// 设置波形生成模式TCCR0A |= (1 << WGM00) | (1 << WGM01); // 设置比较输出模式TCCR0A |= (1 << COM0A1);// 设置PWM 频率OCR0A = (PWM_FREQ / 1000) * 255;}void set_motor_speed(uint8_t speed) {// 设置占空比以控制电机速度OCR0A = speed;}int main(void) {setup_pwm();while (1) {// 循环改变电机速度for (uint8_t i = 0; i < 255; i++) {set_motor_speed(i);_delay_ms(1);}for (uint8_t i = 254; i > 0; i--) {set_motor_speed(i);_delay_ms(1);}}return 0;}```该代码使用Arduino 语法编写,通过设置PWM 频率和占空比,实现对BLDC 电机速度的控制。
直流电机调速程序
#include <reg51.h>#include <absacc.h>#define uchar unsigned char#define uint unsigned int#define Count0 XBYTE[0x1fff] //8254计数器0#define Count1 XBYTE[0x3fff] //8254计数器1#define Con_word XBYTE[0x7fff] //8254控制字#define Con_M P3 //电动机方向控制端口#define Pos_M1 0x02 //电动机1正转#define Re_M1 0x01 //电动机1反转#define Sto_M1 0x03 //电动机1反转#define Pos_M2 0x08 //电动机2正转#define Re_M2 0x04 //电动机2反转#define Sto_M2 0x0c //电动机2反转uint time1=10000,time2=10000; //8254计数器0、计数器1初值uchar M1_flag=0,M2_flag=0;//电机运行标志位/************************函数声明***********************/void delay(uint n); //延时函数void init_T0(void); //定时器T0初始化uchar M_turn(uchar n);//读正反转设置函数uchar key_scan(void);//键盘扫描函数void motor_star(uchar num);//电机启动函数void motor_stop(uchar num);//电机制动函数void motor_inc(uchar num);//电机加速函数void motor_dec(uchar num);//电机减速函数void key_manage(uchar num);//键值处理函数/************************主程序*************************/main(){uchar keynum;init_T0(); /while(1){keynum=key_scan(); //按键监视程序key_manage(keynum); //键盘处理程序}}/************************延时函数*************************/void delay(uint n){uchar i,j;for (i=0;i<n;i++)for (j=19;j>0;j--);}/**********************定时器T0初始化*********************/void init_T0(void){TMOD=0x01; //工作方式1TH0=0x4c; //定时50msTL0=0x00;ET0=1; / EA=1; / TR0=1; / }/********************定时器TO中断服务程序****************/void timer0(void) interrupt 1{TR0=0;TH0=0x4c; //重载定时器T0初值TL0=0x00;Con_word=0x30; //写8254计数器0初值Count0=(uchar)(time1/256);Count0=(uchar)(time1%256);Con_word=0x70; //写8254计数器1初值Count0=(uchar)(time2/256);Count0=(uchar)(time2%256);TR0=1;}/**********************读正反转设置函数********************/uchar M_turn(uchar n){uchar temp,turn;temp=(P3&0x30)>>4;if(n==1){temp&=0x01;switch(temp){case 0x00: /turn=Re_M1;break;case 0x01: /turn=Pos_M1;break;}}else{temp=(temp&0x02)>>1;switch (temp){case 0x00: /turn=Re_M2;break;case 0x01: /turn=Pos_M2;break;}}return(turn);}/**********************电机启动函数***********************/void motor_star(uchar num){uchar temp1,temp2;temp1=Con_M; //读电机控制状态if ((num==1)&(M1_flag!=1)) //电动机1未启动{M1_flag=1; /temp2=M_turn(1); //读电机1设置Con_M=(temp1&0xfc)|temp2;//送电机1控制time1=10000; //设定启动速度}else if((num==2)&(M2_flag!=1)) //若电动机2未启动{M2_flag=1; /temp2=M_turn(2); //读电机2设置Con_M=(temp1&0xfc)|temp2;//送电机1控制time2=10000;//设定启动速度}}/**********************电机制动函数***********************/void motor_stop(uchar num){uchar temp1,temp2;temp1=Con_M; //读电机控制状态if ((num==1)&(M1_flag==1)) //电动机1启动{M1_flag=0; /temp2=Sto_M1; //读电机1设置Con_M=(temp1&0xfc)|temp2;//送电机1控制}else if((num==2)&(M2_flag==1)) //若电动机2启动{M2_flag=0; /temp2=Sto_M2; //读电机2设置Con_M=(temp1&0xfc)|temp2;//送电机1控制}}/***********************电机加速函数**********************/void motor_inc(uchar num){if ((num==1)&(M1_flag==1))//电动机1启动{if(time1>0)time1=time1-2000;}else if((num==2)&(M2_flag==1)) //若电动机2启动{if(time2>0)time2=time2-2000;}}/**********************电机减速函数***********************/void motor_dec(uchar num){if ((num==1)&(M1_flag==1))//电动机1启动{if(time1<20000)time1=time1+2000;}else if((num==2)&(M2_flag==1)) //若电动机2启动{if(time2<20000)time2=time2+2000;}}/**********************键值处理函数*************************/void key_manage(uchar num){switch(num){case 0: /while(P1!=0xff);motor_star(2);break;case 1: /while(P1!=0xff);motor_stop(2);break;case 2: /while(P1!=0xff);motor_inc(2);break;case 3: /while(P1!=0xff);motor_dec(2);break;case 4: /while(P1!=0xff);motor_star(1);break;case 5: /while(P1!=0xff);motor_stop(1);break;case 6: /while(P1!=0xff);motor_inc(1);break;case 7: /while(P1!=0xff);motor_dec(1);break;default:break;}}/**********************键盘扫描函数************************/uchar key_scan(void){uchar i,temp;temp=P1;if (temp!=0xff) //判断有无按键按下{delay(200); //延时去抖动if (temp!=0xff) //判断是否是误动作{for (i=0;i<8;i++) //确定被按键{if ((temp&0x80)==0x80){temp<<=1;}else{return(i); //返回按键键值}}}}return(0x0e);}。
单片机代码-直流电机的调速控制
#include<reg52.h>sbit hc573_sg_le=P2^6; //对用于锁存段数据的573锁存LE端进行定义sbit hc573_bit_le=P2^7; //对用于锁存位选通数据的573锁存LE端进行定义sbit m1=P1^4; //直流电机的控制位sbit s3=P3^6; //按键s3的定义sbit s4=P3^7; //按键s4的定义unsigned int Nt1; //m1为0时,所持续的时间t1在定时器中计数值unsigned char speed_num; //数码管所显示的速度等级unsigned char code display_table[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //0-f 16个数码管显示编码unsigned char code display_en[7]={0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf}; //数码管位选通数据数组,可//对6个数码管的位选通进行//整体控制void display(unsigned char num,unsigned char i){P0=display_table[num]; //将数值的编码送入锁存器的输入端准备段数据的锁存hc573_sg_le=1; // 将段数据送给段数据线hc573_sg_le=0; //锁存段数据,让出P0口以供位选通锁存器使用P0=display_en[i]; //准备点亮第i个数码管hc573_bit_le=1; //将位选通数据送到数码管,数码管亮hc573_bit_le=0; //锁存位选通数据,让出P0口以供段数据锁存器使用}void delay_ms(unsigned int t){unsigned int a,b;for(a=0;a<t;a++){for(b=0;b<113;b++){;}}}void timer0_init() //T0初始化函数{TH0=(65536-Nt1)/256; //装载初值,上电复位默认启动速度等级为9TL0=(65536-Nt1)%256;TMOD=0x01; //定时0工作在方式1ET0=1; //开T0中断EA=1; //开总中断TR0=1; //启动T0}void timer0() interrupt 1 //T0中断服务例程{if(m1==0) //如果当前m1=0,则说明m1=0的定时时间到{m1=1; //现在要翻转m1TH0=(65536-(9216-Nt1))/256; //重新装载m1=1时的定时初值TL0=(65536-(9216-Nt1))%256;}else{m1=0;TH0=(65536-Nt1)/256;TL0=(65536-Nt1)%256;}}void key_scan() //按键扫描函数{s3=1; //读s3前先写1if(s3==0){TR0=0; //判断按键按下时,先关闭定时器,避免中断引起的按键扫描受干扰。
程序15-3:直流电机的单片机控制
ENA IN2 TR0
; ENA=1 ; IN2=0 ; 使能 Timer 0 中断 ; 正转
IN1 R1, #60, RUN TR0 IN1 R1, #0 BUTTON1
; IN1=1 ; 如果 R1 不等于 60,说明转动不够 3 秒,跳到 RUN ; 关 Timer 0 ; IN1=0 ; R1=0 ; 循环 ; 反转
; ENA=0 ; 循环
R3, #60 R4, #248 R4, $ R3, F1
; 延时子程序,30ms源自R1 IN1 TH0, #3CH TL0, #0B0H R4,30H R5, #80 R5, $ R4, D1
; R1 增加 1 ; IN1=0 ; 重装计数初始值 ; 30H 中的数据载入 R4 ; 延时,实现 PWM 信号中的低电平
CALL FILTER ; 调消除开关抖动的子程序,检测到的高电平可 能只是一个由抖动引起的瞬间值,所以应调 FILTER
JNB JMP BUTTON2: JB CALL JNB JMP BUTTON3: JB CALL JNB JMP BUTTON4: JB CALL JNB JMP RUNFOR: MOV ADD MOV MOV MOV TIMER0_LOAD: MOV MOV TH0, #3CH TL0, #0B0H ; 载入计数初始值 3CB0H A, 30H A, #64 30H, A IE,#82H TMOD, #01H ; 30H 上的数据载入 A ; A=A+64 ; 把运行结果存回 30H,作 PWM 信号低电平延时变量 ; 开 Timer 0 中断 ; Timer 0 作定时器,模式 1 CRUISING,BUTTON1 ; 如果 CRUISING 为高电平,就循环检测 FILTER CRUISING,$ RUNCRU ; 调消除开关抖动的子程序 ; 如果 CRUISING 为低电平,就重复执行本行 ; 跌到 RUNCRU,执行空转 BREAK,BUTTON4 FILTER BREAK,$ RUNBRE ; 如果 BREAK 为高电平,就检测 CRUISING ; 调消除开关抖动的子程序 ; 如果 BREAK 为低电平,就重复执行本行 ; 跳到 RUNBRE,执行刹车 BACKWARD,BUTTON3 ; 如果 BACKWARD 为高电平,就检测 BREAK FILTER BACKWARD,$ RUNBAC ; 调消除开关抖动的子程序 ; 如果 BACKWARD 为低电平,就重复执行本行 ; 跳到 RUNBAC,执行反转 FORWARD,$ RUNFOR ; 如果 FORWARD 为低电平,就重复执行本行 ; 跳到 RUNFOR,执行正转
电机正反转简单程序
电机正反转简单程序引言电机正反转是在许多电气控制和机器人领域中常见的需求。
通过编写简单的程序,我们可以实现对电机的正向和反向旋转控制。
本文将介绍如何编写一个简单的程序来实现电机的正反转。
什么是电机正反转?在控制电机时,正反转是指改变电机的旋转方向。
电机正转是指电机按照设定的方向进行旋转,而电机反转则是电机按照相反的方向进行旋转。
通过控制电机的正反转,我们可以实现精确的运动控制和机器人运动的调整。
电机正反转的原理电机正反转的实现原理基于电机的构造和控制电路。
常见的电机类型包括直流电机和交流电机。
无论是直流电机还是交流电机,它们的正反转控制原理都是通过控制电极之间的电流流向来实现的。
在直流电机中,通过改变直流电源的电极连接方式,可以改变电流在电机绕组中的流向,从而改变电机的旋转方向。
在交流电机中,可以通过改变电机的相序来改变绕组中电流的流向,进而改变旋转方向。
编写电机正反转程序的步骤编写电机正反转程序的步骤主要包括以下几个方面:1. 确定电机类型和控制方式首先,我们需要确定使用的电机类型和控制方式。
根据不同的电机类型和控制方式,编写电机正反转程序的方法会有所不同。
例如,直流电机的正反转控制需要改变电机电极的连接方式,而交流电机的正反转控制需要改变电机的相序。
2. 确定控制电路和接口在编写程序之前,我们需要确定控制电路和接口。
控制电路和接口用于将控制信号传输到电机,从而实现对电机正反转的控制。
常见的电机控制电路和接口包括H桥电路、驱动模块和微控制器。
3. 编写程序根据电机类型和控制方式,我们可以编写相应的程序。
程序的主要任务是向电机发送控制信号以实现正反转。
在编写程序时,需要根据电机的规格和特性设置合适的参数,如电机转速、转向等。
4. 测试和调试完成程序编写后,我们需要进行测试和调试,以确保程序能够正常运行。
在测试和调试过程中,我们可以通过观察电机的转动方向和速度来验证程序的正确性。
如果程序存在问题,可以对程序进行调试和修改。
基于单片机的直流电机控制(正反转、开关控制)
基于单片机的直流电机控制(正反转,开关控制)原理图如下:程序如下:/*用电机来代表门的转动情况*/#include <reg51.h>//定义变量sbit kaimen=P0^0;sbit zanting=P0^1;sbit fanxiang=P0^2;sbit P2_0=P2^0;sbit P2_1=P2^1;bit Flag = 1;//定义电机正反向标志//函数声明void motor_turn(void); //正反向控制void Timer0_init(void); //定义定时器0初始化/******************************延时处理***************************/void Delay(unsigned int z){unsigned int x,y;for(x=z;x>0;x--)for(y=110;y>0;y--);}/***************************************************************/void Timer0_int(void) interrupt 1 using 1//定时器0中断处理主要用来处理换方向的时候{TR0 = 0;TL0=(65536-50000)/ 256; //定时50msTH0=(65536-50000)% 256;TR0 = 1;if(Flag == 1)//代表改变方向{P2_0 = 0;P2_1 = 1;}else //方向不变{P2_1 = 0;P2_0 = 1;}}/****************开始转动:人满时候开始转动**************/void motor_start(void){ if(kaimen==1){//Delay(10);if(kaimen==1){P2_0 = 0;P2_1 = 1;}}}/***************有人但是人未满时或者有夹到人的时候暂停*************/void motor_pause(void){ if(zanting==1){Delay(10);if(zanting==1){EA=0;P2_0 = 0;P2_1 = 0;}}}/**********************电机方向转动**************************/ void motor_turn(void)//电机反向转动{if(fanxiang == 1){Delay(10);if(fanxiang == 1){Flag = ~Flag;}while(fanxiang == 1);}}/***********************定时器0初始化**************************/ void Timer0_init(void)//定时器0初始化{TMOD=0x01;TH0=(65536-50000)/ 256;TL0=(65536-50000) % 256;TR0=1;ET0=1;}/**********************主函数***********************/void main(void)//主函数{Timer0_init();while(1){ if(kaimen==1)EA=1; //开始时候开启中断motor_start();motor_pause();motor_turn();}}。
dc_motor_control
DC MOTOR CONTROLRoger Aarenstruproger.aarenstrup@C O N T E N T S Introduction (3)The dc motor model (4)Speed control with pid (6)Continuous control (6)Discrete Control (7)Choosing parameters (9)Hand Code testing (10)Using Simulink Control Design Products (10)Rapid Prototyping (10)Fixed Point (11)Production Code Generation (11)Position control using state feedback pole placement (12)Attempt 1 - state feedback and static gain (12)Attempt 2 – integral action (12)Attempt 3 – Observer (13)Attempt4 – The servo case (13)Considerations (14)Bandwidth (14)Sample rate (16)References (17)INT RO D UC TI ONThis example describes how to develop speed and position control systems for a DC motor with a load. Various methods are used and the focus is on how to model and implement the various parts and not of parameter tuning. An important thing to note is that a good controlleris not just a text book implementation but requires a number of additional parts to work properly. It is a good idea to go through this text together with the models and some control design literature that gives more details about the theory. Please see the references chapter for some suitable books.This text and models come with absolutely no guarantee if you find anything incorrect please let me know:Roger.aarenstrup@THE D C M OT O R M O DE LThe models used here can be downloaded from matlab central with the link; /matlabcentral/fileexchange/loadFile.do?objectId=11829&objectType=file The model is a quite simple linear DC motor model with a flexible load. In the library there are MATLAB files describing a state-space representation of the model. How to derive them is described in many control system books and also in the control system toolbox documentation:/access/helpdesk/help/toolbox/control/getstart/buildmo4.htmlThe model described there doesn’t include the flexible load how to add that is described here and in the models included.I have found that a linear model is good enough for many control systems with DC motors. Modeling is not about making the most detailed model but to make a model good enough for the task.There are a few things worth to notice. I have taken the dc motor parameters from the maxon motors product catalog. In the future I hope that it is possible to download models directly from their web page, just like it now is possible to download CAD models over the mechanics. In the catalog there is no specific value for the mechanical damping. Using the common expression for the mechanical time constant, Tm = bm/Jm, where bm is the mechanical damping and Jm is the rotor inertia doesn’t give the correct no-load-current. This might be due to non-linear parts here. I used simulations to estimate a mechanical damping that gives a close match for the mechanical rise time and no-load-current. If you know more about this, please let me know.The figure below presents the DC motor model and load. The load is represented by a double integrator with inertia. Since it is coupled with the motor rotor through a somewhat flexible link there will be a spring action if the positions of the rotor and the load differ. The difference in position (angle) is thus feed back as a counter torque multiplied by the spring constant. The same is true for the damping. The difference in velocities between the two bodies will damp the system. For details, please see the real model.weFigure, DC motor model with flexible load.SP E E D C O NT R OL WI TH P IDIn this chapter we will use model elaboration to go from a crude model to a discrete implementation of a PID based control system for a DC motor with load.CONTINUOUS CONTROLThis first attempt to control the dc motor and load uses a continuous time (LaPlace representation) PID with approximate derivative, see figure below. See model a_pid_cont.mdl for the complete model. When tuning the parameters it is important to consider the control signal (output from the controller) to make su re it won’t exceed the limits of the amplifier. To meet this for the entire step the controller gain (proportional) has to be quite low, we will see how to improve this further on.Figure, PID with approximate derivative (from a_pid_cont.mdl).In some cases it is not desirable to derive the commanded input signal because it might be a step or similar that will give bad results when derived. In the next model b_pid_cont.mdl a variant of the PID controller is used where only the output of the plant is derived.Figure, PID with approximate derivative of the plant output. Note that the derivative part uses only y, the output from the plant, and not the error. (from b_pid_cont.mdl) Now, lets add some more details to the model. This is model-based-design, MBD, where more and more details are added until the final implementation is reached. And during development the model can be verified continuously by Simulation and later code generation. Let’s add a simple model of an amplifier. The amplifier works as a low-pass filter and a saturation of the output signal. Sometimes it is useful to use an amplifier that can output a voltage larger than the nominal voltage for the dc motor, to improve control during short periods of time. Let’s also add the tachometer model. The resulting model is c_pid_cont.mdl.It is often a good idea not to have a pure step as input because the step can cause spikes and might excite modes in the system. To make the reference signal more smooth a simple low-pas filter is added. However, it is usually a good idea to calculate a function, as polynomials for example, for the input.The main result from a simulation with this more detailed model is that there are now ripple on the output caused by the tachometer.DISCRETE CONTROLTo be able to implement the controller on a micro processor we first need to convert it to discrete time in Simulink. There are a number of different way to do that, the one chosen here is taken from [1]. See d_pid.mdl for a model example. Here sample time colors are turned on showing that the controller part is discrete with a sample time of 100 us. More about choosing sample times later. The sample time is set in a model callback, called when the model is loaded.In the discrete version the derivative part depends only on the plant output, just like the second continuous version. It is also advantageous, in many cases, to let the proportional gainonly to depend on a part of the commanded signal. This factor is also introduced in the discrete controller and is called b.Figure, Discrete PID controller. (From d_pid_disc.mdl)Let’s continue with anti-windup for the integrator. There are several ways to add anti-windup for an integrator. The easiest way is to just limit the internal state of the integrator to a reasonable value, this can be done directly in the Discrete-Time Integrator Block. It is also possible to just stop updating the integrator state when the actuator is saturated. Since windup generally is a problem because there is an actuator (amplifier) that has a bounded (non-linear) output, another way is to take the difference between the input to the actuator and subtract that from the output of the actuator and feed that signal back to the integrator path in the controller. This means that if the actuator is saturated the feedback will decrease the integral action. An advantage with this method is that it can be applied to any kind of actuators, not just actuators with limited output.The last case above is implemented in the next model. The implementation is mainly to demonstrate how to implement anti-windup, since there is not any real windup problem in this model. You can experiment with the input to see if you get windup.The new controller, in the figure below and model file e_pid_cont.mdl, uses a model of the actuator, a saturation block, to get the saturated value. Another way would be to measure the actual value but that would add costs.Figure, Discrete PID with anti-windup. (See e_pid_disc.mdl)The model with this controller, e_pid.mdl, also samples the reference signal from an external source and isolates the sampled parts that will later be implemented on a micro controller.CHOOSING PARAMETERSThere are a number of parameters that have to be chosen for the discrete PID controller; K, Ti, Td, Tt, b, N, Umin, Umax and Ts. Parameter tuning and selection is not the purpose of this tutorial but I include some guidelines.For the controller parameters, K, Ti, Td and Ti, there are several methods to choose from, see [1].Tt, the integrator anti-windup feedback, is related to Ti and can sometimes be equal to it but typically it is 0.1-0.5 Ti [1].Parameter b, how much of the reference signal to be used for the proportional gain, should be in the interval 0.1 to 1.N is typically 3-20 but can be as high as 100.Umin and Umax, the saturation points for the actuator model in the controller, should be as close to the real actuator as possible.Ts, the sample time, is related to many things. For PID controllers it has to be lower than for PI for example. It is also related to disturbances. In the examples above I use 100 us which isprobably faster than necessary.HAND CODE TESTINGNow if you’re not using any advanced tools for code generation and you are using a floatingpoint processor, you can write an equivalent discrete controller in c-code and include it, with an s-function block, in the simulation to verify the implementation. In the model directory there is ac-file with the discrete controller implemented, without anti-windup. You can compile from matlab with:mex sfun_wrp_pid.c handcode_pid.cThe file sfun_wrp_pid.c is an s-function wrapper for the controller handling the interface between Simulink and the controller code.Now you can experiment and verify your final implementation so you are sure it works before messing with the real hardware, use model f_pid_disc.mdl. If you have a fixed-point processor you should first model the controller using Simulink Fixed Point then you can do the same thing with it, see later section about fixed-point.USING SIMULINK CONTROL DESIGN PRODUCTSThere are several tools from the MathWorks that can be used for control design. The most widely used is Control System Toolbox that includes many useful tools. For Simulink there are three products in particular that are the most useful; Simulink Control Design, Simulink Parameter Estimation and Simulink Response optimization. With the response optimization tool for example you can specify a desired time domain response and let Simulink optimize the controller parameters to fit the desired response.RAPID PROTOTYPINGModels don’t always describe the real system per fectly. Before putting effort in the final implementation it is generally a good idea to test it with the real plant. This is done by rapid prototyping where code is automatically generated from Simulink with Real-Time Workshop and downloaded to a target system like the xPC target box. Then you can try the implementation of the controller and tune parameters against the real plant.Figure, example model that can be compiled for the xPC target for rapid-prototyping, g_pid_disc.mdl.If you on the other hand have the controller implemented on the target processor and want totest it you can compile the plant instead of the controller and run the plant on the xPC target against your processor. In this way you can verify your implementation before you have the plantand without damaging it. This is called Hardware-In-The-Loop.FIXED POINTIf you are using a fixed-point processor it is now time to convert the floating point model to fixed point using Simulink Fixed-Point. It is out of the scope here to show that.PRODUCTION CODE GENERATIONThe next step would be to produce code that is suitable for production. It can of course be hand coded, like the one we used before in an s-function but there are a number of big advantages using production code generation with Real-Time Workshop Embedded Coder. It is not just a time saving thing; you also avoid hand coding error that is usually random to the nature. If you have to change your controller later it is a lot easier to just regenerate the code than starting a project to re-write some old hand code. It is also a lot harder to make sure that the final implementation actually matches the simulated version. I Simulink you do bit-accurate simulations.In the next section there is example code produced by Real-Time Workshop Embedded Coder in the Controller_ert_rtw folder.P OSI TI ON C ON TR OL USI NG STATE FE E DBA CK P OLE P LACE M E NT Now we are going to make a position controller instead of speed controller. In this version we will use a different amplifier that controls the current fed to the dc motor instead of the voltage. The current is measured in the amplifier and with a simple feedback the current is controlled. This provides a great advantage since the torque of the dc motor is directly proportional to the current through the torque constant Kt. The result is that we can neglect the electrical part of the dc motor and thus have one state less in our control design, saving computational time and development effort.In the file ss_dc_motor_load.m there is a reduced state space model for this new application. The file also includes the control design code for the models in this part.The idea is to feed all states back with a vector gain L, by doing so it is possible to use a method to place the closed loop poles on desired locations. Pole placement is very efficient in getting desired performance but might not give optimal solutions for power consumption for example. It is also not suitable for higher order systems. Higher order can be 5-7 and up. In these cases an LQG approach might be a better choice. But for the DC motor with load in this case a pole placement approach works fine.ATTEMPT 1 - STATE FEEDBACK AND STATIC GAINThe first approach can be seen in a_ss_controller.mdl, and in the file ss_dc_motor_load.m it is called attempt 1. Here the four closed loop poles are placed on the positive real axis (discrete systems are stable when poles are inside the unit circle) on 0.9875, 0.9863, 0.9850 and 0.9838. For the pole placement algorithm to work well the poles can’t be too close to each other. When simulating only this part, without the Kstat constant, gives a reasonable result. However the static gain is quite high, ca 478.4. Usually it is desirable to have a static gain of 1. By just adding the Kstat = 1/478.4 this is corrected. The Kstat can be obtained by simulation or by calculating the closed loop static gain, as in the ss_dc_motor_load.m file.This gives a quite reasonable result and the design is very easy, compared to PID tuning. However, the static gain will be sensitive to model errors and also there is nothing that compensates for disturbances in this first approach.ATTEMPT 2 – INTEGRAL ACTIONAs always to get a static gain to be 1 and to reduce sensitivity to disturbances we introduce integral action. This integral part should have an anti-windup mechanism similar to the PID version but that is not handled here, take it as an exercise to add it. By introducing an integrator we introduce one more state in our model, see attempt 2 in ss_dc_motor_load.m. The integrator should be connected to the error of the output we need to control, in this case the position of the load. It is, however, more common that an encoder is attached to the rotor of the DC motor than to the load. Also since the static position of the load and the rotor is equal, it is possible to use the rotor encoder instead of the position of the load for the integral action.Now we have one more pole to place because of the integrator. Where should we put it? It is tempting to make it faster (closer to 0) than the other poles not to interfere with the response time of the system. However, by doing so will increase the sensitivity to disturbances significantly.A better choice is to put it slightly slower than the rest. This will make the system responsesomewhat slower also but less sensitive to disturbances. This is acceptable for the control casebut not for the servo case. In the servo case we will not track the reference signal fast enough. So what should we do? If we keep the feed forward gain Kff we will introduce a zero in the closedloop system. By selecting that zero carefully we can use it to cancel the slower pole introduced by the integrator. By doing the symbolic math it turns out that cancellation happens when K ff = Lint / (p int–1). Where L int is the feeback for the integrator and pint is the pole placed for the integrator. See ss_dc_motor_load.m file for how this is done.I would also like to remind you that the Simulink Control Design tool that creates a linear model is a very efficient tool to examine any verify designs. Just select inputs and outputs and create the linear model. Then you can watch poles and zeroes, step response, bode plots etc, with the LTI Viewer.ATTEMPT 3 – OBSERVERIn attempt 2 we got a quite nice result, we have control over the movement and we are not very sensitive to disturbances and modeling errors. However, to control we use information about all four states plus the output we are trying to control. This information is easy to obtain in the model for simulation but in a real system that would requite us to measure all the states which generally is not possible or suitable. The solution is to use an observer. The observer takes the input to the amplifier and the output from the encoder and calculates an estimation of the states. Those states can then be used by our controller. Model c_ss_controller.mdl shows how this can be implemented.It turns out that obtaining an observer from the state-space model of the plant is very similar to designing the feedback gain. See attempt 3 in ss_dc_motor_load.m. An input is added for the measured position and then the same pole placement is applied and we get the observer. Please see reference literature for details. To make the observer work well together with the controller its poles should be placed quite a lot closer to 0 or even in the negative real axis. See c_ss_controller.mdl.Note 1! This case illustrates how efficient simulation tools are for designing control systems. We can design the initial controller and verify it by simulation then add details, model elaboration, such as the observer and simulate again to verify. Without simulation tools we need to implement the entire controller with observer without possibility to verify the implementation nor the parameters for it. If we add rapid prototyping to test the algorithm and tune parameters we have probably saved 90% development time.Note 2! The reason why an observer, with both input and output from the real plant, is used instead of just a model over the plant is that with the feedback K, we are more robust to modeling errors.ATTEMPT4 – THE SERVO CASEThis case is just to show an example of how the server control can be implemented. I refer to literature about how to design these controllers, for example [1]. The example model is named d_ss_controller.mdl.CO NSI DE RATI O NSBANDWIDTHHere we will make three different definitions of bandwidth, see [2] for more details.Definition 1: Bandwidth is the frequency area in which control is effective [ω1 ω2].Usually we want the lower frequency to be 0, since we don’t want any static error, we can then call ω2 = ωB the bandwidth.The word effective is not clear but here we assume that it means that we gain something by using the controller in this area.If T is the closed loop system, S is the sensitivity function (S=1-T), r is the reference input, y is the output and e is the error, e = y – r, we get the following:y = T re = y – r = (T – 1) r = -S rFrom this we see that T should be as close to 1 as possible. –S should be as low as possible, since it tells how much of the error that affects the output. This is very interesting because it gives us two more precise definitions of bandwidth.We start with the one based on y = T r. If we say that control is effective as long as |T(jω)|> - 3 dB, the bandwidth will be the frequency where |T(jω)| crosses – 3 dB from above. |T(jω)| is plotted in the figure below.Fig ure, |T(jω)| for the system in model c_ss_controller.mdl. It is position input to encoderoutput that is plotted.The S and T functions are defined in the ss_dc_motor_load.m file and can be examined with ltiview(S, T).From the figure we can see that the bandwidth, according to the definition above, is somewhere between 50 and 60 rad/s. The bandwidth is directly related to the poles we placed. If you increase the frequency of the poles you will get a higher bandwidth. So with this definitionwe can specify our bandwidth with the poles we select for the controller. This definition is also good since it is similar to how bandwidth is defined in other application areas such as signal processing.Definition 2: Bandwidth, ωBT, is the frequency where |T(jω)| crosses – 3 dB from above.Now let’s examine the sensitivity function instead. The error e = -S r. Since we want the error to be as small as possible we want |S(jω)| to be as small as possible. If we con sider it small when smaller than – 3 dB we get another definition of bandwidth. The figure below shows S and from it we can see that the bandwidth would be approximately 25 rad/s.Figure, |S(jω)| for the system in model c_ss_controller.mdl. It is position input to encoder output that is plotted.Definition 3: Bandwidth, ωBS, is the frequency where |S(jω)| crosses – 3 dB from below.The later definition seems to be the preferable one. Just considering the amplitude of T is not enough; the phase must also be under consideration. If you plot T with its phase diagram you can see that the phase is almost -180 in the area of the bandwidth. This suggests that control there might not improve the system performance. However, considering the sensitivity functioninstead, that we want to be small, and if it is small enough we don’t have to care about the phase.In the next figure the magnitude diagrams of S, T and the open loop system are plotted together. As you can see the open loop systems cross over frequency is just in between the bandwidth frequencies for S and T.Figure, Magnitude diagram of S, T and the open loop systemSAMPLE RATESo what sample rate should be used? It depends on the system dynamics, disturbances etc. In this case we have a bandwidth from the second definition, from T, of 60 rad/s (about 10 Hz). We also have a frequency of the flexible load of about 3000 rad/s. 3000 rad/s is about 477 Hz. According to some (there are many) rule of thumb you should sample 10-40 times faster than the dynamics and that would mean 4.8 – 48 kHz. In the models here I have chosen 100 us or 10 kHzas sample rate.RE FE RE N CE S[1] Åström, Karl, Wittenmark, Björn, Computer Controlled Systems, Prentice Hall, 1990.[2] Skogestad, Postlethwaite, Multivariable Feedback Control, Wiley, 1997.[3] Glad, Ljung, Reglerteori, Studentlitteratur, 1997.。
直流电机正反转、加减速、暂停控制的程序及模拟仿真图
SJMP DISPLAYORG 0003HLJMP BUTTON ; 外部0中断入口地址ORG 000BHLJMP DINGSHI ; 定时中断T0入口地址RS EQU P3.0RW EQU P3.1E EQU P3.4ORG 0030H ; 此次直流电机的设计以LCD字符夜晶的; 显示程序为主程序DISPLAY:SETB EA ; 打开中断总开关SETB EX0 ; 打开外部中断0开关SETB IT0 ; 打开外部中断0下降沿触发MOV TMOD,#01H ; 设置定时工作方式MOV TL0,#0FFH ; 设置定时初值MOV TH0,#0FFHSETB ET0 ; 打开定时中断T0开关CLR P0.5CLR P0.6CLR P0.7MOV DPTR,#TAB ; 夜晶显示的字符首地址MOV R0,#00H ; 脉宽的初值MOV R1,#16 ; "SET SPEED PLEASE"的字符个数MOV R3,#00HMOV R4,#00HLP9:LCALL CHUSHILP2:ACALL BUSYMOV A,#00HMOVC A,@A+DPTRMOV P1,AACALL DATASINC DPTRDJNZ R1,LP2LP3:CJNE R3,#00H,LP4CJNE R4,#00H,LP4LP4: MOV R7,#00HMOV R5,#09H ;ACALL BUSYMOV P1,#0C0HACALL ENABLEMOV DPTR,#MMTABACALL BUSYLP5:MOV A,#00HMOVC A,@A+DPTRMOV P1,AINC DPTR; 中断的标志CURRENT : 的字符个数SETB TR0 ; 定时器T0开始定时ACALL DATASACALL BUSYDJNZ R5,LP5MOV DPTR,#STABMOV A,R2MOV P1,AACALL DATASACALL BUSYMOV A,R3 ; 显示速度的十位MOVC A,@A+DPTRMOV P1,AACALL DATASACALL BUSYMOV A,R4 ; 显示速度的个位MOVC A,@A+DPTRMOV P1,AACALL DATAS ; 使夜晶始终显示当前电机的速度LP8:CJNE R7,#00H,LP7 ; 速度不变时等待LJMP LP8 ; 速度变时重新读入速度LP7: SJMP LP4CHUSHI: ; 使夜晶显示的一些初始设置ACALL BUSYMOV P1,#00000001B ; 清屏并光标复位ACALL ENABLEACALL BUSYMOV P1,#00111000B ; 设置显示模式:8位2行5x7点阵ACALL ENABLE ACALL BUSYMOV P1,#00001111B ; 显示器开、光标开、光标允许闪烁ACALL ENABLEACALL BUSYMOV P1,#00000110B ; 文字不动,光标自动右移ACALL ENABLE ACALL BUSYMOV P1,#80H ; 写入显示起始地址ACALL ENABLE RETENABLE: ; SETB ECLR RSCLR RWCLR ERETDATAS: ; SETB ESETB RSCLR RWCLR ERETBUSY: ; CLR EMOV P1,#0FFHCLR RSSETB RWSETB EJB P1.7,BUSYRETORG 2000HDINGSHI: ; CPL P0.7写入控制命令的子程序写入数据子程序准备写入数据定时中断服务程序JNB P0.7,Z1 ; 周期一定MOV A,#0FFHSUBB A,R0MOV TH0,ASETB TR0RETIZ1: MOV TH0,R0 ; 脉宽SETB TR0RETIBUTTON: ; PUSH ACC CLR EX0CLR EAINC R7 ;MOV A,#0FFHMOV P2,AMOV A,P2JNB ACC.0,AA0JNB ACC.1,KK0JNB ACC.2,ZZJNB ACC.3,FFJNB ACC.4,WW0AJMP QQAA0: CJNE R0,#0FFH, AA1 AJMP QQAA1: MOV A,R0ADD A,#5MOV R0,AAJMP QQKK0: CJNE R0,#00,MM ; AJMP QQ从控制键盘中读取操作命令加速操作减速操作; MM: MOV A,R0 SUBB A,#5MOV R0,AAJMP QQQQ: MOV A,R0 MOV B,#5 DIV ABMOV B,#10DIV ABMOV R3,AMOV R4,BSETB EX0LCALL DELAYLCALL DELAYLCALL DELAYLCALL DELAYSETB EAPOP ACCRETIZZ: SETB P0.5 ; CLR P0.6MOV R2,#2BH ; LCALL DELAYLCALL DELAYLCALL DELAYSETB EX0SETB EAPOP ACCRETIFF: CLR P0.5 ; SETB P0.6正转操作正转标志"+" 反转操作MOV R2,#2DH ; 反转标志"-" LCALL DELAYLCALL DELAYLCALL DELAYSETB EX0SETB EAPOP ACCRETIWW0: CLR P0.5 ; 停止操作CLR P0.6LCALL DELAYLCALL DELAYLCALL DELAYSETB EX0SETB EAPOP ACCRETIDELAY: ; 延时子程序MOV R5,#0E0HMM0: MOV R6,#30H MM1: DJNZ R6,MM1DJNZ R5,MM0RETTAB: DB 53H,45H,54H,20H DB 53H,50H,45H,45H ; "SET SPEED PLEASE" 代码DB 44H,20H,50H,4CHDB 45H,41H,53H,45HSTAB: DB 30H,31H,32H,33H DB 34H,35H,36H,37H ; "0,1,2,3,4,5,6,7" 代码DB38H,39H,41H,42H ; "8,9,A,B,C,D,E,F" DB 43H,44H,45H,46HMMTAB: DB 43H,4FH,52H,52HDB 45H,4EH,54H,20H ; " CURRENT : " 代码DB 3AHEND。
电机正反转英语缩写代码
电机正反转英语缩写代码
电机正转反转
Motor forward rotation and reverse rotation
正转按钮 SBF
反转按钮 SBR
电机正反转是指电机顺时针转动和逆时针转动,广泛应用于日常生活中,如行车、木工用的电刨床、台钻、刻丝机、甩干机、车床等。
电机在日常使用中需要正反转,可以说电机的正反转在广泛使用。
例如行车、木工用的电刨床、台钻、刻丝机、甩干机、车床等。
伴随着接触器的诞生,电机的正反转电路也有了进一步的发展。
可以更加灵活方便的控制电机的正反转,并且在电路中增加了保护电路—互锁和双重互锁。
可以实现低电压和远距离频繁控制。
为工业机器人的发展奠定了基础。
PWM控制电机正反转
}
else //电机反转
{
PWM2=0;
time++;
if(time<count)
{
PWM1=1;
}
else
PWM1=0;
if(time>=100)
{
time=0;
}
}
}
下面的程序功能为单片机I/O口P2^0、P2^1输出1000HZ,占空比为%50,并能过P3^7按键控制正电机的正反转。
/*******************************************************************/
/*程序名:PWM直流电机调速*/
/*晶振:11.00592 MHz CPU型号:STC89C52 */
{
TMOD=0x01; //定时器0工作于方式1
TH0=(65536-10)/256;
TL0=(65536-10)%256;
TR0=1;
ET0=1;
EA=1;
}
/**************定时0中断处理******************/
void timer0_int(void) interrupt 1
uchar time,count=50,flag=1;//低电平的占空比
sbit PWM1=P2^0;//PWM通道1,反转脉冲
sbit PWM2=P2^1;//PWM通道2,正转脉冲
sbit key_turn=P3^7; //电机换向
/************函数声明**************/
/*功能:直流电机的PWM波控制,可以通过按键控制正反转*/
/*****************************************************************/
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
time++;
}
void KEY_CHECK(void)
{
// if(KEY1==0)
//delay();
if(KEY1==0)
{
P1_1=0;
time=0;
if(time==high) //高电平持续时间结束,变低
#include <reg51.h>
typedef unsigned char uchar;
sbit P1_0=P1^0;
sbit P1_1=P1^1;
sbit KEY1 = P1^2;
sbit KEY2 = P1^3;
uchar time=0;
uchar period=15;
EA=1; /*开CPU中断*/
ET0=1; /*开定时器0中断*/
TR0=1;/*启动定时器0*/
while(1) /*等待中断*/
{
KEY_CHECK();
}
}
uchar high=2;
void KEY_CHECK(void);
void delay(void);
/*void timer0() interrupt 1 using 1
{
TH0=0xFF; //定时器初值重装载
TL0=0xF7;
time++;
if(time==high) //高电平持续时间结束,变低
{ P1_0=0;}
else if(time==period) //周期时间到,变高
{ time=0;
else if(time==period) //周期时间到,变高
{ time=0;
P1_1=1;
}
}
else P1_0=1;
//延时程序
{
uchar m,n;
for(m=20;m>0;m--);
for(n=20;n>0;n--);
}
void main()
{
TMOD=0x01; /*定时器0方式1*/
TH0=0xFF; /*定时器装载初值*/
TL0=0xF7;
{
P1_0=0;
}
else if(time==period) //周期时间到,变高
{ time=0;
P1_0=1;
}
P1_0=1;
}
}
*/
void timer0() interrupt 1 using 1
{
TH0=0xFF; /*定时器初值重装载*/
}
else P1_1=1;
//if(KEY2==0)
//delay();
if(KEY2==0)
{
P1_0=0;
time=0;
if(time==high) //高电平持续时间结束,变低
{
P1_1=0;
}