题目
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/196bf976376e28310687c4c04bd6b9a5.jpeg#pic_center)
main.c
#include<stc15f2k60s2.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define s0 0
#define s1 1
#define duty1 2
#define duty2 3
#define duty3 7
uchar TemperGet();
uchar semg[13] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90,
0xbf, 0xc6, 0xff};
uchar semg_temp[8] = {12, 12, 12, 12, 12, 12, 12, 12};
uchar semg_bit[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
int timeout = 60;
int duty = 2;
uchar state = s0;
uchar mode = 1;
void Timer0Init(void)
{
AUXR |= 0x80;
TMOD &= 0xF0;
TL0 = 0xAE;
TH0 = 0xFB;
TF0 = 0;
TR0 = 1;
EA = 1;
ET0 = 1;
}
void pwm()
{
static int time_duty = 0, time_temp = 0;
time_duty++;
if(time_duty == 10)
time_duty = 0;
if(timeout)
if(time_duty < duty)
P34 = 1;
else
P34= 0;
else
P34 = 0;
if(timeout)
time_temp++;
if(time_temp == 10000)
{
timeout--;
time_temp = 0;
}
if(timeout == 0)
duty = 0;
else
timeout = timeout;
}
void display()
{
uchar i;
P2 = 0xe0; P0 = semg[semg_temp[i]];
P2 = 0xc0; P0 = semg_bit[i];
i++;
if(i == 8)
i = 0;
P2 = 0x00; P0 = 0Xff;
}
void led()
{
if(timeout == 0)
P0 = 0xff;
else
{
if(mode == 1)
P00 = 0;
else if(mode == 2)
P01 = 0;
else if(mode == 3)
P02 = 0;
else ;
P0 = P0 | 0x78;
}
P2 = 0X80;
P2 = 0x00; P0 = 0Xff;
}
void fun1() interrupt 1
{
uchar i;
i++;
pwm();
if(i == 20)
{
i = 0;
led();
display();
}
}
void menu(char keyvalue, uchar tempr)
{
if(mode == 1)
duty = duty1;
else if(mode == 2)
duty = duty2;
else if(mode == 3)
duty = duty3;
if(keyvalue == 4)
{
mode++;
if(mode > 3)
mode = 1;
}
else if(keyvalue == 5)
{
timeout = timeout + 60;
if(timeout > 60 && timeout < 120)
timeout = 120;
if(timeout > 120)
timeout = 0;
}
else if(keyvalue == 6)
{
timeout = 0;
duty = 0;
}
switch(state)
{
case s0:
{
semg_temp[0] = 10;
semg_temp[1] = mode;
semg_temp[2] = 10;
semg_temp[3] = 12;
semg_temp[4] = 0;
semg_temp[5] = timeout / 100;
semg_temp[6] = (timeout - semg_temp[5] * 100) / 10;
semg_temp[7] = timeout % 10;
if(keyvalue == 7)
state = s1;
} break;
case s1:
{
semg_temp[0] = 10;
semg_temp[1] = 4;
semg_temp[2] = 10;
semg_temp[3] = 12;
semg_temp[4] = 12;
semg_temp[5] = tempr / 10;
semg_temp[6] = tempr % 10;
semg_temp[7] = 11;
if(keyvalue == 7)
state = s0;
} break;
default: state = s0; break;
}
}
char keyscan()
{
uchar keybefore, keycode, trg;
static char keyvalue = -1;
keycode = (P3 ^ 0xff) & 0x0f;
trg = keycode & (keycode ^ keybefore);
keybefore = keycode;
if(trg == 0x01) keyvalue = 7;
else if(trg == 0x02) keyvalue = 6;
else if(trg == 0x04) keyvalue = 5;
else if(trg == 0x08) keyvalue = 4;
else keyvalue = -1;
return keyvalue;
}
void Delay7ms()
{
unsigned char i, j;
_nop_();
_nop_();
i = 76;
j = 74;
do
{
while (--j);
} while (--i);
}
void allinit()
{
P2 = 0x80; P0 = 0xff;
P2 = 0xa0; P0 = 0x00;
P2 = 0xc0; P0 = 0xff;
P2 = 0xe0; P0 = 0xff;
}
void main()
{
char keyvalue, tempr;
allinit();
Timer0Init( );
while(1)
{
keyvalue = keyscan();
tempr = TemperGet();
menu(keyvalue, tempr);
Delay7ms();
}
}
ds18b20.c
#include<stc15f2k60s2.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
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;
}
uchar TemperGet()
{
uchar low,high;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay_OneWire(500);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
low=Read_DS18B20();
high=Read_DS18B20();
return (high<<4) | (low>>4);
}