Hi
Running AS7, C99 and using the simulator when debugging
I am working on a function to setup and configure the 4 USARTS of a 2560 with 1 of 9 Baud Rates, to be configurable through SW control in my finished project by passing two variables to my function eg 2, Baud_19200 for the port number and Baud Rate value
To facilitate this I have implemented a 4 way Switch-Case for the ports and a 9 way nested Switch-Case underneath each for the Baud rates for a value to put in UBRRnH/UBRRnL.
Now the problem I am getting is for what ever Baud rate value I pass to the function, this value gets acted on for all subsequent Port values.
Example :- Say I pass 'Function(1, Baud_9600)' meaning I want to set USART0 to 9600 Baud and return. USART0 will be set to 9600, great but then USART 1 will also be set to 9600 , then USART2 will be set 9600 and lastly USART3 will get set to 9600. Equally if I run 'Function(3, Baud_57600) USART3 will be correctly set to 57600 then USART4 will be set to 57600
I have included a section of code which is just repeated ( I cut and paste)
Secondary, there must be a better way to achieve this with less code - an Array possibly suggestion will be welcome
////////////////////////////////////////////////////////////////////////////// // My Setup Configuration // ////////////////////////////////////////////////////////////////////////////// // USART Definitions // Baud rates #define baud_4800 0xCF // UBRR Value used in USART_Setup Switch Statement #define baud_9600 0x67 // UBRR Value USART_Setup Switch Statement #define baud_19200 0x33 // UBRR Value USART_Setup Switch Statement #define baud_28800 0x21 // UBRR Value USART_Setup Switch Statement #define baud_38400 0x19 // UBRR Value USART_Setup Switch Statement #define baud_57600 0x10 // UBRR Value USART_Setup Switch Statement #define baud_76800 0x0C // UBRR Value USART_Setup Switch Statement #define baud_115200 0x07 // UBRR Value USART_Setup Switch Statement #define baud_230400 0x03 // UBRR Value USART_Setup Switch Statement // USART Ports #define initial 0 // used once for initial port 8n1 setup #define USART0 1 #define USART1 2 #define USART2 3 #define USART3 4 // Initial USARTs Setup - Asynchronous, 8 Data, Stop, no Parity // //#define AsyncMode 0b00000000 // 0x00 in UCSRnC //#define CharSize 0b00000110 // 0x06 in UCSRnC //#define StopBit 0b00000000 // 0x00 in UCSRnC //#define ParityMode 0b00000000 // 0x00 in UCSRnC //#define CharSizeH 0b0000000 // 0x00 in UCSRnB #define MyUCSRnC 06 // 0b11001110 (from above) #define MyUCSRnB 00 // 0b00000100 (from above) // Declare my Function void Usart_Setup(uint8_t , uint8_t); //<<<<<<<<<<<<<<<<<<< Start of MAIN >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> int main(void) { Usart_Setup(0,0); // Actual values at runtime 8n1 on all Usarts Usart_Setup(2, baud_19200); // simulated values for debugging Switch return 0; } void Usart_Setup(uint8_t Port, uint8_t Speed) { switch (Port) { case 0: // Setting up Async mode on all ports @8n1 UCSR0C = MyUCSRnC; // Usart 0 UCSR0B = MyUCSRnB; UCSR1C = MyUCSRnC; // Usart 1 UCSR1B = MyUCSRnB; UCSR2C = MyUCSRnC; // Usart 2 UCSR2B = MyUCSRnB; UCSR3C = MyUCSRnC; // Usart 3 UCSR3B = MyUCSRnB; break; case 1: switch (Speed) { case 0xCF: UBRR0H = (uint8_t) (baud_4800>>8); UBRR0L = baud_4800; break; case 0x67: UBRR0H = (uint8_t) (baud_9600>>8); UBRR0L = baud_9600; break; case 0x33: UBRR0H = (uint8_t) (baud_19200>>8); UBRR0L = baud_19200; break; case 0x21: UBRR0H = (uint8_t) (baud_28800>>8); UBRR0L = baud_28800; break; case 0x19: UBRR0H = (uint8_t) (baud_38400>>8); UBRR0L = baud_38400; break; case 0x10: UBRR0H = (uint8_t) (baud_57600>>8); UBRR0L = baud_57600; break; case 0x0C: UBRR0H = (uint8_t) (baud_76800>>8); UBRR0L = baud_76800; break; case 0x07: UBRR0H = (uint8_t) (baud_230400>>8); UBRR0L = baud_230400; break; case 0x03F: UBRR0H = (uint8_t) (baud_4800>>8); UBRR0L = baud_4800; break; default: break; }
Thnaks