For the coding it take a lot of time and even us have to change the circuit from using the sensor to push buttons. By using the push buttons we manage to construct the coding successfully. We set the coding of 4 push buttons differently as the output display from LCD will show with low speed to high speed.
Coding:
// DIGITAL SPEEDOMETER
// MR HAFIZZULLAH AND MR SYAFIQ
// MICRO C MINI PROJECT
#include <reg51.h>
sfr LCD_port = 0xA0; //port 2
sbit RS = P3^0;
sbit RW = P3^1;
sbit EN = P3^2;
sbit SW1 = P1^0; //push button 1
sbit SW2 = P1^1; //push button 2
sbit SW3 = P1^2; //push button 3
sbit SW4 = P1^3; //push button 4
sbit SW5 = P1^4; //push button 5
void delay_ms (int ms)
{
int i, j;
for (i=0; i<ms; i++)
for (j=0; j<100; j++);
}
void lcd_command(unsigned char value)
{
LCD_port = value;
RS = 0;
RW = 0;
EN = 1;
delay_ms(1);
EN = 0;
}
void lcd_init(void)
{
LCD_port = 0;
delay_ms(15);
lcd_command(0x30);
delay_ms(5);
lcd_command(0x30);
delay_ms(1);
lcd_command(0x30);
delay_ms(1);
lcd_command(0x30); //set to 8-bit mode operation
delay_ms(15);
lcd_command(0x30);
delay_ms(5);
lcd_command(0x30);
delay_ms(1);
lcd_command(0x30);
delay_ms(1);
lcd_command(0x30);
lcd_command(0x38); //function set: 8-bit mode, 2 lines, 5x7 font
lcd_command(0x10); //shift cursor position to left
lcd_command(0x06); //increment cursor (shift to right)
lcd_command(0x0C); //display ON, cursor OFF
lcd_command(0x01); //clear display
delay_ms(5);
}
void lcd_data(unsigned char value)
{
LCD_port = value;
RS = 1;
RW = 0;
EN = 1;
delay_ms(1);
EN = 0;
}
void lcd_position(unsigned char line, unsigned char column)
{
unsigned char position;
if (line == 1)
{
position = 0x80 + column;
lcd_command(position);
}
if (line == 2)
{
position = 0xC0 + column;
lcd_command(position);
}
}
void lcd_putchar(unsigned char c)
{
if (c == '\f')
lcd_command(0x01);
else if (c == '\n')
lcd_command(0xC0);
else
lcd_data(c);
}
void lcd_puts(unsigned char *txt)
{
while(*txt)
lcd_putchar(*txt++);
}
void main(void)
{
if (SW1 == 0 )
{
char text1[] = "HAFIZZULLAH";
char text2[] = "SYAFIQ";
lcd_init();
lcd_puts(text1);
lcd_position(2,5);
lcd_puts(text2);
}
if (SW2 == 0)
// while(1)
{
char text3[] = "ENGINE START";
char text4[] = "0 KM/H";
lcd_init();
lcd_puts(text3);
lcd_position(2,3);
lcd_puts(text4);
}
else
if (SW3 == 0)
//while(1)
{
char text5[] = "SLOW SPEED";
char text6[] = "40 KM/H";
lcd_init();
lcd_puts(text5);
lcd_position(2,3);
lcd_puts(text6);
}
else
if (SW4 == 0)
// while(1)
{
char text7[] = "NORMAL SPEED";
char text8[] = "80 KM/H";
lcd_init();
lcd_puts(text7);
lcd_position(2,3);
lcd_puts(text8);
}
else
if (SW5 == 0)
// while(1)
{
char text9[] = "HIHWAY TOP SPEED";
char text10[] = "110 KM/H";
lcd_init();
lcd_puts(text9);
lcd_position(2,3);
lcd_puts(text10);
}
}
No comments:
Post a Comment