#include <avr/io.h>
#define F_CPU 8000000 // CPU @ 8MHZ
#define USART_BAUDRATE 9600 // SERIAL Communication @ 9600 baud
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1) // baudrate formula
#include <util/delay.h> // header file defining delay
int main(void) // main code begins
{
DDRB = 0xff; // PORT D as output
DDRD = 0xfc; // PORT D PIN 2-7 as output.
UCSRB |= (1 << RXEN) | (1 << TXEN); // Enable Rx & Tx of USART
UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // 8bit, 1 Stop bit, no parity
UBRRL = BAUD_PRESCALE; // setting baud rate @ 9600 baud
UBRRH = (BAUD_PRESCALE >> 8);
while(1) // loop forever
{
//bot1 code
unsigned char s=0; // for use as a counter
unsigned char a[] = {0x23, 0x02, 0x30, 0x52, 0x00}; // Tx packet in the forrmat of //char array a[] = {0x23, 0x02, 0x30, 0x52, 0x00}, where 0x00 is not the part of ac-tual //tx packet but is rather used to indicate within this code the end of packet.
while (a[s] != '\0') // Loop to send tx packet through USART while scanning for ‘\0’
{
while ((UCSRA & (1 << UDRE)) == 0); // Loop until UDR buffer is ready to re-ceive // next byte to be transmitted.
UDR = a[s]; // Add next byte to be transmitted to the UDR buffer
s++; // Increment the counter
_delay_ms(50); // delay of .02ms
}
_delay_ms(2000); // delay of 2 sec
s=0; // reset counter
a[3] = 0x53; //modify char array to a[] = {0x23, 0x02, 0x30, 0x53, 0x00},
while (a[s] != '\0') // Loop to send tx packet through USART while scanning for ‘\0’
{ // While loop opened.
while ((UCSRA & (1 << UDRE)) == 0); // Loop until UDR buffer is ready to re-ceive // next byte to be transmitted.
UDR = a[s]; // Add next byte to be transmitted to the UDR buffer
s++; // Increment the counter
_delay_ms(50); // delay of .02ms
}