题目
程序
timer.c
#include <timer.h>
void Timer0Init(void) //1毫秒@11.0592MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0xCD; //设置定时初值
TH0 = 0xD4; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
EA=ET0=1;
}
timer.h
#ifndef _TIMER_H_
#define _TIMER_H_
#include <STC15F2K60S2.H>
void Timer0Init(void);
#endif
smg.c
#include <smg.h>
unsigned char code smgduan[13]={
0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,
0XBF,0XFF,0XC6};
//10:- 11:灭 12:C
unsigned char dspbuf[8];
void smg_display(void)
{
static unsigned char i=0;
P2=(P2&0X1F)|0XE0;
P0=0XFF;
P2&=0X1F;
P2=(P2&0X1F)|0XC0;
P0=1<<i;
P2&=0X1F;
P2=(P2&0X1F)|0XE0;
P0=smgduan[dspbuf[i]];
P2&=0X1F;
if(++i==8)
i=0;
}
smg.h
#ifndef _SMG_H_
#define _SMG_H_
#include <STC15F2K60S2.H>
extern unsigned char dspbuf[8];
void smg_display(void);
#endif
key.c
#include <key.h>
unsigned char key_sum=0,k4_press,k5_press,k6_press,k7_press;
void key_read(void)
{
static unsigned char k4_time=0,k5_time=0,k6_time=0,k7_time=0;
key_sum=0;
if(k4==0)
{
k4_time++;
if(k4_time==1)
key_sum=4;
else if(k4_time==20)
{
key_sum=0;
k4_time=1;
}
}
else
k4_time=0;
if(k5==0)
{
k5_time++;
if(k5_time==1)
key_sum=5;
else if(k5_time==20)
{
key_sum=0;
k5_time=1;
}
}
else
k5_time=0;
if(k6==0)
{
k6_time++;
if(k6_time==1)
key_sum=6;
else if(k6_time==20)
{
key_sum=0;
k6_time=1;
}
}
else
k6_time=0;
if(k7==0)
{
k7_time++;
if(k7_time==1)
key_sum=7;
else if(k7_time==20)
{
key_sum=0;
k7_time=1;
}
}
else
k7_time=0;
}
void key_control(void)
{
if(key_sum==4) k4_press=1;
else if(key_sum==5) k5_press=1;
else if(key_sum==6) k6_press=1;
else if(key_sum==7) k7_press=1;
}
key.h
#ifndef _KEY_H_
#define _KEY_H_
#include <STC15F2K60S2.H>
sbit k4=P3^3;
sbit k5=P3^2;
sbit k6=P3^1;
sbit k7=P3^0;
extern unsigned char k4_press;
extern unsigned char k5_press;
extern unsigned char k6_press;
extern unsigned char k7_press;
void key_read(void);
void key_control(void);
#endif
ds18b20.c
#include <ds18b20.h>
#include <intrins.h>
sbit DQ = P1^4; //单总线接口
//单总线延时函数
void Delay_OneWire(unsigned int t) //STC89C52RC
{
t*=12;
while(t--);
}
void Delay1ms() //@11.0592MHz
{
unsigned char i, j;
_nop_();
_nop_();
_nop_();
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
}
//通过单总线向DS18B20写一个字节
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);
}
//从DS18B20读取一个字节
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;
}
//DS18B20设备初始化
bit init_ds18b20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(12);
DQ = 0;
Delay_OneWire