This code is tested on AVR ATmega8 microcontroller with buzzer, 16x2 LCD and IR proximity sensors. In this code, time taken is measured for a vehicle to cross fix distance. And from obtained time and distance, speed is culcutated. Buzzer is used to sound when time measurement is starts and then at the time of crossing the second point where time measurement is ended.
Note : Pin connection should be in according to code below. if any confusion then ask in comments below...
//start global variables
char str[10] = "000";
unsigned char body_temp_str[7]="000 ";
unsigned char heart_beat_str[4]="00 0";
unsigned char in_char =0;
unsigned char out_char =0;
float time = 1;
float speed = 0;
// End global variables
// LCD module connections
sbit LCD_RS at PORTB0_bit;
sbit LCD_EN at PORTB1_bit;
sbit LCD_D4 at PORTB2_bit;
sbit LCD_D5 at PORTB3_bit;
sbit LCD_D6 at PORTB4_bit;
sbit LCD_D7 at PORTB5_bit;
sbit LCD_RS_Direction at DDB0_bit;
sbit LCD_EN_Direction at DDB1_bit;
sbit LCD_D4_Direction at DDB2_bit;
sbit LCD_D5_Direction at DDB3_bit;
sbit LCD_D6_Direction at DDB4_bit;
sbit LCD_D7_Direction at DDB5_bit;
// End LCD module connections
//start buttons
#define button_PIN1 PINC
#define button_DDR1 DDRC
#define button_PORT1 PORTC
#define button1_1 2
#define button1_2 3
#define button1_3 4
void init_buttons(){ //initialise buttons
button_DDR1 &= ~((1<<button1_3)|(1& lt;<button1_2)|(1<&l t;button1_1));
button_PORT1 |= (1<<button1_3)|(1< ;<button1_2)|(1<< button1_1);
}
//End buttons
//Start Sensors
#define sensors_PIN1 PINC
#define sensors_DDR1 DDRC
#define sensors_PORT1 PORTC
#define sensor1_0 0
#define sensor1_1 1
//define sensor1_2 2
void init_sensors(){ //initialise sensors
sensors_DDR1 &= ~((1<<sensor1_0)|(1& lt;<sensor1_1));
sensors_PORT1 &= ~((1<<sensor1_0)|(1& lt;<sensor1_1));
}
//End sensors
// Start outputs
#define outputs_PORT1 PORTD
#define outputs_DDR1 DDRD
#define buzzer 0 //pin number
//#define relay 1 //pin number
void init_outputs(){ //initialise outputs
outputs_DDR1 |= 1<<buzzer;
//outputs_DDR1 |= 1<<relay;
}
//End outputs
/********************* ************************** ************************** ******
* MAIN PROGRAM
********************** ************************** ************************** *****/
void main(){
Lcd_Init(); // Initialize Lcd
init_buttons();
init_sensors();
init_outputs();
outputs_PORT1 &=~(1<<buzzer); //buzzer OFF
Delay_ms(100); //wait for UART Initialize
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,1,"HWAY SPD CHECKER");
Lcd_Out(2,1,"Initiati ng.......");
Delay_ms(1000);
< div style="background:#eeeeee; border:1px solid #cccccc; padding:5px 10px"> while(1){
////////////////////// /////////////ENTRY start///////////////////// //////////////
unsigned time = 1;
unsigned int speed = 0;
if((sensors_PIN1 & (1<< sensor1_1)))
{ outputs_PORT1 |= (1<<buzzer); //buzzer ON
Lcd_Out(2,1,"SP D calculating..");
Delay_ms(10);
outputs_PORT1 &=~(1<<buzzer); //buzzer OFF
while(1) {
time++;
if((sensors_PIN1 & (1<< sensor1_0))) {
break;
}
Delay_ms(10);
}
speed = (1000/time);
FloatToStr(speed, str);
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,1,"HWAY SPD CHECKER");
Lcd_Out(2,1,"SPEED=&q uot;);
Lcd_Out(2,7," ");
Lcd_Out(2,7,str);
Delay_ms(2000);
if(!(button_PIN1 & (1<< button1_1))) {
if(speed < 41) {
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_ OFF); // Cursor off
Lcd_Out(1,1,"HI GHWAY SPD CHECKER");
Lcd_Out(2,1,"SP EED =");
Lcd_Out(2,7," ");
Lcd_Out(2,7,str);
Lcd_Out(2,13,"L OW ");
outputs_PORT1 |= (1<<buzzer); //buzzer ON
Delay_ms(5000);
Delay_ms(5000);
Delay_ms(5000);
Delay_ms(5000);
outputs_PORT1 &=~(1<<buzzer); //buzzer OFF
goto start;
}
}
Comments...
Prajna: Can you explain the program once please?
Mahesh Nigam(Scientist): @Prajna: ok, where do you getting problem?
Rohit: I need a circuit diagram... would you help me with that?
Admin: Circuit diagram is very simple, go through above codes and note down which pin is connected to which pin of the components. Main components are ir obstacle detector (2pcs port C0 and C1), buzzer (port D0), relay (port D1), button micro switch (port C3) and 16x2LCD (port B0-B5)
Prem kumar: Can you please send me the circuit diagram.
Atharv: Can you give the circuit diagram and more simplified code,?
Write Your Comment