分类: C/C++
2009-05-19 12:04:12
//************************************************
#include
#include
#include
#define uchar unsigned char
//****** HD7279A 指令 ******
#define CMD_RESET 0xa4 ///复位指令 全部复位
#define CMD_TEST 0xbf ///测试指令 LED全部亮并处于闪烁的状态
#define DECODE0 0xc8 ///下载数据0xc8 //译码方式1
//#define DECODE1 0x80 ///下载数据0x80 //译码方式0
#define CMD_READ 0x15 /////读键盘数据
#define UNDECODE 0x80 ////下载数据但不译码
#define RTL_CYCLE 0xa3 //循环左移
#define RTR_CYCLE 0xa2 //循环右移
#define RTL_UNCYL 0xa1 ///左移 指令
#define RTR_UNCYL 0xa0 ///右移指令
#define ACTCTL 0x98 ///消隐 控制
#define SEGON 0xe0 ///段点亮指令
#define SEGOFF 0xc0 ///段关闭指令
#define BLINKCTL 0x88 ///闪烁控制
#define GANG 0X0A //-显示
#define miaodanyuanxie 0x80
#define time_shuaxin()
#define Ram 0xc0
////////////////////////////////////////////////////////////////////////////////////////
//#define shezhis()
sbit ring=P3^7;
//1302
sbit TIMECLK=P1^0;
sbit TIMERST=P1^2;
sbit TIMEIO=P1^1;
#define uint unsigned int
sbit P3_7=P3^7; //蜂鸣器驱动
uint rti;
uint m;
uchar code toneh[22]={0x00,0xfe,0xfc,0xfd,0xfd,0xfd,0xfd,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff};
uchar code tonel[22]={0x00,0x44,0xac,0xba,0x33,0x83,0xc7,0x07,0x22,0x56,0x85,0x9a,0xc1,0xe3,0x03,
0x10,0x2b,0x40,0x4c,0x60,0x71,0x81};
uchar code song[]="12311231345534555654315654312a112a11";
uchar code length[]="4444444444444444222244222244444544454";
//*** 变量及I/O口定义 ***
//7279
sbit cs=P2^7;
sbit clk=P1^6;
sbit dat=P1^7;
sbit key=P1^3;
sbit kaiguan=P3^0;
sbit fengmingqi=P3^1;
bit shoudaokey=1,shoudaokey2=1,shezhi=1,jieshu=1,naozhongflag=0;
unsigned char key_number, j, k,shoudao=0,xianxian=0x80,i;
unsigned char shi1,shi2,fen1,fen2,miao1,miao2,shi=17,fen=44,miao=53,naomiao,naofen,naoshi;
unsigned char shijianxp[4];
unsigned char *ip,*ipp,*ippp;
unsigned char miaojishu=0,miaoflag=0,jianpan=0xff;
char jianpanflag=0x0f;
//music
//DS1302读写C51源程序
//------------------------------------------------
//打开DS1302
void TimeSpiOpen(void)
{
TIMECLK = 0;
TIMERST = 0;//禁止DS1302
TIMEIO = 1;//释放数据总线
TIMERST = 1;//使能DS1302
}
//关闭DS1302
void TimeSpiClose(void)
{
TIMERST = 0;//禁止DS1302
TIMEIO = 1;//释放数据总线
TIMECLK = 1;
}
//读写DS1302
unsigned char TimeSpiReadWrite(unsigned char val)
{
unsigned char data i,j,k;
j= val; //取8位数据
for (i = 8;i > 0; i--)
{
TIMECLK = 0; //时钟下降沿输入数据(DS1302读)
k=k>>1;
if(TIMEIO) //接收串行数据到CY
{
k=k|0x80;
}
TIMEIO = j&0x01; //发送串行数据
j=j>>1;
TIMECLK = 1; //时钟上升沿打入数据(DS1302写)
TIMEIO = 1; //释放数据总线
}
return k;
}
//读DS1302时钟字节
unsigned char TimeByteRead(unsigned char address)
{
unsigned char data i;
TimeSpiOpen(); //打开DS1302
TimeSpiReadWrite(address); //写入DS1302命令
i = TimeSpiReadWrite(0xff); //读入DS1302数据
TimeSpiClose(); //关闭DS1302
return i; //返回数据
}
//读DS1302RAM字节
unsigned char TimeRamByteRead(unsigned char address)
{
unsigned char i;
TimeSpiOpen();//打开DS1302
TimeSpiReadWrite((address << 1) | 0xc1);//写入DS1302命令
i = TimeSpiReadWrite(0xff);//读入DS1302数据
// i=address;
TimeSpiClose();//关闭DS1302
return i;//返回数据
}
// 写DS1302时钟字节
void TimeByteWrite(unsigned char address, unsigned char val)
{
TimeSpiOpen(); //打开DS1302
TimeSpiReadWrite(address); //写地址
TimeSpiReadWrite(val); //写数据
TimeSpiClose(); //关闭DS1302
}
//写DS1302字节
void TimeRamByteWrite(unsigned char address, unsigned char val)
{
TimeSpiOpen();//打开DS1302
TimeSpiReadWrite((address << 1) | 0xc0);//写地址
TimeSpiReadWrite(val);//写数据
TimeSpiClose();//关闭DS1302
}
void TimeWriteEnable(void)
{
TimeByteWrite( 0x8e,0x80); //使能DS1302写保护,写地址,再写数据
}
void TimeWrieDisbale(void)
{
TimeByteWrite(0x8e, 0x00); //开放DS1302写保护
}
//DS1302工作开始
void TimeWorkStart(void)
{
unsigned char val;
val = TimeByteRead(0x80);//读秒数据
val &= 0x7f;//打开晶振
TimeWrieDisbale();//开放DS1302写保护
TimeByteWrite(0x80, val);//打开晶振
TimeByteWrite(0x90, 0xa5);//打开充电二极管
TimeWriteEnable();//使能DS1302写保护
}
/********************************************************************
函 数 名:Set1302()
功 能:设置初始时间
说 明:先写地址,后读命令/数据(寄存器多字节方式)
调 用:W1302()
入口参数:pClock: 设置时钟数据
地址格式为: 秒 分 时 日 月 星期 年
7Byte (BCD码)1B 1B 1B 1B 1B 1B 1B
返 回 值:无
设 计:李三豹
***********************************************************************/
void Set1302(void)
{
TimeWrieDisbale();//开放DS1302写保护
TimeByteWrite(miaodanyuanxie,shijianxp[3]); //写秒
TimeByteWrite(miaodanyuanxie+2,shijianxp[2]); //写分
TimeByteWrite(miaodanyuanxie+4,shijianxp[1]); //写时
TimeByteWrite(miaodanyuanxie+6,0x28); //写日
TimeByteWrite(miaodanyuanxie+8,0x04); //写月
TimeByteWrite(miaodanyuanxie+10,0x02); //写星期
TimeByteWrite(miaodanyuanxie+12,0x09); //写年
TimeWriteEnable(); //使能DS1302写保护
}
//*****************************7279****************************
void long_delay(void)
{
unsigned char i;
for (i=0;i<0x30;i++);
}
void short_delay(void)
{
unsigned char i;
for (i=0;i<8;i++);
}
void send_byte( unsigned char out_byte) //发送
{
unsigned char i;
cs=0;
long_delay();
for (i=0;i<8;i++)
{
if (out_byte&0x80)
{
dat=1;
}
else
{
dat=0;
}
clk=1;
short_delay();
clk=0;
short_delay();
out_byte=out_byte*2;
}
dat=0;
}
unsigned char receive_byte(void) //接受
{
unsigned char i, in_byte;
dat=1; // set to input mode
long_delay();
for (i=0;i<8;i++)
{
clk=1;
short_delay();
in_byte=in_byte*2;
if (dat)
{
in_byte=in_byte|0x01;
}
clk=0;
short_delay();
}
dat=0;
return (in_byte);
}
// ************************* 延时 n*10ms *************************
void write7279(unsigned char cmd, unsigned char dta) /// 先写命令再写 数据
{
send_byte (cmd);
send_byte (dta);
}
unsigned char read7279(unsigned char command) ///单单的写命令
{
send_byte(command);
return(receive_byte());
}