#include <STC15F2K60S2.H>
#include "Delay.h"
#include "inithc138.h"
#include "ds1302.h"
#include "onewire.h"
#include "iic.h"
#define de 5
code unsigned char Seg_Table[17] =
{
0xc0, //0
0xf9, //1
0xa4, //2
0xb0, //3
0x99, //4
0x92, //5
0x82, //6
0xf8, //7
0x80, //8
0x90, //9
0x88, //A
0x83, //b
0xc6, //C
0xa1, //d
0x86, //E
0x8e, //F
0xbf
};
bit canshu_xianshi = 0;//选择参数还是显示
unsigned char mode = 0;//数据显示模式代号
unsigned char mode2 = 0;//参数界面模式代号
unsigned char ds1302writeaddr[4] = {0x80,0x82,0x84,0x86};
unsigned char ds1302readaddr[4] = {0x81,0x83,0x85,0x87};
unsigned char time[4] = {0x01,0x20,0x20,0x01};
unsigned char voltage = 0;
unsigned int guang = 0;
unsigned int temp = 0;//温度
bit liang_an = 0;//暗为1亮为0
bit liang_an2 = 0;//3秒后暗为1亮为0
bit ce = 0;//是否进入测量状态
unsigned char xiaoshicanshu = 17;//小时参数
unsigned char xiaoshicanshucertain = 17;//确认生效
unsigned char tempcan = 25;//温度参数
unsigned char tempcancertain = 25;//确认生效
unsigned char zhishideng = 4;//指示灯参数
unsigned char tmptime = 0;
unsigned char zhishidengcertain = 4;//确认生效
bit flag = 0;//到了时间参数
unsigned char date = 100;
unsigned char ledstat = 0xff;
unsigned int count1 = 0;//暗下来数三秒
unsigned int count2 = 0;//亮了数三秒
void allselect();
void scankey();
//*****************************************定时器
void Timer0_Isr(void) interrupt 1
{
if(liang_an){
count1++;
}
if(count1 >= 60){
liang_an2 = 1;
count1 = 0;
}
if((liang_an == 0) && (liang_an2 == 0)){
count1 = 0;
}
if((liang_an == 0) && (liang_an2 == 1)){//亮了以后又暗了
count2++;
}
if((liang_an == 1) && (liang_an2 == 1)){
count2 = 0;
}
if(count2 >= 60){
liang_an2 = 0;
count2 = 0;
}
}
void Timer0_Init(void) //50毫秒@12.000MHz
{
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0xB0; //设置定时初始值
TH0 = 0x3C; //设置定时初始值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0 = 1; //使能定时器0中断
EA = 1;
}
//*****************************************
//*****************************************ds1302
void ds1302config(){
unsigned char i;
Write_Ds1302_Byte(0x8e,0x00);
for(i = 0;i < 4;i++){
Write_Ds1302_Byte(ds1302writeaddr[i],time[i]);
}
Write_Ds1302_Byte(0x8e,0x80);
}
void ds1302read(){
unsigned char i;
for(i = 0;i < 4;i++){
time[i] = Read_Ds1302_Byte(ds1302readaddr[i]);
}
tmptime = (time[2] / 16) * 10 + (time[2] % 16);
}
//*****************************************
//*****************************************温度测量
void delaysmg(unsigned char xms){
while(xms--){
allselect();
scankey();
Delay(1);
}
}
void wendu(){
unsigned char LSB,MSB;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
ce = 1;
delaysmg(100);
ce = 0;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
LSB = Read_DS18B20();
MSB = Read_DS18B20();
init_ds18b20();
temp = (MSB << 8) | LSB;
temp = temp * 0.625;
}
//*****************************************
//*****************************************数据界面
void shijianshuju(){//时间数据显示
showsmg(1,Seg_Table[time[2] / 16]);
showsmg(2,Seg_Table[time[2] % 16]);
showsmg(3,Seg_Table[16]);
showsmg(4,Seg_Table[time[1] / 16]);
showsmg(5,Seg_Table[time[1] % 16]);
showsmg(6,Seg_Table[16]);
showsmg(7,Seg_Table[time[0] / 16]);
showsmg(8,Seg_Table[time[0] % 16]);
}
void wendushuju(){//温度数据显示
showsmg(1,Seg_Table[12]);
showsmg(6,Seg_Table[temp / 100]);
showsmg(7,Seg_Table[temp / 10 % 10] & 0x7f);
showsmg(8,Seg_Table[temp % 10]);
}
void lianganpanduan(){//亮暗状态判断
voltage = guangmin();
Delay(1);
guang = voltage * 100 / 51;
if(voltage < 20){
liang_an = 1;
}else{
liang_an = 0;
}
}
void lianganzhuangtai(){//亮暗状态显示
showsmg(1,Seg_Table[14]);
showsmg(3,Seg_Table[guang / 100] & 0x7f);
showsmg(4,Seg_Table[guang / 10 % 10]);
showsmg(5,Seg_Table[guang % 10]);
showsmg(8,Seg_Table[liang_an]);
}
void showselect(){//显示模式选择
switch(mode){
case 0:shijianshuju();break;
case 1:if(ce == 0){wendu();}wendushuju();break;
case 2:lianganzhuangtai();break;
}
}
//*****************************************
//*****************************************参数界面
void shijiancanshu(){//时间参数
showsmg(1,0x8c);
showsmg(2,Seg_Table[1]);
showsmg(7,Seg_Table[xiaoshicanshu / 10]);
showsmg(8,Seg_Table[xiaoshicanshu % 10]);
}
void wenducanshu(){//温度参数
showsmg(1,0x8c);
showsmg(2,Seg_Table[2]);
showsmg(7,Seg_Table[tempcan / 10]);
showsmg(8,Seg_Table[tempcan % 10]);
}
void zhishidengcanshu(){//指示灯参数
showsmg(1,0x8c);
showsmg(2,Seg_Table[3]);
showsmg(8,Seg_Table[zhishideng]);
}
void canselect(){
switch(mode2){
case 0:shijiancanshu();break;
case 1:wenducanshu();break;
case 2:zhishidengcanshu();break;
}
}
//*****************************************
//*****************************************
void allselect(){
if(canshu_xianshi == 0){
showselect();
}else if(canshu_xianshi){
canselect();
}
}
//*****************************************
//*****************************************LED指示灯
void shijianpanduan(){//时间判断
if(tmptime >= xiaoshicanshucertain){
if(flag == 0){date = time[3];}
flag = 1;
if((date != time[3]) && (tmptime > 8)){
flag = 0;
}
}else{
flag = 0;
}
}
void led(){
shijianpanduan();
if(flag){
ledstat = ledstat & ~0x01;
}else{
ledstat = ledstat | 0x01;
}
if((temp / 10) < tempcancertain){
ledstat = ledstat & ~0x02;
}else{
ledstat = ledstat | 0x02;
}
if(liang_an2){
ledstat = ledstat & ~(0x01 << (zhishidengcertain-1));
}else{
ledstat = ledstat | (0x01 << (zhishidengcertain-1));
}
outputp0(4,ledstat);
}
//*****************************************
//*****************************************按键扫描
void scankey(){
P33 = 0;P32 = 1;P44 = 1;P35 = 1;
if(P44 == 0){Delay(de);while(P44 == 0){allselect();}//S4
canshu_xianshi = ~canshu_xianshi;
xiaoshicanshucertain = xiaoshicanshu;
tempcancertain = tempcan;
zhishidengcertain = zhishideng;
mode = 0;mode2 = 0;
}
if(P35 == 0){Delay(de);while(P35 == 0){allselect();}//S8
if(canshu_xianshi){
if(mode2 == 0){xiaoshicanshu--;if(xiaoshicanshu > 100){xiaoshicanshu = 0;}};
if(mode2 == 1){tempcan--;if(tempcan > 100){tempcan = 0;}};
if(mode2 == 2){zhishideng--;if(zhishideng < 4){zhishideng = 4;}};
}
}
P33 = 1;P32 = 0;P44 = 1;P35 = 1;
if(P44 == 0){Delay(de);while(P44 == 0){allselect();}
if(canshu_xianshi == 0){mode++;mode %= 3;}
if(canshu_xianshi == 1){mode2++;mode2 %= 3;}
}
if(P35 == 0){Delay(de);while(P35 == 0){allselect();}
if(canshu_xianshi){
if(mode2 == 0){xiaoshicanshu++;if(xiaoshicanshu > 23){xiaoshicanshu = 23;}};
if(mode2 == 1){tempcan++;if(tempcan > 99){tempcan = 99;}};
if(mode2 == 2){zhishideng++;if(zhishideng > 8){zhishideng = 8;}};
}
}
}
//*****************************************
void main(){
initsys();
ds1302config();
wendu();
ds1302read();
Timer0_Init();
while(1){
lianganpanduan();
ds1302read();
scankey();
allselect();
led();
}
}