#include "reg52.h"
#include "iic.h"
#include "onewire.h"
#define smgTime 2
#define _C_ 0xc6
#define _P_ 0x8c
#define _A_ 0x88
sfr P4=0xc0;
sfr AUXR=0x8e;
sbit P32=P3^2;
sbit P33=P3^3;
sbit P42=P4^2;
sbit P44=P4^4;
unsigned int temp16=0;
double temp_dat=0.0;
unsigned int temp_smg;
unsigned char temp_para=25;
unsigned char da_dat=3.25;
unsigned char seg_code[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned int volt_dat;
unsigned char ui_mod=1;
unsigned char mod=1;
unsigned char counter=0;
unsigned char led_stat=0xff;
void display_ctrl();
void led_ctrl();
void Delay1ms() //@12.000MHz
{
unsigned char i, j;
i = 12;
j = 169;
do
{
while (--j);
} while (--i);
}
void delay_ms(unsigned char t){
while(t--){
Delay1ms();
}
}
void delay_with_scan(unsigned char t){
while(t--){
display_ctrl();
}
}
void enable_latch(unsigned char n){
switch(n){
case 4:
P2&=0x1f;
P2|=0x80;
break;
case 5:
P2&=0x1f;
P2|=0xa0;
break;
case 6:
P2&=0x1f;
P2|=0xc0;
break;
case 7:
P2&=0x1f;
P2|=0xe0;
break;
case 0:
P2&=0x1f;
//P2|=0x80;
break;
}
}
void write_latch(unsigned char n,unsigned char val){
P0=val;
switch(n){
case 4:
enable_latch(4);
break;
case 5:
enable_latch(5);
break;
case 6:
enable_latch(6);
break;
case 7:
enable_latch(7);
break;
}
enable_latch(0);
}
void read_temp(){
unsigned char TH;
unsigned char TL;
Init_DS18B20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
delay_with_scan(25);
Init_DS18B20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
TL=Read_DS18B20();
TH=Read_DS18B20();
Init_DS18B20();
temp16=0;
temp16|=TH;
temp16=temp16<<8;
temp16|=TL;
temp_dat=(double)temp16*(0.0625);
temp_smg=(unsigned int)(temp_dat*100);
if(mod==1){
if(temp_dat>temp_para){
da_dat=0xff;
volt_dat=500;
}
else{
da_dat=0x00;
volt_dat=0;
}
}
else if(mod==2){
da_dat=(unsigned char)(((0.15*temp_dat-2.0)*255.0)/5.0);
volt_dat=(unsigned int)((0.15*temp_dat-2.0)*100);
}
}
void set_da_out(void){
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x40);
IIC_WaitAck();
IIC_SendByte(da_dat);
IIC_SendAck(1);
IIC_Stop();
}
void display_bit(unsigned char pos,unsigned char segCode,unsigned char dot){
write_latch(6,0x01<<(pos-1));
if(dot){
write_latch(7,segCode&0x7f);
}
else{
write_latch(7,segCode);
}
}
void smg_off(unsigned char pos){
write_latch(6,0x01<<(pos-1));
write_latch(7,0xff);
}
void display_temp(){
display_bit(1,_C_,0);
delay_ms(smgTime);
smg_off(1);
display_bit(2,0xff,0);
delay_ms(smgTime);
smg_off(2);
display_bit(3,0xff,0);
delay_ms(smgTime);
smg_off(3);
display_bit(4,0xff,0);
delay_ms(smgTime);
smg_off(4);
display_bit(5,seg_code[temp_smg/1000],0);
delay_ms(smgTime);
smg_off(5);
display_bit(6,seg_code[(temp_smg/100)%10],1);
delay_ms(smgTime);
smg_off(6);
display_bit(7,seg_code[(temp_smg/10)%10],0);
delay_ms(smgTime);
smg_off(7);
display_bit(8,seg_code[(temp_smg%10)],0);
delay_ms(smgTime);
smg_off(8);
}
void display_para(){
display_bit(1,_P_,0);
delay_ms(smgTime);
smg_off(1);
display_bit(2,0xff,0);
delay_ms(smgTime);
smg_off(2);
display_bit(3,0xff,0);
delay_ms(smgTime);
smg_off(3);
display_bit(4,0xff,0);
delay_ms(smgTime);
smg_off(4);
display_bit(5,0xff,0);
delay_ms(smgTime);
smg_off(5);
display_bit(6,0xff,0);
delay_ms(smgTime);
smg_off(6);
display_bit(7,seg_code[temp_para/10],0);
delay_ms(smgTime);
smg_off(7);
display_bit(8,seg_code[temp_para%10],0);
delay_ms(smgTime);
smg_off(8);
}
void display_da(){
display_bit(1,_A_,0);
delay_ms(smgTime);
smg_off(1);
display_bit(2,0xff,0);
delay_ms(smgTime);
smg_off(2);
display_bit(3,0xff,0);
delay_ms(smgTime);
smg_off(3);
display_bit(4,0xff,0);
delay_ms(smgTime);
smg_off(4);
display_bit(5,0xff,0);
delay_ms(smgTime);
smg_off(5);
display_bit(6,seg_code[volt_dat/100],1);
delay_ms(smgTime);
smg_off(6);
display_bit(7,seg_code[(volt_dat/10)%10],0);
delay_ms(smgTime);
smg_off(7);
display_bit(8,seg_code[volt_dat%10],0);
delay_ms(smgTime);
smg_off(8);
}
void display_ctrl(){
if(ui_mod==1){
display_temp();
}
if(ui_mod==2){
display_para();
}
if(ui_mod==3){
display_da();
}
}
void scan_keys(){
P32=0;P33=1;
if(P42==0){
delay_with_scan(1);
if(P42==0){
//s9
temp_para++;
}
}
if(P44==0){
delay_with_scan(1);
if(P44==0){
//s5
if(mod==1)mod=2;
else if(mod==2)mod=1;
}
}
P32=1;P33=0;
if(P42==0){
delay_with_scan(1);
if(P42==0){
//s8
temp_para--;
}
}
if(P44==0){
delay_with_scan(1);
if(P44==0){
//s4
if(ui_mod==1){
ui_mod=2;
}
else if(ui_mod==2){
ui_mod=3;
}
else if(ui_mod==3){
ui_mod=1;
}
}
}
}
void init_sys(){
write_latch(4,0xff);
write_latch(5,0x00);
write_latch(6,0xff);
write_latch(7,0xff);
}
void Timer0Init(void) //1ms@12.000MHz
{
EA=1;
ET0=1;
AUXR |= 0x80;
TMOD &= 0xF0;
TMOD |= 0x01;
TL0 = 0x40;
TH0 = 0xA2;
TF0 = 0;
TR0 = 1;
}
void t0server() interrupt 1{
TL0 = 0x40;
TH0 = 0xA2;
counter++;
if(counter==40){
scan_keys();
led_ctrl();
counter=0;
}
}
void led_ctrl(){
if(mod==1){
led_stat&=0xfe;
enable_latch(4);
P0=led_stat;
enable_latch(0);
}
else{
led_stat|=0x01;
enable_latch(4);
P0=led_stat;
enable_latch(0);
}
if(ui_mod==1){
led_stat&=0xfd; //1111 1101
enable_latch(4);
P0=led_stat;
enable_latch(0);
}
else{
led_stat|=0x02; //0000 0010
enable_latch(4);
P0=led_stat;
enable_latch(0);
}
if(ui_mod==2){
led_stat&=0xfb; //1111 1011
enable_latch(4);
P0=led_stat;
enable_latch(0);
}
else {
led_stat|=0x04; //0000 0100
enable_latch(4);
P0=led_stat;
enable_latch(0);
}
if(ui_mod==3){
led_stat&=0xf7; //1111 0111
enable_latch(4);
P0=led_stat;
enable_latch(0);
}
else{
led_stat|=0x08; //0000 1000
enable_latch(4);
P0=led_stat;
enable_latch(0);
}
}
void main(){
init_sys();
Timer0Init();
while(1){
read_temp();
set_da_out();
display_ctrl();
}
}
题目内容可见于蓝桥杯官网蓝桥杯大赛历届真题 - 单片机组程序设计题(本科组) - 蓝桥云课 (lanqiao.cn)