Challenge

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

Challenge: Project 2

The implementation of code for “Measuring distance of an object using ultrasonic distance sensor (HC-SR04)

Code:

#include <avr/io.h>

#define F_CPU 16000000L

#include <util/delay.h>

#define rs_high() PORTD |=(1<<2);

#define rs_low() PORTD &= ~(1<<2);

#define en_high() PORTD |= (1<<3);

#define en_low() PORTD &= ~(1<<3);

#define lcdport PORTB

void lcd_command(unsigned char cmd)

{

lcdport = cmd;

rs_low();

en_high();

_delay_ms(1);

en_low();

}

void lcd_data(unsigned char dat)

{

lcdport = dat;

rs_high();

en_high();

_delay_ms(1);

en_low();

}

void lcd_init()

{

lcd_command(0x38);_delay_ms(5);

lcd_command(0x02);_delay_ms(5);

lcd_command(0x01);_delay_ms(5);

lcd_command(0x0c);_delay_ms(5);

lcd_command(0x06);_delay_ms(5);

lcd_command(0x80);_delay_ms(5);

}

void cursorpos(char x, char y)

{

if( (x < 1 || x > 2) || (y < 1 || y > 16) )

{

x = 1;

y = 1;

}

if(x == 1)

lcd_command(0x7F+y);

else

lcd_command(0xBF+y);

}

void lcd_out(char x,char y,char *str)

{

cursorpos(x,y);

while(*str)

{

lcd_data(*str);

str++;

}

}

int main(void)

{

DDRB =OXFF;

DDRD|=(1<<3);

DDRD|=(1<<2);

int i = 0;

lcd_init();

while(1)

{

lcd_out(1,4, "This is");

lcd_out(2,3 "session 39");

i++;

}

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.

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.