Project 2 - Measuring distance of an object using ultrasonic sensor (HC-SR04)
MAGNA
question 6b:
#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_cmd(unsigned char cmd)
{
lcdport=cmd; //assign formal argument to PORTB
rs_low; //register select D.1 =0; to write command
en_high; //enable =1;
_delay_ms(1); //delay of 1ms
en_low; //enable =0;
}
void lcd_data(unsigned char data)
{
lcdport=data;
rs_high; //register select D.1=1; to write data
en_high;
_delay_ms(1);
en_low;
}
void lcd_init()
{
lcd_cmd(0x02); //to set cursor to row 0 , column 0
lcd_cmd(0x38); //set the operation in 8 bit mode ,5x7 matrix, 2 line mode
lcd_cmd(0x0E); //cursor on
lcd_cmd(0x01); //clear the display and set cursor to row 0, column 0
lcd_cmd(0x0c); //turn off the cursor
}
void cursor_pos(char x,char y) //user defined function for cursor position
{
if(x==2)
lcd_cmd(0xBF+y); //set the cursor in row 2 and column=y position
else
lcd_cmd(0x7F+y); //set the cursor in row 1 and column=y position
}
void lcd_out(char x,char y, char *str) //user defined function to display string data on LCD
{
cursor_pos(x,y); //calling the function
while(*str) //loop runs until it meets '\0' until the end of the string
{
lcd_data(*str); //*str gives the char present in the address of str
str++; //increment the pointer to next position
}
}
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 char flag=0;
if(row==0||coloumn==0)
{
cursor_pos(1,1);
}
else
{
cursor_pos(row,coloumn);
}
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>5)
{
lcd_data('e');
}
}
int adc_read(char channel)
{
unsigned int a,result;
ADMUX = channel; // selecting the adc channel
ADCSRA = 0x80; //enabling the ADC
ADCH = 0x00; //initialising the ADC registers to hold higher byte
ADCL = 0x00; // initialising the ADC registers to hold lower byte
ADCSRA |=(1 << 6); //enable the ADC start conversion bit
//wait till the conversion get finished
while( ( ADCSRA & 0X40) != 0);
a = ADCL; //store lower byte data
result = ADCH; // store higher byte data
result = result << 8; //combine both register using bitwise left shift operator
result = result | a;
return result;
}
int main(void)
{
unsigned int volt;
lcd_init();
while(1)
{
volt = adc_read(3);
lcd_out(1,1,"Digi value="); // function call
lcd_print(1,12,volt,4); //function call
lcd_out(2,1,"Temperature=");
volt=(volt/2.048); //value mapping for 10mv variation = 1 degree
lcd_print(2,13,volt,3);
lcd_out(2,16,"C");
_delay_ms(1000); //delay of 1sec
}
}
Output:

Project 2:
Project team members:
- Ratan Sanjay Palkar
- Goutham S
- Shobhith KC
- Gokulkrishna S
- Karthikeya Udupa
Project description:
We built our code into different modules and used modular programming in our project.
Please find the following files in file section.
Our project involves
- Controlling vehicle speed (DC motor) through throttle (potentiometer )using adc and PWM.
- Accessign the ultrasonic sensor and measure the distance between the vehicle and the obstacle.
- Measuring the temperature through sensor(potentiometer as temperature sensor).
- Display all the measured parameters on the 2x16 LCD display.
Output:
