??? 05/08/08 05:07 Read: times |
#154562 - That is a LOT of code Responding to: ???'s previous message |
I did not compile it but:
void RS232_Write(unsigned char reg, unsigned char dat0, unsigned char dat1) { unsigned char REG_H, REG_L, DAT1_H, DAT1_L, DAT2_H, DAT2_L, CHECKSUM_H, CHECKSUM_L, CHECKSUM_T, HexValue ; REG_H = Hex2Ascii(reg >> 4); // do the Upper Nibble REG_L = Hex2Ascii(reg & 0x0F); // do the Lower Nibble DAT1_H = Hex2Ascii(dat0 >> 4); // do the Upper Nibble DAT1_L = Hex2Ascii(dat0 & 0x0F); // do the Lower Nibble DAT2_H = Hex2Ascii(dat1 >> 4); // do the Upper Nibble DAT2_L = Hex2Ascii(dat1 & 0x0F); // do the Lower Nibble CHECKSUM_T = (cam_header + cam_ID_H + cam_ID_L + REG_H + REG_L + DAT1_H + DAT1_L + DAT2_H + DAT2_L); CHECKSUM2_H = Hex2Ascii(CHECKSUM_T >> 4); // do the Upper Nibble CHECKSUM_L = Hex2Ascii(CHECKSUM_T & 0x0F); // do the Lower Nibble //*********************************************************************** uart_init(); uart_transmit(cam_header); // Header uart_transmit(cam_ID_H); // ID H uart_transmit(cam_ID_L); // ID L uart_transmit(REG_H); // Register H uart_transmit(REG_L); // Register L uart_transmit(DAT1_H); // Data 1 H uart_transmit(DAT1_L); // Data 1 L uart_transmit(DAT2_H); // Data 2 H uart_transmit(DAT2_L); // Data 2 L uart_transmit(CHECKSUM_H); // Send calculated checksum 1 uart_transmit(CHECKSUM_L); // Send calculated checksum 2 //*********************************************************************** } unsigned char Hex2Ascii(unsigned char HexValue) { code const unsigned char hex[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; if(HexValue < 16)return hex[HexValue]; else return '0'; } would be shorter. if you look at your switch you can have multiple labels with the same code. switch (HexValue) { case 0x00: // Numbers case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06 case 0x07: case 0x08: case 0x09: return (HexValue + '0'); break; case 0x0A: // Letters case 0x0B: case 0x0C: case 0x0D: case 0x0E: case 0x0F: return (HexValue + ('A'- 0x0A));// The compiler should convert to a single add break; } Otherwise you might as well just return the value. Note that this might be better as an if statement. Anyway just a critique. |