ds1302.c
/*
程序说明: DS1302驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台 8051,12MHz
日 期: 2011-8-9
*/
#include "ds1302.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设置时间函数
void Set_Clock(u8 hour,u8 minute,u8 second)
{
Write_Ds1302_Byte(0x8e,0x00);//关闭写保护
Write_Ds1302_Byte(0x80,DecToBcd(second));
Write_Ds1302_Byte(0x82,DecToBcd(minute));
Write_Ds1302_Byte(0x84,DecToBcd(hour));
Write_Ds1302_Byte(0x8e,0x80);//开启写保护
}
ds1302.h
#ifndef __DS1302_H
#define __DS1302_H
#include "sys.h"
#define DecToBcd(dec) (dec/10*16)+(dec%10)
#define BcdToDec(bcd) (bcd/16*10)+(bcd%16)
void Write_Ds1302(unsigned char temp);
void Write_Ds1302_Byte( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302_Byte( unsigned char address );
//设置初始时间
void Set_Clock(u8 hour,u8 minute,u8 second);
#endif
main.c
#include "sys.h"
#include "tim.h"
#include "smg.h"
#include "key.h"
#include "onewire.h"
#include "iic.h"
#include "ds1302.h"
HextoBin led_ctrl,uln_ctrl;//LED,蜂鸣器,继电器控制变量
u16 cnt_key;//三行按键扫描标志位,1ms加一次
u16 cnt_temperature;//温度读取标志位,1ms加一次
u16 cnt_ADC;//ADC读取标志位,1ms加一次
u32 temperature=850000;
u8 start_times = 0;//开机次数
u16 ch1 = 0;//光敏电阻值
u16 ch3 = 0;//分压电阻值
u8 num = 0;
u8 shi,fen,miao;
void System_Init()
{
led_ctrl.hex = 0xff;
Device_Ctrl(0x80,led_ctrl.hex);//关闭LED灯
uln_ctrl.hex = 0x00;
Device_Ctrl(0xa0,uln_ctrl.hex);//关闭蜂鸣器和继电器
}
void SMG_Process()//数码管处理函数
{
smg_buf[0] = smg_duanxuan[shi/10];
smg_buf[1] = smg_duanxuan[shi%10];
smg_buf[2] = ~0xbf;
smg_buf[3] = smg_duanxuan[fen/10];
smg_buf[4] = smg_duanxuan[fen%10];;
smg_buf[5] = ~0xbf;
smg_buf[6] = smg_duanxuan[miao/10];
smg_buf[7] = smg_duanxuan[miao%10];
}
void KEY_Process()//按键处理模块
{
if(cnt_key>=10)
{
cnt_key = 0;
KBD_ThreeLine();
if(Trig_KBD == 0x48)//s5
{
num++;
}
if(Trig_KBD == 0x88)//S4
{
num--;
}
}
}
void DS18B20_Process()
{
if(cnt_temperature>=100)
{
cnt_temperature = 0;
temperature = rd_temperature()*10000;
}
}
void EEPROM_Process()
{
start_times = Read_EEPROM(0x01);
start_times++;
Write_EEPROM(0x01,start_times);
}
void ADC_Process()
{
if(cnt_ADC>=10)
{
cnt_ADC = 0;
Read_ADC(0x41);
ch1 = Read_ADC(0x41);//通道1:光敏电阻值
//ch1 = ch1/51.1f+1;
ch1 = ch1*100/51;
Read_ADC(0x43);
ch3 = Read_ADC(0x43);//通道3:分压电阻值
//ch3 = ch3/51.1f+1;
ch3 = ch3*100/51;
}
}
void Read_Clock()
{
miao = BcdToDec(Read_Ds1302_Byte(0x81));
fen = BcdToDec(Read_Ds1302_Byte(0x83));
shi = BcdToDec(Read_Ds1302_Byte(0x85));
}
void main()
{
System_Init();
EEPROM_Process();
Set_Clock(23,59,55);
Timer2Init();
while(1)
{
SMG_Process();
DS18B20_Process();
ADC_Process();
Read_Clock();
}
}
void Timer2() interrupt 12
{
cnt_key++;cnt_temperature++;cnt_ADC++;
display_bit();
KEY_Process();
}