// The refrigeration temperature test, setting the temperature through the screen, through the measurement display current temperature. #include #include sbit RTR1=P1^0;/*Refrigeration temperature relay stops cooling after reaching the set temperature.*/ unsignedchar data_start,data_ok,RX5A,RXA5,RXLEN,RXCMD,RXADRH,RXADRL,RXDLEN,TXTIME; unsignedchar RX_BUF[2]; /*Reads the SET data variable buffer (0x83 instruction) 2Byte variable data. */ unsignedchar TX_BUF[16]={0x5A,0xA5,0x05,0x83,0x10,0x00,0x00,0x01, // Read data register port 0x83 corresponding to address 0x1000 address. 0x5A,0xA5,0x05,0x82,0x00,0x00}; /*Write data register (0x82 instruction), write 2Byte variable data, corresponding to 0x0000 address*/ unsignedchar TX_CNT,RX_CNT;//send and receive buffer pointer position. void main(void) { int temp_s,temp_c; //The power is initialized PCON=PCON|0x80;/*Configure serial port baud rate clock is 22.1184bps, baud rate is 115200bps*/ SCON=0x50;// Receive mode 1, allow serial port to receive. TMOD=0x21;//Timer 1 mode 2, timer 0 mode 1. TH1=255; TL1=255; TR1=1; TH0=0xB8;//10ms timed interruption , calculation //(2^16-0xB800)*12*1/(22.1184*10^6)=0.01 TL0=0x00; TR0=1; TXTIME=100;//Start sending data after 1s. data_start=0; data_ok=0; RTR1=1;//Turn on the refrigeration relay on the power supply. ES=1; ET0=1; EA=1; while(1) { temp_c=Readtemp();//Reads the current measurement temperature if(data_ok==0xff) { data_ok=0; temp_s=RX_BUF[0]*256+RX_BUF[1];// Read the set temperature } if(temp_c>temp_s)// By comparing the On-off relays RTR1=0; else RTR1=1; TX_BUF[14]=temp_c/256;// Displays the current temperature value send buffer TX_BUF[15]=temp_c%256; } } //Measure the temperature function int Readtemp(void) { return20;// Here for the user to write temperature test program, here only return constant. } void Time0_interrupt(void)interrupt2 { TF0=0;// Clears the interrupt flag bit TH0=0xB8; TL0=0x00; TXTIME--; if(TXTIME==0) { TXTIME=20;// 200ms send once TX_CNT=0; TI=0; SBUF=TX_BUF[0];//TI = 1; } } void Uart_interrupt(void)interrupt4 { unsignedchar temp; if(RI) { temp=SBUF; RI=0; if(data_start==0) { RX5A=RXA5; RXA5=RXLEN; RXLEN=RXCMD; RXCMD=RXADRH; RXADRH=RXADRL; RXADRL=RXDLEN; RXDLEN=temp; // Check synchronization frame data if((RX5A==0x5A)&&(RXA5==0xA5)&&(RXLEN==0x08)&&(RXCMD==0x83) &&(RXADRH=0x10)&&(RXADRL=0x00)&&(RXDLEN=0x01)) { data_start=0xff; RX_CNT=0; } } else { RX_BUF[RX_CNT]=temp; RX_CNT++; if(RX_CNT==2) { data_start=0; data_ok=0xff; } } } if(TI)// Send interrupt { TI=0; TX_CNT++; if(TX_CNT<16) {SBUF=TX_BUF[TX_CNT];}//TI = 1; } }
|