Challenge

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

/*
* project2.c
*
* Created: 09-12-2022 15:27:22
* Author : sarat
*/
#define F_CPU 16000000L
#include
#include
#include

#define rs_high() PORTD |=(1<<0);
#define rs_low() PORTD &= ~(1<<0);

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

#define lcdport PORTB
#define Trigger_pin PA0                                        //Trigger pin

uint8_t t = 0;


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);               //2 lines and 5×7 matrix

_delay_ms(5);
lcd_command(0x02);               //reset home

_delay_ms(5);
lcd_command(0x01);               //Clear display screen

_delay_ms(5);
lcd_command(0x0c);               //Display ON, cursor OFF

_delay_ms(5);
lcd_command(0x06);              //Increment cursor (shift cursor to right)

_delay_ms(5);
lcd_command(0x80);              //Force cursor to beginning of first line

_delay_ms(5);

}
void lcd_print(char row, char coloumn, unsigned int value, int digits)
{
unsigned int temp;
unsigned int unit;
unsigned int tens;
unsigned int hundred;
unsigned int thousand;
unsigned int million;
unsigned int tmillion;
unsigned char flag=0;

if(row==0||coloumn==0)
{
cursorpos(1,1);
}
else
{
cursorpos(row,coloumn);

}
if(digits==6|| flag==1)
{
tmillion=value/100000 +48;
lcd_data(tmillion);
flag=1;
}
if(digits==5|| flag==1)
{

million=value/10000+48;
lcd_data(million);
flag=1;
}
if(digits==4 || flag==1)
{

temp = value/1000;
thousand = temp%10 +48;
lcd_data(thousand);
flag=1;
}
if(digits==3 || flag==1)
{

temp = value/100;
hundred = temp%10 +48;
lcd_data(hundred);
flag=1;
}
if(digits==2 || flag==1)
{

temp = value/10;
tens= temp%10 +48;
lcd_data(tens);
flag=1;
}
if(digits==1 || flag==1)
{

unit = value%10 + 48;
lcd_data(unit);
}
if(digits>6)
{
lcd_data('e');
}
}


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)
{
double count,a,b;
double distance;
//DDRC |=(1<<7);
DDRB =0xFF;                             //port b as output for lcd data
DDRD |=(1<<0)|(1<<1);            //O AND 1 PINS OF PORT D AS OUPUT FOR rs and en of lcd
DDRA = 0xFF;                             // Make trigger pin as output
PORTD = (1<<6);                       // Turn on Pull-up at 6th bit for echo input
lcd_init();
sei();                                         // Enable global interrupt
TIMSK |= (1<<2);                     // Enable Timer1 overflow interrupts
TCCR1A = 0;                             //Set all bit to zero Normal operation

while(1)
{
/* Give 10us trigger pulse on trig. pin to HC-SR04 */
PORTA = 0xFF;
_delay_us(10);
PORTA = 0x00;                         // (~(1 <<0));

TCNT1 = 0;                                          // Clear Timer counter
TCCR1B = 0x41;                                  // Capture on rising edge, No prescaler
TIFR = 1< TIFR = 1< while ((TIFR & (1 << ICF1)) == 0);            // Wait for rising edge using input capture bit of TIFR
TCNT1 = 0;                                               // Clear Timer counter
TCCR1B = 0x01;                                        // Capture on falling edge, No prescaler
TIFR = 1< TIFR = 1< t = 0;                                                       // Clear Timer overflow count

while ((TIFR & (1 << ICF1)) == 0);            // Wait for falling edge
b=ICR1;
a=65535 * t;
count =a+b;                                            // Take count
distance = count /932.95 ;                       //((34300*count*0.0625microsec)/2)
lcd_out(1,1,"distance is");
lcd_print(2,1,distance,5);
lcd_out(2,8,"cm");
_delay_ms(100);
}
}

ISR(TIMER1_OVF_vect)
{
t=t+1;                           // Increment Timer Overflow count
}

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.