Hello forum,
I bought two arduino pro mini boards based on ATmega328P.
I wrote a program in Atmel Studio and I used avrdude and an ISP programmer (usbasp) to flash the hex.
My intention was to connect that tiny board with an RFM95 (LoRa module).
I didn't want to make a PCB from scratch. I found those boards very cheap and I decided to use them to make some LoRa nodes.
The problem is that UART seems lose its synchronization when timer0 is enabled.
Same thing happens when I use timer1 instead of timer0.
when the function SystemTimerInit() is executed, UART stopped working for some reason and I read gibberish on my terminal.
when timer0 (or timer1) is disabled it works fine.
Fuses are: (E:FD, H:D9, L:E2)
Chip runs at 8MHz with internal RC.
Board is powered through the ISP programmer at 3.3V I also used it with other power supply but it didn't work
UART BaudRate is 38400. The error according to datasheet is about 0.2%
This is the code for timer0
void SystemTimerInit( void ) { cpuTimeSinceLastBoot = ( uint64_t )0; TCCR0B = ( 1 << CS01 ) | ( 1 << CS00 ); TIMSK0 = ( 1 << TOIE0 ); //Enable Timer0 Overflow Interrupt TCNT0 = 131; } ISR( TIMER0_OVF_vect ) { cpuTimeSinceLastBoot++; TCNT0 = 131; }
this is the code for UART
void UartInit(void) { UCSR0B |= (1 << RXEN0) | (1 << TXEN0); // Use 8-bit character sizes - URSEL bit set to select the UCRSC register UCSR0C |= (1 << UCSZ00) | (1 << UCSZ01); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register UBRR0H = (uint8_t)(BAUD_PRESCALE >> 8); // Load lower 8-bits of the baud rate value into the low byte of the UBRR register UBRR0L = (uint8_t)BAUD_PRESCALE; } uint8_t UartSendByte(uint8_t data) { /* Wait for empty transmit buffer */ while (!(UCSR0A & (1<<UDRE0))); /* Put data into buffer, sends the data */ UDR0 = (data & 0xFF); return 1; } void PrintLine(char *data_ptr) { while(*data_ptr) { UartSendByte(*(data_ptr++)); } return; }
and this is the main function
int main(void) { UartInit(); sei(); SystemTimerInit(); for (;;) // Loop forever { PrintLine("Test firmware\r\n"); } }