如果有用请点赞,还会继续更新的
这个是用ds1302写的,还有一个是用中断写得,如果有需要,主页见!
题目:
思路:
通过Ds1302来实现 时间的控制,onewrie.c来读取温度,并且显示出来。按键的功能和判断已经很常规了,要注意的就是在延时的时候,数码管显示的函数依旧要工作。
源代码:
main.c
#include"reg52.h"
#include"intrins.h"
#include"ds1302.h"
#include"onewire.h"
void smg_Display();
int i=0;
char code xianshi[13]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf,0xc6};
unsigned int temper=0;
int nao_time=0,count=0;
int nao_h=0,nao_m=0,nao_s=0;
sbit S4=P3^3;
sbit S5=P3^2;
sbit S6=P3^1;
sbit S7=P3^0;
sbit L1=P0^7;
int K4=0,K5=0,K6=0,K7=0;
unsigned int code Write_ADDR[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
unsigned int code Read_ADDR[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
unsigned int Time[3]={0x55,0x59,0x23};
void Put_in_time()
{
int i=0;
Write_Ds1302_Byte(0x8e,0x00);
for(i=0;i<3;i++)
{
Write_Ds1302_Byte(Write_ADDR[i],Time[i]);
}
Write_Ds1302_Byte(0x8e,0x80);
}
void Get_out_time()
{
int i;
for(i=0;i<3;i++)
{
Time[i]=Read_Ds1302_Byte(Read_ADDR[i]);
}
}
void choose138(int n)
{
switch(n)
{
case(0):P2=(P2&0x1f)|0x00;break;
case(4):P2=(P2&0x1f)|0x80;break;
case(5):P2=(P2&0x1f)|0xa0;break;
case(6):P2=(P2&0x1f)|0xc0;break;
case(7):P2=(P2&0x1f)|0xe0;break;
}
}
void clean()
{
choose138(4);
P0=0xff;
choose138(5);
P0=0x00;
}
//-----------------------------------------------------nao_zhang
void Delay200ms() //@11.0592MHz
{
unsigned char i, j;
_nop_();
_nop_();
i = 76;
j = 74;
do
{ smg_Display();
while (--j);
} while (--i);
}
void nao_zhong()
{
if(nao_s==((Time[0]/16)*10+(Time[0]%16))&&nao_m==((Time[1]/16)*10+(Time[1]%16))&&nao_h==((Time[2]/16)*10+(Time[2]%16)))
{
for(nao_time=0;nao_time<13;nao_time++)
{
if(S4==0||S5==0||S6==0||S7==0)
{break;}
else
{
Get_out_time();
smg_Display();
P0=0xff;
choose138(4);
L1=~L1;
Delay200ms();
choose138(4);
P0=0xff;
Delay200ms();
}
}
choose138(4);
P0=0xff;
}
}
//-----------------------------------------------------
//----------------------------------------------------- wei_du
void Get_temper()
{
char HBS,LBS;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay_OneWire(200);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
LBS=Read_DS18B20();
HBS=Read_DS18B20();
temper=HBS<<4;
temper=temper|(LBS>>4);
}
//-----------------------------------------------------
//----------------------------------------------------- an_jian
void Delay5ms() //@11.0592MHz
{
unsigned char i, j;
i = 54;
j = 199;
do
{
while (--j);
} while (--i);
}
void key_panduan_time()
{
if(S7==0&&K6==0)
{
Delay5ms();
if(S7==0)
{
K7++;
if(K7==4)
K7=0;
}
while(!S7);
}
if(K7==1&&S5==0)
{
int temp;
Delay5ms();
if(S5==0)
{
Get_out_time();
temp=Time[2]%16+(Time[2]/16)*10;
temp++;
if(temp==25)
temp=0;
Time[2]=(temp+(temp/10)*6);
Put_in_time();
}
while(!S5);
}
if(K7==2&&S5==0)
{
int temp;
Delay5ms();
if(S5==0)
{
Get_out_time();
temp=Time[1]%16+(Time[1]/16)*10;
temp++;
if(temp==60)temp=0;
Time[1]=(temp+(temp/10)*6);
Put_in_time();
}
while(!S5);
}
if(K7==3&&S5==0)
{
int temp;
Delay5ms();
if(S5==0)
{
Get_out_time();
temp=Time[0]%16+(Time[0]/16)*10;
temp++;
if(temp==60)temp=0;
Time[0]=(temp+(temp/10)*6);
Put_in_time();
}
while(!S5);
}
if(K7==1&&S4==0)
{
int temp;
Delay5ms();
if(S4==0)
{
Get_out_time();
temp=Time[2]%16+(Time[2]/16)*10;
temp--;
if(temp==-1)
temp=23;
Time[2]=(temp+(temp/10)*6);
Put_in_time();
}
while(!S4);
}
if(K7==2&&S4==0)
{
int temp;
Delay5ms();
if(S4==0)
{
Get_out_time();
temp=Time[1]%16+(Time[1]/16)*10;
temp--;
if(temp==-1)temp=59;
Time[1]=(temp+(temp/10)*6);
Put_in_time();
}
while(!S4);
}
if(K7==3&&S4==0)
{
int temp;
Delay5ms();
if(S4==0)
{
Get_out_time();
temp=Time[0]%16+(Time[0]/16)*10;
temp--;
if(temp==-1)temp=59;
Time[0]=(temp+(temp/10)*6);
Put_in_time();
}
while(!S4);
}
if(S6==0&&K7==0)
{
Delay5ms();
if(S6==0)
{
K6++;
if(K6==4)
K6=0;
}
while(!S6);
}
if(K6==1&&S5==0)
{
Delay5ms();
if(S5==0)
{
nao_h++;
if(nao_h==24)nao_h=0;
}
while(!S5);
}
if(K6==2&&S5==0)
{
Delay5ms();
if(S5==0)
{
nao_m++;
if(nao_m==60)nao_m=0;
}
while(!S5);
}
if(K6==3&&S5==0)
{
Delay5ms();
if(S5==0)
{
nao_s++;
if(nao_s==60)nao_s=0;
}
while(!S5);
}
if(K6==1&&S4==0)
{
Delay5ms();
if(S4==0)
{
nao_h--;
if(nao_h==-1)nao_h=23;
}
while(!S4);
}
if(K6==2&&S4==0)
{
Delay5ms();
if(S4==0)
{
nao_m--;
if(nao_m==-1)nao_m=59;
}
while(!S4);
}
if(K6==3&&S4==0)
{
Delay5ms();
if(S4==0)
{
nao_s--;
if(nao_s==-1)nao_s=59;
}
while(!S4);
}
}
//-----------------------------------------------------
//-----------------------------------------------------SMG xian_shi
void Delay300us() //@11.0592MHz
{
unsigned char i, j;
_nop_();
_nop_();
i = 4;
j = 54;
do
{
while (--j);
} while (--i);
}
void SMG_display(int wei,int dat)
{
choose138(6);
P0=0x80>>(wei-1);
choose138(7);
P0=xianshi[dat];
}
void smg_Display()
{
if(K6==0&&K7==0&&S4==0)
{
Delay5ms();
while(S4==0)
{
SMG_display(1,12);
Delay300us();
SMG_display(2,temper%10);
Delay300us();
SMG_display(3,temper/10);
Delay300us();
SMG_display(4,10);
Delay300us();
SMG_display(5,10);
Delay300us();
SMG_display(6,10);
Delay300us();
SMG_display(7,10);
Delay300us();
SMG_display(8,10);
Delay300us();
}
}
if(K6==0&&K7==0)
{
SMG_display(1,Time[0]%16);
Delay300us();
SMG_display(2,Time[0]/16);
Delay300us();
SMG_display(3,11);
Delay300us();
SMG_display(4,Time[1]%16);
Delay300us();
SMG_display(5,Time[1]/16);
Delay300us();
SMG_display(6,11);
Delay300us();
SMG_display(7,Time[2]%16);
Delay300us();
SMG_display(8,Time[2]/16);
Delay300us();
}
if(K6==0&&K7==1)
{
i++;
SMG_display(1,Time[0]%16);
Delay300us();
SMG_display(2,Time[0]/16);
Delay300us();
SMG_display(3,11);
Delay300us();
SMG_display(4,Time[1]%16);
Delay300us();
SMG_display(5,Time[1]/16);
Delay300us();
SMG_display(6,11);
Delay300us();
if(i<=70)
{
SMG_display(7,10);
Delay300us();
SMG_display(8,10);
Delay300us();
}
else if(i>70&&i<140)
{
SMG_display(7,Time[2]%16);
Delay300us();
SMG_display(8,Time[2]/16);
Delay300us();
}
else if(i>140)i=0;
}
if(K6==0&&K7==2)
{
i++;
SMG_display(1,Time[0]%16);
Delay300us();
SMG_display(2,Time[0]/16);
Delay300us();
SMG_display(3,11);
Delay300us();
SMG_display(7,Time[2]%16);
Delay300us();
SMG_display(8,Time[2]/16);
Delay300us();
SMG_display(6,11);
Delay300us();
if(i<=70)
{
SMG_display(4,10);
Delay300us();
SMG_display(5,10);
Delay300us();
}
else if(i>70&&i<140)
{
SMG_display(4,Time[1]%16);
Delay300us();
SMG_display(5,Time[1]/16);
Delay300us();
}
else if(i>140)i=0;
}
if(K6==0&&K7==3)
{
i++;
SMG_display(7,Time[2]%16);
Delay300us();
SMG_display(8,Time[2]/16);
Delay300us();
SMG_display(3,11);
Delay300us();
SMG_display(4,Time[1]%16);
Delay300us();
SMG_display(5,Time[1]/16);
Delay300us();
SMG_display(6,11);
Delay300us();
if(i<=70)
{
SMG_display(1,10);
Delay300us();
SMG_display(2,10);
Delay300us();
}
else if(i>70&&i<140)
{
SMG_display(1,Time[0]%16);
Delay300us();
SMG_display(2,Time[0]/16);
Delay300us();
}
else if(i>140)i=0;
}
if(K7==0&&K6==1)
{
i++;
SMG_display(1,nao_s%10);
Delay300us();
SMG_display(2,nao_s/10);
Delay300us();
SMG_display(3,11);
Delay300us();
SMG_display(4,nao_m%10);
Delay300us();
SMG_display(5,nao_m/10);
Delay300us();
SMG_display(6,11);
Delay300us();
if(i<70)
{
SMG_display(7,10);
Delay300us();
SMG_display(8,10);
Delay300us();
}
else if(i>70&&i<140)
{
SMG_display(7,nao_h%10);
Delay300us();
SMG_display(8,nao_h/10);
Delay300us();
}
else if(i>140)i=0;
}
if(K7==0&&K6==2)
{
i++;
SMG_display(1,nao_s%10);
Delay300us();
SMG_display(2,nao_s/10);
Delay300us();
SMG_display(3,11);
Delay300us();
SMG_display(7,nao_h%10);
Delay300us();
SMG_display(8,nao_h/10);
Delay300us();
SMG_display(6,11);
Delay300us();
if(i<70)
{
SMG_display(4,10);
Delay300us();
SMG_display(5,10);
Delay300us();
}
else if(i>70&&i<140)
{
SMG_display(4,nao_m%10);
Delay300us();
SMG_display(5,nao_m/10);
Delay300us();
}
else if(i>140)i=0;
}
if(K7==0&&K6==3)
{
i++;
SMG_display(7,nao_h%10);
Delay300us();
SMG_display(8,nao_h/10);
Delay300us();
SMG_display(3,11);
Delay300us();
SMG_display(4,nao_m%10);
Delay300us();
SMG_display(5,nao_m/10);
Delay300us();
SMG_display(6,11);
Delay300us();
if(i<70)
{
SMG_display(1,10);
Delay300us();
SMG_display(2,10);
Delay300us();
}
else if(i>70&&i<140)
{
SMG_display(1,nao_s%10);
Delay300us();
SMG_display(2,nao_s/10);
Delay300us();
}
else if(i>140)i=0;
}
}
//-----------------------------------------------------
void main()
{
clean();
Put_in_time();
while(1)
{
Get_out_time();
key_panduan_time();
nao_zhong();
Get_temper();
smg_Display();
}
}
ds1302.c
/*
程序说明: 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(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
SCK=0;
SDA=temp&0x01;
temp>>=1;
SCK=1;
}
}
void Write_Ds1302_Byte( unsigned char address,unsigned char dat )
{
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(address);
Write_Ds1302(dat);
RST=0;
}
unsigned char Read_Ds1302_Byte ( unsigned char address )
{
unsigned char i,temp=0x00;
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(address);
for (i=0;i<8;i++)
{
SCK=0;
temp>>=1;
if(SDA)
temp|=0x80;
SCK=1;
}
RST=0; _nop_();
SCK=0; _nop_();
SCK=1; _nop_();
SDA=0; _nop_();
SDA=1; _nop_();
return (temp);
}
ds1302.h
#ifndef __DS1302_H
#define __DS1302_H
void Write_Ds1302(unsigned char temp);
void Write_Ds1302_Byte( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302_Byte( unsigned char address );
#endif
onewire.c
/*
程序说明: 单总线驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台(外部晶振12MHz) STC89C52RC单片机
日 期: 2011-8-9
*/
#include "reg52.h"
sbit DQ = P1^4; //单总线接口
//单总线延时函数
void Delay_OneWire(unsigned int t) //STC89C52RC
{
t=t*12;
while(t--);
}
//通过单总线向DS18B20写一个字节
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(5);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(5);
}
//从DS18B20读取一个字节
unsigned char Read_DS18B20(void)
{
unsigned char i;
unsigned char dat;
for(i=0;i<8;i++)
{
DQ = 0;
dat >>= 1;
DQ = 1;
if(DQ)
{
dat |= 0x80;
}
Delay_OneWire(5);
}
return dat;
}
//DS18B20设备初始化
bit init_ds18b20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(12);
DQ = 0;
Delay_OneWire(80);
DQ = 1;
Delay_OneWire(10);
initflag = DQ;
Delay_OneWire(5);
return initflag;
}
onewire.h
#ifndef __ONEWIRE_H
#define __ONEWIRE_H
unsigned char rd_temperature(void); //; ;
void Write_DS18B20(unsigned char dat);
unsigned char Read_DS18B20(void);
bit init_ds18b20(void);
void Delay_OneWire(unsigned int t);
#endif
**
码字不容易,点个赞再走!!!!
**