LCM(DM12864M-带字库,ST7920控制芯片)mega8控制显示程序
程序功能:该程序是用AVR来控制对DM12864M
读写信号等的操作.
硬件连接:采用串行方式显示,分配PD.3=SID,PD.2=SCK,PB.1=LCD_ON
三个I/O口分别作为LCM数据,时钟信号,背光开关
的控制。
CS和/RESET直接接VCC;PSB直接接GND
****************************************************************************************/
#include
#include
#include
#define LCDDDR DDRD
#define LCDDDR_IN LCDDDR&=~BIT(PD3);
#define LCDDDR_OUT LCDDDR|=BIT(PD0)|BIT(PD3)|BIT(PD2)|BIT(PD1);
/*===============================================================*/
#define LCDCTL PORTD
#define SID_ON LCDCTL |= BIT(PD3) //数据
#define SID_OFF LCDCTL &= ~BIT(PD3)
#define SCK_ON LCDCTL |= BIT(PD2) //时钟
#define SCK_OFF LCDCTL &= ~BIT(PD2)
#define LCD_ON LCDCTL |= BIT(PB1) //开启背光
#define LCD_OFF LCDCTL &= ~BIT(PB1)
//#define uchar unsigned char
/***********************函数体部分*********************************/
unsigned char AC_TABLE[]={
0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,
0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,
0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,
0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,
};
unsigned char str2[]="欢迎光临AVRLCD!!";
unsigned char str1[]="云母屏风烛影深,长河渐落晓星沉。嫦娥应悔偷灵药,碧海青天夜夜心。";
unsigned char bmp1[];//先声明一下就可以把表放在函数后面啦,呵呵
//=========发送一个字节程序=========
void SendByte(unsigned char Dbyte)
{
unsigned char i;
for(i=0;i<8;i++)
{
if(Dbyte&0x80)SID_ON;
else SID_OFF;
SCK_ON;
SCK_OFF;
Dbyte=Dbyte<<1;
}
}
//接收一个字节程序((0xf0&temp1)+(0x0f&temp2))
unsigned char ReceiveByte(void)
{
unsigned char i,temp1,temp2;
temp1=temp2=0;
for(i=0;i<8;i++)
{
LCDDDR_IN;
temp1=temp1<<1;
if ((PINC&0x02)==0)temp1++; //与端口设置有关,主要修改端口时修改此位
LCDDDR_OUT;
SCK_ON;
SCK_OFF;
}
for(i=0;i<8;i++)
{
LCDDDR_IN;
temp2=temp2<<1;
if ((PINC&0x02)==0)temp2++; //与端口设置有关,主要修改端口时修改此位
LCDDDR_OUT;
SCK_ON;
SCK_OFF;
}
return ((0xf0&temp1)+(0x0f&temp2));
}
//液晶忙检查CheckBusy
void CheckBusy( void )
{
do SendByte(0xfc); //11111,RW(1),RS(0),0
while(0x80&ReceiveByte());
}
//========写命令========
void WriteCommand( unsigned char Cbyte )
{
CheckBusy();
SendByte(0xf8); //11111,RW(0),RS(0),0
SendByte(0xf0&Cbyte);
SendByte(0xf0&Cbyte<<4);
}
//========写数据========
void WriteData( unsigned char Dbyte )
{
CheckBusy();
SendByte(0xfa); //11111,RW(0),RS(1),0
SendByte(0xf0&Dbyte);
SendByte(0xf0&Dbyte<<4);
}
//========读数据========
unsigned char ReadData( void )
{
CheckBusy();
SendByte(0xfe); //11111,RW(1),RS(1),0
return ReceiveByte();
}
//===延时程序 ,12M/ms==
void Delay(unsigned int MS)
{
unsigned char us,usn;
while(MS!=0) //for 12M
{
usn = 2;
while(usn!=0)
{
us=0xf5;
while (us!=0){us--;};
usn--;
}
MS--;
}
}
//=====液晶初始化======
void LcmInit( void )
{
WriteCommand(0x30); //基本指令,选择8位CPU控制方式,关图显
WriteCommand(0x03); //复位地址计数器
WriteCommand(0x0C); //开整体显示
WriteCommand(0x01); //清除屏显的数据
WriteCommand(0x06); //设置地址计数器递增
}
//=======清文字========
void LcmClearTXT( void )
{
LcmInit();
}
//=======清图片========
void LcmClearBMP( void )
{
unsigned char i,j;
WriteCommand(0x34); //关图显
for(i=0;i<32;i++)
{
WriteCommand(0x80|i); //写入列地址
WriteCommand(0x80); //写入行地址
for(j=0;j<32;j++)
WriteData(0);
}
//WriteCommand(0x36);//开图显
}
//==写入一段文字,写完一屏,清屏,从头在写新的一屏=======
void PutStr(unsigned char row,unsigned char col,unsigned char *puts)
{
WriteCommand(0x30); //基本指令,选择8位CPU控制方式,关图显
WriteCommand(AC_TABLE[8*row+col]);
while(*puts != '\0')
{
if(col==8)
{
col=0;
row++;
}
if(row==4) row=0;
WriteCommand(AC_TABLE[8*row+col]);
WriteData(*puts); //一个汉字的ASC码是一个16位的,所以要写入两个字节
puts++;
WriteData(*puts);
puts++;
col++;
}
}
//========写入一屏图片128*64=======
void PutBMP(unsigned char *puts)
{
unsigned char x,y,i,j,k;
x=0x80;
WriteCommand(0x34); //关图显
for(k=0;k<2;k++)
{
y=0x80;
for(j=0;j<32;j++)
{
WriteCommand(y); //写入列地址
WriteCommand(x); //写入行地址
for(i=0;i<16;i++)
{
WriteData(*(puts++));
}
y++;
}
x=0x88;
}
WriteCommand(0x36); //开图显
}
//===========主函数===========
void main( void )
{
LcmInit();
Delay(10);
PORTB=0xff;
LCD_ON;
while(1)
{
PutStr(1,0,str2);
Delay(10000);
LcmClearTXT();
Delay(10);
PutStr(0,0,str1);
Delay(10000);
LcmClearTXT();
Delay(10);
PutBMP(bmp1);
Delay(10000);
LcmClearBMP();
}
}
//==============================================================================
unsigned char bmp1[]={/*生日蛋糕*/
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,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x01,0x20,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x19,0x80,0x07,0x38,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0x00,0x00,0x04,0x00,0x00,0x40,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0C,0x00,0x00,0xC0,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x78,0x00,0x00,0xC0,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x10,0x40,0x00,0x00,0xC0,0x00,0x01,0xC0,
0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0x80,0x10,0x40,0x00,0x03,0x98,0x00,0x01,0xC0,
0x00,0x00,0x12,0x00,0x00,0x00,0x40,0x80,0x11,0xF0,0x00,0x01,0x08,0x00,0x03,0x80,
0x00,0x00,0x13,0x00,0x00,0x30,0x40,0x80,0x10,0x50,0x06,0x01,0x08,0x00,0x03,0x80,
0x00,0x00,0x11,0x00,0x00,0x48,0x40,0x80,0x10,0x50,0x19,0x83,0xFF,0x80,0x03,0x00,
0x00,0x00,0x17,0x80,0x01,0xCE,0x40,0x80,0x5B,0xFC,0x06,0x00,0x08,0x00,0x07,0x00,
0x00,0x00,0x1F,0xE0,0x00,0x30,0x7F,0x80,0xD4,0x80,0x00,0x00,0x08,0x00,0x06,0x00,
0x00,0x00,0x11,0x00,0x00,0x30,0x40,0x80,0x91,0xA0,0x00,0x00,0xC9,0x00,0x00,0x00,
0x00,0x00,0x31,0x00,0x00,0x00,0x40,0x80,0x11,0x30,0x00,0x01,0x89,0x80,0x04,0x00,
0x00,0x00,0x67,0xC0,0x00,0x00,0x40,0x80,0x13,0x18,0x00,0x03,0x08,0xC0,0x0E,0x00,
0x00,0x00,0xC1,0x00,0x00,0x00,0x40,0x80,0x16,0x0C,0x00,0x02,0x08,0x60,0x0E,0x00,
0x00,0x00,0x01,0x00,0x00,0x00,0x7F,0x80,0x14,0x04,0x00,0x00,0x78,0x00,0x04,0x00,
0x00,0x00,0x01,0x00,0x00,0x00,0x41,0x80,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x7F,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x08,0x00,0x0E,0x00,0x00,0x00,0x00,0xC0,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x1B,0x00,0x00,0x00,0x01,0xF0,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x1B,0x80,0x00,0x00,0x01,0x30,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x34,0xC0,0x03,0x00,0x07,0x5C,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x31,0xC0,0x03,0x80,0x06,0x18,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x39,0xC0,0x06,0xC0,0x02,0x98,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x0F,0x00,0x0C,0xC0,0x03,0xF0,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x18,0x70,0x00,0x40,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x1F,0xF8,0x2C,0x28,0x3D,0xF0,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x1F,0x86,0xCC,0x66,0xC3,0xF1,0x80,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x06,0x3F,0x81,0x06,0xC1,0x81,0xFE,0xE0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x19,0xDF,0xFF,0xFF,0xFF,0xFF,0xF7,0x30,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x61,0xDF,0x80,0x07,0xC0,0x01,0xFC,0x0C,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0xF1,0x1F,0x80,0x07,0xC0,0x01,0xF0,0x1F,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x03,0x1C,0x0F,0x00,0x07,0xC0,0x00,0xE0,0x71,0x80,0x00,0x00,0x00,
0x00,0x00,0x00,0x03,0x1E,0x00,0x00,0x03,0x80,0x00,0x00,0xC1,0x80,0x00,0x00,0x00,
0x00,0x00,0x00,0x01,0xE3,0x1E,0x00,0x00,0x00,0x00,0x71,0x0F,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x03,0x30,0xE3,0x38,0x38,0x38,0x39,0x8E,0x19,0x80,0x00,0x00,0x00,
0x00,0x00,0x00,0x01,0x0F,0xC3,0xC6,0xC6,0xC6,0xC7,0x07,0xF1,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x03,0x00,0x3F,0x01,0x81,0x01,0x01,0xF8,0x01,0x80,0x00,0x00,0x00,
0x00,0x00,0x00,0x01,0xC0,0x00,0xFF,0xFF,0xFF,0xFE,0x00,0x07,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x03,0x38,0x00,0x00,0x00,0x00,0x00,0x00,0x39,0x80,0x00,0x00,0x00,
0x00,0x00,0x00,0x01,0x87,0xC0,0x00,0x00,0x00,0x00,0x07,0xC1,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x03,0x00,0x3F,0xC0,0x00,0x00,0x07,0xF8,0x09,0x80,0x00,0x00,0x00,
0x00,0x00,0x00,0x01,0xF0,0x00,0x7F,0xFF,0xFF,0xF8,0x04,0x2F,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x03,0x1E,0x22,0x00,0x04,0x10,0x04,0x40,0xF1,0x80,0x00,0x00,0x00,
0x00,0x00,0x00,0x1F,0x03,0xC0,0x20,0x40,0x84,0x80,0x07,0x01,0xF0,0x00,0x00,0x00,
0x00,0x00,0x00,0x23,0x00,0x3F,0xF2,0x08,0x00,0x47,0xFC,0x01,0x88,0x00,0x00,0x00,
0x00,0x00,0x00,0xC1,0x00,0x00,0x1F,0xFF,0xFF,0xFC,0x00,0x01,0x06,0x00,0x00,0x00,
0x00,0x00,0x00,0xC0,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x0E,0x06,0x00,0x00,0x00,
0x00,0x00,0x00,0xC4,0x1E,0x00,0x00,0x00,0x00,0x00,0x00,0xF0,0x06,0x00,0x00,0x00,
0x00,0x00,0x00,0xC0,0x01,0xF0,0x00,0x00,0x00,0x00,0x0F,0x00,0x42,0x00,0x00,0x00,
0x00,0x00,0x00,0x30,0x60,0x1F,0xC0,0x00,0x00,0x07,0xF0,0x1C,0x1C,0x00,0x00,0x00,
0x00,0x00,0x00,0x1F,0xC0,0x00,0x3F,0xFF,0xFF,0xF8,0x00,0x07,0xF0,0x00,0x00,0x00,/*
0x00,0x00,0x00,0x00,0xC2,0x00,0x00,0x00,0x00,0x00,0x00,0x86,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0xC0,0x18,0x00,0x00,0x00,0x00,0x30,0x06,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x30,0x78,0x00,0xC1,0x06,0x08,0x38,0x18,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x1F,0xCC,0x00,0xE0,0x06,0x00,0x37,0xE0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x06,0x13,0x38,0x59,0x82,0xC0,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x03,0xFE,0x0F,0xF0,0xFF,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*/
};
上一篇:AT24CXX的mega8控制程序
下一篇:AVR的模拟比较器功能实验
推荐阅读最新更新时间:2024-03-16 15:17
设计资源 培训 开发板 精华推荐
- 有奖直播|TI 超声波镜头清洁技术
- 遇见春天,“愚”你同乐 快来与keysight一起学习 光通信测试测量解决方案 下载赢好礼!
- 英特尔 SoC FPGA 开发者论坛 (ISDF) 预约报名享好礼!
- 一大波MCU开发板正在靠近,速来——瑞萨电子设计大赛(2015)
- 趣味电子技术史话第二讲:开关电源的早期历史
- 揭秘正确选择探头的5大要素 (泰克原装探头促销,高贵不贵,199元起)
- 有奖直播 | 罗彻斯特电子为您细说:半导体停产后的挑战与解决方案
- 【双节献礼】LM3S9B96开发套件等你来拿!!!
- Deyisupport博文阅读季:推荐你喜欢的放大器博文,抢楼赢好礼!
- #Micropython大作战#第三弹:抢楼!大家一起来整理micropython资料