只需一步,快速开始
举报
void SerialOpen(uint8_t port, uint32_t baud) { uint8_t h = ((F_CPU / 4 / baud -1) / 2) >> 8; uint8_t l = ((F_CPU / 4 / baud -1) / 2); switch (port) { #if defined(PROMINI) case 0: UCSR0A = (1<<U2X0); UBRR0H = h; UBRR0L = l; UCSR0B |= (1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0); break; #endif #if defined(PROMICRO) #if (ARDUINO >= 100) && !defined(TEENSY20) case 0: UDIEN &= ~(1<<SOFE); break;// disable the USB frame interrupt of arduino (it causes strong jitter and we dont need it) #endif case 1: UCSR1A = (1<<U2X1); UBRR1H = h; UBRR1L = l; UCSR1B |= (1<<RXEN1)|(1<<TXEN1)|(1<<RXCIE1); break; #endif #if defined(MEGA) case 0: UCSR0A = (1<<U2X0); UBRR0H = h; UBRR0L = l; UCSR0B |= (1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0); break; case 1: UCSR1A = (1<<U2X1); UBRR1H = h; UBRR1L = l; UCSR1B |= (1<<RXEN1)|(1<<TXEN1)|(1<<RXCIE1); break; case 2: UCSR2A = (1<<U2X2); UBRR2H = h; UBRR2L = l; UCSR2B |= (1<<RXEN2)|(1<<TXEN2)|(1<<RXCIE2); break; case 3: UCSR3A = (1<<U2X3); UBRR3H = h; UBRR3L = l; UCSR3B |= (1<<RXEN3)|(1<<TXEN3)|(1<<RXCIE3); break; #endif } }
void SerialOpenUNO(uint32_t baud) { uint16_t baud_setting; bool use_u2x = true; #if F_CPU == 16000000UL if (baud == 57600) { use_u2x = false; } #endif try_again: if (use_u2x) { UCSR0A = (1<<U2X0); baud_setting = ((F_CPU / 4 / baud -1) / 2); } else { UCSR0A = 0; baud_setting = ((F_CPU / 8 / baud -1) / 2); } if ((baud_setting > 4095) && use_u2x) { use_u2x = false; goto try_again; } uint8_t h = ((F_CPU / 4 / baud -1) / 2) >> 8; uint8_t l = ((F_CPU / 4 / baud -1) / 2); UBRR0H = baud_setting >> 8; UBRR0L = baud_setting; UCSR0B |= (1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0); } void SerialOpen(uint8_t port, uint32_t baud) { #if defined(PROMINI) SerialOpenUNO(baud); #else uint8_t h = ((F_CPU / 4 / baud -1) / 2) >> 8; uint8_t l = ((F_CPU / 4 / baud -1) / 2); switch (port) { // #if defined(PROMINI) // case 0: UCSR0A = (1<<U2X0); UBRR0H = h; UBRR0L = l; UCSR0B |= (1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0); break; // #endif #if defined(PROMICRO) #if (ARDUINO >= 100) && !defined(TEENSY20) case 0: UDIEN &= ~(1<<SOFE); break;// disable the USB frame interrupt of arduino (it causes strong jitter and we dont need it) #endif case 1: UCSR1A = (1<<U2X1); UBRR1H = h; UBRR1L = l; UCSR1B |= (1<<RXEN1)|(1<<TXEN1)|(1<<RXCIE1); break; #endif #if defined(MEGA) case 0: UCSR0A = (1<<U2X0); UBRR0H = h; UBRR0L = l; UCSR0B |= (1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0); break; case 1: UCSR1A = (1<<U2X1); UBRR1H = h; UBRR1L = l; UCSR1B |= (1<<RXEN1)|(1<<TXEN1)|(1<<RXCIE1); break; case 2: UCSR2A = (1<<U2X2); UBRR2H = h; UBRR2L = l; UCSR2B |= (1<<RXEN2)|(1<<TXEN2)|(1<<RXCIE2); break; case 3: UCSR3A = (1<<U2X3); UBRR3H = h; UBRR3L = l; UCSR3B |= (1<<RXEN3)|(1<<TXEN3)|(1<<RXCIE3); break; #endif } #endif }
本版积分规则 发表回复 回帖后跳转到最后一页
小黑屋|Archiver|手机版|Arduino中文社区
GMT+8, 2024-11-28 02:54 , Processed in 0.168282 second(s), 18 queries .
Powered by Discuz! X3.4
Copyright © 2001-2021, Tencent Cloud.