如何用单片机控制直流电机
的有关信息介绍如下:通过与档判祥单片机相连的按键控制直流电机停启的电路如下图所示,通过P3.6口按键触发启动直流电机,P3.7口的按键触发停止直流电机的运行。由图可冲颂知,当P1.0输出高电平“1”时,NPN型三极管导通,直流电机得电转动;当P1.0输出低电平“0”时,NPN型三极管截止,直流电机停止转动。
通过单片机产生PWM波控制直流电机程序
#include "reg52.h"
#define uchar unsigned char
#define uint unsigned int
uchar code table[10]={0x3f,0x06,0x5b,
0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //共阴数码管显示码(0-9)
sbit xiaoshudian=P0^7;
sbit wei1=P2^4; //数码管位选定义
sbit wei2=P2^5;
sbit wei3=P2^6;
sbit wei4=P2^7;
sbit beep=P2^3; //蜂鸣器控制端
sbit motor = P1^0; //电机控制
sbit s1_jiasu = P1^4; //加速按键
sbit s2_jiansu= P1^5; //减速按键
sbit s3_jiting=P1^6; //停止/开始按键
uint pulse_count; //INT0接收到的脉冲数
uint num=0; //num相当于占空比调节的精度
uchar speed[3]; //四位速度值存储
float bianhuasudu; //当前速度(理论计算值)
float reallyspeed; //实际测得的速度
float vv_min=0.0;vv_max=250.0;
float vi_Ref=60.0; //给定值
float vi_PreError,vi_PreDerror;
uint pwm=100; //相当于占空比标志变量
int sample_time=0; //采样标志
float v_kp=1.2,v_ki=0.6,v_kd=0.2; //比例,积分,微分常数
void delay (uint z)
{
uint x,y;
for(x=z;x>0;x--)
for (y=20;y>0;y--);
}
void time_init()
{
ET1=1; //允许定时器T1中断
ET0=1; //允行搏许定时器T0中断
TMOD = 0x15; //定时器0计数,模式1;定时器1定时,模式1
TH1 = (65536-100)/256; //定时器1值,负责PID中断 ,0.1ms定时
TL1 = (65536-100)%6;
TR0 = 1; //开定时器
TR1 = 1;
IP=0X08; //定时器1为高优级
EA=1; //开总中断
}
void keyscan()
{
float j;
if(s1_jiasu==0) //加速
{
delay(20);
if(s1_jiasu==0)
vi_Ref+=10;
j=vi_Ref;
}
while(s1_jiasu==0);
if(s2_jiansu==0) //减速
{
delay(20);
if(s2_jiansu==0)
vi_Ref-=10;
j=vi_Ref;
}
while(s2_jiansu==0);
if(s3_jiting==0)
{
delay(20);
motor=0;
P1=0X00;
P3=0X00;
P0=0x00;
}
while(s3_jiting==0);
}
float v_PIDCalc(float vi_Ref,float vi_SpeedBack)
{
register float error1,d_error,dd_error;
error1=vi_Ref-vi_SpeedBack; //偏差的计算
d_error=error1-vi_PreError; //误差的偏差
dd_error=d_error-vi_PreDerror; //误差变化率
vi_PreError=error1; //存储当前偏差
vi_PreDerror=d_error;
bianhuasudu=(v_kp*d_error+v_ki*vi_PreError+v_kd*dd_error);
return (bianhuasudu);
}
void v_Display()
{
uint sudu;
sudu=(int)(reallyspeed*10); //乘以10之后强制转化成整型
speed[3]=sudu/1000; //百位
speed[2]=(sudu00)/100; //十位
speed[1]=(sudu0)/10; //个位
speed[0]=sudu; //小数点后一位
wei1=0; //第一位打开
P0=table[speed[3]];
delay(5);
wei1=1; //第一位关闭
wei2=0;
P0=table[speed[2]];
delay(5);
wei2=1;
wei3=0;
P0=table[speed[1]];
xiaoshudian=1;
delay(5);
wei3=1;
wei4=0;
P0=table[speed[0]];
delay(5);
wei4=1;
}
void BEEP()
{
if((reallyspeed)>=vi_Ref+5||(reallyspeed
{
beep=~beep;
delay(4);
}
}
void main()
{
time_init();
motor=0;
while(1)
{
v_Display();
BEEP();
}
if(s3_jiting==0) //对按键3进行扫描,增强急停效果
{
delay(20);
motor=0;
P1=0X00;
P3=0X00;
P0=0x00;
}
while(s3_jiting==0);
}
void timer0() interrupt 1
{
}
void timer1() interrupt 3
{
TH1 = (65536-100)/256; //1ms定时
TL1 = (65536-100)%6;
sample_time++;
if(sample_time==5000) //采样时间0.1ms*5000=0.5s
{
TR0=0; //关闭定时器0
sample_time=0;
pulse_count=TH0*255+TL0; //保存当前脉冲数
keyscan(); //扫描按键
reallyspeed=pulse_count/(4*0.6); //计算速度
pwm=pwm+v_PIDCalc(vi_Ref,reallyspeed);
if(pwm
if(pwm>100)pwm=100;
TH0=TL0=0;
TR0=1; //开启定时器0
}
num++;
if(num==pwm) //此处的num值,就是占空比
{
motor=0;
}
if(num==100) //100相当于占空比调节的精度
{
num=0;
motor=1;
}
}