用STC15w408單片機6線驅動1602液晶屏
1602的驅動最多需要11線,其中數據線需要8條。也可以采用4條數據線來驅動,只寫不讀的話,還可以省去RW線,這樣最少只需要6線就可以驅動1602。
采用4條數據線一般是使用單片機端口的高四位,但16腳封裝的STC15W408沒有完整的高四位可以采用,所以就使用了低四位。
單片機程序如下:
/**
LCD1602 6線驅動顯示
使用STC15W408AS , 數據口使用P1.0-P1.3 低四位
**/
#include <STC15.h>
#include <intrins.h>
#define uchar unsigned char
sbit RS = P5^5;
sbit EN = P5^4;
void Delay1us() //@11.0592MHz //STC15W408AS使用
{
_nop_();
_nop_();
_nop_();
}
void LCD_en_write()
{
EN = 1;
Delay1us();
EN = 0;
}
void Delay1ms() //@11.0592MHz //STC15W408AS使用
{
unsigned char i, j;
_nop_();
_nop_();
_nop_();
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
}
void LCD1602_Write(uchar style,uchar input) //使用STC15W408AS低四位發送數據
{
uchar temp,high;
Delay1ms();
if(style==0)
RS = 0; //命令‘0’
else
RS = 1; //命令‘1’
high = input>>4; //數據高四位移到低四位
temp= P1 & 0XF0; //清低四位
P1 = temp |( high & 0x0f); //寫高四位
Delay1ms();
LCD_en_write();
temp= P1 & 0XF0; //清低四位
P1 = temp |( input & 0x0f);//寫低四位
Delay1ms();
LCD_en_write();
}
void LCD_Display(uchar x, uchar y, uchar *str)
{
if(y)
LCD1602_Write(0,(0xc0+x));
else
LCD1602_Write(0,(0x80+x));
while(*str !='\0')
LCD1602_Write(1,*str++);
}
void LCD1602_initial()
{
LCD1602_Write(0,0x28); //設置16*2顯示,5*7點陣,4位數據接口
Delay1ms();
LCD1602_Write(0,0x28);
Delay1ms();
LCD_en_write();
Delay1ms();
LCD1602_Write(0,0x28);
LCD1602_Write(0,0x0c); //開顯示 無光標
LCD1602_Write(0,0x06); //讀寫一字節后地址指針加1
LCD1602_Write(0,0x01); //清屏
Delay1ms();
}
/*
void LCD1602_set_XY(uchar x,uchar y)
{
if(y)
LCD1602_Write(0,(0xc0+x));
else
LCD1602_Write(0,(0x80+x));
}
void LCD1602_write_data(uchar x,uchar y,uchar dat)
{
LCD1602_set_XY(x,y);
LCD1602_Write(1,dat);
}
*/
void main()
{ uchar TestStr[] = {"Welcome!"};
uchar str[] = {"LCD1602 display"};
LCD1602_initial();
Delay1ms();
LCD_Display(4, 0, &TestStr); //顯示字符串
Delay1ms();
LCD_Display(0, 1, &str);
while(1);
}
|