请帮我优化这段代码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); } }
时间: 2024-01-31 20:03:58 浏览: 119
这段代码可以进行如下的优化:
1. 将LCD的RS、RW、EN引脚的控制函数封装成一个公共函数,避免代码重复。
2. 可以使用宏定义或枚举类型来定义LED_PIN和BUZZER_PIN,提高代码的可读性。
3. 在executeCommand函数中,可以使用switch-case语句来代替if-else语句,提高代码的可读性。
4. 在UART_receiveChar函数中,可以使用do-while循环来代替while循环,提高代码的可读性。
5. 在executeCommand函数中,可以使用strtok函数来解析命令字符串,提高代码的可读性。
6. 在executeCommand函数中,可以使用strcmp函数来比较字符串,避免使用strncmp函数时出现错误。
7. 在执行LED_PIN = 1或BUZZER_PIN = 1之前,可以先检查这些引脚是否已经被初始化为输出模式,避免出现意外错误。
8. 在executeCommand函数中,可以添加一些错误处理代码,以防止出现错误的命令。
9. 可以添加注释,提高代码的可读性。
下面是优化后的代码:
相关问题
#include <reg51.h> #include <stdio.h> #include <stdlib.h> int a; char c; #define FOSC 11059200L #define BAUD 9600 void Usart_Init() { TMOD &= 0x0F; TMOD |= 0x20; SCON = 0x50; TH1 = (256 - FOSC / 12 / BAUD); TL1 = TH1; TR1 = 1; // 启动定时器 1 ES = 1; // 允许串口中断 EA = 1; // 开启总中断 } void Usart_SendChar(char c) { TI = 0; // 清除发送完成标志 SBUF = c; while (!TI); // 等待发送完成 } void Usart_String(char *s) { while (*s) { Usart_SendChar(*s++); } } char Usart_ReceiveChar() { while (!RI); // 等待接收完成 RI = 0; // 清除接收完成标志 return SBUF; } int Usart_ReceiveString(char *s) { char c; while ((c = Usart_ReceiveChar()) != '\r') { *s++ = c; Usart_SendChar(c); // 回显 } *s = 0; } 修改为主机AT89C51通过P3.1口向从机AT89C51P3.0口输出数据的头文件
#include <reg51.h>
#include <stdio.h>
#include <stdlib.h>
int a;
char c;
#define FOSC 11059200L
#define BAUD 9600
sbit TX = P3^1; // 将TX引脚定义为P3.1
sbit RX = P3^0; // 将RX引脚定义为P3.0
void Usart_Init()
{
TMOD &= 0x0F;
TMOD |= 0x20;
SCON = 0x50;
TH1 = (256 - FOSC / 12 / BAUD);
TL1 = TH1;
TR1 = 1;
ES = 1;
EA = 1;
}
void Usart_SendChar(char c)
{
TI = 0;
SBUF = c;
while (!TI);
}
void Usart_String(char *s)
{
while (*s)
{
Usart_SendChar(*s++);
}
}
char Usart_ReceiveChar()
{
while (!RI);
RI = 0;
return SBUF;
}
int Usart_ReceiveString(char *s)
{
char c;
while ((c = Usart_ReceiveChar()) != '\r') {
*s++ = c;
Usart_SendChar(c);
}
*s = 0;
}
根据下列c语言写出功能相同的汇编代码#include<reg52.h> #include<stdio.h> #include"delay.h" #include"LCD1602.h" sbit DCOUT = P2^5; sbit key1=P1^0; sbit key2=P1^1; sbit key3=P1^2; sbit key4=P1^3; sbit m=P2^0; sbit n=P2^1; unsigned char PWM_ON,S; #define CYCLE 12 void Init_Timer0(void); void main (void) { int S=0; char displaytemp[16]; PWM_ON=0; LCD_Init(); Init_Timer0(); LCD_Write_String(2,1,"V:"); while (1) { if(key3==0) { DelayMs(10); if(key3==0) { if(PWM_ON<CYCLE) PWM_ON++; S++; if(S>=12)S=12; } while(!key3); } else if(key4==0) { DelayMs(10); if(key4==0) { if(PWM_ON>0) PWM_ON--; S--; if(S<=0)S=0; } while(!key4); } if(key1==0) { DelayMs(10); if(key1==0) { m=1;n=0; LCD_Write_String(0,0,"T"); } while(!key1); } else if(key2==0) { DelayMs(10); if(key2==0) { m=0;n=1; LCD_Write_String(0,0,"N"); } while(!key2); } sprintf(displaytemp,"%3d",S); LCD_Write_String(4,1,displaytemp); } } void Init_Timer0(void) { TMOD |= 0x01; //TH0=0x00; //TL0=0x00; EA=1; ET0=1; TR0=1; } void Timer0_isr(void) interrupt 1 { static unsigned char count; TH0=(65536-2000)/256; TL0=(65536-2000)%256; if (count==PWM_ON) { DCOUT = 0; } count++; if(count == CYCLE) { count=0; if(PWM_ON!=0) DCOUT = 1; } }
以下是功能相同的汇编代码:
```
#include <reg52.h>
sbit DCOUT = P2^5;
sbit key1 = P1^0;
sbit key2 = P1^1;
sbit key3 = P1^2;
sbit key4 = P1^3;
sbit m = P2^0;
sbit n = P2^1;
unsigned char PWM_ON, S;
#define CYCLE 12
void Init_Timer0(void);
void main (void) {
int S = 0;
char displaytemp[16];
PWM_ON = 0;
LCD_Init();
Init_Timer0();
LCD_Write_String(2, 1, "V:");
while (1) {
if (key3 == 0) {
DelayMs(10);
if (key3 == 0) {
if (PWM_ON < CYCLE) {
PWM_ON++;
S++;
if (S >= 12) S = 12;
}
while (!key3);
}
} else if (key4 == 0) {
DelayMs(10);
if (key4 == 0) {
if (PWM_ON > 0) {
PWM_ON--;
S--;
if (S <= 0) S = 0;
}
while (!key4);
}
}
if (key1 == 0) {
DelayMs(10);
if (key1 == 0) {
m = 1;
n = 0;
LCD_Write_String(0, 0, "T");
}
while (!key1);
} else if (key2 == 0) {
DelayMs(10);
if (key2 == 0) {
m = 0;
n = 1;
LCD_Write_String(0, 0, "N");
}
while (!key2);
}
sprintf(displaytemp, "%3d", S);
LCD_Write_String(4, 1, displaytemp);
}
}
void Init_Timer0(void) {
TMOD |= 0x01;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void Timer0_isr(void) interrupt 1 {
static unsigned char count;
TH0 = (65536 - 2000) / 256;
TL0 = (65536 - 2000) % 256;
if (count == PWM_ON) {
DCOUT = 0;
}
count++;
if (count == CYCLE) {
count = 0;
if (PWM_ON != 0) DCOUT = 1;
}
}
```
阅读全文