#include "display.h"
#define LCD_port P0 //定义LCD数据口
#define clear_dis 0x01 //清除显示
#define add_location 0x02 //地址归位
#define show_state 0x0c //显示状态 游标关闭
#define enter_set 0x06 //进入点设定
#define draw_off 0x34 //功能设定 绘图显示关
#define draw_on 0x36 //功能设定 绘图显示开
#define base_function 0x30 //基本功能设定
#define head_add 0x80 //初始显示地址(水平地址、垂直地址)
#define second_add 0x88 //第二屏显示地址
/*#define Line1 0x80 //第一行
#define Line2 0x90 //第二行
#define Line3 0x88 //第三行
#define Line4 0x98 //第四行*/
/*----------------定义端口--------------------*/
sbit EN = P3^4; //使能信号
sbit RS = P3^5; //数据/命令选择端
sbit RW = P3^6; //读/写选择端
sbit RD = P3^7; //并/串选择端
void LCD_init_port(void)
{
EN = 1;
_nop_();
RS = 1;
_nop_();
RW = 1;
_nop_();
RD = 1;
_nop_();
}
void LCD_w_directive(unchar dir)
{
EN = 0;
RS = 0;
RW = 0;
LCD_port = dir;
_nop_();
EN = 1;
_nop_();
EN = 0;
}
void LCD_w_data(unchar da)
{
EN = 1;
RS = 1;
RW = 0;
LCD_port = da;
_nop_();
EN = 1;
_nop_();
EN = 0;
}
void LCD_test_busy(void)
{
EN = 0;
RS = 0;
RW = 1;
_nop_();
EN = 1;
_nop_();
LCD_port = 0xff;
while(LCD_port & 0x80);
}
unchar LCD_r_data(void)
{
unchar value;
EN = 0;
RS = 1;
RW = 1;
_nop_();
EN = 1;
_nop_();
LCD_port = 0xff;
value = LCD_port;
delay(1);
EN = 0;
return (value);
}
/*-------------清除GDRAM---------------*/
void LCD_clear_GDRAM(void)
{
unchar x,y;
LCD_test_busy();
LCD_w_directive(draw_off);
LCD_test_busy();
for(x=0;x<32;x++)
{
LCD_test_busy();
LCD_w_directive(head_add+x); //写竖列
LCD_test_busy();
LCD_w_directive(head_add); //写横列
for(y=0;y<32;y++)
{
LCD_test_busy();
LCD_w_data(0x00);
}
}
LCD_test_busy();
LCD_w_directive(draw_on);
}
void LCD_picture(unchar x,unchar y,unchar width,unchar height,unchar *pic)
{
static unchar next_height = 0;
unchar LCD_x = head_add+x,LCD_y = head_add+y;
unchar j,k;
width /= 8;
if(height > 32)
{
next_height = height;
height = 32-y;
}
LCD_test_busy();
LCD_w_directive(draw_off);
LCD_test_busy();
loop:
for(j=0;j<height;j++)
{
next_height--;
LCD_test_busy();
LCD_w_directive(LCD_y+j);
LCD_test_busy();
LCD_w_directive(LCD_x);
for(k=0;k<width;k++)
{
LCD_test_busy();
LCD_w_data(*pic++);
}
}
if(next_height != 0)
{
height = next_height;
LCD_x = second_add+x;
LCD_y = head_add;
goto loop;
}
LCD_test_busy();
LCD_w_directive(draw_on);
LCD_test_busy();
LCD_w_directive(base_function);
}
void LCD_picture_dot(unchar x,unchar y)
{
unchar bit_x,add_x;
unchar LSB,HSB;
LCD_test_busy();
LCD_w_directive(draw_off);
add_x = x / 16;
bit_x = x % 16;
LCD_test_busy();
LCD_w_directive(head_add+y);
LCD_test_busy();
LCD_w_directive(head_add+add_x);
LCD_test_busy();
HSB = LCD_r_data(); //假读
LCD_test_busy();
HSB = LCD_r_data();
LCD_test_busy();
LSB = LCD_r_data();
if(bit_x<8)
{
LSB |= 0x01 << bit_x;
}
else
{
bit_x -= 8;
HSB |= 0x01 << bit_x;
}
LCD_test_busy();
LCD_w_directive(head_add+y);
LCD_test_busy();
LCD_w_directive(head_add+add_x);
LCD_test_busy();
LCD_w_data(HSB);
LCD_test_busy();
LCD_w_data(LSB);
LCD_test_busy();
LCD_w_directive(draw_on);
LCD_test_busy();
LCD_w_directive(base_function);
}
void LCD_write_string(unchar x,unchar *p)
{
unchar z;
LCD_test_busy();
LCD_w_directive(head_add+x);
LCD_test_busy();
for(z=strlen(p);z>0;z--)
{
LCD_test_busy();
LCD_w_data(*p++);
}
}
void LCD_dynamic_dis(unchar x,unchar value)
{
unchar ge,shi,bai;
ge = value % 10;
shi = value % 100 / 10;
bai = value / 100;
LCD_test_busy();
LCD_w_directive(head_add+x);
LCD_test_busy();
LCD_w_data(0x30+bai);
LCD_test_busy();
LCD_w_data(0x30+shi);
LCD_test_busy();
LCD_w_data(0x30+ge);
}
void LCD_setup(void)
{
LCD_init_port();
LCD_test_busy();
LCD_w_directive(base_function);
LCD_test_busy();
LCD_w_directive(clear_dis);
LCD_test_busy();
LCD_w_directive(add_location);
LCD_test_busy();
LCD_w_directive(show_state);
LCD_test_busy();
LCD_w_directive(enter_set);
LCD_test_busy();
LCD_clear_GDRAM();
LCD_test_busy();
LCD_w_directive(base_function);
}
评论0