#define F_CPU 16000000UL // 16MHz clock from the debug processor
#define BAUD 9600 // set the baud rate to 9600
#define BAUDRATE ((F_CPU/(16UL*BAUD)) - 1) // calculate the baudrate
#include <avr/io.h> // library to be able to use the functions
#include <util/delay.h> // time delay library
#include <string.h> // include string library
void initialize() {
// clear the high bits because they are not needed for this lab. Don't need that many bits
UBRR0H = (BAUDRATE>>8);
UBRR0L = BAUDRATE; // set the Low byte register to the BAUDRATE
UCSR0B |= (1<<TXEN0 ) ; // enable the transmitter
UCSR0C |= (1<<UCSZ00) | (1<<UCSZ01); // set to 8 bit data format
// don't need to set the stop bit since it is 1 by default
}
void Transmit(char strChar) {
while ( !( UCSR0A & (1<<5) ) ) {} // Wait for empty transmit buffer (isolates the 5th bit)
UDR0 = strChar;
}
int main(void) {
char userString[] = "827695876\r\n"; // string for the RedID characters
int strSize = sizeof(userString); // get the size of the string
initialize()
while(1){
for (int i = 0; i < strSize; i++) {
Transmit(userString[i]);
}
_delay_ms(500); // 500ms delay
}
}
The program uses six USART registers: UCSR0A, UCSR0B, UCSR0C, UBRR0H, UBRR0L. UCSR0A is used to check for the transmit buffer's status: busy or empty. UCSR0B is used to enable or disable the receiver and transmitter; in this case, it was enabled. UCSR0C is used to configure the 8 bit data format. UBRR0H and UBRR0L are used to set the baud rate for the high and low bits of UBRR0 - the USART baud rate register. In addition to registers, variables are also utilized in the code. The variable ‘userString[]’ is an array that is used to store the string that will be transmitted. The variable following it,’strSize’ stores the number of characters in the array ‘userString[]’. Other variables such as F_CPU, BAUD, and BAUDRATE are also used in the program. While F_CPU defines the CPU clock frequency, BAUD specifies the baud rate for the USART communications. Lastly, BAUDRATE stores the calculated value that is stored in UBRR0.
The FT232R Breakout board and the Atmel Xplained Mini board were utilized for this assignment. Pin PD1 on the Atmel board was connected to the TXD pin on the FT232R board using a wire to set up the transmission. The grounds of both boards were also connected.