//复习的都没考,感觉没有区分度,靠选择题拿分,总体来说比较简单,跟省赛差不多,结果是国二
#include "tim.h"
#include "seg.h"
#include "stdio.h"
#include "key.h"
#include "STC15F2K60S2.h"
#include "i2c.h"
#include "ds1302.h"
#include "onewire.h"
unsigned char ucSec, ucLed;
unsigned char ucKey_Dly, ucKey_Old;
unsigned char pucSeg_Buf[9], pucSeg_Code[8], ucSeg_Pos;//pucseg_buf储存阿拉伯数字,code储存16进制
unsigned long ulms,ulKey_Time;
unsigned int ulms1=0;
unsigned long ulms2=0;//判断进入亮暗程度时间
unsigned int ulms3=0;//每250ms读取时钟
unsigned char pucRtc[3] = {16, 59, 50};//设置时间及读取时间
unsigned int temp=0;//读取的温度
unsigned int liangan=0;//Pcf8591_adc读取数据
unsigned char pucRtc_judge=17;//时间参数
unsigned int led_judge=4;//指示灯参数
unsigned int temp_judge=25;//温度参数
unsigned char mode=0;//mode0用来判断时间、温度、亮暗界面
unsigned char mode1=0;//mode1用来判断是否为参数设置界面
unsigned char mode2=0;//判断是否3s结束
unsigned char mode3=0;//mode3用来判断是否为遮挡状态,分压0.5V下判断为暗数
unsigned char led1=0;//决定led1是否亮灭
unsigned char led2=0;//决定led2是否亮灭
unsigned char led3=0;//决定led3是否亮灭
void Key_Proc(void);//按键检测函数
void show();//数码管显示函数
void leddo()//led工作
{
unsigned char ledshow=0;//传递led参数
led1=0;
led2=0;
if(pucRtc_judge<=8)//时间应在两者之间
{
if(pucRtc[0]>=pucRtc_judge&&pucRtc[0]<=8)
{
led1=1;
}
}
else if(pucRtc_judge>8)
{
if(pucRtc[0]>=pucRtc_judge&&pucRtc[0]<=32)//第二天8点
{
led1=1;
}
}
if(temp<temp_judge*16)
{
led2=1;
}
if(mode3==1)
{
ledshow=led1|(led2<<1)|(led3<<2)|(1<<led_judge-1);
}
else
{
ledshow=led1|(led2<<1)|(led3<<2);
}
Led_Disp(ledshow);
}
void show()
{
int i=0;
for(;i<10;i++)//将数据清空,防止存储错误
{
pucSeg_Buf[i]=' ';
}
leddo();
if(mode1==0)//数据界面
{
if(mode==0)//时钟显示界面
{
sprintf(pucSeg_Buf, "%2u-%2u-%2u", (unsigned int)pucRtc[0],(unsigned int)pucRtc[1],(unsigned int)pucRtc[2]);
}
if(mode==1)//温度显示界面
{
pucSeg_Buf[0]='C';
sprintf(pucSeg_Buf+4, " %03.1f", (int)temp/16.0);//打入字符串,以字符形式储存
}
if(mode==2)//adc光亮暗程度显示界面,分压0.5V下判断为暗
{
pucSeg_Buf[0]='E';
sprintf(pucSeg_Buf+2, "%03.2f %u", (float)(liangan*5.0/256.0),(unsigned int)mode3);//打入字符串,以字符形式储存
}
}
else if(mode1==1)//参数设置界面
{
if(mode==0)//时钟参数界面
{
sprintf(pucSeg_Buf, "P%u %2u",(unsigned int)mode+1,(unsigned int)pucRtc_judge);
}
if(mode==1)//温度参数界面
{
sprintf(pucSeg_Buf, "P%u %2u",(unsigned int)mode+1,(unsigned int)temp_judge);
}
if(mode==2)//指示灯参数界面
{
sprintf(pucSeg_Buf, "P%u %u",(unsigned int)mode+1,(unsigned int)led_judge);
}
}
Seg_Tran(pucSeg_Buf, pucSeg_Code);//将阿拉伯数字转换成16进制
}
void key_do(unsigned char key_value)
{
if(key_value==4&&mode1==0)//进入参数设置界面
{
mode=0;
mode1=1;
}
else if(key_value==4&&mode1==1)//进入显示界面
{
mode=0;
mode1=0;
}
else if(key_value==5)//切换
{
mode+=1;
if(mode>2)
{
mode=0;
}
}
else if(key_value==8&&mode1==1)//参数减
{
if(mode==0)//时间参数
{
if(pucRtc_judge==0)//减到23点
{
pucRtc_judge=23;
}
else
{
pucRtc_judge-=1;
}
}
else if(mode==1)//温度参数
{
if(temp_judge!=0)//最小为01
{
temp_judge-=1;
}
}
else if(mode==2)//LED参数
{
if(led_judge!=4)//最小为01
{
led_judge-=1;
}
}
}
else if(key_value==9&&mode1==1)//参数加
{
if(mode==0)//时间参数
{
if(pucRtc_judge==23)//加到0点
{
pucRtc_judge=0;
}
else
{
pucRtc_judge+=1;
}
}
else if(mode==1)//温度参数
{
if(temp_judge<99)//最大为99
{
temp_judge+=1;
}
else
{
temp_judge=99;
}
}
else if(mode==2)//LED参数
{
if(led_judge!=8)//最小为01
{
led_judge+=1;
}
}
}
}
void main(void)
{
Cls_Peripheral();
Timer1Init();
sprintf(pucSeg_Buf, "%2u-%2u-%2u", (unsigned int)pucRtc[0],(unsigned int)pucRtc[1],(unsigned int)pucRtc[2]);
Seg_Tran(pucSeg_Buf, pucSeg_Code);//将阿拉伯数字转换成16进制
TR1=1;
Set_RTC(pucRtc);
while(1)
{
if(ulms==150)
{
show();//数码管显示
}
Key_Proc();//按键检测
if(ulms1==150)
{
temp=DS18B20_Read();//读取温度
}
if(ulms1==50)
{
liangan=Pcf8591_adc();//读取亮暗程度
if(liangan<26)
{
if(mode3==0)
{
mode2=1;//计时标志位
ulms2=0;
}
mode3=1;
}
else
{
if(mode3==1)
{
mode2=1;//计时标志位
ulms2=0;
}
mode3=0;
}
}
}
}
void Time_1(void) interrupt 3
{
if(++ucKey_Dly == 10)
ucKey_Dly = 0;
ulms++;
ulms1++;
ulms3++;
if(ulms3==150)
{
Read_RTC(pucRtc);//读取时钟
}
if(ulms3>300)
{
ulms3=0;
}
if(mode2==1&&ulms2<1801)
{
ulms2++;
}
if(ulms2>1900)
{
ulms2=0;
mode2=0;
if(mode3==0)
{
led3=0;
}
else
{
led3=1;
}
}
if(ulms1>201)
{
ulms1=0;
}
if(ulms==200)
{
ulms=0;
}
Seg_Disp(pucSeg_Code, ucSeg_Pos);
if(++ucSeg_Pos == 8) ucSeg_Pos = 0;
}
void Key_Proc(void)
{
unsigned char ucKey_Val, ucKey_Down;
if(ucKey_Dly) return;
ucKey_Dly = 1;
ucKey_Val = Key_Read();
ucKey_Down = ucKey_Val & (ucKey_Old ^ ucKey_Val);//^按位异或
ucKey_Old = ucKey_Val;
if(ucKey_Down)//判断old是否和新的一样,一样说明是同一个,不会进入
{
ulKey_Time = ulms;
key_do(ucKey_Down);
}
}