Challenge

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

#define F_CPU 16000000UL

#include <avr/io.h>

#include <avr/interrupt.h>

#include <util/delay.h>

#include <string.h>

#include <stdlib.h>

#define LCD_Data DDRD // Define LCD data port direction

#define LCD_Comm DDRC // Define LCD command port direction register

#define LCD_Data_Port PORTD // Define LCD data port

#define LCD_Command_Port PORTC // Define LCD data port

#define EN PC2 // Define Enable signal pin

#define RW PC3 //Define Read/Write signal pin

#define RS PC4 // Define Register Select signal pin

#define Trigger_pin PA0 // Trigger pin

int TimerOverflow = 0;

void LCD_Command (char); // LCD command write function

void LCD_Char (char); // LCD data write function

void LCD_Init (void); // LCD Initialize function

void LCD_print (char*); // Send string to LCD function

void LCD_cursor (char,char,char*);// Send row, position and string to LCD function

void LCD_Command (char cmd) // LCD command write function

{

LCD_Data = cmd;

LCD_Comm &= ~((1<<RS)|(1<<RW));

LCD_Comm |= (1<<EN);

_delay_us(1);

LCD_Comm &= ~(1<<EN);

_delay_ms(3);

}

void LCD_Char (char char_data) // LCD data write function

{

LCD_Data = char_data;

LCD_Comm &= ~(1<<RW);

LCD_Comm |= (1<<EN)|(1<<RS);

_delay_us(1);

LCD_Comm &= ~(1<<EN);

_delay_ms(1);

}

void LCD_Init (void) /* LCD Initialize function */

{

LCD_Comm |= (1<<RS)|(1<<RW)|(1<<EN);

LCD_Data = 0xFF;

_delay_ms(20);

LCD_Command (0x38);

LCD_Command (0x0C);

LCD_Command (0x06);

LCD_Command (0x01);

LCD_Command (0x80);

}

void LCD_print (char *str) // Send string to LCD function

{

int i;

for(i=0;str[i]!=0;i++)

{

LCD_Char (str[i]);

}

}

void LCD_cursor (char row, char pos, char *str) // Send string to LCD function */

{

if (row == 1)

LCD_Command((pos & 0x0F)|0x80);

else if (row == 2)

LCD_Command((pos & 0x0F)|0xC0);

LCD_print(str);

}

ISR(TIMER1_OVF_vect)

{

TimerOverflow++; // Increment Timer Overflow count

}

int main(void)

{

char string[10];

long count;

double distance;

DDRA = 0x01; // Make trigger pin as output

PORTB = 0xFF;//Turn on Pull-up

LCD_Init();

LCD_cursor(1, 0, "Ultrasonic");

LCD_cursor(2, 0, "Dist = ");

sei(); // Enable global interrupt

TIMSK = (1 << TOIE1); // Enable Timer1 overflow interrupts

TCCR1A = 0;

while(1)

{

PORTA |= (1 << Trigger_pin);

_delay_us(10);

PORTA &= (~(1 << Trigger_pin));

TCNT1 = 0;

TCCR1B = 0x41; //Capture on rising edge

TIFR = 1<<ICF1; // Clear ICP flag (Input Capture flag)

TIFR = 1<<TOV1; // Clear Timer Overflow flag

while ((TIFR & (1 << ICF1)) == 0);// Wait for rising edge

TCNT1 = 0; // Clear Timer counter

TCCR1B = 0x01; // Capture on falling edge

TIFR = 1<<ICF1; //Clear ICP flag (Input Capture flag)

TIFR = 1<<TOV1; //Clear Timer Overflow flag

TimerOverflow = 0; //Clear Timer overflow count

while ((TIFR & (1 << ICF1)) == 0); // Wait for falling edge

count = ICR1 + (65535 * TimerOverflow);//Take count

distance = (double)count / 466.47;

dtostrf(distance, 2, 2, string);// distance

strcat(string, " cm ");

LCD_cursor(2, 7, string); //Print distance

_delay_ms(200);

}

}

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.