onewire.c
/* # 单总线代码片段说明
1. 本文件夹中提供的驱动代码供参赛选手完成程序设计参考。
2. 参赛选手可以自行编写相关代码或以该代码为基础,根据所选单片机类型、运行速度和试题
中对单片机时钟频率的要求,进行代码调试和修改。
*/
//
#include <STC15F2K60S2.H>
#include "onewire.h"
sbit DQ = P1^4;
void Delay_OneWire(unsigned int t)
{
unsigned char i;
while(t--){
for(i=0;i<12;i++);
}
}
//
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;
if(DQ)
{
dat |= 0x80;
}
Delay_OneWire(5);
}
return dat;
}
//
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 Read_temp()
{
unsigned int temp;
unsigned char MSB,LSB;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
LSB = Read_DS18B20();
MSB = Read_DS18B20();
temp = MSB << 8 | LSB;
return temp * 0.0625;
}
onewire.h
#ifndef __onewire_h
#define __onewire_h
void Delay_OneWire(unsigned int t);
void Write_DS18B20(unsigned char dat);
unsigned char Read_DS18B20(void);
bit init_ds18b20(void);
unsigned int Read_temp();
#endif
iic.c
/* # I2C代码片段说明
1. 本文件夹中提供的驱动代码供参赛选手完成程序设计参考。
2. 参赛选手可以自行编写相关代码或以该代码为基础,根据所选单片机类型、运行速度和试题
中对单片机时钟频率的要求,进行代码调试和修改。
*/
#include <STC15F2K60S2.H>
#include "iic.h"
#include "intrins.h"
#define DELAY_TIME 5
sbit scl = P2^0;
sbit sda = P2^1;
//
static void I2C_Delay(unsigned char n)
{
do
{
_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();
}
while(n--);
}
//
void I2CStart(void)
{
sda = 1;
scl = 1;
I2C_Delay(DELAY_TIME);
sda = 0;
I2C_Delay(DELAY_TIME);
scl = 0;
}
//
void I2CStop(void)
{
sda = 0;
scl = 1;
I2C_Delay(DELAY_TIME);
sda = 1;
I2C_Delay(DELAY_TIME);
}
//
void I2CSendByte(unsigned char byt)
{
unsigned char i;
for(i=0; i<8; i++){
scl = 0;
I2C_Delay(DELAY_TIME);
if(byt & 0x80){
sda = 1;
}
else{
sda = 0;
}
I2C_Delay(DELAY_TIME);
scl = 1;
byt <<= 1;
I2C_Delay(DELAY_TIME);
}
scl = 0;
}
//
unsigned char I2CReceiveByte(void)
{
unsigned char da;
unsigned char i;
for(i=0;i<8;i++){
scl = 1;
I2C_Delay(DELAY_TIME);
da <<= 1;
if(sda)
da |= 0x01;
scl = 0;
I2C_Delay(DELAY_TIME);
}
return da;
}
//
unsigned char I2CWaitAck(void)
{
unsigned char ackbit;
scl = 1;
I2C_Delay(DELAY_TIME);
ackbit = sda;
scl = 0;
I2C_Delay(DELAY_TIME);
return ackbit;
}
//
void I2CSendAck(unsigned char ackbit)
{
scl = 0;
sda = ackbit;
I2C_Delay(DELAY_TIME);
scl = 1;
I2C_Delay(DELAY_TIME);
scl = 0;
sda = 1;
I2C_Delay(DELAY_TIME);
}
unsigned int Read_v()
{
unsigned int temp;
I2CStart();
I2CSendByte(0x90);
I2CWaitAck();
I2CSendByte(0x03);
I2CWaitAck();
I2CStart();
I2CSendByte(0x91);
I2CWaitAck();
temp = I2CReceiveByte();
I2CSendAck(1);
I2CStop();
return temp;
}
unsigned int Read_light()
{
unsigned int temp;
I2CStart();
I2CSendByte(0x90);
I2CWaitAck();
I2CSendByte(0x01);
I2CWaitAck();
I2CStart();
I2CSendByte(0x91);
I2CWaitAck();
temp = I2CReceiveByte();
I2CSendAck(1);
I2CStop();
return temp;
}
void Write_AT24C02_Page(unsigned char *buf,unsigned char addr,unsigned char num)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
while(num--)
{
I2CSendByte(*buf++);
I2CWaitAck();
}
I2CStop();
}
void Read_AT24C02_Page(unsigned char *buf,unsigned char addr,unsigned char num)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
I2CStart();
I2CSendByte(0xa1);
I2CWaitAck();
while(num--)
{
*buf++ = I2CReceiveByte();
if(num) I2CSendAck(0);
else I2CSendAck(1);
}
I2CStop();
}
iic.h
#ifndef __iic_h
#define __iic_h
static void I2C_Delay(unsigned char n);
void I2CStart(void);
void I2CStop(void);
void I2CSendByte(unsigned char byt);
unsigned char I2CReceiveByte(void);
unsigned char I2CWaitAck(void);
void I2CSendAck(unsigned char ackbit);
unsigned int Read_v();
unsigned int Read_light();
void Write_AT24C02_Page(unsigned char *buf,unsigned char addr,unsigned char num);
void Read_AT24C02_Page(unsigned char *buf,unsigned char addr,unsigned char num);
#endif
ds1302.c
/* # DS1302代码片段说明
1. 本文件夹中提供的驱动代码供参赛选手完成程序设计参考。
2. 参赛选手可以自行编写相关代码或以该代码为基础,根据所选单片机类型、运行速度和试题
中对单片机时钟频率的要求,进行代码调试和修改。
*/
//
#include <STC15F2K60S2.H>
#include "ds1302.h"
#include "intrins.h"
sbit SCK = P1^7;
sbit SDA = P2^3;
sbit RST = P1^3;
code unsigned char Write_addr[] = {0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
code unsigned char Read_addr[] = {0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
unsigned char time[] = {0x55,0x59,0x23};
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);
}
void Write_time()
{
char i;
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 Read_time()
{
char i;
for(i = 0;i < 3;i++)
time[i] = Read_Ds1302_Byte(Read_addr[i]);
}
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 );
void Write_time();
void Read_time();
#endif
sys.c
#include <STC15F2K60S2.H>
#include "sys.h"
void Delay_ms(unsigned int t) //@12.000MHz
{
while(t--)
{
unsigned char i, j;
i = 12;
j = 169;
do
{
while (--j);
} while (--i);
}
}
void Select_Hc573(char n)
{
switch(n)
{
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;
}
P2 = P2 & 0x1f;
}
void Sys_Init()
{
P0 = 0x00;
Select_Hc573(5);
P0 = 0xff;
Select_Hc573(4);
}
void Select_Bit(unsigned char pos,dat)
{
P0 = 0x01 << pos;
Select_Hc573(6);
P0 = dat;
Select_Hc573(7);
Delay_ms(1);
P0 = 0xff;
Select_Hc573(7);
}
sys.h
#ifndef __sys_h
#define __sys_h
void Delay_ms(unsigned int t);
void Select_Hc573(char n);
void Sys_Init();
void Select_Bit(unsigned char pos,dat);
#endif
main.c
#include <STC15F2K60S2.H>
#include "sys.h"
#include "ds1302.h"
#include "iic.h"
#include "onewire.h"
#include "stdio.h"
sbit S5 = P3^2;
sbit S4 = P3^3;
code unsigned char SMG[] = { ~0x3F,~0x06,~0x5B,~0x4F,~0x66,~0x6D,~0x7D,~0x07,~0x7F,~0x6F,~0x39,~0x76,~0x40};
extern unsigned char time[];
xdata unsigned int temp;
xdata unsigned int v,light,hum;
xdata unsigned char str[20];
xdata unsigned char rec[20];
unsigned char index,index_old;
bit flag_10ms,flag_500ms,flag_1s;
unsigned char count1,count2,count3;
bit str_judge;
bit flag_send;
bit yes;
bit mode;//0-自动传输模式 1-手动记录模式
unsigned char mode_dis;//0-温湿度 1-时钟 2-停留时间
unsigned char key_val;
unsigned char time_stop;
xdata unsigned char save[10];
xdata unsigned char rec2[10];
unsigned char Key_Scan()
{
unsigned char key_temp = 0;
static unsigned char cnt4 = 0;
static unsigned char cnt5 = 0;
if(S4 == 0) cnt4++;
if(S4 == 1)
{
if(cnt4 > 2) key_temp = 4;
cnt4 = 0;
}
if(S5 == 0) cnt5++;
if(S5 == 1)
{
if(cnt5 > 2) key_temp = 5;
cnt5 = 0;
}
return key_temp;
}
void Key_Pro()
{
if(key_val == 4)
{
mode = ~mode;
}
else if(key_val == 5)
{
if(++mode_dis > 2)
mode_dis = 0;
}
}
void Display_temp()
{
Select_Bit(0,SMG[temp / 10]);
Select_Bit(1,SMG[temp % 10]);
Select_Bit(2,SMG[10]);
Select_Bit(5,SMG[hum / 10]);
Select_Bit(6,SMG[hum % 10]);
Select_Bit(7,SMG[11]);
}
void Display_time()
{
Select_Bit(0,SMG[time[2] / 16]);
Select_Bit(1,SMG[time[2] % 16]);
Select_Bit(2,SMG[12]);
Select_Bit(3,SMG[time[1] / 16]);
Select_Bit(4,SMG[time[1] % 16]);
Select_Bit(5,SMG[12]);
Select_Bit(6,SMG[time[0] / 16]);
Select_Bit(7,SMG[time[0] % 16]);
}
void Display_time_stop()
{
Select_Bit(3,SMG[12]);
Select_Bit(4,SMG[time_stop / 1000]);
Select_Bit(5,SMG[time_stop / 100 % 10]);
Select_Bit(6,SMG[time_stop / 10 % 10]);
Select_Bit(7,SMG[time_stop % 10]);
}
void UartInit(void) //115200bps@11.0592MHz
{
SCON = 0x50; //8位数据,可变波特率
AUXR &= 0xBF; //定时器1时钟为Fosc/12,即12T
AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
TMOD &= 0x0F; //设定定时器1为16位自动重装方式
TL1 = 0xFE; //设定定时初值
TH1 = 0xFF; //设定定时初值
ET1 = 0; //禁止定时器1中断
TR1 = 1; //启动定时器1
ES = 1;
}
void Send_byte(unsigned char dat)
{
SBUF = dat;
while(TI == 0);
TI = 0;
}
void Send_string(unsigned char *dat)
{
while(*dat != '\0')
Send_byte(*dat++);
}
void Uart_isr() interrupt 4
{
if(RI)
{
rec[index++] = SBUF;
RI = 0;
}
}
void Uart_Pro()
{
if(index > 0)
{
if(flag_500ms)
{
if(index != index_old)
{
index_old = index;
str_judge = 0;
}
else
{
str_judge = 1;
}
flag_500ms = 0;
}
}
if(str_judge)
{
if(!mode)
{
if(index == 6)
{
if(rec[0] == 'A' && rec[1] == 'A' && rec[2] == 'A' && rec[3] == 'S' && rec[4] == 'S' && rec[5] == 'S')
flag_send = 1;
else flag_send = 0;
}
else flag_send = 0;
}
else
{
if(index == 6)
{
if(rec[0] == 'A' && rec[1] == 'A' && rec[2] == 'A' && rec[3] == 'S' && rec[4] == 'S' && rec[5] == 'S')
{
sprintf(str,"12345\r\n");
Send_string(str);
}
}
}
index = 0;
str_judge = 0;
}
}
void Timer0Init(void) //10毫秒@12.000MHz
{
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0xF0; //设置定时初值
TH0 = 0xD8; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0 = 1;
}
void Timer0_isr() interrupt 1
{
flag_10ms = 1;
if(++count1 > 50)
{
count1 = 0;
flag_500ms = 1;
}
if(mode) flag_send = 0;
if(++count2 > 100)
{
count2 = 0;
if(flag_send)
{
sprintf(str,"{%d-%d}{%d-%d-%d}{%d}\r\n",temp,hum,(int)(time[2] / 16 * 10+ time[2] % 16),(int)(time[1] / 16 * 10 + time[1] % 16),(int)(time[0] / 16 * 10 + time[0] % 16),(int)yes);
Send_string(str);
}
}
if(mode && light < 160)
{
if(++count3 > 100)
{
count3 = 0;
time_stop++;
}
}
}
void Led(unsigned char addr,enable)
{
static unsigned char temp = 0x00;
static unsigned char temp_old = 0xff;
if(enable) temp |= 0x01 << addr;
else temp &= ~(0x01 << addr);
if(temp != temp_old)
{
P0 = ~temp;
Select_Hc573(4);
temp_old = temp;
}
}
void Led_Pro()
{
if(!mode) Led(0,1);
else Led(0,0);
if(yes) Led(2,1);
else Led(2,0);
}
void main()
{
Sys_Init();
Timer0Init();
Read_temp();
Delay_ms(750);
Write_time();
UartInit();
EA = 1;
while(1)
{
Read_time();
temp = Read_temp();
hum = Read_light() / 51.0 * 20;
if(hum > 99) hum = 99;
light = Read_v();
if(light < 160) yes = 1;
else yes = 0;
if(light < 160)
if(light > 160)
{
save[0] = temp;save[1] = hum;
save[2] = time[0];save[3] = time[1];save[4] = time[2];
save[5] = time_stop;
Write_AT24C02_Page(save,0x00,6);
}
key_val = Key_Scan();
Key_Pro();
Uart_Pro();
Led_Pro();
switch(mode_dis)
{
case 0:Display_temp();break;
case 1:Display_time();break;
case 2:Display_time_stop();break;
}
}
}