优化这段代码#include <REGX52.H> #include "lcd1602.h" #include "send_IR.h" #include "notsend_IR.h" #include "keybored.h" #include "Delay.h" sbit IR_EN = P2^0; void Send_NEC_0() { NotSend_IR(19); Send_IR(19); } void Send_NEC_1() { NotSend_IR(60); Send_IR(19); } void send(unsigned char s1,unsigned char s2) { unsigned char temp,i; Send_NEC_1(); Send_NEC_0(); Send_NEC_1(); Send_NEC_1(); Send_NEC_0(); Send_NEC_0(); Send_NEC_1(); Send_NEC_0(); Send_NEC_0(); Send_NEC_1(); Send_NEC_0(); Send_NEC_0(); Send_NEC_1(); Send_NEC_1(); Send_NEC_0(); Send_NEC_1(); temp = s1 & 0x001; for(i = 0;i<12;i++) { if(temp) { Send_NEC_1(); } else { Send_NEC_0(); } temp = temp>>1; } Send_NEC_0(); Send_NEC_0(); Send_NEC_0(); Send_NEC_0(); temp = s2 & 0x001; for(i = 0;i<12;i++) { if(temp) { Send_NEC_1(); } else { Send_NEC_0(); } temp = temp>>1; } //????? Send_NEC_1(); Send_NEC_1(); Send_NEC_1(); Send_NEC_1(); } void Send1(unsigned char s1,unsigned char s2) { Send_IR(167); NotSend_IR(167); Send_IR(167); send(s1,s2); Send_IR(167); NotSend_IR(167); Send_IR(167); send(s1,s2); Send_IR(167); NotSend_IR(167); Send_IR(167); Send_NEC_1(); Send_NEC_1(); Send_NEC_0(); Send_NEC_1(); Send_NEC_0(); Send_NEC_1(); Send_NEC_0(); Send_NEC_1(); Send_NEC_0(); Send_NEC_1(); Send_NEC_1(); Send_NEC_0(); Send_NEC_0(); Send_NEC_1(); Send_NEC_1(); Send_NEC_0(); Send_NEC_0(); Send_NEC_0(); Send_NEC_0(); Send_NEC_0(); Send_NEC_0(); Send_NEC_0(); Send_NEC_0(); Send_NEC_0(); Send_NEC_0(); Send_NEC_0(); Send_NEC_0(); Send_NEC_0(); Send_NEC_0(); Send_NEC_0(); Send_NEC_0(); Send_NEC_0(); Send_NEC_0(); Send_NEC_0(); Send_NEC_0(); Send_NEC_0(); Send_NEC_0(); Send_NEC_0(); Send_NEC_0(); Send_NEC_0(); Send_NEC_0(); Send_NEC_0(); Send_NEC_0(); Send_NEC_0(); Send_NEC_0(); Send_NEC_1(); Send_NEC_0(); Send_NEC_0(); Send_NEC_1(); Send_NEC_0(); Send_NEC_1(); Send_NEC_0(); }
时间: 2023-09-10 20:15:37 浏览: 142
首先,可以考虑将常量定义为宏,这样可以减少代码量,并且提高代码可读性。例如:
#define NEC_0 19
#define NEC_1 60
#define IR_CODE 167
然后,可以考虑将 send 函数拆分为多个小函数,这样可以提高代码的可维护性。例如:
void send_bit(unsigned char bit) {
if (bit) {
Send_NEC_1();
} else {
Send_NEC_0();
}
}
void send_data(unsigned char data) {
for (unsigned char i = 0; i < 12; i++) {
send_bit(data & 0x01);
data >>= 1;
}
}
void send(unsigned char s1, unsigned char s2) {
Send_NEC_1();
Send_NEC_0();
Send_NEC_1();
Send_NEC_1();
Send_NEC_0();
Send_NEC_0();
Send_NEC_1();
Send_NEC_0();
Send_NEC_0();
Send_NEC_1();
Send_NEC_0();
Send_NEC_0();
Send_NEC_1();
Send_NEC_1();
Send_NEC_0();
Send_NEC_1();
send_data(s1);
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
send_data(s2);
Send_NEC_1();
Send_NEC_1();
Send_NEC_1();
Send_NEC_1();
}
最后,可以考虑将 Send1 函数拆分为多个小函数,这样可以提高代码的可读性和可维护性。例如:
void send_repeat() {
Send_IR(IR_CODE);
NotSend_IR(IR_CODE);
Send_IR(IR_CODE);
}
void send_finish() {
Send_NEC_1();
Send_NEC_1();
Send_NEC_0();
Send_NEC_1();
Send_NEC_0();
Send_NEC_1();
Send_NEC_0();
Send_NEC_1();
Send_NEC_0();
Send_NEC_1();
Send_NEC_1();
Send_NEC_0();
Send_NEC_0();
Send_NEC_1();
Send_NEC_1();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
Send_NEC_0();
}
void Send1(unsigned char s1, unsigned char s2) {
send_repeat();
send(s1, s2);
send_repeat();
send(s1, s2);
send_repeat();
send_finish();
}
阅读全文