The microcontroller source program is as follows:
Wendu.h (header file)
#define jump_ROM 0xCC //ds18B20 definition
#define start 0x44
#define read_EEROM 0xBE
void delay(unsigned int N);
void delay_1();
unsigned char Reset(void); //Reset DS18B20
unsigned char read_bit(void); //Read a bit value from DS18B20
void write_bit(unsigned char bitval); //Write one bit to DS18B20
unsigned char read_byte(void); //Read a byte from DS18B20
void write_byte(unsigned char val); // Write a byte to DS18B20
Wendu.c (implementation of Wendu.h)
#include #include"wendu.h" sbit DQ = P2^3; //DS18B20 data port /******************************************************************** * Name: delay() * Function: Delay, the delay time is about 140US. * Input: None * Output: None ***********************************************************************/ void delay_1() { int i,j; for(i=0; i<=10; i++) for(j=0; j<=2; j++); } /******************************************************************** * Name: delay() * Function: Delay function * Input: None * Output: None ***********************************************************************/ void delay(unsigned int N) { int i; for(i=0; i /******************************************************************** * Name: Reset() * Function: Reset DS18B20 * Input: None * Output: None ***********************************************************************/ unsigned char Reset(void) { unsigned char deceive_ready; DQ = 0; delay(29); DQ = 1; delay(3); deceive_ready = DQ; delay(25); return(deceive_ready); } /******************************************************************** * Name: read_byte() * Function: Read a byte from DS18B20 * Input: None * Output: The value read from DS18B20 ***********************************************************************/ /******************************************************************** * Name: read_bit() * Function: Read a bit value from DS18B20 * Input: None * Output: A bit value read from DS18B20 ***********************************************************************/ unsigned char read_bit(void) { unsigned char i; DQ = 0; DQ = 1; for(i=0; i<3; i++); return(DQ); } /******************************************************************** * Name: write_bit() * Function: Write a bit to DS18B20 * Input: bitval (the bit value to be written to DS18B20) * Output: None ***********************************************************************/ void write_bit(unsigned char bitval) { DQ=0;if(bitval==1) DQ=1; delay(5); DQ=1; } /******************************************************************** * Name: read_byte() * Function: Read a byte from DS18B20 * Input: None * Output: The value read from DS18B20 ***********************************************************************/ unsigned char read_byte(void) { unsigned char i,m,receive_data; m = 1; receive_data = 0; for(i=0; i<8; i++) { if(read_bit()) { receive_data = receive_data + (m << i); } delay(6); } return(receive_data); } /******************************************************************** * Name: write_byte() * Function: Write a byte to DS18B20 * Input: val (command value to be written to DS18B20) * Output: None ***********************************************************************/ void write_byte(unsigned char val) { unsigned char i,temp; for(i=0; i<8; i++) { temp = val >> i; temp = temp & 0x01; write_bit(temp); delay(5); } } Implementation of the main function #include #include"wendu.h" sbit key1=P1^4; //Matrix keyboard definition sbit key2=P1^5; sbit key3=P1^6; sbit key4=P1^7; sbit S1=P3^2; //button definition sbit S2=P3^3; sbit S3=P3^4; sbit RS=P2^5; //12864 definition sbit RW=P2^6; sbit E=P2^7; sbit BEEP = P2^0; //Buzzer definition sbit Hot=P2^1; unsigned char TMPH,TMPL,key41=1,Temp,T; //temperature constant unsigned int count1,count2,count3; //time constant unsigned int countor,m,s; //timing constant unsigned char button,sound; //other constants unsigned char code table[]={"0123456789"}; /******************Subroutine Description********************/ void delay_1ms(unsigned int i); //1ms延时 void delay(); //delay subroutine void beep(); //bell program void set(char n); //12864 write instruction subroutine void weizhi(int hang,int lie); //12864 character display position void wdata(unsigned char d); //Write data to 12864, single character void wsdata(char tab[]); //Write data to 12864, string void scan(); //Scan the first column of the matrix keyboard to determine the input time void setTemp(); //Temperature setting, using scanning independent keyboard to determine input void setTime(); //Time setting void showTime(); //Display time void chu12864(); //initialize 12864 void chuDS(); //Initialize DS18B20 /************************Main program****************************/ void main() //Main function { chu12864(); //12864 initialization setTemp(); setTime(); while(1) { chuDS(); if(Temp==T) TR0=1; showTemp(); showTime(); if(sound==1) beep(); } } /******************************************/ void time0(void) interrupt 1 using 1 //Timer interrupt program { countor++; if(countor==20) { if(m==0&&s==0) //Countdown { m=0; s=0; sound=1; Hot=0; TR0=0; } else if(s==0) { m--;s=59;} else s--; countor=0; } TH0=(65536-50000)/256; TL0=(65536-50000)%256; } /************************************************/ void delay_1ms(unsigned int i) //1ms延时 { unsigned char x,j; for(j=0;j for(x=0;x<=148;x++); } /************************************************/ void delay() //delay subroutine { int a,b; for(a=0;a<100;a++) for(b=0;b<10;b++); }/************************************************/ void beep() //bell program { unsigned char i; for (i=0;i<100;i++) { delay_1ms(1); BEEP=!BEEP; //BEEP取反 } BEEP=1; //Turn off the buzzer } /************************************************/ void set(char n) //12864 write instruction subroutine { P0=n; RS=0; RW=0; E=0; delay(); E=1; delay(); } /******************************************/ void weizhi(int hang,int lie) //12864 character display position { char a; if(hang==1) a=0x80; if(hang==2) a=0xc0; a=a+lie-1; set(a); } /******************************************/ void wdata(unsigned char d) //Write data to 12864, single character { RS=1; RW=0; P0=d; E=0; delay(); E=1; delay(); } /******************************************/ void wsdata(char tab[]) //Write data to 12864, string { int i=0; RS=1; RW=0; while(tab[i]!='') //Execute when it is not the end character { P0=tab[i]; E=0; delay(); E=1; delay(); i++; } } /******************************************/ void scan() //Scan the first column of the matrix keyboard to determine the input time { P1=0xfe; if(key1==0) { delay(); if(key1==1) { count1++; beep(); } } if(key2==0) { delay(); if(key2==1) { count2++; beep(); } } if(key3==0) { delay(); if(key3==1) { count3++; beep(); } } if (key4==0) { key41=0; beep(); } } /******************************************/ void setTemp() //Temperature setting, using scanning independent keyboard to determine input { while(1) { P3=0xff; if(S1==0) button=1; //20 degrees if(S2==0) button=2; //30 degrees if(S3==0) button=3; //50 degrees if(button==1) { T=20; weizhi(2,5); wdata(table[2]); delay_1ms(1); weizhi(2,6); wdata(table[0]); } if(button==2) { T=30; weizhi(2,5); wdata(table[3]); delay_1ms(1); weizhi(2,6); wdata(table[0]); } if(button==3) { T=50; weizhi(2,5); wdata(table[5]); delay_1ms(1); weizhi(2,6); wdata(table[0]); } if(button==1||button==2||button==3) break; } } /****************************************/ void setTime() //Time setting { while(1) { scan(); if(key41==0) break; } m=(1*count1)+(2*count2)+(5*count3); //Calculate the set time Hot=1; } void showTime() //Display time { weizhi(2,11); wdata(table[m/10]); //Display points delay_1ms(1); weizhi(2,12); wdata(table[m%10]); delay_1ms(1); weizhi(2,13); wdata(':'); delay_1ms(1); weizhi(2,14); wdata(table[s/10]); //Display seconds delay_1ms(1); weizhi(2,15); wdata(table[s%10]); delay_1ms(1); } /****************************************/ void showTemp() //Display temperature { delay_1ms(1); weizhi(1,13); wdata(table[Temp/10]); delay_1ms(1); weizhi(1,14); wdata(table[Temp%10]); delay_1ms(1); } /****************************************/ void chu12864() // Initialize 12864 { set(0x01); //Clearing set(0x38); //display mode set(0x0c); //display on set(0x06); //Cursor setting weizhi(1,4); //Define the character display position first row, fourth column wsdata("Welcome"); //Display the startup welcome message weizhi(2,3); wsdata("T="); weizhi(2,7); wsdata("oC"); IE=0x82; //Set the timer working mode TMOD=0X01; TH0=(65536-50000)/256; //Set the timer duration to 50MS
Previous article:Four-way buzzer based on Puzhong Technology 51 single-chip microcomputer
Next article:Single chip microcomputer 1602 Chinese characters and time display program (year, month, day 3 Chinese characters)
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Showing off the goods + the first wave of development boards
- RF Circuit Engineering Design
- How to obtain additional information of Pingtouge scenario-based Bluetooth MESH
- I have encountered a problem with the iTOP4418 development board. Is there any big guy who can help me?
- FLASH cannot be downloaded
- [GD32E231 DIY Contest] 3. Timer + button (supports long press and short press) + LED
- New findings from Nature's sub-journal: Do we need to work 10% more per week after working from home?
- Harbin in my eyes in 2020
- RISC-V IDE MRS usage notes (I): Target mode doesn't match
- How to distinguish live wire, neutral wire and ground wire? Is it reliable to distinguish them by color?