#include
#include
#define uchar unsigned char
void delay_1ms();
void delay_10us();
void delay_10us()
{
unsigned int i =100;
i--;
i--;
i--;
}
void delay_1ms()
{
uchar i,j;
for(i=0;i>2;i++)
{
for(j=0;j<20;j++)
;
}
}
bool lcd_1602_busy()
{
uchar tmp;
P1DIR = 0xff;
P1OUT = 0xff;
//lcd_1602_RW = 1;
//lcd_1602_RS = 0;
P2DIR=0xff;
P2OUT |=BIT1;//BIT1=0x0002
P2OUT &=~BIT2;//bit2=0x0004 =0000 0100 ~bit2 =1111 1011
P2OUT |=BIT0;
delay_10us();
P1DIR =0x00;
tmp = P1IN;
P2OUT &=~BIT0;
//lcd_1602_EN = 0;
return (bool)(0x80 & tmp);
}
void lcd_1602_write_cmd(uchar cmd)
{
while(lcd_1602_busy());
P2DIR=0xff;
P2OUT &=~BIT1;
//lcd_1602_RW = 0;
P2OUT &= ~BIT2;
//lcd_1602_RS = 0;
P1DIR =0xff;
P1OUT =cmd;
//P0 = cmd;
P2OUT |=BIT0;
//lcd_1602_EN = 1;
delay_10us();
P2OUT &= ~BIT0;
//lcd_1602_EN = 0;
}
void lcd_1602_write_data(uchar dat)
{
while(lcd_1602_busy());
P2DIR =0xff;
P2OUT &=~BIT1;
//lcd_1602_RW = 0;
//lcd_1602_RS = 1;
//P0 = dat;
P2OUT |=BIT2;
P1DIR=0xff;
P1OUT=dat;
//lcd_1602_EN =1;
delay_10us();
P2OUT &= ~BIT0;
//lcd_1602_EN = 0;
}
void lcd_1602_init()
{
lcd_1602_write_cmd(0x01);
lcd_1602_write_cmd(0x02);
lcd_1602_write_cmd(0x06);
lcd_1602_write_cmd(0x38);
lcd_1602_write_cmd(0x01);
delay_1ms();
}
void lcd_1602_write_char(uchar x, uchar y, uchar dat)
{
if(x == 0)
{
lcd_1602_write_cmd(0x80 +y%16);
lcd_1602_write_data(dat);
}
else
{
lcd_1602_write_cmd(0xc0 + y%16);
lcd_1602_write_data(dat);
}
}
void lcd_1602_write_string(uchar x, uchar y, uchar *string)
{
while(*string != '\0')
{
lcd_1602_write_char(x,y++,*(string));
}
}
void main()
{
P1SEL=0X00;
P2SEL=0X00;
lcd_1602_write_cmd(0x0e);
lcd_1602_write_cmd(0x80);
lcd_1602_write_data('1');
lcd_1602_write_data('2');
lcd_1602_write_data('3');
lcd_1602_write_data('4');
lcd_1602_write_data('5');
}
阅读(1602) | 评论(0) | 转发(0) |