发布时间:2025-10-15 18:13:40    次浏览
/******************************舵机控制头文件***********************/#ifndef__tuoji_H__#define__tuoji_H__externucharfront;externvoidmotor_init(void);//初使化externvoidstop(void);//停车extern void go(uchar speed); //小车前或者后,具体看标志位extern void l_r_front(uchar p); //小车向前走时的转弯函数extern void l_r_back(uchar p); //小车向后走时的转弯函数#endif/******************************舵机控制C文件***********************/#include 'config.h'void motor_init(void){DDRB|=0B01100000;//pd5.6为OCR1A,OCR1BTCCR1A=0X00;TCCR1B=0X00;TCCR1A=0XA0; //模式8,64分频TCCR1B=0X13;ICR1=1152; //20MS周期}void stop(void) //停车{OCR1A=86;//ICR1=1152时为87OCR1B=86;}/*小车向前向后函数*/void go(uchar speed) //小车前或者后,具体看标志位{if(front==1){OCR1A=100;//1152时87停车,1000时86OCR1B=70;}else{OCR1A=70;//1152时87停车,1000时86OCR1B=100;}}/*小车向左向右的函数向前走时*/void l_r_front(uchar p) //小车向前走时的转弯函数{switch(p){case 'r' :{OCR1A=100;//1152时87停车,1000时86OCR1B=83;break;} //两轮速度不同而转弯case 'l' :{OCR1A=60;//1152时87停车,1000时86 OCR1B=90;break;}}}void l_r_back(uchar p) //小车向后走时的转弯函数{switch(p){case 'r' :{OCR1A=100;//1152时87停车,1000时86OCR1B=83;break;}case 'l' :{OCR1A=60;//1152时87停车,1000时86OCR1B=90;break;}}}/******************************主文件***********************/#include iom128v.h#include macros.h#define uchar unsigned char#define uint unsigned int#define left_speed 87 //OCR1A 速度起点不同#define right_speed 87 //OCR1B 因电机不平衡//87为停机uchar flag;unsigned char front;/*一毫秒的延时*/void delayms(uint i){unsigned int a, b;for (a = 1; a i; a++){for (b = 1; b1141; b++){;}}}void motor_init(void){DDRB|=0B01100000;//pd5.6为OCR1A,OCR1BTCCR1A=0X00;TCCR1B=0X00;TCCR1A=0XA0; //模式8,64分频TCCR1B=0X13;ICR1=1152; //20MS周期}void stop(void) //停车{OCR1A=87;//ICR1=1152时为87OCR1B=87;}/*小车向前向后函数*/void go(uchar speed) //小车前或者后,具体看标志位{if(front==1){OCR1A=87+speed; //1152时87停车,1000时86OCR1B=87-speed; //因为右电机要87-6左右车才平衡}else{OCR1A=87-speed;//1152时87停车,1000时86OCR1B=87+speed;//因为右电机要87-6左右车才平衡}}/*小车向左向右的函数向前走时*/void l_r_front(uchar p) //小车向前走时的转弯函数{switch(p){case 'r' :{OCR1A=left_speed+20;//1152时87停车,1000时86OCR1B=right_speed-10;break;} //两轮速度不同而转弯case 'l' :{OCR1A=left_speed+10;//1152时87停车,1000时86OCR1B=right_speed-20;break;}case 'R' :{OCR1A=left_speed+30;//1152时87停车,1000时86OCR1B=right_speed-0;break;} //两轮速度不同而转弯case 'L' :{OCR1A=left_speed+0;//1152时87停车,1000时86OCR1B=right_speed-30;break;}}}void l_r_back(uchar p) //小车向后走时的转弯函数{switch(p){case 'r' :{OCR1A=100;//1152时87停车,1000时86OCR1B=83;break;}case 'l' :{OCR1A=60;//1152时87停车,1000时86OCR1B=90;break;}}}void main(){ uint i,temp;front=1;motor_init();while(1){temp=PINA 0B00011111;PORTC=temp;switch (temp){case 0b00010011: {l_r_front('l');break;}case 0b00011001: {l_r_front('r');break;}case 0b00010111: {l_r_front('l');break;}case 0b00011101: {l_r_front('r');break;}case 0b00001111: {l_r_front('L');break;}case 0b00000111: {l_r_front('L');break;}case 0b00000011: {l_r_front('L');break;}case 0b00000001: {l_r_front('L');break;}case 0b00011110: {l_r_front('R');break;}case 0b00011100: {l_r_front('R');break;}case 0b00011000: {l_r_front('R');break;}case 0b00010000: {l_r_front('R');break;}case 0b00000000: {stop();break;}default: { go(20);break;}}}}原文链接:http://www.eeworld.com.cn/mcu/article_2016101930603.html