timer:send_after

请帮我优化这段代码include <reg52.h> #include <stdio.h> #include <string.h> #define LCD_DATA P0 #define LCD_RS P2_0 #define LCD_RW P2_1 #define LCD_EN P2_2 #define LED_PIN P1_0 #define BUZZER_PIN P1_1 void delay(unsigned int ms); void LCD_init(); void LCD_command(unsigned char cmd); void LCD_data(unsigned char dat); void LCD_string(char *str); void LCD_clear(); void UART_init(); void UART_sendChar(unsigned char ch); void UART_sendString(char *str); unsigned char UART_receiveChar(); void executeCommand(char *command); void main() { char command[20]; UART_init(); LCD_init(); while (1) { if (UART_receiveChar() == ':') { UART_receiveChar(); // Ignore space after ':' fgets(command, sizeof(command), stdin); executeCommand(command); UART_sendString(command); // Send back the received command } } } void delay(unsigned int ms) { unsigned int i, j; for (i = 0; i < ms; i++) for (j = 0; j < 110; j++); } void LCD_init() { LCD_command(0x38); // 2 lines, 5x7 matrix LCD_command(0x0C); // Display on, cursor off LCD_command(0x06); // Increment cursor LCD_command(0x01); // Clear display delay(2); } void LCD_command(unsigned char cmd) { LCD_RS = 0; LCD_RW = 0; LCD_EN = 1; LCD_DATA = cmd; delay(2); LCD_EN = 0; } void LCD_data(unsigned char dat) { LCD_RS = 1; LCD_RW = 0; LCD_EN = 1; LCD_DATA = dat; delay(2); LCD_EN = 0; } void LCD_string(char *str) { while (*str) { LCD_data(*str++); } } void LCD_clear() { LCD_command(0x01); // Clear display delay(2); } void UART_init() { TMOD = 0x20; // Timer1 mode 2: 8-bit auto-reload TH1 = 0xFD; // 9600 baud rate SCON = 0x50; // Serial mode 1: 8-bit data, 1 stop bit, receive enabled TR1 = 1; // Start Timer1 } void UART_sendChar(unsigned char ch) { SBUF = ch; while (TI == 0); // Wait for transmission to complete TI = 0; // Clear transmission flag } void UART_sendString(char *str) { while (*str) { UART_sendChar(*str++); } } unsigned char UART_receiveChar() { while (RI == 0); // Wait for reception to complete RI = 0; // Clear reception flag return SBUF; } void executeCommand(char *command) { if (strncmp(command, "LED on", 6) == 0) { LED_PIN = 1; } else if (strncmp(command, "buzzer on", 9) == 0) { BUZZER_PIN = 1; } else if (strncmp(command, "showstr", 7) == 0) { char *str = command + 8; // Get the string after "showstr" LCD_clear(); LCD_command(0x80); // Move cursor to the beginning of the first line LCD_string(str); } }
最新发布
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值