|
发表于 2011-10-11 15:43:04
|
显示全部楼层
***************************************************/
//HT1621驱动模块通信驱动2010-11月.
//对HT1621 的读写 – 使用C 语言
//对s1302时钟芯片的读写
/**************************************************/
#include <pic.h>
#define uchar unsigned char
#define uint unsigned int
#define nop() asm("nop")
/***************************************************/
//ht1621设置函数
/**************************************************/
#define BIAS 0x24 //设定偏压与com口
#define SYSEN 0x01 //打开系统震荡
#define LCDOFF 0x02 //关闭LCD显示
#define LCDON 0x03 //打开LCD显示
#define cs RC0 //ht1621液晶驱动模块
#define wr RC4 //ht1621
#define dat RC5 //ht1621
const uchar zm_1[]={0x5f,0x06,0x6b,0x2f,0x36,0x3d,0x7d,0x07,0x7f,0x3f};//LCD 1 2 3的0-9的数码
const uchar zm_2[]={0x5f,0x06,0x3d,0x2f,0x66,0x6b,0x7b,0x0e,0x7f,0x6f};//LCD 4 5 6 7的0-9的数码
void SendBit_1621(uchar data,uchar cnt);
void SendDataBit_1621(uchar data,uchar cnt);
void SendCmd(uchar command);
void Write_1621(uchar addr,uchar data);
void WriteAll_1621(uchar addr,uchar *p,uchar cnt);
/***************************************************/
//延时设置函数
/**************************************************/
void delay(uint x); //延时函数
/***************************************************/
//ds1302设置函数
/**************************************************/
const uchar table1[]={0x01,0x21,0x15,0x14,0x11,0x7,0x10};//秒 分 时 日 星期 月 年
const uchar write_table[7]={0x80,0x82,0x84,0x86,0x88,0x08a,0x8c};//写秒 分 时 日 星期 月 年地址
const uchar read_table[7]={0x81,0x83,0x85,0x87,0x89,0x08b,0x8d}; //读秒 分 时 日 星期 月 年地址
uchar table2[7];
#define rst RC1 //ds1302时钟芯片
#define sck RC3 //ds1302时钟芯片
#define ioo TRISC2 //ds1302时钟芯片
#define io RC2 //ds1302时钟芯片
uchar write_8bits(uchar add);
void write_shineng(uchar add,uchar date);
void write_ren();
uchar read_8bits(uchar add);
void COL();
//void shijianchuli();
/***************************************************/
//主控制函数
/**************************************************/
void main()
{
uchar i,shi,fen,miao,a1,a2,a3,a4,j=0x00;
uchar a[31];a[0]=0x00;a[1]=0x00;a[2]=0x00;a[3]=0x00;a[4]=0x00;a[5]=0x00;a[6]=0x00;a[7]=0x00;a[8]=0x00;a[9]=0x00;a[10]=0x00;
a[11]=0x00;a[12]=0x00;a[13]=0x00;a[14]=0x00;a[15]=0x00;a[16]=0x00;a[17]=0x00;a[18]=0x00;a[19]=0x00;a[20]=0x00;
a[21]=0x00;a[22]=0x00;a[23]=0x00;a[24]=0x00;a[25]=0x00;a[26]=0x00;a[27]=0x00;a[28]=0x00;a[29]=0x00;a[30]=0x00;
TRISC=0X00; //将端口设为输出
SendCmd(0x24); //设置偏压和占空比
SendCmd(0x01); //打开系统振荡器
SendCmd(0x03); //打开LCD偏压发生器
//WriteAll_1621(0,a,31); //在起始地址为0 处连续写入5 个字节数据
//a为写入数据的起始地址,5 为写入的字节
//write_ren(); //一次性写入ds1302的准确时间
for(i=0;i<31;i++)
{
Write_1621(j,0x00);
j++;
}
while(1)
{
fen=read_8bits(0x83);
shi=read_8bits(0x85);
a1=fen&0x0f; //分钟的个位
a2=fen>>4; //分钟的十位
a3=shi&0x0f; //小时的个位
a4=shi>>4; //小时的十位
SendCmd(0x03);
//COL();
Write_1621(0x11,zm_2[a4]&0x0f);
Write_1621(0x10,zm_2[a4]>>4);
delay(3);
//SendCmd(0x02);
Write_1621(0x13,zm_2[a3]&0x0f);
Write_1621(0x12,zm_2[a3]>>4);
delay(3);
// SendCmd(0x02);
Write_1621(0x15,zm_2[a2]&0x0f);
Write_1621(0x14,zm_2[a2]>>4);
delay(3);
//SendCmd(0x02);
Write_1621(0x17,zm_2[a1]&0x0f);
Write_1621(0x16,zm_2[a1]>>4);
delay(3);
//SendCmd(0x02);
Write_1621(0x08,0x0f);
Write_1621(0x09,0x0f);
//delay(30);
SendCmd(0x02);
}
SendCmd(0x02); //关闭LCD 显示
}
/***************************************************/
//主板初始化
/***************************************************/
void zhuban(void)
{
TRISC=0X00;
}
/***************************************************/
//写HT1621地址字节函数
/***************************************************/
void SendBit_1621(uchar data,uchar cnt) //data 的高cnt 位写入HT1621,高位在前
{
uchar i;
for(i =0; i <cnt; i ++)
{
if((data&0x80)==0) dat=0;
else dat=1;
wr=0;
nop();
wr=1;
data<<=1;
}
}
/****************************************************/
//写HT1621地址内数据函数
/****************************************************/
void SendDataBit_1621(uchar data,uchar cnt) //data 的低cnt 位写入HT1621,低位在前
{
uchar i;
for(i =0; i <cnt; i ++)
{
if((data&0x01)==0) dat=0;
else dat=1;
wr=0;
nop();
wr=1;
data>>=1;
}
}
/***************************************************/
//写HT1621命令模式函数,
/***************************************************/
void SendCmd(uchar command)
{
cs=0;
SendBit_1621(0x80,3); //写入标志码"100"
SendBit_1621(command,9); //写入9 位数据,其中前8 位为command 命令,最后1 位任意
cs=1;
}
/***************************************************/
//写HT1621地址与地址内数据函数
/***************************************************/
void Write_1621(uchar addr,uchar data)
{
cs=0;
SendBit_1621(0xa0,3); //写入标志码"101"
SendBit_1621(addr<<2,6); //写入6 位addr
SendDataBit_1621(data,4); //写入data 的低4 位
cs=1;
}
/***************************************************/
//写HT1621地址内连续写数据函数
/****************************************************/
void WriteAll_1621(uchar addr,uchar *p,uchar cnt)
{
uchar i;
cs=0;
SendBit_1621(0xa0,3); //写入标志码"101"
SendBit_1621(addr<<2,6); //写入6 位addr
for(i =0; i <cnt; i ++,p++) //连续写入数据
{
SendDataBit_1621(*p,8);
}
cs=1;
}
/***************************************************/
//写ds1302单个地址函数
/***************************************************/
uchar write_8bits(uchar add)//单个字节写入
{
uchar i;
ioo=0;
nop();
for(i=0;i<8;i++)
{
sck=0;
io=add&0x01;
add=add>>1;
sck=1;
nop();
}
}
/***************************************************/
//写ds1302地址数据函数
/***************************************************/
void write_shineng(uchar add,uchar date)
{
rst=0;
nop();
sck=0;
nop();
rst=1;
nop();
write_8bits(add);
write_8bits(date);
rst=1;
nop();
io=1;
sck=1;
}
/***************************************************/
//写ds1302地址数据函数,写入秒 分 时 日 星期 月 年
/***************************************************/
void write_ren()
{
uchar j,i;
write_shineng(0x8e,0x00);
for(j=0;j<7;j++)
{
write_shineng(write_table[j],table1[j]);
}
write_shineng(0x8e,0x80);
}
/***************************************************/
//读ds1302单个地址数据函数,带参数返回。秒 分 时 日 星期 月 年
/***************************************************/
uchar read_8bits(uchar add)
{
uchar y,ass;
sck=0;
nop();
rst=0;
nop();
rst=1;
nop();
write_8bits(add);
ioo=1;
for(y=0;y<8;y++)
{
ass=ass>>1;
sck=0;
if(io)
{
ass=ass|0x80;
}
else ass=ass&0x7f;
sck=1;
nop();
}
rst=0;
nop();
sck=0;
nop();
sck=1;
io=1;
ioo=0;
return(ass);
}
/***************************************************/
//延时函数ms
/***************************************************/
void delay(uint x)
{
uint a,b;
for(a=x;a>0;a--)
for(b=110;b>0;b--);
}
/***************************************************/
//将LCD时间COL两点处理到HT1621上显示每秒闪烁一次函数
/***************************************************/
void COL()
{
Write_1621(0x14,0x08);
delay(500);
Write_1621(0x14,0x00);
delay(500);
} |
|