赞
踩
目录
一、为什么不能直接用单片机IO口驱动电机,要用电机驱动模块?
由于单片机内部结构原因,其等效内阻极大, 导致输出电流极小,尽管I0口输出高电平为5V,但输出功率极小,无法满足直接电机的额定功率,所以我们一般只把单片机当控制器,而不是驱动器。
重点解释:
- 板载5V使能:插上跳帽就可以由“12V供电”口,通过内部的降压模块,给内部的芯片供电;拔出跳帽则由"5V供电"直接给芯片供电,但如果电压过高则会烧坏芯片(不建议使用)
- 通道A/B使能:是控制两个逻辑输入口是否正常工作,插上跳帽则默认一直工作。
- 逻辑输入:控制输出是否正常进行
用7-12V电源给L298n供电,GND连接电源负级和单片机的GND,5V给单片机供电(也可以不接)
不使用通道A/B使能口,不去掉跳帽,向两个逻辑输入口输入PWM信号来控制电机:
用按键控制的代码:(STC89C52RC)
- #include <reg52.h>
- sbit left_wheel_1 = P2^0;
- sbit left_wheel_2 = P2^1;
- sbit right_wheel_1 = P2^2;
- sbit right_wheel_2 = P2^3;
- sbit key1 = P0^1;
- sbit key2 = P0^2;
- sbit key3 = P0^3;
- sbit key4 = P0^4;
- sbit key5 = P0^5;
- sbit key6 = P0^6;
- unsigned char count = 0;
- unsigned char timer1 = 100;
- unsigned char turn_sign;
- #define forword_left 1;
- #define forword_rignt 2;
- #define back_left 3;
- #define back_rignt 4;
- /*定时器T0初始化*/
- void Timer0_Init()
- {
- unsigned char time;
- TMOD = 0x02; //定时器T0设置成8位自动重装载
- time = 92;
- TH0 = 256-time; //定时常数 0.1ms 晶振为11.0592MHz
- TL0 = TH0;
-
- ET0 = 1;
- TR0 = 1;
- EA=1;
- }
-
- /*T0中断初始化*/
- void Time0_Init() interrupt 1
- {
- if(count <= timer1 && count > 0) //高电平时间
- {
- switch(turn_sign)
- {
- case 1:left_wheel_1 = 1;break;
- case 2:right_wheel_1 = 1;break;
- case 3:left_wheel_2 = 1;break;
- case 4:right_wheel_2 = 1;break;
- }
- }
- else
- {
- switch(turn_sign)
- {
- case 1:left_wheel_1 = 0;break;
- case 2:right_wheel_1 = 0;break;
- case 3:left_wheel_2 = 0;break;
- case 4:right_wheel_2 = 0;break;
- }
- }
- count++;
- if(count >= 200) //脉冲周期T = 20ms
- {
- count = 0;
- }
- }
- void delay_ms(unsigned int time_out)//12HZ STC89C52RC
- {
- unsigned int i,j;
- for(i=time_out;i>0;i--)
- for(j=95;j>0;j--);
- }
-
- void Forword();
- void Stop();
- void Forword_left();
- void Forword_right();
- void Back();
- void Back_left();
- void Back_right();
- void main()
- {
- Timer0_Init();//定时器初始化
- while(1)
- {
- /*6个按键控制*/
- if(!key1)
- {
- delay_ms(10);
- if(!key1)
- {
- Forword_left();
- while(!key1);
- Stop();
- }
- }
- if(!key2)
- {
- delay_ms(10);
- if(!key2)
- {
- Forword();
- while(!key2);
- Stop();
- }
- }
- if(!key3)
- {
- delay_ms(10);
- if(!key3)
- {
- Forword_right();
- while(!key3);
- Stop();
- }
- }
- if(!key4)
- {
- delay_ms(10);
- if(!key4)
- {
- Back_left();
- while(!key4);
- Stop();
- }
- }
- if(!key5)
- {
- delay_ms(10);
- if(!key5)
- {
- Back();
- while(!key5);
- Stop();
- }
- }
- if(!key6)
- {
- delay_ms(10);
- if(!key6)
- {
- Back_right();
- while(!key6);
- Stop();
- }
- }
-
- }
- }
- //全速前进
- void Forword()
- {
- left_wheel_1 = 1;
- left_wheel_2 = 0;
- right_wheel_1 = 1;
- right_wheel_2 =0;
- }
- //停止
- void Stop()
- {
- ET0 = 0; //关闭定时器
- TR0 = 0;
- left_wheel_1 = 0;
- left_wheel_2 = 0;
- right_wheel_1 = 0;
- right_wheel_2 =0;
- }
- void Forword_left()
- {
- ET0 = 1; //开启定时器
- TR0 = 1;
- count = 0;//重新计数
- turn_sign = forword_left;//确定标志,详情看中断函数
- left_wheel_2 = 0;
- right_wheel_1 = 1;
- right_wheel_2 =0;
- }
- /***************其他函数和Forword_left函数同理*********************/
- void Forword_right()
- {
- ET0 = 1;
- TR0 = 1;
- count = 0;
- turn_sign = forword_rignt;
- left_wheel_1 = 1;
- left_wheel_2 = 0;
- right_wheel_2 =0;
- }
-
- void Back()
- {
- left_wheel_1 = 0;
- left_wheel_2 = 1;
- right_wheel_1 = 0;
- right_wheel_2 =1;
- }
- void Back_right()
- {
- ET0 = 1;
- TR0 = 1;
- count = 0;
- turn_sign = back_left;
- left_wheel_1 = 0;
- right_wheel_1 = 0;
- right_wheel_2 =1;
- }
- void Back_left()
- {
- ET0 = 1;
- TR0 = 1;
- count = 0;
- turn_sign = back_rignt;
- left_wheel_1 = 0;
- left_wheel_2 = 1;
- right_wheel_1 =0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。