ds1302.h
#ifndef __DS1302_H
#define __DS1302_H
#include <STC15F2K60S2.H>
#include <intrins.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 );
extern unsigned char ds1302_buf[];
void write_ds1302();
void read_ds1302();
#endif
ds1302.c
#include "ds1302.h"
sbit SCK = P1^7;
sbit SDA = P2^3;
sbit RST = P1^3;
//写字节
void Write_Ds1302(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
SCK = 0;
SDA = temp&0x01;
temp>>=1;
SCK=1;
}
}
//向DS1302寄存器写入数据
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;
}
//从DS1302寄存器读出数据
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);
}
unsigned char ds1302_buf[] = { 0x23, 0x59, 0x55, };
void write_ds1302()
{
Write_Ds1302_Byte(0x84, ds1302_buf[0]);
Write_Ds1302_Byte(0x82, ds1302_buf[1]);
Write_Ds1302_Byte(0x80, ds1302_buf[2]);
}
void read_ds1302()
{
ds1302_buf[0] = Read_Ds1302_Byte(0x85);
ds1302_buf[1] = Read_Ds1302_Byte(0x83);
ds1302_buf[2] = Read_Ds1302_Byte(0x81);
}
iic.h
#ifndef _IIC_H
#define _IIC_H
#include <STC15F2K60S2.H>
#include "intrins.h"
void IIC_Start(void);
void IIC_Stop(void);
bit IIC_WaitAck(void);
void IIC_SendAck(bit ackbit);
void IIC_SendByte(unsigned char byt);
unsigned char IIC_RecByte(void);
void write_at24c02(unsigned char addr, unsigned char Data);
unsigned char read_at24c02(unsigned char addr);
unsigned char read_pcf8591(unsigned char channle);
#endif
iic.c
#include "iic.h"
sbit SDA = P2^1;
sbit SCL = P2^0;
#define DELAY_TIME 5
//I2C总线内部延时函数
void IIC_Delay(unsigned char i)
{
do{_nop_();}
while(i--);
}
//I2C总线启动信号
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 0;
}
//I2C总线停止信号
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 1;
IIC_Delay(DELAY_TIME);
}
//发送应答或非应答信号
void IIC_SendAck(bit ackbit)
{
SCL = 0;
SDA = ackbit; //0应答 1非应答
IIC_Delay(DELAY_TIME);
SCL = 1;
IIC_Delay(DELAY_TIME);
SCL = 0;
SDA = 1;
IIC_Delay(DELAY_TIME);
}
//等待应答
bit IIC_WaitAck(void)
{
bit ackbit;
SCL = 1;
IIC_Delay(DELAY_TIME);
ackbit = SDA;
SCL = 0;
IIC_Delay(DELAY_TIME);
return ackbit;
}
//I2C总线发送一个字节数据
void IIC_SendByte(unsigned char byt)
{
unsigned char i;
for(i=0; i<8; i++)
{
SCL = 0;
IIC_Delay(DELAY_TIME);
if(byt & 0x80) SDA = 1;
else SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 1;
byt <<= 1;
IIC_Delay(DELAY_TIME);
}
SCL = 0;
}
//I2C总线接收一个字节数据
unsigned char IIC_RecByte(void)
{
unsigned char i, da;
for(i=0; i<8; i++)
{
SCL = 1;
IIC_Delay(DELAY_TIME);
da <<= 1;
if(SDA) da |= 1;
SCL = 0;
IIC_Delay(DELAY_TIME);
}
return da;
}
void write_at24c02(unsigned char addr, unsigned char Data)
{
IIC_Start();
IIC_SendByte(0xA0);
IIC_WaitAck();
IIC_SendByte(addr);
IIC_WaitAck();
IIC_SendByte(Data);
IIC_WaitAck();
IIC_Stop();
}
unsigned char read_at24c02(unsigned char addr)
{
unsigned char buf;
IIC_Start();
IIC_SendByte(0xA0);
IIC_WaitAck();
IIC_SendByte(addr);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0xA1);
IIC_WaitAck();
buf = IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
return buf;
}
unsigned char read_pcf8591(unsigned char channle)
{
unsigned char buf;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(channle);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
IIC_RecByte();
IIC_SendAck(0); //应答
buf = IIC_RecByte();
IIC_SendAck(1); //非应答
IIC_Stop();
return buf;
}
onewire.h
#ifndef __ONEWIRE_H
#define __ONEWIRE_H
#include <STC15F2K60S2.H>
unsigned int rd_temperature(void);
#endif
onewire.c
#include "onewire.h"
sbit DQ = P1^4;
//单总线内部延时函数
void Delay_OneWire(unsigned int t)
{
t *= 12;
while(t--);
}
//单总线写操作
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);
}
//单总线读操作
unsigned char Read_DS18B20(void)
{
unsigned char i;
unsigned char dat;
for(i=0;i<8;i++)
{
DQ = 0;
dat >>= 1;
DQ = 1;
Delay_OneWire(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;
}
unsigned int rd_temperature(void)
{
unsigned char MSB, LSB;
unsigned int temp;
init_ds18b20();
Write_DS18B20(0xCC);
Write_DS18B20(0x44);
// Delay_OneWire(200);
init_ds18b20();
Write_DS18B20(0xCC);
Write_DS18B20(0xBE);
LSB = Read_DS18B20();
MSB = Read_DS18B20();
temp = (unsigned int)MSB << 8 | LSB;
return (float)temp * 6.25;
}
main.c
#include <STC15F2K60S2.H>
#include "hardware.h"
#include "ds1302.h"
#include "iic.h"
#include "onewire.h"
void main()
{
Timer1Init(); //基本(led dig timer)
write_ds1302(); //时钟初值
Timer0Init(); //ne555频率测量
while (1)
{
if (timer10ms >= 10)
{
timer10ms = 0;
/* input */
key_scan_v2();
/* process */
/* output */
}
}
}
hardware.c
#include "hardware.h"
unsigned char led_buf = 0x00;
void led_output()
{
P0 = ~led_buf;
P2 |= 0x80;
P2 &= ~0xE0;
}
/************* 本地常量声明 **************/
unsigned char code t_display[]={ //标准字库
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,
//black - H J K L N o P U t G Q r M y
0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e,
0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46}; //0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -1
unsigned char code T_COM[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; //位码
unsigned char dig_buf[][8] =
{
{ 16, 16, 16, 16, 16, 16, 16, 16, }, //test
{ 16, 16, 17, 16, 16, 17, 16, 16, }, //时间
{ 16, 16, 16, 16, 16, 16, 16, 16, }, //e2prom
{ 16, 16, 16, 16, 16, 16, 16, 0x0C, }, //temperature
{ 0x0F, 16, 16, 16, 16, 16, 16, 16, }, //ne555_freq
};
unsigned char *digs = dig_buf;
void dig_output()
{
static unsigned char dig_com = 0;
P0 = 0x00; //消影
P2 |= 0xC0;
P2 &= ~0xE0;
P0 = ~t_display[digs[dig_com]]; //段选
P2 |= 0xE0;
P2 &= ~0xE0;
P0 = T_COM[dig_com]; //位选
P2 |= 0xC0;
P2 &= ~0xE0;
if (++dig_com >= 8)
{
dig_com = 0;
}
}
void Timer1Init(void) //1毫秒@12.000MHz
{
AUXR |= 0x40; //定时器时钟1T模式
TMOD &= 0x0F; //设置定时器模式
TL1 = 0x20; //设置定时初值
TH1 = 0xD1; //设置定时初值
TF1 = 0; //清除TF1标志
TR1 = 1; //定时器1开始计时
ET1 = 1;
EA = 1;
}
unsigned char timer10ms = 0;
unsigned char timer100ms = 0;
unsigned int timer1s = 0;
unsigned int timer_ne555 = 0;
unsigned int ne555_freq = 0;
//unsigned char count = 0;
//unsigned char duty = 0;
//unsigned char T = 10;
void Timer1Isr() interrupt 3
{
unsigned int ne555_temp;
// if (++count > T)
// {
// count = 0;
// }
// if (count <= duty)
// {
led_output();
// }
// else
// {
// P0 = 0xFF;
// P2 |= 0x80;
// P2 &= ~0xE0;
// }
dig_output();
timer10ms++;
timer100ms++;
timer1s++;
if (++timer_ne555 >= 100) //ne555
{
timer_ne555 = 0;
TR0 = 0;
ne555_temp = TL0 | ((unsigned int)TH0 << 8);
TL0 = 0x00; //设置定时初值
TH0 = 0x00; //设置定时初值
TR0 = 1;
ne555_freq = ne555_temp * 10;
}
}
unsigned char key_value = 0; //按键键值
unsigned char key_state = 0; //按键状态
/* 每大概10ms调用一次 */
void key_scan_v2()
{
P3 |= 0x0F;
if (~P3 & 0x0F) // 如果有按键按下
{
/* 按键状态更新 */
if (key_state < 255)
key_state++;
/* 按键键值判断 */
if (P33 == 0)
key_value = 4;
if (P32 == 0)
key_value = 5;
if (P31 == 0)
key_value = 6;
if (P30 == 0)
key_value = 7;
}
else //如果没有按键按下
{
key_value = 0;
key_state = 0;
}
}
void Timer0Init(void) //1毫秒@12.000MHz
{
TMOD &= 0xF0; //设置定时器模式
TMOD |= 0x04; //计数模式
TL0 = 0x00; //设置定时初值
TH0 = 0x00; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
}
hareware.h
#ifndef __HARDWARE_H__
#define __HARDWARE_H__
#include <STC15F2K60S2.H>
extern unsigned char led_buf;
extern unsigned char dig_buf[][8];
extern unsigned char *digs;
extern unsigned int timer1s;
extern unsigned char timer10ms;
extern unsigned char timer100ms;
extern unsigned int ne555_freq;
extern unsigned char key_value; //按键键值
extern unsigned char key_state; //按键状态
//extern unsigned char duty;
void Timer0Init(void);
void Timer1Init(void);
void key_scan_v2();
#endif