Challenge

Project 2 - Measuring distance of an object using ultrasonic sensor (HC-SR04)

#include <avr/io.h>
#include <util/delay.h>

void USART_Init(unsigned int baud) {
UBRRH = (unsigned char)(baud >> 8);
UBRRL = (unsigned char)baud;
UCSRB = (1 << RXEN) | (1 << TXEN); // Enable receiver and transmitter
UCSRC = (1 << URSEL) | (1 << UCSZ1) | (1 << UCSZ0); // Set frame format: 8data, 1stop bit
}

unsigned char USART_Receive(void) {
while (!(UCSRA & (1 << RXC))); // Wait for data to be received
return UDR; // Return received data from buffer
}

void USART_Transmit(unsigned char data) {
while (!(UCSRA & (1 << UDRE))); // Wait for empty transmit buffer
UDR = data; // Put data into buffer, sends the data
}

int main() {
USART_Init(51); // Set baud rate to 9600
while (1) {
char c = USART_Receive(); // Receive a character
if (c >= 'a' && c <= 'z') { // If character is lowercase
c -= 32; // Convert to uppercase
}
USART_Transmit(c); // Send back the character
}
return 0;
}

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.