This code I get the time is changed, the original is the control of the four-wheeled car code, the back to control the triple-wheeled.
Code:
#include <reg52.h>//here 01 is going forward and 10 is going to go afterSbit h1=p0^1;sbit h2=p0^2;sbit h3=p0^3;sbit h4=p0^4;//--definition of IO port--//sbit pwm1=p3^1;sbit pwm2=p3^2;sbit PWM3=P3^3;sbit Pwm4=p3^4;sbit ena=p3^5;sbit enb=p3^6;//--defines a global variable--//unsigned char timer1; void Time1config ();******************************************************************************** Function Name: Main* function function: main function* Input: None* Output: None*******************************************************************************/void speed1 (unsigned int d)//Go straight, change the speed by changing the value of D {time1config ();While (1) { if (timer1>100)//PWM period is 100*0.5ms {timer1=0; break; }if (Timer1 < D)//change D This value can change the speed of the DC motor {pwm1=0;pwm2=1;pwm3=0;pwm4=1;ena=1;enb=1; }Else {pwm1=0;pwm2=0;pwm3=0;pwm4=0;ena=0;enb=0; } } }void Speed2 (unsigned int d)//Turn left {time1config ();While (1) { if (timer1>100) {timer1=0; break; }if (Timer1 < D) {pwm1=1;pwm2=0;pwm3=0;pwm4=1;ena=1;enb=1; }Else {pwm1=0;pwm2=0;pwm3=0;pwm4=0;ena=0;enb=0; } } } void speed3 (unsigned int d)//right-turn {time1config ();While (1) { if (timer1>100)//PWM period is 100*0.5ms {timer1=0; break; }if (Timer1 < D)//change 30 This value can change the speed of the DC motor {pwm1=0;pwm2=1;pwm3=1;pwm4=0;ena=1;enb=1; }Else {pwm1=0;pwm2=0;pwm3=0;pwm4=0;ena=0;enb=0; } } } void speed4 (unsigned int d) {time1config ();While (1) { if (timer1>100)//PWM period is 100*0.5ms {timer1=0; break; }if (Timer1 < D)//change D This value can change the speed of the DC motor {pwm1=0;pwm2=0;pwm3=0;pwm4=0;ena=0;enb=0; }Else {pwm1=0;pwm2=0;pwm3=0;pwm4=0;ena=0;enb=0; } } }void Main () {while (1) {if (h3==1&&h2==1&&h1==0| | h3==1&&h2==0&&h1==0) { Speed2 (10);} Right, you should turn left.if (h3==0&&h2==1&&h1==0| | h3==1&&h2==1&&h1==1| | h3==1&&h2==0&&h1==1| | h3==0&&h2==0&&h1==0){speed1 (15);} Go straightif (h3==0&&h2==0&&h1==1| | H3==0&&h2==1&&h1==1){speed3 (10);} Turn right} }/******************************************************************************** Function Name: Time1config* function function: Set timer* Input: None* Output: None*******************************************************************************/void time1config ()//memory Timer {tmod|= 0x10; Set the timer counter to work 1 for the timer//--Timer assigned initial value, 12MHZ timer 0.5ms--//TH1 = 0xFE;TL1 = 0x0C;ET1 = 1; Turn on Timer 1 interruptEA = 1;TR1 = 1; Turn on timer}/******************************************************************************** Function Name: Time1* function function: Interrupt function of Timer 1* Input: None* Output: None*******************************************************************************/void Time1 (void) Interrupt 3//3 for Timer 1 interrupt number 1 Timer 0 interrupt number 0 External interrupt 1 2 external interrupt 2 4 serial Interrupt {TH1 = 0xFE; Re-assign initial valueTL1 = 0x0C;timer1++; }
Trolley steering and speed control