You are on page 1of 5

/*

* GccApplication8.c
*
* Created: 15-10-2020 13:25:50
* Author : hp
*/

#include <avr/io.h>
//header to enable data flow control over pins
#define F_CPU 1000000
//telling controller crystal frequency attached
#include <util/delay.h>
//header to enable delay function in program
#define E 5
//giving name “enable” to 5th pin of PORTD, since it Is connected to LCD enable pin
#define RS 6
//giving name “registerselection” to 6th pin of PORTD, since is connected to LCD RS
pin
void send_a_command(unsigned char command);
void send_a_character(unsigned char character);
void send_a_string(char *string_of_characters);
static volatile int pulse = 0;//integer to access all though the program
static volatile int i = 0;// integer to access all though the program
int main(void)
{
DDRB = 0xFF;
//putting portB output pins
DDRD = '00b11111011';
_delay_ms(50);
//giving delay of 50ms
DDRA = '00FF';//Taking portA as output.
GICR|=(1<<INT0);//enabling interrupt0
MCUCR|=(1<<ISC00);//setting interrupt triggering logic change
int16_t COUNTA = 0;//storing digital output
char SHOWA [3];//displaying digital output as temperature in 16*2 lcd
send_a_command(0x01); //Clear Screen 0x01 = 00000001
_delay_ms(50);
send_a_command(0x38);//telling LCD we are using 8bit command /data mode
_delay_ms(50);
send_a_command(0b00001111);//LCD SCREEN ON and courser blinking

while(1)

{
PORTD|=(1<<PIND0);
_delay_us(15);///triggering the sensor for 15usec
PORTD &=~(1<<PIND0);
COUNTA = pulse/58;
if((PINC&0b00010000)==0)
PORTC=0X02;
else PORTC=0XF8;
if (pulse>=500)
PORTC=0X05;
send_a_string ("DISTANCE=");// displaying name
itoa(COUNTA,SHOWA,10); //command for putting variable number in LCD(variable
number, in which character to replace, which base is variable(ten here as we are
counting number in base10))
send_a_string(SHOWA); //telling the display to show character(replaced by
variable number) after positioning the courser on LCD
send_a_string ("cm ");
send_a_command(0x80 + 0); //retuning to first line first shell
}
}
ISR(INT0_vect)//interrupt service routine when there is a change in logic
level
{
if (i==1)//when logic from HIGH to LOW

{
TCCR1B=0;//disabling counter
pulse=TCNT1;//count memory is updated to integer
TCNT1=0;//resetting the counter memory
i=0;
}
if (i==0)//when logic change from LOW to HIGH
{
TCCR1B|=(1<<CS10);//enabling counter
i=1;

}
void send_a_command(unsigned char command)
{
PORTA = command;
PORTD &= ~ (1<<RS); //putting 0 in RS to tell LCD we are
sending command
PORTD |= 1<<E; //telling LCD to receive command /data at the
port
_delay_ms(50);
PORTD &= ~1<<E;//telling LCD we completed sending data
PORTA= 0;
}
void send_a_character(unsigned char character)
{
PORTA= character;
PORTD |= 1<<RS;//telling LCD we are sending data not commands
PORTD |= 1<<E;//telling LCD to start receiving command/data
_delay_ms(50);
PORTD &= ~1<<E;//telling LCD we completed sending data/command
PORTA = 0;
}
void send_a_string(char *string_of_characters)
{
while(*string_of_characters > 0)
{
send_a_character(*string_of_characters++);
}
}
Code for ultrasonic sensor

/*
* GccApplication9.c
*
* Created: 15-10-2020 21:56:34
* Author : hp
*/
/* Ultrasonic_Sensor.cpp
*
* Created: 8/7/2017 12:06:09 PM
*/

#include <avr/io.h>
#include <avr/interrupt.h>
#define F_CPU 16000000UL
#include <util/delay.h>
#include <stdlib.h>
#include <string.h>

#define Trigger_pin PIND0 /* Trigger pin */

int main(void)
{
char string[10];
long count;
double distance;
DDRB = 0xff;
DDRD = 0x01;
PORTD = 0xff;
DDRA = 0xff;
DDRD |= 1<<PIND5;
DDRC &=~1<<PINC2;
sei(); /* Enable global interrupt */
TIMSK = (1 << TOIE1); /* 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 */
PORTD |= (1 << Trigger_pin);
_delay_us(10);
PORTD &= (~(1 << Trigger_pin));

TCNT1 = 0; /* Clear Timer counter */


TCCR1B = 0x41; /* Capture on rising edge, No pr scaler*/
TIFR = 1<<ICF1; /* Clear ICP flag (Input Capture flag) */
TIFR = 1<<TOV1; /* Clear Timer Overflow flag */

/*Calculate width of Echo by Input Capture (ICP) */

while ((TIFR & (1 << ICF1)) == 0);/* Wait for rising edge */
TCNT1 = 0; /* Clear Timer counter */
TCCR1B = 0x01; /* Capture on falling edge, No pr scaler */
TIFR = 1<<ICF1; /* Clear ICP flag (Input Capture flag) */
TIFR = 1<<TOV1; /* Clear Timer Overflow flag */

while ((TIFR & (1 << ICF1)) == 0);/* Wait for falling edge */
/* 16MHz Timer freq, sound speed =343 m/s */
distance =(double)count /1000;
dtostrf(distance, 2, 2, string);/* distance to string */
strcat(string, " cm "); /* Con cat unit i.e.cm */
_delay_ms(200);

}
}

You might also like