试题
代码
sys.h
#ifndef __SYS_H__
#define __SYS_H__
#include <STC15F2K60S2.H>
//onewire.c
float getT();
//sys.c
extern unsigned char UI;
extern bit touch_mode;
extern float jiaozhun;
extern float canshu;
extern float temper;
void init74hc138(unsigned char n);
void init();
void get_temper();
void led_ui_touch_c();
//seg_key.c
extern unsigned char Seg_Buff[8];
void Seg_Loop();
void Key_Loop();
void seg_ui();
#endif
main.c
#include "sys.h"
bit flag_seg=0;
bit flag_key=0;
bit flag_temper=0;
void Timer0_Init(void) //100微秒@12.000MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x50; //设置定时初始值
TH0 = 0xFB; //设置定时初始值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0 = 1; //使能定时器0中断
EA = 1;
}
void main(){
init();
Timer0_Init();
while(getT()==85.0);
while(1){
led_ui_touch_c();
if(flag_seg){
flag_seg=0;
Seg_Loop();
}
if(flag_key){
flag_key=0;
Key_Loop();
seg_ui();
}
if(flag_temper){
flag_temper=0;
get_temper();
seg_ui();
}
}
}
void Timer0_Isr(void) interrupt 1
{
static unsigned char count1=0;
static unsigned char count2=0;
static unsigned int count3=0;
count1++;count2++;count3++;
if(count1==2){
count1=0;
flag_seg=1;
}
if(count2==50){
count2=0;
flag_key=1;
}
if(count3==1000){
count3=0;
flag_temper=1;
}
}
sys.c
#include "sys.h"
unsigned char UI=0;//0为温度界面,1为校准值界面,2为参数界面
bit touch_mode=0;//0为上触发模式,1为下触发模式
float jiaozhun=0.0;//校准值
float canshu=26.0;//参数
float temper;
void init74hc138(unsigned char n){
P2=(P2&0x1f)|(n<<5);
P2&=0x1f;
}
void init(){
P0=0x00;
init74hc138(5);
P0=0xff;
init74hc138(4);
}
void get_temper(){
temper=getT()+jiaozhun;
}
void led(unsigned char addr){
P0=~(0x01<<(addr-1));
init74hc138(4);
}
void led_ui_touch_c(){
switch(UI){
case 0:
led(1);break;
case 1:
led(2);break;
case 2:
led(3);break;
}
if(!touch_mode){
led(4);
if(temper>canshu) led(8);
}else{
led(5);
if(temper<canshu) led(8);
}
}
seg_key.c
#include "sys.h"
sbit COL1=P4^4;
sbit COL2=P4^2;
sbit ROW3=P3^2;
sbit ROW4=P3^3;
code unsigned char Seg_Table[] =
{
0xc0, //0
0xf9, //1
0xa4, //2
0xb0, //3
0x99, //4
0x92, //5
0x82, //6
0xf8, //7
0x80, //8
0x90, //9
0xc6, //C 10
0x86, //E 11
0x89, //H 12
0xbf, //- 13
0xff //熄灭 14
};
unsigned char Seg_Buff[8]={14,14,14,14,14,14,14,14};
unsigned char keyval,keyold,keyup,keydown;
void seg(unsigned char addr,unsigned char num){
P0=0xff;
init74hc138(7);
P0=0x01<<addr;
init74hc138(6);
P0=Seg_Table[num];
if(UI==0&&addr==6) P0&=0x7f;
init74hc138(7);
}
void Seg_Loop(){
static unsigned char i=0;
seg(i,Seg_Buff[i]);
i++;
if(i==8) i=0;
}
unsigned char key_scan(){
COL1=0;COL2=1;
if(ROW4==0) return 4;
if(ROW3==0) return 5;
COL1=1;COL2=0;
if(ROW4==0) return 8;
if(ROW3==0) return 9;
return 0;
}
void Key_Loop(){
keyval=key_scan();
keydown=keyval&(keyold^keyval);
keyup=~keyval&(keyold^keyval);
//s4负责切换界面
if(keyval==4&&keyold!=4){
UI++;
if(UI==3) UI=0;
}
//s5负责切换触发模式
if(keyval==5&&keyold!=5){
touch_mode=~touch_mode;
}
//s8负责减
if(keyval==8&&keyold!=8){
if(UI==1){
if(jiaozhun>-99) jiaozhun--;
}
if(UI==2){
if(canshu>-99) canshu--;
}
}
//s9负责加
if(keyval==9&&keyold!=9){
if(UI==1){
if(jiaozhun<99) jiaozhun++;
}
if(UI==2){
if(canshu<99) canshu++;
}
}
keyold=keyval;
keyval=0;
}
void ui0(){//温度界面
Seg_Buff[7]=(unsigned char)((temper+0.05)*10)%10;
Seg_Buff[6]=(unsigned char)(temper+0.05)%10;
Seg_Buff[5]=(unsigned char)temper/10;
Seg_Buff[4]=14;
Seg_Buff[3]=14;
Seg_Buff[2]=14;
Seg_Buff[1]=14;
Seg_Buff[0]=10; //C
}
void ui1(){//校准值界面
float temp=jiaozhun;
if(jiaozhun<0) temp=-jiaozhun;
Seg_Buff[7]=(unsigned char)temp%10;
if((unsigned char)temp>=10){
Seg_Buff[6]=(unsigned char)temp/10;
Seg_Buff[5]=14;
if(jiaozhun<0)
Seg_Buff[5]=13;
}else{
Seg_Buff[6]=14;
Seg_Buff[5]=14;
if(jiaozhun<0)
Seg_Buff[6]=13;
}
Seg_Buff[4]=14;
Seg_Buff[3]=14;
Seg_Buff[2]=14;
Seg_Buff[1]=14;
Seg_Buff[0]=11; //E
}
void ui2(){//参数界面
float temp=canshu;
if(canshu<0) temp=-canshu;
Seg_Buff[7]=(unsigned char)temp%10;
if((unsigned char)temp>=10){
Seg_Buff[6]=(unsigned char)temp/10;
Seg_Buff[5]=14;
if(canshu<0)
Seg_Buff[5]=13;
}else{
Seg_Buff[6]=14;
Seg_Buff[5]=14;
if(canshu<0)
Seg_Buff[6]=13;
}
Seg_Buff[4]=14;
Seg_Buff[3]=14;
Seg_Buff[2]=14;
Seg_Buff[1]=14;
Seg_Buff[0]=12; //H
}
void seg_ui(){
switch(UI){
case 0:
ui0();break;
case 1:
ui1();break;
case 2:
ui2();break;
}
}
onewire.c
#include "sys.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;
}
float getT(){
unsigned char tH,tL;
float temp;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
tL=Read_DS18B20();
tH=Read_DS18B20();
temp=(float)(tH<<8|tL)*0.0625;
return temp;
}