|
#include <reg52.h>
#include "delay.h"
sbit RS = P2^2;
sbit RW = P2^1;
sbit E = P2^0;
sbit RES = P2^5;
sbit PSB = P2^3;
#define DataPort P0 //MCU P0<------> LCM
/*
unsigned char code user16x16[]={ //笑臉圖片
0x0F,0xF0,0x10,0x08,0x20,0x04,0x40,0x02,0x9C,0x39,0xBE,0x7D,0x80,0x01,0x80,0x01,
0x80,0x01,0x88,0x11,0x84,0x21,0x43,0xC2,0x20,0x04,0x10,0x08,0x0F,0xF0,0x00,0x00,
};
*/
/*------------------------------------------------
檢測(cè)忙位
------------------------------------------------*/
void Check_Busy()
{
RS=0;
RW=1;
E=1;
DataPort=0xff;
while((DataPort&0x80)==0x80);//忙則等待
E=0;
}
/*------------------------------------------------
寫命令
------------------------------------------------*/
void Write_Cmd(unsigned char Cmd)
{
Check_Busy();
RS=0;
RW=0;
E=1;
DataPort=Cmd;
DelayUs2x(5);
E=0;
DelayUs2x(5);
}
/*------------------------------------------------
寫數(shù)據(jù)
------------------------------------------------*/
void Write_Data(unsigned char Data)
{
Check_Busy();
RS=1;
RW=0;
E=1;
DataPort=Data;
DelayUs2x(5);
E=0;
DelayUs2x(5);
}
/*------------------------------------------------
液晶屏初始化
------------------------------------------------*/
void Init_ST7920()
{
DelayMs(40); //大于40MS的延時(shí)程序
PSB=1; //設(shè)置為8BIT并口工作模式
DelayMs(1); //延時(shí)
RES=0; //復(fù)位
DelayMs(1); //延時(shí)
RES=1; //復(fù)位置高
DelayMs(10);
Write_Cmd(0x30); //選擇基本指令集
DelayUs2x(50); //延時(shí)大于100us
Write_Cmd(0x30); //選擇8bit數(shù)據(jù)流
DelayUs2x(20); //延時(shí)大于37us
Write_Cmd(0x0c); //開顯示(無游標(biāo)、不反白)
DelayUs2x(50); //延時(shí)大于100us
Write_Cmd(0x01); //清除顯示,并且設(shè)定地址指針為00H
DelayMs(15); //延時(shí)大于10ms
Write_Cmd(0x06); //指定在資料的讀取及寫入時(shí),設(shè)定游標(biāo)的移動(dòng)方向及指定顯示的移位,光標(biāo)從右向左加1位移動(dòng)
DelayUs2x(50); //延時(shí)大于100us
}
/*------------------------------------------------
用戶自定義字符
------------------------------------------------
void CGRAM()
{
int i;
Write_Cmd(0x30);
Write_Cmd(0x40);
for(i=0;i<16;i++)
{
Write_Data(user16x16[i*2]);
Write_Data(user16x16[i*2+1]);
}
} */
/*------------------------------------------------
顯示用戶自定義字符
------------------------------------------------
void DisplayCGRAM(unsigned char x,unsigned char y)
{
switch(y)
{
case 1: Write_Cmd(0x80+x);break;
case 2: Write_Cmd(0x90+x);break;
case 3: Write_Cmd(0x88+x);break;
case 4: Write_Cmd(0x98+x);break;
default:break;
}
Write_Data(00);
Write_Data(00);
} */
/*------------------------------------------------
顯示字符串
x:橫坐標(biāo)值,范圍0~8
y:縱坐標(biāo)值,范圍1~4
------------------------------------------------*/
void LCD_PutString(unsigned char x,unsigned char y,unsigned char code *s)
{
switch(y)
{
case 1: Write_Cmd(0x80+x);break;
case 2: Write_Cmd(0x90+x);break;
case 3: Write_Cmd(0x88+x);break;
case 4: Write_Cmd(0x98+x);break;
default:break;
}
while(*s>0)
{
Write_Data(*s);
s++;
DelayUs2x(50);
}
}
/*------------------------------------------------
清屏
------------------------------------------------*/
void ClrScreen()
{
Write_Cmd(0x01);
DelayMs(15);
}
/*------------------------------------------------
顯示圖片
------------------------------------------------*/
void LCD_PutGraphic(unsigned char code *img)
{
int i,j;
//顯示上半屏內(nèi)容設(shè)置
for(i=0;i<32;i++)
{
Write_Cmd(0x80 + i); //SET 垂直地址 VERTICAL ADD
Write_Cmd(0x80); //SET 水平地址 HORIZONTAL ADD
for(j=0;j<16;j++)
{
Write_Data(*img);
img++;
}
}
//顯示下半屏內(nèi)容設(shè)置
for(i=0;i<32;i++)
{
Write_Cmd(0x80 + i); //SET 垂直地址 VERTICAL ADD
Write_Cmd(0x88); //SET 水平地址 HORIZONTAL ADD
for(j=0;j<16;j++)
{
Write_Data(*img);
img++;
}
}
}
/*------------------------------------------------
設(shè)置到繪圖模式
------------------------------------------------*/
void SetGraphicMode()
{
Write_Cmd(0x36); //選擇8bit數(shù)據(jù)流 圖形模式
DelayUs2x(20);
}
void display_init()
{
LCD_PutString(0,1,"智能家居控制系統(tǒng)");
LCD_PutString(0,2,"學(xué)生:鄭家波");
LCD_PutString(0,3,"老師:李湘文");
LCD_PutString(0,4,"時(shí)間:2015/6/2");
}
/*------------------------------------------------
主程序
------------------------------------------------
main()
{
unsigned char i;
// CGRAM(); //寫入自定義字符
Init_ST7920(); //初始化
LCD_PutString(0,1,"智能家居控制系統(tǒng)");
LCD_PutString(0,2,"學(xué)生:鄭家波");
LCD_PutString(0,3,"老師:李湘文");
LCD_PutString(0,4,"時(shí)間:2015/5/5");
//延時(shí)30x200ms
for(i=0;i<30;i++)
DelayMs(200);
ClrScreen();
while(1)
{
LCD_PutString(0,1,"窗簾OFF 燈光OFF");
LCD_PutString(0,2,"煙霧OK 溫度OK ");
LCD_PutString(0,3," 溫度:28°C");
LCD_PutString(0,4," 光照度:53% ");
//延時(shí)30x200ms
for(i=0;i<30;i++)
DelayMs(200);
LCD_PutString(0,1,"窗簾ON 燈光OFF");
for(i=0;i<30;i++)
DelayMs(200);
ClrScreen();
LCD_PutString(0,2," 短信通知中..."); //"正”字顯示不出
for(i=0;i<20;i++)
DelayMs(200);
}
}
*/
|
|