Project 2 - Measuring distance of an object using ultrasonic sensor (HC-SR04)
#include <avr/io.h>
#define F_CPU 16000000ul
#include <util/delay.h>
#include <avr/interrupt.h>
#include <string.h>
#include <stdlib.h>
#define LCD_Data_Dir DDRD // define LCD data port direction
#define LCD_Command_Dir DDRC // define LCD command port direction
#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(data reg./command reg.)
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_String(char*); // send string to LCD
void LCD_String_xy (char,char,char*); // send row position
void LCD_Command (char cmd) // LCD command write function
{
LCD_Data_Port = cmd; // write command to LCD data port
LCD_Command_Port&=~((1<<RS)|(1<<RW)); // make RS low (command reg.) and RW low(write)
LCD_Command_Port|= (1<<EN); // high to low transition on EN
_delay_ms(1);
LCD_Command_Port&=~(1<<EN);
_delay_ms(3); // delay provided
}
void LCD_Char(char char_data) // LCD data write function
{
LCD_Data_Port= char_data; // write data to LCD data port
LCD_Command_Port&=~(1<<RW); // make RW low(write)
LCD_Command_Port|=((1<<EN)|(1<<RS)); // make RS high and high to low transition
_delay_ms(1);
LCD_Command_Port&=~(1<<EN);
_delay_ms(1); //wait little bit
}
void LCD_init(void) // LCD initialize function
{
LCD_Command_Dir|= (1<<RS)|(1<<RW)|(1<<EN); // LCD command port direction as output
LCD_Command_Dir = 0xff; //make LCD data port direction as output
_delay_ms(20); // LCD power up time to get things ready. it should always >15ms
LCD_Command(0x38); // initialize 16x2 LCD 8 bit
LCD_Command(0x0C); // display On cursor off command
LCD_Command(0x06); // auto increment cursor
LCD_Command(0x01); // clear LCD command
LCD_Command(0x80); // 8 is the line and 0 is other position
}
void LCD_String(char*str) // send string to LCD function
{
int i;
for(i=0;str[i]!=0;i++) // sending each char string till the null
{
LCD_Char (str[i]); // call LCD data write
}
}
void LCD_String_xy(char row,char pos,char*str) // send string to LCD function
{
if (row == 1)
LCD_Command((pos & 0x0f)|0x80); //command of first row and required positions<16
else if (row == 2)
LCD_Command((pos & 0x0f)|(0xC0)); //command of 2nd row and required positions<16
LCD_String(str); // call LCD string function
}
#define trigger_pin PA0 // trigger pin
int TimerOverFlow = 0;
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; // pull-up turn on
LCD_init();
LCD_String_xy(1,0,"ultrasonic");
sei(); // enable global interrupt
TIMSK = (1<< TOIE1); // enable timer 1 overflow interrupt
TCCR1A = 0; // set all bit to zero normal position
while(1)
{ //give 10 us trigger pulse on trig.pin to HC-SR04
PORTA |= (1<< trigger_pin);
_delay_ms(10);
PORTA &=(~(1<< trigger_pin));
TCNT1 = 0; //clear timer counter
TCCR1B = 0x41; // capture on rising edge, no pre-scaler
TIFR = 1<<ICF1; // clear ICP 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, no prescaler
TIFR = 1<<ICF1; // clear ICP 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 to string
strcat(string,"cm"); // count unit
LCD_String_xy(2,0,"dist= ");
LCD_String_xy(2,7,string); // print distance
_delay_ms(200);
}
}
