|
发表于 2012-9-28 14:17:03
|
显示全部楼层
//STC单片机控制HT1621B驱动LCD
#include"STC12C5A60S2.h"
#include"stdio.h"
#include"intrins.h"
#define uchar unsigned char
#define uint unsigned int
#define BIAS 0X52 //1/3偏压,4背极 0x50 1/2偏压,
#define RC256K 0X30 //系统时钟选择为片内RC振荡器
#define WDTDIS1 0X0A //禁止WDT溢出标志输出
#define TIMERDIS 0X08 //时基输出禁止
#define SYSEN 0X02 //打开系统时钟
#define SYSDIS 0X00 //关闭系统时钟和偏压发生器
#define LCDON 0X06 //打开LCD偏压发生器
#define LCDOFF 0X04 //关闭LCD偏压发生器
#define TONEON 0X12 //打开音频输出
#define TONEOFF 0X10 //关闭音频输出
//HT1621控制位(液晶模块接口定义,根据自已的需要更改)
sbit HT1621_DAT=P1^4; //HT1621数据引脚
sbit HT1621_CS=P1^5; //HT1621使能引脚
sbit HT1621_WR=P1^6; //HT1621时钟引脚
//HT1621段地址
#define LCD_1 4 //①
#define LCD_2 5 //②
#define LCD_3 6 //③
#define LCD_4 7 //④
#define LCD_T4 8 //T4
#define LCD_T1 9 //T1
#define LCD_T2 10 //T2
#define LCD_T3 11 //T3
#define LCD_T5 16 //T5
#define LCD_1B 17 //数码管1
#define LCD_1G 18
#define LCD_1C 19
#define LCD_1A 21
#define LCD_1F 22
#define LCD_1E 23
#define LCD_1D 24
#define LCD_NSZ 25 //逆时针箭头
#define LCD_2B 26 //数码管2
#define LCD_2G 27
#define LCD_2C 28
#define LCD_2A 29
#define LCD_2F 30
#define LCD_2E 31
#define LCD_2D 32
#define LCD_TPD 33 //TPD
#define LCD_3B 34 //数码管3
#define LCD_3G 35
#define LCD_3C 36
#define LCD_3A 37
#define LCD_3F 38
#define LCD_3E 39
#define LCD_3D 40
#define LCD_SSZ 41 //顺时针箭头
#define LCD_4B 42 //数码管4
#define LCD_4G 43
#define LCD_4C 44
#define LCD_4A 45
#define LCD_4F 46
#define LCD_4E 47
#define LCD_4D 48
#define LCD_LINE 49 //LINE
#define LCD_5B 50 //数码管5
#define LCD_5G 51
#define LCD_5C 52
#define LCD_5A 53
#define LCD_5F 54
#define LCD_5E 55
#define LCD_5D 56
//unsigned char data LCD_clear[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
//unsigned char data LED_smg0[]={0xFA,0x0A,0xD6,0x9E,0x2E,0xBC,0xFC,0x1A,0xFE,0xBE,0xFF,0x00}; //时针标志0 不显示
// 0 1 2 3 4 5 6 7 8 9 on off
//unsigned char data LED_smg[]={0xFB,0x0B,0xD7,0x9F,0x2F,0xBD,0xFD,0x1B,0xFF,0xBF,0xFF,0x00}; //时针标志1 显示
// 0 1 2 3 4 5 6 7 8 9 on off
//unsigned char data LED_smg1[]={0x00,0x10,0x20,0x40,0x80,0x02,0x06,0x0e,0x0f,0xff};
// off ① ② ③ ④ T1 T2 T3 T4 on
/*
void delay(uchar i) //j=12延时约4.3us
{
while(i--)
{
uchar j=0;
while(j<8)
j++;
}
} */
/*
void delay(uchar z) //1us
{
unsigned int x,y;
for(x=z;x>0;x--)
for(y=6;y>0;y--);
} */
void delay(unsigned int n) // STC 1T系列单片机 1.25us
{
while(n--);
}
void Delayms(unsigned int z) //STC 1T系列单片机 24MHZ z=1时精确延时1ms
{
unsigned int x,y;
for(x=z;x>0;x--)
for(y=1850;y>0;y--);
}
/******************************************************
SendBit_1621(送数据程序)
*******************************************************/
void SendBit_1621(uchar dat ,uchar cnt) //data 的高cnt 位写入HT1621,高位在前
{
uchar i;
for(i =0; i <cnt; i ++)
{
if((dat&0x80)==0) HT1621_DAT=0;
else HT1621_DAT=1;
HT1621_WR=0;
delay(5);
HT1621_WR=1;
delay(5); //需加延迟 否则4.6V以下显示不了
dat=dat<<1;
}
}
/******************************************************
写数据函数,cnt为传送数据位数,数据传送为低位在前
*******************************************************/
void SendDataBit_1621(uchar dat,uchar cnt)
{
uchar i;
for(i =0; i <cnt; i ++)
{
if((dat&0x01)==0) HT1621_DAT=0;
else HT1621_DAT=1;
HT1621_WR=0;
delay(5);
HT1621_WR=1;
delay(5); //需加延迟 否则4.6V以下显示不了
dat=dat>>1;
}
}
/********************************************************
函数名称:void SendCmd(uchar command)
功能描述: HT1621命令写入函数
参数说明:Cmd为写入命令数据
返回说明:无
说 明:写入命令标识位100
********************************************************/
void SendCmd(uchar command)
{
HT1621_CS=0;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
SendBit_1621(0x80,4); //写入命令标志100
SendBit_1621(command,8); //写入命令数据
HT1621_CS=1;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
/********************************************************
函数名称:void Write_1621(uchar addr,uchar dat)
功能描述: HT1621在指定地址写入数据函数
参数说明:Addr为写入初始地址,Data为写入数据
返回说明:无
说 明:因为HT1621的数据位4位,所以实际写入数据为参数的后4位
********************************************************/
void Write_1621(uchar addr,uchar dat)
{
HT1621_CS=0;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
SendBit_1621(0xa0,3); //写入数据标志101
SendBit_1621(addr,6); //写入地址数据
SendDataBit_1621(dat,4); //写入数据
HT1621_CS=1;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
/********************************************************
函数名称:void WriteAll_1621(uchar addr,uchar *p,uchar cnt)
功能描述: HT1621连续写入方式函数
参数说明:Addr为写入初始地址,*p为连续写入数据指针, cnt为写入数据总数
返回说明:无
说 明:HT1621的数据位4位,此处每次数据为8位,写入数据总数按8位计算
********************************************************/
void WriteAll_1621(uchar addr,uchar *p,uchar cnt)
{
uchar i;
HT1621_CS=0;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
SendBit_1621(0xa0,3); //写入数据标志101
SendBit_1621(addr,6); //写入地址数据
for(i =0; i <cnt; i ++,p++)
{
SendDataBit_1621(*p,8);//写入数据
}
HT1621_CS=1;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
/********************************************************
函数名称:void Ht1621_Init(void)
功能描述: HT1621初始化
返回说明:无
说 明:初始化后,液晶屏所有字段均显示
********************************************************/
void Ht1621_Init(void)
{
HT1621_CS=1;
HT1621_WR=1;
HT1621_DAT=1;
Delayms(1000); //延时使LCD工作电压稳定
SendCmd(BIAS); //1/3偏压,4背极
SendCmd(RC256K); //使用内部振荡器
SendCmd(SYSDIS); //关闭系统时钟和偏压发生器
SendCmd(WDTDIS1); //禁止WDT溢出标志输出
SendCmd(SYSEN); //打开系统时钟
SendCmd(LCDON); //打开LCD偏压发生器
}
|
|