关于以下代码的解释:
//============================================
#define LongToBin(n) \ // DEF 1
( \
((n >> 21) & 0x80) | \
((n >> 18) & 0x40) | \
((n >> 15) & 0x20) | \
((n >> 12) & 0x10) | \
((n >> 9) & 0x08) | \
((n >> 6) & 0x04) | \
((n >> 3) & 0x02) | \
((n ) & 0x01) \
)
#define Bin(n) LongToBin(0x##n##L) // DEF 2
//============================================
A. 代码的目的是,将8位十进制(如1111 1111)转为十六进制(如0xFF)。
B. 代码的核心思想是,将8位十进制数视为Long型数(32bits,如1111 1111视为0b 0001 0001 0001 0001 0001 0001 0001 0001)——代码中DEF2的目的即在于此;然后将待转换数(即n)的每 半 个字节(4bits)的最低位(非0即1)按高低顺序,转移到单个字节(8bits)的对应位上,若实现如此转换,Long型由高到低的有效位(半字节最低位)分别需要右移21、18、15、12、9、6、3和0个位置,再与上0xff达到保值的效果——代码中DEF1的目的即在于此。
C. 可能需要解释的符号——
1、 " \ ",由于宏定义规定,为便于阅读,需要换行时,可以在行尾加上符号,表示下一行内容也是本定义中内容;
2、" ## ",属于宏定义中的字符(串)连接符,即,将符号两端的字符(串)接为一个整体,如以上代码中,在调用Bin(n)时,若n=1111 1111,该宏的作用是将1111 1111视为字符串,替换"0x##n##L"中的"n",并与"0x"和"L"连接为一体,即Bin(1111 1111)等价于调用LongToBin(0x 1111 1111 L),而符号"0x"和"L"则分别为十六进制数和Long型数的标记符。
综上,DEF1的作用是将Long型数缩为Char(Byte)型数,DEF2的作用是将十进制数(字符)转为十六进制Long型数。
应用串口的初始化:(Atmega128A)
//初始化串口相关寄存器,设置波特率,并开启串口的中断 void init_uart(unsigned char port_no, unsigned char baudrate) { static unsigned int __farflash BAUDRATE_SET[] = {B2400,B4800,B9600,B19200,B38400,B57600,B115200,B230400}; //if (baudrate_set == sizeof(BAUDRATE_SET)/sizeof(unsigned int)) return FALSE; if (port_no == 0) { UCSR0B = 0x00; //disable while setting baud rate UCSR0A = Bin(11000010); //doubling the transfer rate /* ||||||||_____ MPCMn: Multi-Processor Communication Mode */ /* |||||||______ U2Xn: Double the USART Transmission Speed */ /* ||||||_______ UPEn: Parity Error */ /* |||||________ DORn: Data OverRun */ /* ||||_________ FEn: Frame Error */ /* |||__________ UDREn: USART Data Register Empty */ /* ||___________ TXCn: USART Transmit Complete */ /* |____________ RXCn: USART Receive Complete */ UCSR0C = Bin(00000110); //Asynchronous Operation; none paUCSRA_Bit7ty; 1 stop bits; 8 data bits /* ||||||||_____ UCPOLn: Clock Polarity */ /* |||||||______ */ /* ||||||_______ Bit 2:1 – UCSZn1:0: Character Size */ /* |||||________ USBSn: Stop Bit Select */ /* ||||_________ */ /* |||__________ Bit 5:4 – UPMn1:0: Parity Mode */ /* ||___________ UMSELn: USART Mode Select */ /* |____________ - */ UBRR0L = BAUDRATE_SET[baudrate]; //set baud rate lo UBRR0H = BAUDRATE_SET[baudrate]>>8; //set baud rate hi UCSR0B = Bin(11011000); /* ||||||||_____ TXB8n: Transmit Data Bit 8 */ /* |||||||______ RXB8n: Receive Data Bit 8 */ /* ||||||_______ UCSZn2: Character Size */ /* |||||________ TXENn: Transmitter Enable */ /* ||||_________ RXENn: Receiver Enable */ /* |||__________ UDRIEn: USART Data Register Empty Interrupt Enable */ /* ||___________ TXCIEn: TX Complete Interrupt Enable */ /* |____________ RXCIEn:RX Complete Interrupt Enable */ } else { UCSR1B = 0x00; //disable while setting baud rate UCSR1A = Bin(11000010); //doubling the transfer rate /* ||||||||_____ MPCMn: Multi-Processor Communication Mode */ /* |||||||______ U2Xn: Double the USART Transmission Speed */ /* ||||||_______ UPEn: Parity Error */ /* |||||________ DORn: Data OverRun */ /* ||||_________ FEn: Frame Error */ /* |||__________ UDREn: USART Data Register Empty */ /* ||___________ TXCn: USART Transmit Complete */ /* |____________ RXCn: USART Receive Complete */ UCSR1C = Bin(00000110); //Asynchronous Operation; none paUCSRA_Bit7ty; 1 stop bits; 8 data bits /* ||||||||_____ UCPOLn: Clock Polarity */ /* |||||||______ */ /* ||||||_______ Bit 2:1 – UCSZn1:0: Character Size */ /* |||||________ USBSn: Stop Bit Select */ /* ||||_________ */ /* |||__________ Bit 5:4 – UPMn1:0: Parity Mode */ /* ||___________ UMSELn: USART Mode Select */ /* |____________ - */ UBRR1L = BAUDRATE_SET[baudrate]; //set baud rate lo UBRR1H = BAUDRATE_SET[baudrate]>>8; //set baud rate hi UCSR1B = Bin(11011000); /* ||||||||_____ TXB8n: Transmit Data Bit 8 */ /* |||||||______ RXB8n: Receive Data Bit 8 */ /* ||||||_______ UCSZn2: Character Size */ /* |||||________ TXENn: Transmitter Enable */ /* ||||_________ RXENn: Receiver Enable */ /* |||__________ UDRIEn: USART Data Register Empty Interrupt Enable */ /* ||___________ TXCIE: TX Complete Interrupt Enable */ /* |____________ RXCIEn:RX Complete Interrupt Enable */ } }