独闷闷网
标题:
液晶模块12232串行驱动方式的C语言驱动程序
[打印本页]
作者:
ligang
时间:
2013-12-23 22:51
标题:
液晶模块12232串行驱动方式的C语言驱动程序
//来自电子开发者论坛
// ST7920 演示程序(串口)
//连线表: CPUClock=12Mhz *
// LCM ----- CPU *
// CS ----- P3.0 *
// SCK ----- P3.1 *
// SID ----- P3.2 *
// 注:串口模式下仅能写不能读 *
//***************************************************************
#include <reg52.h>
#include <intrins.h>
#define LcmXPixel 128 //横像素
#define LcmYPixel 32 //纵像素
sbit CS =P3^0;
sbit SCK=P3^1;
sbit SID=P3^2;
sbit Key=P3^4;
unsigned char code AC_TABLE[]={
0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87, //第一行汉字位置
0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97, //第二行汉字位置
};
unsigned char code str1[]="液晶模块 12232 !内含标准一级字库";
unsigned char code str2[]="简体字 or "; //简体字or
unsigned char code str3[]={0xC1,0x63,0xC5,0xE9,0xA6,0x72,0};//繁體字
unsigned char code bmp1[];
//串口发送一个字节
void SendByte(unsigned char Dbyte)
{
unsigned char i;
for(i=0;i<8;i++)
{
SCK = 0;
//Dbyte=Dbyte<<1; //左移一位
//SID = CY; //移出的位给SID
SID = Dbyte>>(7-i)&0x01;
SCK = 1;
SCK = 0;
}
}
void WriteCommand( unsigned char Cbyte )
{
CS = 1;
SendByte(0xf8); //11111,RW(0),RS(0),0
SendByte(0xf0&Cbyte); //高四位
SendByte(0xf0&Cbyte<<4); //低四位(先执行<<)
CS = 0;
}
void WriteData( unsigned char Dbyte )
{
CS = 1;
SendByte(0xfa); //11111,RW(0),RS(1),0
SendByte(0xf0&Dbyte); //高四位
SendByte(0xf0&Dbyte<<4); //低四位(先执行<<)
CS = 0;
}
void DelayMS(unsigned int MS)
{
unsigned char us,usn;
while(MS!=0)
{
usn = 2; //for 12M
while(usn!=0)
{
us=0xf6;
while (us!=0){us--;};
usn--;
}
MS--;
}
}
void DelayKey(unsigned int Second , unsigned int MS100)
{
unsigned int i;
for(i=0;i<Second*100+MS100*10;i++)
{
if(Key==0)
{
DelayMS(20);
while(Key==0) {DelayMS(20);}
break;
}
else DelayMS(10);
}
}
void LcmInit( void )
{
WriteCommand(0x30); //8BitMCU,基本指令集合
WriteCommand(0x03); //AC归0,不改变DDRAM内容
WriteCommand(0x0C); //显示ON,游标OFF,游标位反白OFF
WriteCommand(0x01); //清屏,AC归0
WriteCommand(0x06); //写入时,游标右移动
}
void LcmClearTXT( unsigned char FillByte )
{
unsigned char i;
WriteCommand(0x30); //8BitMCU,基本指令集合
WriteCommand(0x80); //AC归起始位
for(i=0;i<64;i++)
WriteData(FillByte);
}
void LcmClearBMP( unsigned char FillByte )
{
unsigned char i,j;
WriteCommand(0x34); //8Bit扩充指令集,即使是36H也要写两次
WriteCommand(0x36); //绘图ON,基本指令集里面36H不能开绘图
for(i=0;i<32;i++) //12832实际为256x32
{
WriteCommand(0x80|i);
WriteCommand(0x80);
for(j=0;j<32;j++)
WriteData(FillByte);
}
}
void PutStr(unsigned char row,unsigned char col,unsigned char *puts)
{
WriteCommand(0x30); //8BitMCU,基本指令集合
WriteCommand(AC_TABLE[8*row+col]); //起始位置
while(*puts != '\0')
{
if(col==8)
{
col=0;
row++;
}
if(row==2) row=0; //一屏显示完,回到屏左上角
WriteCommand(AC_TABLE[8*row+col]);
WriteData(*puts);
puts++;
WriteData(*puts);
puts++;
col++;
}
}
void PutBMP(unsigned char *puts)
{
unsigned int x=0;
unsigned char i,j;
WriteCommand(0x34); //8Bit扩充指令集,即使是36H也要写两次
WriteCommand(0x36); //绘图ON,基本指令集里面36H不能开绘图
for(i=0;i<32;i++)
{
WriteCommand(0x80|i); //行位置
WriteCommand(0x80); //列位置
for(j=0;j<16;j++)
{
WriteData(puts[x]);
x++;
}
}
}
void PutDots(unsigned char DotByte,unsigned char n)
{
unsigned char i,j,k;
WriteCommand(0x34); //8Bit扩充指令集,即使是36H也要写两次
WriteCommand(0x36); //绘图ON,基本指令集里面36H不能开绘图
for(i=0;i<(32/n);i++)
{
for(k=0;k<n;k++)
{
WriteCommand(0x80|(i*n+k)); //行位置
WriteCommand(0x80); //列位置
for(j=0;j<16;j++)
{
WriteData(DotByte);
}
}
DotByte=~DotByte;
}
}
//来自电子开发者论坛
void main( void )
{
DelayMS(100); //等待复位
LcmInit();
LcmClearTXT(0x20);
LcmClearBMP(0x00);
while(1)
{
LcmClearTXT(0x20);
PutBMP(bmp1);
DelayKey(1,5);
LcmClearBMP(0xFF);
DelayKey(1,5);
LcmClearBMP(0x00);
PutStr(0,0,str1);
DelayKey(1,5);
PutStr(0,0,str2);
PutStr(0,5,str3);
PutStr(1,0,str2);
PutStr(1,5,str3);
DelayKey(1,5);
LcmClearTXT(0x20);
PutDots(0x55,1);
DelayKey(1,5);
PutDots(0xAA,1);
DelayKey(1,5);
PutDots(0x33,2);
DelayKey(1,5);
PutDots(0xCC,2);
DelayKey(1,5);
PutDots(0x0F,4);
DelayKey(1,5);
PutDots(0xF0,4);
DelayKey(1,5);
}
}
unsigned char code bmp1[]={
//********************************************************//
//***** 源图形宽度 * 高度:128 * 32
//***** 调整后宽度 * 高度:128 * 32
//***** 字模格式:横向取模,冗余模式,字节正序,正色取模,
//***** 位图点阵占用512字节
//********************************************************//
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xC0,0x42,0x00,0x84,0x07,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x80,0x4D,0xE1,0xFF,0xC4,0x22,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x3E,0xFC,0x48,0xA1,0x04,0x04,0x22,0x08,0x38,0x71,0xE1,0xC0,0x00,0x00,
0x00,0x00,0x04,0x88,0x08,0xA1,0x04,0x04,0x22,0x18,0x44,0x8A,0x12,0x20,0x00,0x00,
0x00,0x00,0x15,0x28,0xE8,0xA3,0x3F,0x87,0xFE,0x38,0x44,0x8A,0x12,0x20,0x00,0x00,
0x00,0x00,0x1C,0x20,0x28,0xA3,0x24,0x84,0x22,0x08,0x04,0x08,0x10,0x20,0x00,0x00,
0x00,0x00,0x0C,0x60,0x28,0xA1,0x3F,0x84,0x22,0x08,0x08,0x10,0x60,0x40,0x00,0x00,
0x00,0x00,0x0C,0x60,0x2E,0xA1,0x04,0x07,0xFE,0x08,0x10,0x20,0x10,0x80,0x00,0x00,
0x00,0x00,0x1E,0x60,0x2C,0xE1,0x14,0x04,0x22,0x08,0x20,0x42,0x11,0x00,0x00,0x00,
0x00,0x00,0x12,0x90,0x20,0x81,0x1C,0x04,0x22,0x08,0x40,0x82,0x12,0x00,0x00,0x00,
0x00,0x00,0x21,0x8C,0x70,0x01,0x0E,0x04,0x22,0x08,0x7C,0xF9,0xE3,0xE0,0x00,0x00,
0x00,0x00,0x03,0x04,0x8F,0xF1,0x31,0xC8,0x66,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,0xC0,0x42,0x00,0x84,0x07,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x80,0x4D,0xE1,0xFF,0xC4,0x22,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x3E,0xFC,0x48,0xA1,0x04,0x04,0x22,0x08,0x38,0x71,0xE1,0xC0,0x00,0x00,
0x00,0x00,0x04,0x88,0x08,0xA1,0x04,0x04,0x22,0x18,0x44,0x8A,0x12,0x20,0x00,0x00,
0x00,0x00,0x15,0x28,0xE8,0xA3,0x3F,0x87,0xFE,0x38,0x44,0x8A,0x12,0x20,0x00,0x00,
0x00,0x00,0x1C,0x20,0x28,0xA3,0x24,0x84,0x22,0x08,0x04,0x08,0x10,0x20,0x00,0x00,
0x00,0x00,0x0C,0x60,0x28,0xA1,0x3F,0x84,0x22,0x08,0x08,0x10,0x60,0x40,0x00,0x00,
0x00,0x00,0x0C,0x60,0x2E,0xA1,0x04,0x07,0xFE,0x08,0x10,0x20,0x10,0x80,0x00,0x00,
0x00,0x00,0x1E,0x60,0x2C,0xE1,0x14,0x04,0x22,0x08,0x20,0x42,0x11,0x00,0x00,0x00,
0x00,0x00,0x12,0x90,0x20,0x81,0x1C,0x04,0x22,0x08,0x40,0x82,0x12,0x00,0x00,0x00,
0x00,0x00,0x21,0x8C,0x70,0x01,0x0E,0x04,0x22,0x08,0x7C,0xF9,0xE3,0xE0,0x00,0x00,
0x00,0x00,0x03,0x04,0x8F,0xF1,0x31,0xC8,0x66,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
欢迎光临 独闷闷网 (http://dumenmen.com/)
Powered by Discuz! X3.2