如何用单片机控制驱动直流电机?当我们还是个孩子的时候,玩的四驱动车给上边的直流电动机装上电池就嗖嗖的跑了,这是直流电机最简单的控制方式,只要调整正极和负极就能改变电机的转向。在单片机控制时需要加驱动电路,我至今还没驱动起直流电机,就是因为之前不知道怎么来连接驱动电路。
什么是单片机:
单片机又称单片微控制器,它不是完成某一个逻辑功能的芯片,而是把一个计算机系统集成到一个芯片上。相当于一个微型的计算机,和计算机相比,单片机只缺少了I/O设备。概括的讲:一块芯片就成了一台计算机。它的体积小、质量轻、价格便宜、为学习、应用和开发提供了便利条件。同时,学习使用单片机是了解计算机原理与结构的最佳选择。单片机的使用领域已十分广泛,如智能仪表、实时工控、通讯设备、导航系统、家用电器等。各种产品一旦用上了单片机,就能起到使产品升级换代的功效,常在产品名称前冠以形容词——“智能型”,如智能型洗衣机等。单片机更详细介绍请看《什么是单片机?这一篇文章让你彻底搞懂》这篇文章
控制直流电机的转速的是用PWM波形来控制,就是控制高电平持续时间在一个周期中所占的比例,这个有个专用的名词叫占空比。
用单片机得到PWM波形有三种方式
方式1:利用软件延时,当高电平延时时间到时的时候,对I/O口电平取反,然后咋延时,当低电平延时时间到后,在对I/O口进行取反,如此循环就得到PWM波形
方式2:利用的是定时器延时,控制方法同上
方式3:利用单片机自带的PWM控制器
示例代码:得到PWM波形
//利用软件延时:并且用键盘扫描得到不同的PWM
#include
#define uint unsigned int
#define uchar unsigned char
sbit PWN = P1^1;
sbit key1 = P3^1;
sbit key2 = P3^2;
uchar num;
uint yanshi;
uchar code table[]={0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e};
void delayms(uint z)
{
uint x,y;
for(x = z;x>0;x--)
for(y=110;y>0;y--);
}
void main()
{
while(1)
{
P3 = 0xff;
if(key1 ==0)
{
delayms(2);
if(key1 ==0)
{
while(!key1);
num++;
if(num ==7)
{
num =0;
}
yanshi = 200*num;
}
}
switch(num)
{
case 1:
{
PWN = ~PWN;
P0 = table[num];
delayms(yanshi);
}
break;
case 2:
{
PWN = ~PWN;
P0 = table[num];
delayms(yanshi);
}
break;
case 3:
{
PWN = ~PWN;
P0 = table[num];
delayms(yanshi);
}
break;
case 4:
{
PWN = ~PWN;
P0 = table[num];
delayms(yanshi);
}
break;
case 5:
{
PWN = ~PWN;
P0 = table[num];
delayms(yanshi);
}
break;
case 6:
{
P0 = table[num];
PWN = 1;
}
break;
}
}
}
示例代码2:
#include
#define uint unsigned int
#define uchar unsigned char
#define TIME 600
sbit led1 = P1^1;
void delay(uint xx)
{
while(xx--);
}
void main()
{
uint x;
while(1)
{
for(x = 0;x
{
led1 = 0;
delay(TIME-x);
led1 = 1;
delay(x);
}
delay(65535);
for(x=TIME;x>0;x--)
{
led1 = 0;
delay(TIME-x);
led1 = 1;
delay(x);
}
}
}
步进电机:
步进电机是将电脉冲信号转变为角位移和线位移的开环控制元件,在非超载的情况下,电机的转速,停止的位置,只取决于脉冲信号的频率数,和脉冲数,而不受负载变化的影响
基本的步进电机的驱动程序:
#include
#define uint unsigned int
main()
{
uint i,j;
while(1)
{
P1 = 0x01;//0000 0001
for(i = 0;i<800;i++);
P1 = 0x02;//0000 0010
for(i = 0;i<800;i++);
P1 = 0x04;//0000 0100
for(i = 0;i<800;i++);
P1 = 0x08;//0000 1000
for(i = 0;i<800;i++);
}
}
综合程序;用按键实现步进电机的启动,停止,增速,减速,并在数码管上显示出来:
#include
#define uint unsigned int
#define uchar unsigned char
sbit dula = P2^7;
sbit wela = P2^6;
sbit key1 = P3^3;//这里是解决我的单片机上按键坏掉的问题
sbit key2 = P2^4;
sbit key3 = P2^5;
sbit key4 = P2^1;
uchar aa,flag,speed,flag1,i;//flag 是用来标记启动和停止的标志位
//flag1 是用来标记正转还是反转的标志位
uchar code table[]={0x01,0x02,0x04,0x08};
uchar code table1[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void init()
{
TMOD = 0x01;
TH0 = (65536-2400)/256;
TL0 = (65536-2400)%256;
EA = 1;
ET0 = 1;
}
void delay(uint z)
{
uint x ,y;
for(x = z;x>0 ;x--)
for(y = 110 ;y>0;y--);
}
void display1()//on显示函数
{
wela = 1;
P0 = 0xf7;
wela = 0;
P0 = 0;
dula = 1;
P0 = 0x3f;
dula = 0;
delay(5);
P0 = 0xff;
wela = 1;
P0 = 0xef;
wela =0;
P0 = 0;
dula = 1;
P0 = 0x54;
dula = 0;
delay(5);
}
void display2()//off显示函数
{
wela = 1;
P0 = 0xf7;
wela = 0;
P0 = 0;
dula = 1;
P0 = 0x3f;
dula = 0;
delay(5);
P0 = 0xff;
wela = 1;
P0 = 0xef;
wela =0;
P0 = 0;
dula = 1;
P0 = 0x71;
dula = 0;
delay(5);
wela = 1;
P0 = 0xdf;
wela = 0;
P0 = 0;
dula = 1;
P0 = 0x71;
dula = 0;
delay(5);
P0 = 0xff;
}
void display()//显示函数
{
uchar gw,sw;
gw = speed%10;
sw = speed/10;
wela = 1;
P0 = 0xfe;
wela = 0;
P0 = 0;
dula = 1;
P0 = table1[sw];
dula = 0;
delay(5);
P0 = 0xff;
wela = 1;
P0 = 0xfd;
wela =0;
P0 = 0;
dula = 1;
P0 = table1[gw];
dula = 0;
delay(5);
}
void key_scan()//键盘扫描函数
{
P3 = 0xff;
if(key2==0)
{
delay(5);
if(key2==0)
{
while(!key2)
{
{
display2();
}
flag = 0;
}
}
if(key1==0)
{
delay(5);
if(key1 ==0)
{
while(!key1)
{
display1();
display();
}
flag = 1;//步进电机启动
}
}
if(flag==1)
{
if(key4 == 0)
{
delay(5);
if(key4==0)
{
while(!key4)
{
display();
display1();
}
speed --;
flag1 = 1;
if(speed ==-1)
speed = 20;
}
}
if(key3==0)
{
delay(5);
if(key3==0)
{
while(!key3)
{
display();
display1();
}
speed ++;
flag1 = 0;
if(speed==21)
{
speed = 1;
}
}
}
}
}
main()
{
init();
while(1)
{
key_scan();
if(flag == 1)
{
TR0 = 1;
display();
display1();
}
if(flag == 0)
{
TR0 = 0;
display2();
}
}
}
void time0() interrupt 1
{
TH0 = (65536-2400)/256;
TL0 = (65536-2400)%256;
aa ++;
if(aa ==speed)
{
aa = 0;
if(flag1 == 1)//正转的标志
{
i++;
if(i ==4)
i = 0;
P1 = table[i];
}
if(flag1 == 0)//反转的标志
{
i --;
if(i == -1)
i = 3;
P1 = table[i];
}
}
}