avr或51单片机pwm控制小车左右电机并调速

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

avr或51单片机pwm控制小车左右电机并调速,红外对管检测的程序//Crystal:8.0000Mhz#includeiom16v.h#includemacros.h#defineucharunsignedchar#defineuintunsignedintuintdiscrepancy=0;//functiondeclarationvoidport_init(void);voidmotor(ucharindex,ucharspeed);//inputPWMwavevoidsensor_state(void);//gettherunningconditionvoidrevise_to_line(void);//0=runforward,1=left,2=right,3=severleft,4=severrightvoiddelayms(uintMS);//callthisroutinetoinitializeallperipherals调用这个例程初始化所有外设voidinit_devices0(void){//stoperrantinterruptsuntilsetupCLI();//disableallinterruptstimer0_init();MCUCR=0x00;GICR=0x00;TIMSK=0x02;//timerinterruptsourcesSEI();//re-enableinterrupts//allperipheralsarenowinitialized}//initializeT/C1voidtimer1_init(void){TCCR1B=0x00;//停止定时器TIMSK|=0x00;//中断允许TCNT1H=0x00;TCNT1L=0x00;//初始值OCR1AH=0x00;OCR1AL=0xF0;//匹配A值OCR1BH=0x00;OCR1BL=0xF0;//匹配B值ICR1H=0xFF;ICR1L=0xFF;//输入捕捉匹配值TCCR1A=0xA1;TCCR1B=0x01;//启动定时器}//callthisroutinetoinitializeallperipheralsvoidinit_devices1(void){CLI();//禁止所有中断timer1_init();MCUCR=0x00;MCUCSR=0x80;//禁止JTAGGICR=0x00;SEI();//开全局中断}//PWM调速,通过改变占空比,周期性地开闭使能端,调节电机的有效电压。//usePD4,PD5tooutputPWM,speed(0~255)voidmotor(ucharindex,ucharspeed){if(index==1){OCR1AH=0x00;OCR1AL=speed;}if(index==2){OCR1BH=0x00;OCR1BL=speed;}}//delaytimebymsvoiddelayms(uintMS){uinti,j;for(i=0;iMS;i++)for(j=0;j1141;j++);//1141是在8MHz晶振下}//portinitialvoidport_init(){DDRA=0x00;//inputrunningstateDDRB=0xFF;//controltwomotorsDDRC=0x00;DDRD=0x30;//PWMwavePORTA=0x00;PORTB=0x00;PORTC=0x00;PORTD=0x30;}/*****************传感器函数********************///获取小车的行驶状态。0(直线),1(左偏),2(右偏),3(严重左偏),4(严重右偏)//从左到右传感器(光电对管)对应的输入端口:PA0,PA1,PA2,PA3,PA4//正常行驶时引导线夹在PA1,PA3之间//根据端口PA0,PA1,PA3,PA4的值判断行驶状态。(中间传感器PA2并未使用)voidsensor_state(void)//传感器状态{ucharstate,state1,state2;//暂存PINA0~PINA4相应位的值state1=0b00000010&PINA;state2=0b00000001&PINA;if(state1==0b00000010){if(state2==0b00000001){discrepancy=4;return;}else{discrepancy=2;return;}}state1=0b00001000&PINA;state2=0b00010000&PINA;if(state1==0b00001000){if(state2==0b00010000){discrepancy=3;return;}else{discrepancy=1;return;}}//补充严重左偏或严重右偏的情况state=0b00000001&PINA;if(state==0b00000001){discrepancy=4;return;}state=0b00010000&PINA;if(state==0b00010000){discrepancy=3;return;}discrepancy=0;}/************************循迹函数******************///ENA---PD4,ENB---PD5,PORTB0~3----IN1~3,L298电机驱动电路//PB0,PB1控制右电机,PB1,PB2控制左电机voidrevise_to_line()//0=runforward,1=left,2=right,3=severleft,4=severright{if(discrepancy==0){//PORTB=0b11111001;PORTB|=((1PB3)|(1PB0));PORTB&=~((1PB1)|(1PB2));delayms(10);return;}if(discrepancy==1){//PORTB=0b11111000;PORTB|=(1PB3);PORTB&=~((1PB0)|(1PB1)|(1PB2));while((0b00001000&PINA)==0b00001000);//getthevalueofPINA3return;}if(discrepancy==2){//PORTB=0b11110001;PORTB|=(1PB0);PORTB&=~((1PB1)|(1PB2)|(1PB3));while((0b00000010&PINA)==0b00000010);//getthevalueofPINA1return;}if(discrepancy==3){//PORTB=0b11111000;PORTB|=(1PB3);PORTB&=~((1PB0)|(1PB1)|(1PB2));while((0b00001000&PINA)==0b00001000);//getthevalueofPINA3return;}if(discrepancy==4){PORTB|=(1PB0);PORTB&=~((1PB1)|(1PB2)|(1PB3));while((0b00000010&PINA)==0b00000010);//getthevalueofPINA1return;}}//mainfunction:actionstructure////voidmain(void){port_init();init_devices1();//setspeedvaluemotor(1,200);motor(2,200);while(1){sensor_state();//传感器状态revise_to_line();}}

1 / 5
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功