/* UART Transmitter Code */
void init_USART0(){
volatile uint16_t UBRR_value = 103; // UBBR0 = ((clock frequency/(16*baud rate)) - 1)
// UBBR0 = ((16,000,000/(16*9600)) -1) = 103
UBRR0H = (unsigned char)(UBRR_value >> 8);
UBRR0L = (unsigned char) UBRR_value; // Select the 9600 baud rate
UCSR0B = (1 << RXEN0) | (1 << TXEN0); // enable Tx and Rx
UCSR0C = (1 << USBS0) | (3 << UCSZ00); // set 2 stop bit & 8 data bit
}
void setup() {
DDRD &= ~4; // set port-D 2nd pin as Input
DDRB = 1; // set port-B 0th pin as Output
init_USART0();
while(1){
if((PIND & 4)==0){
PORTB ^= 1;
while(!(UCSR0A & (1 << UDRE0))); // wait untill the UDRE0 bit is set
UDR0 = 'A';
for(volatile long i=0; i<100000; i++); // switch debouncing delay
}
}
}
void loop() {}