I'm using a mega3208 and trying to understand the main clock, and prescaler activity.
I would like the main clock to run at 20Mhz, so my fuses are thus:
So, FREQSEL is 20Mhz and OSCLOCK is clear.
Then my startup code looks like this:
CLKCTRL.MCLKLOCK = 0; // Ensure clock-lock is clear. CLKCTRL.MCLKCTRLB = 0; // Switch off the pre-scaler. uart_init(0, 115200UL);
Then uart_init(...) like this: (F_PER is: #define F_PER 20000000)
void uart_init(uint8_t uart, uint32_t baud) { if(uart >= UART_MAX_UARTS) // sanity check. { return; } // First set up the ring buffers for this UART ring_buffer_init(&uart_rx_rb[uart], &uart_rx_buffer[uart][0], UART_RX_BUFF_SIZE); ring_buffer_init(&uart_tx_rb[uart], &uart_tx_buffer[uart][0], UART_TX_BUFF_SIZE); // Now configure the registers for this uart if(uart==0) { PORTA_DIRSET = (1<<0); // TX on PA0 PORTA_DIRCLR = (1<<1); // RX on PA1 PORTA_OUTSET = (1<<0); // set TX high. // Baud USART0_BAUD = ((64*F_PER) / (16*baud)); // RX Interrupt enable USART0_CTRLA = USART_RXCIE_bm; // Transmitter / receiver enable USART0_CTRLB = USART_RXEN_bm | USART_TXEN_bm; } }
And then I send 0x55 out of uart, and check it with my oscilloscope, thus:
Which is clearly 19200 bps, not 115200 bps.
But - that is exactly 1/6th of the desired rate, which is what the default clock prescaler is. But that should be off, as per my startup code.
No matter what I do with the PEN bit of MCLKCTRLB, the baudrate remains the same. It does not change either way.
What have I missed?