本帖最后由 zhangjw258 于 2018-3-18 11:31 编辑
LCD12864液晶显示器的串行驱动
先上电路图(如图1)。与前面并行驱动不同的是,第4脚是片选引脚,高电平有效。因为只有一个LCD12864,所以直接接到VCC,始终有效。第5脚SID,串行数据传输引脚,接P1.0。第6脚SCLK,串行时钟引脚,接P1.1。第15脚PSB是串并选择引脚,串行接GND。
图1
下面图2是串行传输一个字节的步骤,首先要CS为高电平。接着是5个同步时钟1,接着是RW,读或写,RS,指令或数据,最后跟一个0。然后将要发送的一个字节拆成两个字节,分别放在这两个字节的高4位上发送。如上所述,写指令0xF8,写数据0xFA。详见下图。
图2
下面是串行发送一个位的时序图(如图3)。可以看出数据是在SCLK的上升沿发送出去的。
图3
需要说明的是,LCD12864不具备串行读取的能力。并且LCD12864也没有内部的数据或指令的缓存区,所以对LCD12864的写操做,必须要等到前一个操做完成后再进行下一个操作。前文的并行操作可以用判忙的方法,这里就只能用延时的方法进行写操作了。
//文件12864s.h
#ifndef __12864S_H__
#define __12864S_H__
#include <reg52.h>
#include "ZhangType.h"
#include "fun.h"
#define SETMODE8 0x30 //并口8位数据接口,DL = 1,基本指令
#define SETMODE8_OFF 0x34 //并行8位数据接口,DL = 1,绘图OFF
#define SETMODE8_ON 0x36 //并行8位数据接口,DL = 1,绘图ON
#define SETMODE4 0x20 //并行4位数据接口,DL = 0,基本指令
#define SETMODE4_OFF 0x24 //并行4位数据接口,DL = 0,绘图OFF
#define SETMODE4_ON 0x26 //并行4位数据接口,DL = 0,绘图ON
#define DISOPEN 0x0C //显示开,不显示光标,光标不闪烁
#define DISMODE 0x06 //读写字符后地址加1,屏显不移动
#define SETADDR1 0x80 //设置数据地址指针第一行
#define SETADDR2 0x90 //设置数据地址指针第二行
#define SETADDR3 0x88 //设置数据地址指针第三行
#define SETADDR4 0x98 //设置数据地址指针第四行
#define CLEAR 0x01 //清屏,数据指针清零
#define RET 0x02 //回车,数据指针清零
#define WRCOM 0xF8 //写指令
#define WRDAT 0xFA //写数据
sbit SID = P1^0;
sbit SCLK = P1^1;
void Init12864s(); //串行初始化
void Write12864s_Com(uint8 com); //写指令
void Write12864s_Dat(uint8 dat); //写数据
#endif
//文件12864s.c
#include "12864s.h"
void SendByte(uint8 Byte)
{
uint8 i;
SCLK = 0;
for(i = 0; i < 8; i++)
{
Byte <<= 1;
SID = CY;
SCLK = 1;
SCLK = 0;
}
}
void Init12864s()
{
Delay(500);
Write12864s_Com(0x30);
Delay(10);
Write12864s_Com(0x0c);
Delay(10);
Write12864s_Com(0x01);
}
void Write12864s_Com(uint8 com)
{
SendByte(WRCOM);
SendByte(0xF0 & com);
SendByte(com << 4);
}
void Write12864s_Dat(uint8 dat)
{
SendByte(WRDAT);
SendByte(0xF0 & dat);
SendByte(dat << 4);
}
//文件main.c
#include <reg52.h>
#include "12864s.h"
uint8 code dis1[] = "黄河远上白云间,";
uint8 code dis2[] = "一片孤城万仞山.";
uint8 code dis3[] = "羌笛何须怨杨柳,";
uint8 code dis4[] = "春风不度玉门关.";
/*-- 图像宽度x高度=128 x 64 --*/
uint8 code **[1024]=
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3A,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0x00,0x28,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x03,0xF7,0xFF,0xFF,0xF3,0x3E,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x3F,0xC2,0x1F,0xFF,0xFF,0xFD,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x03,0xFF,0xC0,0x0F,0x5F,0xFF,0xFE,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x07,0xFF,0x00,0x0C,0x07,0xFF,0xFF,0xC0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x0F,0xFF,0x80,0x18,0x07,0x49,0xFF,0xF0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x1F,0xFF,0xE0,0x00,0x0E,0x00,0x7F,0xFF,0x80,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x7F,0xFF,0xF8,0x00,0x3C,0x00,0x3F,0xFF,0xC0,0x00,0x00,0x00,
0x00,0x00,0x00,0x01,0x3F,0xFF,0xFE,0x00,0x87,0x80,0x7D,0x3F,0x80,0x00,0x00,0x00,
0x00,0x00,0x00,0x02,0xFF,0xFF,0xFF,0xC0,0x00,0x6D,0xF0,0x1F,0x80,0x00,0x00,0x00,
0x00,0x00,0x00,0x04,0xFF,0xFF,0xFF,0xF8,0x00,0x07,0xE0,0x1C,0x20,0x00,0x00,0x00,
0x00,0x00,0x00,0x0B,0xFF,0xFF,0xF8,0x00,0x00,0x00,0x01,0x70,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x0F,0xFF,0xFF,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x1F,0xFF,0xFE,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x0F,0xFF,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x0C,0x00,0x00,0x00,
0x00,0x00,0x00,0x07,0xFF,0xC0,0x00,0x00,0x00,0x00,0x0F,0xC0,0x1C,0x00,0x00,0x00,
0x00,0x00,0x00,0x07,0xFF,0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0xF8,0x00,0x00,0x00,
0x00,0x00,0x00,0x07,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xF8,0x00,0x00,0x00,
0x00,0x00,0x00,0x0F,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0xF8,0x00,0x00,0x00,
0x00,0x00,0x00,0x0F,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0xF8,0x00,0x00,0x00,
0x00,0x00,0x00,0x0F,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xF0,0x00,0x00,0x00,
0x00,0x00,0x00,0x0F,0xE0,0x00,0x00,0x00,0x00,0xE0,0x00,0x1F,0xE0,0x00,0x00,0x00,
0x00,0x00,0x00,0x0F,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x80,0x00,0x00,0x00,
0x00,0x00,0x00,0x0F,0x80,0x3E,0x80,0x00,0x00,0x03,0xC0,0x0F,0x80,0x00,0x00,0x00,
0x00,0x00,0x00,0x04,0x7F,0x00,0x03,0xC0,0x3C,0x1E,0x0F,0xE3,0x80,0x00,0x00,0x00,
0x00,0x00,0x00,0x07,0x38,0x1F,0xF0,0x7F,0xC0,0xFF,0xC3,0xC7,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x07,0x0C,0x7D,0xCC,0x30,0xC0,0x18,0x02,0x0E,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x03,0x04,0x00,0x00,0x60,0x60,0x00,0x02,0x0C,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x01,0x02,0x00,0x00,0xC0,0x30,0x00,0x04,0x08,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x01,0x01,0x80,0x03,0x80,0x18,0x00,0x10,0x08,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x70,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x70,0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x01,0xFC,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x01,0xFE,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x08,0x00,0x0F,0xF8,0x3F,0x00,0x03,0xFE,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0A,0x00,0x00,0x0F,0xFE,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x1F,0xFE,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x7F,0xFF,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x08,0x78,0x00,0x00,0x00,0x00,0x7F,0xFF,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x08,0x7C,0x00,0x00,0x00,0x00,0x7F,0xFF,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x08,0xF8,0x00,0x00,0x00,0x00,0x3F,0xFF,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x08,0xF0,0x00,0x00,0x00,0x00,0x3F,0xFF,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x01,0xE8,0x00,0x00,0x00,0x00,0x1F,0xFF,0x80,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x17,0xD0,0x00,0x00,0x00,0x00,0x0F,0xFF,0x80,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x1F,0x20,0x00,0x00,0x00,0x00,0x03,0xFF,0x80,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x3E,0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0x80,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0x80,0x00,0x00,0x00,
0x00,0x00,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0x80,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xC0,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
void Disp(uint8 *pic) //显示Gdram内容(显示图片)
{
uint8 x, y, i;
Delay(1000);
Write12864s_Com(0x34); //切换到扩充指令,
for(i = 0; i <= 8; i += 8)
for(y = 0; y < 32; y++)
{
for(x = 0 ;x < 8 ;x++)
{
Write12864s_Com(0x80 + y); //行地址
Write12864s_Com(0x80 + x + i); //列地址
Write12864s_Dat(*pic++); //写数据 D15-D8
Write12864s_Dat(*pic++); //写数据 D7-D0
}
}
Write12864s_Com(0x36); //打开图形显示
}
void main()
{
uint8 len, max, t;
Init12864s();
Delay(1000);
Write12864s_Com(SETADDR1);
max = strlen(dis1);
for(len = 0; len < max; len++)
{
Write12864s_Dat(dis1[len]);
}
Write12864s_Com(SETADDR2);
for(len = 0; len < max; len++)
{
Write12864s_Dat(dis2[len]);
}
Write12864s_Com(SETADDR3);
for(len = 0; len < max; len++)
{
Write12864s_Dat(dis3[len]);
}
Write12864s_Com(SETADDR4);
for(len = 0; len < max; len++)
{
Write12864s_Dat(dis4[len]);
}
for(t = 0; t < 30; t++)
Delay(50000);
Write12864s_Com(0x01);
Delay(100);
Disp(**); //填充图形
while(1);
}
**图象内容,一个女子的头像(如图4)。
图4
/************************************The End*****************************************/
|