Circuit for Toolstick 850
There is a change of port when the toolstick 850 is used. Toolstick 850 only consists a port P0, P1 and P2. Because of the pin is limited there a few changes on coding and circuit as well. for the toolstick 850 circuit we used 4 bit rather than 8 bit cause by the limitation on the pin port of the toolstick. We also change from 4 switch button to 2 switch button that already have in toolstick 850.
Circuit:
Coding:
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <reg51.h>
#include <stdio.h>
#define lcd_clear 0x01
#define return_home 0x02
#define dec_cursor 0x04
#define inc_cursor 0x06
#define display_shift_left 0x05
#define display_shift_right 0x07
#define disp_off_cursor_off 0xE0
#define disp_on_cursor_off 0x0C
#define disp_on_cursor_blink 0x0F
#define shift_left_cursor 0x10
#define shift_right_cursor 0x1C
#define first_line 0x80
#define second_line 0xC0
#define lcd_2line 0x38
#define lcd_4bit 0x28
#define lcd_goto1 lcd_command(first_line)
#define lcd_goto2 lcd_command(second_line)
#define lcd_clr lcd_command(lcd_clear)
#define LCD_port P0
sbit RS = P0^1;
sbit EN = P0^2;
sbit SW1 = P1^7;
sbit SW2 = P2^1;
//const unsigned char set_lcd_4bit[7]={0x20, 0x20, 0x80, 0x00, 0xe0, 0x00, 0x60};
// ------ Private variable definitions ------------------------
signed char buf[20];
void delay_display(unsigned int gap)
{
unsigned int i, j;
for(i=0; i<gap; i++)
for(j=0; j<200; j++);
}
//**********delay function**********//
void delay_cycle(unsigned int interval)
{
unsigned int i, j;
for(i=0; i<interval; i++)
{
for(j=0; j<20; j++);
}
}
//**********LCD Program**********//
void send_nibble(unsigned char value)
{
LCD_port=(LCD_port&0x0F)|(value&0x0F0);
EN=1;
delay_cycle(7);
EN=0;
LCD_port=(LCD_port&0x0F)|((value<<4)&0x0F0);
EN=1;
delay_cycle(7);
EN=0;
}
void lcd_command(unsigned char value)
{
RS=0;
delay_cycle(1);
send_nibble(value);
return;
}
void lcd_data(unsigned char value)
{
RS=1;
delay_cycle(1);
send_nibble(value);
return;
}
void lcd_init(void)
{
LCD_port =0x0F0;
lcd_command(0x33);
lcd_command(0x32);
lcd_command(lcd_4bit); // 4 bit mode
lcd_command(shift_left_cursor); // shift cursor left
lcd_command(inc_cursor); // increment cursor
lcd_command(disp_on_cursor_off); // display on cursor off
lcd_command(lcd_clear); // clear LCD
delay_cycle(20);
}
void lcd_putc(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)
{
unsigned char limit=0;
while(*txt)
lcd_putc(*txt++);
}
void lcd_goto(unsigned char line, unsigned char column)
{
if(line==1){lcd_command(first_line+column);}
if(line==2){lcd_command(second_line+column);}
}
//-----------------------------------------------------------------------------
// main() Routine
// ----------------------------------------------------------------------------
void main(void)
{
unsigned int x=0,n=0, y=0;
lcd_init();
lcd_goto1;
lcd_puts(" HAFIZ ");
delay_display(250);
lcd_goto2;
lcd_puts(" SYAFIQ ");
delay_display(500);
lcd_command(0x01);
lcd_goto1;
lcd_puts(" DIGITAL ");
delay_display(250);
lcd_goto2;
lcd_puts(" SPEEDOMETER");
delay_display(300);
lcd_command(0x01);
while(1)
{
lcd_goto1;
lcd_puts(" KM/H = ");
sprintf(buf,"%d",y);
lcd_puts(buf);
if (SW1==0)
{
n++;
delay_display(100);
if(n<=1)
{y=20;}
else if(n<=2)
{y=40;}
else if(n<=3)
{y=60;}
else if(n<=4)
{y=80;}
else if(n<=5)
{y=100;}
else if(n<=6)
{y=120;}
else if(n<=7)
{y=140;}
}
if (SW2==0)
{
n++;
delay_display(100);
if(n<=8)
{y=140;}
else if(n<=9)
{y=120;}
else if(n<=10)
{y=100;}
if(n<=11)
{y=80;}
else if(n<=12)
{y=60;}
else if(n<=13)
{y=40;}
else if(n<=14)
{y=20;}
else if(n<=15)
{y=0;}
}
}
}

No comments:
Post a Comment