Use pwm wave to adjust the speed of the trolley
1,
main.c
#include "reg52.h"//There is no sbit i/o port in the code, you can not declare it //#include "intrins.h"//There is no _nop_() in the code; you can not declare this #include "motor.h" #include "dalay.h" #include "uart.h" #include "esp.h" #include "pwm.h" extern char speedleft; extern char speedright; void main() { // Delay1000ms(); time0init(); time1init(); // UartInit(); // init_esp(); while (1) { speedleft=15; speedright=40; Delay1000ms(); Delay1000ms(); speedleft=40; speedright=15; Delay1000ms(); Delay1000ms(); } while(1); }
2,
motor.c
#include "reg52.h" sbit right1=P3^2; sbit right2=P3^3; sbit left1=P3^4; sbit left2=P3^5; void goforwardleft() { left1=0; left2=1; } void goforwardright() { right1=0; right2=1; } void goforward() { left1=0; left2=1; right1=0; right2=1; } void goback() { left1=1; left2=0; right1=1; right2=0; } void goleft() { left1=0; left2=0; right1=0; right2=1; } void goright() { left1=0; left2=1; right1=0; right2=0; } void stop() { left1=0; left2=0; right1=0; right2=0; } void stopleft() { left1=0; left2=0; ; } void stopright() { right1=0; right2=0; }motor.h
void goback(); void goforward(); void goleft(); void goright(); void stop(); void goforwardleft(); void goforwardright(); void stopleft(); void stopright();
3.
pwm.c
#include "reg52.h" #include "motor.h" char time_left=0; char time_right=0; char speedleft; char speedright; void time0init() { TMOD=0x01;//Define the mode as a 16-bit register TL0=0x33;//Define the start time, assign the initial value TH0=0xFE; TR0=1;//The timing starts, and TF0 is 1 until the timing ends TF0=0; ET0=1;//Enable timer 0 interrupt EA=1;//Open total interrupt } void time0hander()interrupt 1//The timer register overflows, at this time TF0=1, an interrupt occurs, TF0 is set to 0, and the cycle is repeated { time_left + + ;//for counting TL0=0x33;//reassign the initial value TH0=0xFE; if(time_left<speedleft){ goforwardleft(); } else { stopleft(); } if(time_left==40){ time_left=0;//Cycle 100 times is 1s, recycle } } void time1init() { TMOD & amp;=0x0F;//Define the mode as a 16-bit register TMOD|=0x1<<4; TL1=0x33;//Define the start time, assign the initial value TH1=0xFE; TR1=1;//The timing starts, and TF0 is 1 until the timing ends TF1=0; ET1=1;//Enable timer 0 interrupt EA=1;//Open total interrupt } void time1hander()interrupt 3//The timer register overflows, at this time TF0=1, an interrupt occurs, TF0 is set to 0, and the cycle is repeated { time_right + + ;//for counting TL1=0x33;//reassign the initial value TH1=0xFE; if(time_right<speedright){ goforwardright(); } else { stopright(); } if(time_right==40){ time_right=0;//cycle 100 times for 1s, recycle } }pwm.h
void time0init(); void time1init();
4.
uart.c
#include "reg52.h" #include "motor.h" #include "string.h" #include "dalay.h" #include "esp.h" #define SIZE 12 sfr AUXR=0x8E;//I don’t know AUXR, the error needs to be declared char buffer[SIZE]; extern char AT_ok_flag; void UartInit(void) //[email protected] { AUXR=0x01; SCON=0x50;//Configure serial port working mode 1, ren can receive 01000000 TMOD &=0x0F; TMOD|=0x20;//Timer 1 working mode 8-bit automatic reload configuration TH1=0xFD; TL1=0xFD;//Initial value of 9600 baud rate TR1=1;//Start the timer EA=1;//Open total interrupt ES=1;//Enable serial interrupt } void uart_toutine()interrupt 4 { static int i = 0; char tmp; if(RI) { RI = 0; tmp = SBUF; if(tmp=='M'||tmp=='W'||tmp=='O'||tmp=='L'){//WIFI GOT IP contains both W, including F, the bottom condition meets FI, and D5 is on i = 0; } buffer[i + + ] = tmp; if(buffer[0] == 'O' & amp; & amp; buffer[1] == 'K'){ AT_ok_flag = 1; memset(buffer, '\0', SIZE); } if(buffer[0] == 'L' & amp; & amp; buffer[1] == '0'){ D5 = 0; memset(buffer, '\0', SIZE); } if(buffer[0] == 'L' & amp; & amp; buffer[1] == '1'){ D5 = 1; memset(buffer, '\0', SIZE); } if(buffer[0] == 'M'){ switch(buffer[1]){ case '1': goforward(); Delay10ms(); break; case '2': goleft(); Delay10ms(); break; case '3': goright(); Delay10ms(); break; case '4': goback(); Delay10ms(); break; default: stop(); break; } } } if(i == 12){ memset(buffer, '\0', SIZE); i = 0; } }uart.h
void UartInit(); //[email protected]
5.
dalay.c
#include "intrins.h" void Delay1000ms() //@11.0592MHz { unsigned char i, j, k; _nop_(); i = 8; j = 1; k = 243; do { do { while (--k); } while (--j); } while (--i); } void Delay10ms() //@11.0592MHz { unsigned char i, j; i = 18; j = 235; do { while (--j); } while (--i); }dalay.h
void Delay1000ms(); void Delay10ms();
The following code is the wifi module control code, add it if necessary
esp.c
#include "reg52.h" #include "dalay.h" #include "uart.h" code char LJWL[]="AT + CWJAP="Xiaomi_7EC3","xiaomi123"\r\\ "; code char LJFWQ[]="AT + CIPSTART="TCP","192.168.31.152",8880\r\\ "; char KQTC[]="AT + CIPMODE=1\r\\ "; char KQFS[]="AT + CIPSEND\r\\ "; sbit D6=P3^6; sbit D5=P3^7; char AT_ok_flag; void senddata(char c) { SBUF=c; while(!TI); TI=0; } void sendstring(char*str) { while(*str!='\0'){ senddata(*str); str++; } } void init_esp() { Delay1000ms(); sendstring(LJWL); while(!AT_ok_flag); D6=0; AT_ok_flag=0; D6=1; sendstring(LJFWQ); D5=1; while(!AT_ok_flag); AT_ok_flag=0; sendstring(KQTC); D6=0; while(!AT_ok_flag); AT_ok_flag=0; sendstring(KQFS); D6=1; while(!AT_ok_flag); \t if(AT_ok_flag){ D6=0; D5=0; } }esp.h
sbit D6=P3^6; sbit D5=P3^7; void init_esp();