|
#include <stc15f2k60s2.h>
#include <iic.h>
#include <ds18b20.h>
#include <ds1302.h>
#include <STDIO.H>
#define uchar unsigned char
#define uint unsigned int
uchar code tab[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XBF,0XFF,0xc6,0x89};
uchar yi,er,san,si,wu,liu,qi,ba;
uchar miao,fen,shi,sec_last;
uchar wendu,shidu,liang;
uchar receive_num,receive;
uchar tx_bufi[]="{20-20%} {23-59-50} {0}\r\n";
bit kaishi,moshi;
uchar jiemian,jiejin;
uchar time,tt,jilu;
uchar ci;
uchar wen_du[5],shi_du[5],shi_[5],fen_[5],miao_[5],time_[5];
void delayms(uint i)
{
uint a;
for(i;i>0;i--)
for(a=845;a>0;a--);
}
void smg(uchar yi,er,san,si,wu,liu,qi,ba)
{
P2=0XC0;P0=0X01;P2=0XFF;P0=tab[yi];delayms(1);
P2=0XC0;P0=0X02;P2=0XFF;P0=tab[er];delayms(1);
P2=0XC0;P0=0X04;P2=0XFF;P0=tab[san];delayms(1);
P2=0XC0;P0=0X08;P2=0XFF;P0=tab[si];delayms(1);
P2=0XC0;P0=0X10;P2=0XFF;P0=tab[wu];delayms(1);
P2=0XC0;P0=0X20;P2=0XFF;P0=tab[liu];delayms(1);
P2=0XC0;P0=0X40;P2=0XFF;P0=tab[qi];delayms(1);
P2=0XC0;P0=0X80;P2=0XFF;P0=tab[ba];delayms(1);
P2=0XC0;P0=0XFF;P2=0XFF;P0=0XFF;
}
void key()
{
bit s5,s4;
if(P32==0)
{
delayms(5);
if(P32==0)
{
s5=1;
}
}
else if(P33==0)
{
delayms(5);
if(P33==0)
{
s4=1;
}
}
if((s4==1)&&(P33==1))
{
s4=0;
moshi=~moshi;
}
else if((s5==1)&&(P32==1))
{
s5=0;
jiemian++;
if(jiemian==3)jiemian=0;
}
}
void UartInit(void) //1200bps@11.0592MHz
{
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x40; //定时器1时钟为Fosc,即1T
AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
TMOD &= 0x0F; //设定定时器1为16位自动重装方式
TL1 = 0x00; //设定定时初值
TH1 = 0xF7; //设定定时初值
ET1 = 0; //禁止定时器1中断
TR1 = 1; //启动定时器1
EA=1;ES=1;
}
void Timer0Init(void) //5毫秒@11.0592MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x00; //设置定时初值
TH0 = 0x28; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
EA=1;ET0=1;
}
void send (uchar *p)
{
uchar index=0;
do
{
SBUF=p[index++];
while(TI==0);
TI=0;
}
while(p[index]!=0);
}
void smgxianshi()
{
if(jiemian==0)
{
yi=wendu/10;er=wendu%10;san=12;si=11;wu=11;liu=shidu/10;qi=shidu%10;ba=13;
}
else if(jiemian==1)
{
yi=shi/10;er=shi%10;
si=fen/10;wu=fen%10;
qi=miao/10;ba=miao%10;
if(miao%2==0){san=10;liu=10;}
else {san=11;liu=11;}
}
else if(jiemian==2)
{
yi=11;er=11;san=11;si=10;
wu=11;liu=11;qi=11;ba=11;
}
}
void main()
{
P2=0XA0;P0=0X00;P2=0X80;P0=0XFF;
P2=0XC0;P0=0XFF;P2=0XFF;P0=0XFF;
yi=11;er=11;san=11;si=11;wu=11;liu=11;qi=11;ba=11;
UartInit();
Timer0Init();
Write_Ds1302(0x8e,0x00);
Write_Ds1302(0x84,0x23);
Write_Ds1302(0x82,0x59);
Write_Ds1302(0x80,0x55);
while(1)
{
shidu=ad_du(0x01);
miao=Read_Ds1302(0x81);
fen=Read_Ds1302(0x83);
shi=Read_Ds1302(0x85);
shi=(shi/16)*10+shi%16;
fen=(fen/16)*10+fen%16;
miao=(miao/16)*10+miao%16;
wendu=ds18b20();
liang=ad_du(0x03);
smgxianshi();
if(moshi==0)
{
if(liang>30)
{
P2=0X80;P0=0XFE;
jiejin=0;
}
else //1111 1010
{
P2=0X80;P0=0XFA;
jiejin=1;
}
// if(kaishi==1)
// {
// if(sec_last!=miao)
// {
// sec_last=miao;
// sprintf((char*)tx_bufi,"{%bd-%bd%%} {%0.2bd-%0.2bd-%0.2bd} {%bd}\r\n",wendu,shidu,shi,fen,miao,jiejin);
// send(tx_bufi);
// }
//
// }
}
else if(moshi==1)
{
kaishi=0;
if(liang>30)
{
P2=0X80;P0=0XFD;
if(jiejin==1) //保存数据
{
jiejin=0;
if(ci==0)
{
at24c02_xie(20,wendu);delayms(2);
at24c02_xie(21,shidu);delayms(2);
at24c02_xie(22,shi);delayms(2);
at24c02_xie(23,fen);delayms(2);
at24c02_xie(24,miao);delayms(2);
at24c02_xie(25,time);delayms(2);
ci=1;
}
else if(ci==1)
{
at24c02_xie(30,wendu);delayms(2);
at24c02_xie(31,shidu);delayms(2);
at24c02_xie(32,shi);delayms(2);
at24c02_xie(33,fen);delayms(2);
at24c02_xie(34,miao);delayms(2);
at24c02_xie(35,time);delayms(2);
ci=2;
}
else if(ci==2)
{
at24c02_xie(40,wendu);delayms(2);
at24c02_xie(41,shidu);delayms(2);
at24c02_xie(42,shi);delayms(2);
at24c02_xie(43,fen);delayms(2);
at24c02_xie(44,miao);delayms(2);
at24c02_xie(45,time);delayms(2);
ci=3;
}
else if(ci==3)
{
at24c02_xie(50,wendu);delayms(2);
at24c02_xie(51,shidu);delayms(2);
at24c02_xie(52,shi);delayms(2);
at24c02_xie(53,fen);delayms(2);
at24c02_xie(54,miao);delayms(2);
at24c02_xie(55,time);delayms(2);
ci=4;
}
else if(ci==4)
{
at24c02_xie(60,wendu);delayms(2);
at24c02_xie(61,shidu);delayms(2);
at24c02_xie(62,shi);delayms(2);
at24c02_xie(63,fen);delayms(2);
at24c02_xie(64,miao);delayms(2);
at24c02_xie(65,time);delayms(2);
ci=0;
}
}
}
else //1111 1001 接近时间
{
P2=0X80;P0=0XF9;
jiejin=1;
}
}
if(kaishi==1)
{
// if(moshi==0)
// {
// if(sec_last!=miao)
// {
// sec_last=miao;
// sprintf((char*)tx_bufi,"{%bd-%bd%%} {%0.2bd-%0.2bd-%0.2bd} {%bd}\r\n",wendu,shidu,shi,fen,miao,jiejin);
// send(tx_bufi);
// }
// }
for(ci=0;ci<5;ci++)
{
wen_du[ci]=at24c02_du(20+ci*10);
}
sprintf((char*)tx_bufi,"{%bd-%bd%%} {%0.2bd-%0.2bd-%0.2bd} {%bd}\r\n",wendu,shidu,shi,fen,miao,jiejin);
send(tx_bufi);
}
key();
smg(yi,er,san,si,wu,liu,qi,ba);
}
}
void time0() interrupt 1
{
if(jiejin==1)
{
tt++;
if(tt==200)
{
tt=0;
time++;
}
}
}
void ser() interrupt 4
{
RI=0;
receive=SBUF;
if(receive_num==0)
{
if(receive=='A')receive_num=1;
else receive_num=0;
}
else if(receive_num==1)
{
if(receive=='A')receive_num=2;
else receive_num=0;
}
else if(receive_num==2)
{
if(receive=='A')receive_num=3;
else receive_num=0;
}
else if(receive_num==3)
{
if(receive=='S')receive_num=4;
else receive_num=0;
}
else if(receive_num==4)
{
if(receive=='S')receive_num=5;
else receive_num=0;
}
else if(receive_num==5)
{
if(receive=='S')
{
receive_num=0;
kaishi=1;
}
else receive_num=0;
}
}
////////////////////////////////////////////////////////////////////////////////////
/*
程序说明: IIC总线驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台 8051,12MHz
日 期: 2011-8-9
*/
#include "reg52.h"
#include "intrins.h"
#define somenop {_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();}
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
//总线引脚定义
sbit SDA = P2^1; /* 数据线 */
sbit SCL = P2^0; /* 时钟线 */
//总线启动条件
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
somenop;
SDA = 0;
somenop;
SCL = 0;
}
//总线停止条件
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
somenop;
SDA = 1;
}
//应答位控制
void IIC_Ack(bit ackbit)
{
if(ackbit)
{
SDA = 0;
}
else
{
SDA = 1;
}
somenop;
SCL = 1;
somenop;
SCL = 0;
SDA = 1;
somenop;
}
//等待应答
bit IIC_WaitAck(void)
{
SDA = 1;
somenop;
SCL = 1;
somenop;
if(SDA)
{
SCL = 0;
IIC_Stop();
return 0;
}
else
{
SCL = 0;
return 1;
}
}
//通过I2C总线发送数据
void IIC_SendByte(unsigned char byt)
{
unsigned char i;
for(i=0;i<8;i++)
{
if(byt&0x80)
{
SDA = 1;
}
else
{
SDA = 0;
}
somenop;
SCL = 1;
byt <<= 1;
somenop;
SCL = 0;
}
}
//从I2C总线上接收数据
unsigned char IIC_RecByte(void)
{
unsigned char da;
unsigned char i;
for(i=0;i<8;i++)
{
SCL = 1;
somenop;
da <<= 1;
if(SDA)
da |= 0x01;
SCL = 0;
somenop;
}
return da;
}
unsigned char iic_du(unsigned char add)
{
unsigned char num;
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0xa1);
IIC_WaitAck();
num=IIC_RecByte();
IIC_WaitAck();
IIC_Stop();
return num;
}
void iic_xie(unsigned add,dat)
{
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
///////////////////////////////////////////////////////////////////////////////////
/*
程序说明: DS1302驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台 8051,12MHz
日 期: 2011-8-9
*/
#include <reg52.h>
#include <intrins.h>
sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST = P1^3; // DS1302复位
void Write_Ds1302_Byte(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
SCK=0;
SDA=temp&0x01;
temp>>=1;
SCK=1;
}
}
void Write_Ds1302( unsigned char address,unsigned char dat )
{
RST=0;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
SCK=0;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
RST=1;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
Write_Ds1302_Byte(address);
Write_Ds1302_Byte(dat);
RST=0;
}
unsigned char Read_Ds1302 ( unsigned char address )
{
unsigned char i,temp=0x00;
RST=0;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
SCK=0;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
RST=1;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
Write_Ds1302_Byte(address);
for (i=0;i<8;i++)
{
SCK=0;
temp>>=1;
if(SDA)
temp|=0x80;
SCK=1;
}
RST=0;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
RST=0;
SCK=0;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
SCK=1;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
SDA=0;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
SDA=1;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
return (temp);
}
|
评分
-
查看全部评分
|