如何用單片機控制驅動直流電機?當我們還是個孩子的時候,玩的四驅動車給上邊的直流電動機裝上電池就嗖嗖的跑了,這是直流電機最簡單的控制方式,只要調整正極和負極就能改變電機的轉向。在單片機控制時需要加驅動電路,我至今還沒驅動起直流電機,就是因為之前不知道怎么來連接驅動電路。
什么是單片機:
單片機又稱單片微控制器,它不是完成某一個邏輯功能的芯片,而是把一個計算機系統集成到一個芯片上。相當于一個微型的計算機,和計算機相比,單片機只缺少了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];
}
}
}