- /*-----------------------------------------------
- 名称:st7920字库液晶显示
- 论坛:806899919
- 编写:光汇科技
- 日期:2012.3
- 修改:无
- 内容:通过显示字符、数字、汉字和图片测试液晶基本功能
- ------------------------------------------------*/
- #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,
- };
- */
- /*------------------------------------------------
- 检测忙位
- ------------------------------------------------*/
- 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);
- }
- /*------------------------------------------------
- 写数据
- ------------------------------------------------*/
- 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的延时程序
- PSB=1; //设置为8BIT并口工作模式
- DelayMs(1); //延时
- RES=0; //复位
- DelayMs(1); //延时
- RES=1; //复位置高
- DelayMs(10);
- Write_Cmd(0x30); //选择基本指令集
- DelayUs2x(50); //延时大于100us
- Write_Cmd(0x30); //选择8bit数据流
- DelayUs2x(20); //延时大于37us
- Write_Cmd(0x0c); //开显示(无游标、不反白)
- DelayUs2x(50); //延时大于100us
- Write_Cmd(0x01); //清除显示,并且设定地址指针为00H
- DelayMs(15); //延时大于10ms
- Write_Cmd(0x06); //指定在资料的读取及写入时,设定游标的移动方向及指定显示的移位,光标从右向左加1位移动
- DelayUs2x(50); //延时大于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:横坐标值,范围0~8
- y:纵坐标值,范围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;
- //显示上半屏内容设置
- 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++;
- }
- }
- //显示下半屏内容设置
- 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++;
- }
- }
- }
- /*------------------------------------------------
- 设置到绘图模式
- ------------------------------------------------*/
- void SetGraphicMode()
- {
- Write_Cmd(0x36); //选择8bit数据流 图形模式
- DelayUs2x(20);
- }
- void display_init()
- {
- LCD_PutString(0,1,"智能家居控制系统");
- LCD_PutString(0,2,"学生:郑家波");
- LCD_PutString(0,3,"老师:李湘文");
- LCD_PutString(0,4,"时间:2015/6/2");
- }
- /*------------------------------------------------
- 主程序
- ------------------------------------------------
- main()
- {
- unsigned char i;
- // CGRAM(); //写入自定义字符
- Init_ST7920(); //初始化
- LCD_PutString(0,1,"智能家居控制系统");
- LCD_PutString(0,2,"学生:郑家波");
- LCD_PutString(0,3,"老师:李湘文");
- LCD_PutString(0,4,"时间:2015/5/5");
- //延时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% ");
- //延时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);
-
- }
- }
- */
复制代码 |