#include <reg51.h>
#include <intrins.h>
#include <absacc.h>
#define uchar unsigned char
#define uint unsigned int
#define _Nop() _nop_()
#define BIAS 0x52 //0b1000 0101 0010 1/3duty 4com
#define SYSDIS 0X00 //0b1000 0000 0000 关振系统荡器和LCD偏压发生器
#define LCDOFF 0X04 //0b1000 0000 0100 关LCD偏压
#define LCDON 0X06 //0b1000 0000 0110 打开LCD偏压
#define SYSEN 0X02 //0b1000 0000 0010 打开系统振荡器
#define XTAL 0x28 //0b1000 0010 1000 外部接时钟
#define RC256 0X30 //0b1000 0011 0000 内部时钟
#define TONEON 0X12 //0b1000 0001 0010 打开声音输出
#define TONEOFF 0X10 //0b1000 0001 0000 关闭声音输出
#define WDTDIS 0X0A //0b1000 0000 1010 禁止看门狗
//HT1621控制位
sbit HT1621_DAT=P2^5;
sbit HT1621_CS=P2^6;
sbit HT1621_WR=P2^7;
uchar code Ht1621Tab[]={0xf5,0x05,0xb6,0x57,0x53,0xd3,0xf3,0x85,0xf7,0xd7}; //0,1,...8,9
uchar code Ht1621Tab1[]={0xeb,0x0a,0xad,0x8f,0x4e,0xc7,0xe7,0x8a,0xef,0xcf};
//delay us
void Delay(uchar us)
{
while(--us);
}
//delay ms
void DelayMS(uint iMs)
{
uint i,j;
for(i=0;i<iMs;i++)
for(j=0;j<65;j++) Delay(1);
}
//完成写一个命令包括头(100)
void Ht1621WrCmd(uchar ucTmpCmd)
{
uchar data i,ucCmdByte=0x80;
HT1621_CS=0;
_Nop();
_Nop();
for (i=0;i<4;i++)
{
if (ucCmdByte&0x80) HT1621_DAT=1;
else HT1621_DAT=0;
HT1621_WR=0;
_Nop();
_Nop();
HT1621_WR=1;
_Nop();
ucCmdByte<<=1;
}
for (i=0;i<8;i++)
{
if (ucTmpCmd&0x80) HT1621_DAT=1;
else HT1621_DAT=0;
HT1621_WR=0;
_Nop();
_Nop();
HT1621_WR=1;
_Nop();
ucTmpCmd<<=1;
}
HT1621_CS=1;
_Nop();
_Nop();
}
//写命令+地址
void Ht1621WrDataCmd(uchar ucAddr)
{
uchar data i,ucDataCmd=0x05; //101
ucDataCmd<<=5;
for (i=0;i<3;i++)
{
if (ucDataCmd&0x80) HT1621_DAT=1;
else HT1621_DAT=0;
HT1621_WR=0;
_Nop();
_Nop();
HT1621_WR=1;
_Nop();
ucDataCmd<<=1;
}
ucAddr<<=2;
for (i=0;i<6;i++)
{
if (ucAddr&0x80) HT1621_DAT=1;
else HT1621_DAT=0;
HT1621_WR=0;
_Nop();
_Nop();
HT1621_WR=1;
_Nop();
ucAddr<<=1;
}
}
//完成写一个字节数据
void Ht1621WrByte(uchar ucTmpData)
{
uchar data i;
for (i=0;i<8;i++)
{
if (ucTmpData&0x80) HT1621_DAT=1;
else HT1621_DAT=0;
HT1621_WR=0;
_Nop();
_Nop();
HT1621_WR=1;
_Nop();
ucTmpData<<=1;
}
}
void CPU_Init()
{
P0=0XFF;
P1=0xf8;
P2=0XFF;
P3=0XFF;
}
void Ht1621_Init()
{
HT1621_CS=1;
HT1621_WR=0;
HT1621_DAT=1;
DelayMS(2000); //延时使LCD工作电压稳定
Ht1621WrCmd(BIAS);
Ht1621WrCmd(RC256); //使用内部振荡器
Ht1621WrCmd(SYSDIS);
Ht1621WrCmd(WDTDIS);
Ht1621WrCmd(SYSEN);
Ht1621WrCmd(LCDON);
}
void main()
{
uchar data i;
SP=0X60;
CPU_Init();
Ht1621_Init(); //上电初始化LCD
while(1)
{
for (i=0;i<10;i++)
{
HT1621_CS=0;
Ht1621WrDataCmd(4); //往RAM的地址4写数据 这里的参数范围为:0-31
Ht1621WrByte(Ht1621Tab[i]);
HT1621_CS=1;
DelayMS(2000); //2S
}
}
}
|