<>【 The 12th provincial competition of Blue Bridge Cup 】

#include "reg52.h" #include "onewire.h" #include "iic.h"
/*============================================ The 12th Blue Bridge Cup Date:2022.2.4
Author: Xiao Yin Version:1.0 =============================================*/ sfr P4 =
0xc0; sbit H1 = P3^0; sbit H2 = P3^1; sbit H3 = P3^2; sbit H4 = P3^3; sbit L1 =
P3^4; sbit L2 = P3^5; sbit L3 = P4^2; sbit L4 = P4^4; unsigned char Temperature
= 25; unsigned char Temperature_Param = 25;// Temperature parameter setting unsigned int voltage = 325;
// Voltage expand 100 Times convenient calculation and display unsigned char interface_mode = 0,work_mode = 1; // Interface switching Working mode
unsigned char Temperature_start = 0,DAC_start = 0,count = 0;// temperature DAC Meta Refresh time
unsigned char code SMG_duama[18]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,
0x90,0x88,0x80,0xc6,0xc0,0x86,0x8e,0xbf,0x7f};// Nixie tube segment code without decimal point unsigned char code
SMG_DOT[10] ={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};// Nixie tube segment code with decimal point
//------------------------------ Simple delay ------------------------- void Delay(
unsigned int t) { while(t--); }
//-------------------------74HC138 initialization ------------------------- void Init_74HC138
(unsigned char n) { switch(n) { case 0:P2 = (P2 & 0x1f) | 0x00;break; case 4:P2
= (P2 & 0x1f) | 0x80;break; case 5:P2 = (P2 & 0x1f) | 0xa0;break; case 6:P2 = (
P2& 0x1f) | 0xc0;break; case 7:P2 = (P2 & 0x1f) | 0xe0;break; } }
//--------------------------- System initialization -------------------------- void Init_System(
void) { Init_74HC138(4); P0 = 0xff; Init_74HC138(5); P0 = 0x00; Init_74HC138(0);
} //-------------------------------- Nixie tube position display ----------------- void SMG_DisplayBit
(unsigned char pos,unsigned char dat) { Init_74HC138(6); P0 = (0x01 << pos);
Init_74HC138(7); P0 = dat; }
//------------------------------ Nixie tube display ---------------------- void SMG_Close(void)
{ Init_74HC138(6); P0 = 0xff; Init_74HC138(7); P0 = 0xff; } void
SMG_Display_Volate(void) { SMG_DisplayBit(0,SMG_duama[10]); Delay(200);
SMG_DisplayBit(1,0xff); Delay(200); SMG_DisplayBit(2,0xff); Delay(200);
SMG_DisplayBit(3,0xff); Delay(200); SMG_DisplayBit(4,0xff); Delay(200);
SMG_DisplayBit(5,SMG_DOT[voltage/100]); Delay(200); SMG_DisplayBit(6,SMG_duama[
voltage/10%10]); Delay(200); SMG_DisplayBit(7,SMG_duama[voltage%10]); Delay(200)
; SMG_Close(); Delay(200); } void SMG_Display_Temperature(void) { SMG_DisplayBit
(0,SMG_duama[12]); Delay(200); SMG_DisplayBit(1,0xff); Delay(200);
SMG_DisplayBit(2,0xff); Delay(200); SMG_DisplayBit(3,0xff); Delay(200);
SMG_DisplayBit(4,SMG_duama[Temperature*100/1000]); Delay(200); SMG_DisplayBit(5,
SMG_DOT[Temperature*100/100%10]); Delay(200); SMG_DisplayBit(6,SMG_duama[
Temperature*100/10%10]); Delay(200); SMG_DisplayBit(7,SMG_duama[Temperature*100/
10%10]); Delay(200); SMG_Close(); Delay(200); } void SMG_Set_Temperature(void) {
//P 0011 0001 0X8C SMG_DisplayBit(0,0X8c); Delay(200); SMG_DisplayBit(1,0xff);
Delay(200); SMG_DisplayBit(2,0xff); Delay(200); SMG_DisplayBit(3,0xff); Delay(
200); SMG_DisplayBit(4,0xff); Delay(200); SMG_DisplayBit(5,0xff); Delay(200);
SMG_DisplayBit(6,SMG_duama[Temperature_Param/10]); Delay(200); SMG_DisplayBit(7,
SMG_duama[Temperature_Param%10]); Delay(200); SMG_Close(); Delay(200); }
//--------------------------------DS18B20------------------------ void
Read_Temperature(void) { unsigned char LSB,MSB; init_ds18b20(); Write_DS18B20(
0xcc); Write_DS18B20(0x44); Delay(500); init_ds18b20(); Write_DS18B20(0xcc);
Write_DS18B20(0xbe); LSB = Read_DS18B20(); MSB = Read_DS18B20(); init_ds18b20();
Temperature= (MSB << 8) | LSB; if(Temperature & 0xf800 == 0x0000) { Temperature
= Temperature >> 4; Temperature *= 10; Temperature = Temperature + (LSB & 0x0f)*
0.625; } } //---------------------------------PCF8591---------------------- void
Read_Rd2_DAC(float dat) { IIC_Start(); IIC_SendByte(0x90); IIC_WaitAck();
IIC_SendByte(0x40); IIC_WaitAck(); IIC_SendByte(dat); IIC_WaitAck(); IIC_Stop();
} //---------------------------------- Matrix keyboard -------------------- void Matrix_Key(
void) { H3 = 0; H1 = H2 = H4 = 1; L1 = L2 = L3 = L4 = 1; //s9
Press in the parameter interface S9 Key , Temperature parameter plus 1 if(L3 == 0) { Delay(20); if(L3 == 0) { while(L3 == 0) {
SMG_Set_Temperature(); // Temperature setting interface } if(interface_mode == 1) // Add one to the temperature under the temperature interface {
Temperature_Param++; } } } //S5 Defined as “ pattern ” Switch button . /* pattern 1:DAC The output voltage is temperature dependent .
adopt DS18B20 When the collected real-time temperature is less than the temperature parameter ,DAC output 0V, otherwise ,DAC output 5V. pattern 2:DAC According to the figure 7 Given the relationship between output voltage . */ if(L4 ==
0) { Delay(20); if(L4 == 0) { while(L4 == 0) { SMG_Display_Volate(); } if(
work_mode== 1) { if(Temperature < Temperature_Param) { Read_Rd2_DAC(0);
// When the real-time temperature is less than the temperature parameter ,DAC output 0V, voltage = 0; } else { Read_Rd2_DAC(255); //DAC output 5V
voltage= 500; //5v expand 100 times Convenient calculation } work_mode = 2; } else if(work_mode == 2) {
work_mode= 0; if(Temperature <= 20) { //5/255 = 1/x x->255/5 Read_Rd2_DAC(51);
voltage= 100; //1v } else if(Temperature > 40) { //5/255 = 4/x x-> 204
Read_Rd2_DAC(204); voltage = 400; //4v } // Relational output else if(Temperature > 20 &&
Temperature< 40) { Read_Rd2_DAC((0.15*Temperature-2)*51.0); voltage = (0.15*
Temperature-2)*100; // expand 100 Times convenient calculation } work_mode = 1; } } } H4 = 0; H1 = H2 = H3 = 1
; L1 = L2 = L3 = L4 = 1; //s8 Defined as “ reduce ” Key Press in the parameter interface S8 Key , Temperature parameter minus 1 if(L3 == 0) { Delay(20
); if(L3 == 0) { while(L3 == 0) { SMG_Set_Temperature(); // Temperature setting interface } if(
interface_mode== 1) // Reduce the temperature by one under the temperature interface { Temperature_Param--; } } } //S4
Defined as “ Interface ” Key , Press S4 Key , Switch temperature display interface , Parameter setting interface and DAC Output interface , if(L4 == 0) { Delay(20); if(L4 == 0) {
while(L4 == 0) { if(interface_mode == 0) { interface_mode = 1;
SMG_Display_Temperature(); // Temperature display interface } else if(interface_mode == 1) {
interface_mode= 2; SMG_Set_Temperature(); // Interface temperature setting } else if(interface_mode == 2
) { interface_mode = 0; SMG_Display_Volate(); // Voltage display interface } } } } }
//--------------------------------LED Function processing ------------------- void Led_Running(
void) { // Currently in mode 1 state , indicator light L1 Light up , Otherwise it goes out Init_74HC138(4); if(work_mode == 1) { P0 = 0x7f
; //0111 1111 } switch(interface_mode) { case 0:P0 = 0xfd;break;
// Currently in the temperature display interface , indicator light L2 Light up , Otherwise it goes out case 1:P0 = 0xfb;break;// Currently in parameter setting interface , indicator light L3 Light up , Otherwise it goes out . case
2:P0 = 0xf7;break; // Currently in DAC Output interface , indicator light L4 Light up , Otherwise it goes out } /* if(interface_mode == 0) { P0
= 0xfd; //1011 1111 } // Currently in parameter setting interface , indicator light L3 Light up , Otherwise it goes out . if(interface_mode == 1) { P0 =
0xfb; //1101 1111 } // Currently in DAC Output interface , indicator light L4 Light up , Otherwise it goes out if(interface_mode == 2) { P0 =
0xf7; //1110 1111 } */ }
//-------------------------------- timer initiated ------------------- void Init_Timer(void
) { TMOD = 0x01; TH0 = (65535-50000)/256; //50MS TL0 = (65535-50000)%256; ET0 =
1; TR0 = 1; EA = 1; }
//---------------------------- Main function ----------------------------- void main(void) {
Init_System(); Init_Timer(); while(1) { Matrix_Key(); Read_Temperature(); switch
(interface_mode) { case 0:SMG_Display_Temperature();break; case 1:
SMG_Set_Temperature();break; case 2:SMG_Display_Volate(); break; } Led_Running()
; } } //---------------------------------- Interrupt service function ---------------- // Temperature data refresh time :≤1 second .
//DAC Output voltage refresh time :≤0.5 second void Server_Timer0() interrupt 1 { TH0 = (65535-50000)/256;
TL0= (65535-50000)%256; count++; if(count == 10) //0.5s { if(DAC_start == 0 )
//DAC Meta Refresh { DAC_start = 1; } else if(DAC_start == 1) { DAC_start = 0; } } if(
count== 20)// 1s { if(Temperature_start == 0) // Temperature refresh flag { Temperature_start = 1;
} else if(Temperature_start == 1) { Temperature_start = 0; } count = 0; } }

Technology