Function: time, hour and minute alarm setting, upper and lower temperature limit setting, temperature display.
The alarm can be set to the minute, and the ringing time is still 10 seconds.
Expandable functions: music ringing, alarm setting for any day of the week, just need to add or modify the program. The program is just simply organized, and many places are messy. Friends who need the program had better have good programming skills, otherwise it is difficult to understand the writing thinking of the program.
Simulation diagram
Complete source code download:
http://www.51hei.com/ziliao/file/1602_naoz.rar
#include "reg51.h" #define uchar unsigned char #define uint unsigned int sbit BEEP=P3^7; //// Define the buzzer port unsigned char L,H,Z,Y; //// Define high and low temperature variables ////////////////////////////////////////////////////// /// /**********Alarm sound**************/ bi(uint t) { int c,n; for(c=0;c0;i--) { DQ = 0; //The read cycle starts when the high level is pulled to the low level dat>>=1; DQ = 1; // give pulse signal if(DQ) dat|=0x80; delay_18B20(4); } return(dat); } //////////////////////////////////////////////////// /*************ds18b20 writes a byte****************/ void WriteOneChar(unsigned char dat) { unsigned char i=0; for (i=8; i>0; i--) { DQ = 0; //When pulled from high level to low level, the write cycle starts DQ = dat&0x01; //The lowest bit of data is written first delay_18B20(5); //60us to 120us delay DQ = 1; dat>>=1; //Input from the lowest bit to the highest bit } } ////////////////////////////////////////////////////// ///////// /**************Read the current temperature of ds18b20************/ void ReadTemperature(void) { unsigned char a=0,b=0,t=0; Init_DS18B20 ( ); WriteOneChar ( 0xCC ); // Skip reading the serial number and column number WriteOneChar ( 0x44 ); // Start temperature conversion delay_18B20 ( 100 ); // this message is very important Init_DS18B20 ( ); WriteOneChar ( 0xCC ); // Skip the operation of reading the serial number and column number WriteOneChar ( 0xBE ); //Read temperature registers, etc. (9 registers can be read in total) The first two are temperature delay_18B20 ( 100 ); a=ReadOneChar() ; //Read the low bit of the temperature value b=ReadOneChar() ; //Read the high bit of the temperature value temp1 = b<<4; //value of the last three digits in the upper 8 bits temp1+= ( a&0xf0 )>>4; //The high 4 bits in the low 8 bits plus the last three digits in the high 8 bits temp1 room temperature integer value temp2 = a&0x0f; //decimal value temp = ( ( b*256+a ) >>4 ); // Current collected temperature value divided by 16 to get the actual temperature value zhenshu xs=temp2*0.0625*10; //decimal place, if it is 0.5, it will be counted as 5 to display the decimal of xs xiaoshu } ////////////////////////////////////////////////////// /////////////////// /*************************1602 LCD program************************/ /******Define LCD control port*******/ sbit lcdrs=P2^5; //Define three control ports of LCD sbit lcdrw=P2^6; sbit lcden=P2^7; sbit K1=P3^0; //define three switches sbit K2=P3^1; sbit K3=P3^2; ////////////////////////////////////////////////////// ///////// /******Define variables*******/ uchar count,shi,fen,miao,xq,K1num; //Define the variables needed in the time program ////////////////////////////////////////////////////// ///////////////// /******Define array*******/ // uchar code table [ ] = "H:38 L:25 00 Q:7"; //Contents displayed on the LCD interface //uchar code table1[ ]= "23:59:55 . C"; uchar code table [ ] = "01:00:55 01-01"; //Contents displayed on the LCD interface uchar code table1[ ]= "H:38 L:25 . C"; ////////////////////////////////////////////////////// /////////// /******Delay program*******/ void delay(uint m) //LCD delay program { uint i,j; for(i=m;i>0;i--) for(j=110;j>0;j--); } ////////////////////////////////////////////////// ////////////////////////////////////////////////// /**********Write instruction*************/ void write_com(uchar com) { lcdrs=0; lcdrw=0; lcden=0; P0=com; delay(5); lcden=1; delay(5); lcden=0; } ////////////////////////////////////////////////////// ///// /************Write data*************/ void write_data(uchar date) { lcdrs=1; lcdrw=0; lcden=0; P0=date; delay(5); lcden=1; delay(5); lcden=0; } ////////////////////////////////////////////////////// /////// ////////////////////////////////////////////////////// ////// /***************LCD display program***************/ /*********Separation of hours, minutes and seconds**********/ void write_sfm(uchar add,uchar date) { uchar shi,ge; //Separate numbers, such as 35, into 3 and 5 shi=date/10; ge=date%10; write_com ( 0x80+add ); write_data ( 0x30+shi ); write_data ( 0x30+ge ); } ////////////////////////////////////////////////////// ///////////////// /**********Week***********/ void write_xq(uchar add1,uchar date1) { uchar ge; //Only one number is needed for the week ge=date1%10; write_com ( 0x80+add1 ); write_data ( 0x30+ge ); } ////////////////////////////////////////////////////// /////////////// /************Separation of high and low temperature numbers************/ void write_HL(uchar add2,uchar date2) { uchar shi,ge; shi=date2/10; ge=date2%10; write_com(0x80+0x40+add2); write_data( 0x30+shi ); write_data( 0x30+ge ); } ////////////////////////////////////////////////////// ////////////// ////////////////////////////////////////////////////// //////////////// /***************Key timing program***************/ void keycan() { /**********Function selection**********/ if( K1==0 ) { K1num++; delay(1); if( K1==0 ) { while(!K1);bi(200); if( K1num==1 ) { TR0=0; write_com ( 0x80+1 ); write_com(0x0f); } // if( K1num==2 ) { write_com ( 0x80+4 ); } //points if( K1num==3 ) { write_com ( 0x80+7 ); } //Seconds //if( K1num==4 ) { write_com ( 0x80+11 ); } //Weekday if( K1num==4 ) { write_com ( 0x80+12 ); } //Alarm if( K1num==5 ) { write_com ( 0x80+15 ); } if( K1num==6 ) { write_com ( 0x80+0x40+3 ); } //Upper limit if( K1num==7 ) { write_com ( 0x80+0x40+8 ); } //Lower limit if( K1num==8 ) { K1num=0; write_com(0x0c); TR0=1; } //Exit settings } } ////////////////////////////////////////////////////// ////////////////////// /************Function plus*********/ if( K1num != 0 ) { if( K2==0 ) { delay(1); if( K2==0 ) { while(!K2);bi(200); if( K1num==1 ) { shi++; if( shi==24 ) shi=0; write_sfm ( 0,shi ); write_com ( 0x80+1 ); } if( K1num==2 ) { fen++; if( fen==60 ) fen=0; write_sfm ( 3,fen ); write_com ( 0x80+4 ); } if( K1num==3 ) { miao++; if( miao==60 ) miao=0; write_sfm ( 6,miao); write_com ( 0x80+7 ); } //if( K1num==4 ) { xq++; if( xq==8 ) xq=1; write_xq (11,xq ); write_com ( 0x80+11); } if( K1num==4 ) { Z++; if( Z==24 ) Z=0; write_sfm (11,Z ); write_com ( 0x80+12); } if( K1num==5 ) { Y++; if( Y==60 ) Y=0; write_sfm (14,Y ); write_com ( 0x80+15); } if( K1num==6 ) { H++; if( H==100) H=0; write_HL ( 2,H ); write_com ( 0x80+0x40+3);} if( K1num==7 ) { L++; if( L==100) L=0; write_HL ( 7,L ); write_com ( 0x80+0x40+8);} } } } ////////////////////////////////////////////////// /**********Function reduction************/ if(K1num!=0) { if(K3==0 ) { delay(1); if(K3==0 ) { while(!K3); bi(200); if( K1num==1 ) { shi--; if( shi==-1 ) shi=23; write_sfm ( 0,shi ); write_com ( 0x80+1 ); } if( K1num==2 ) { fen--; if( fen==-1 ) fen=59; write_sfm ( 3,fen ); write_com ( 0x80+4 ); } if( K1num==3 ) {miao--; if( miao==-1 ) miao=59; write_sfm ( 6,miao); write_com ( 0x80+7 ); } // if( K1num==4 ) { xq--; if( xq==0 ) xq=7; write_xq (11,xq ); write_com ( 0x80+11); } if( K1num==4 ) { Z--; if( Z==-1 ) Z=23; write_sfm (11,Z ); write_com ( 0x80+12); } if( K1num==5 ) { Y--; if( Y==-1 ) Y=59; write_sfm (14,Y ); write_com ( 0x80+15); } if( K1num==6 ) { H--; if( H==-1 ) H=99; write_HL ( 2,H ); write_com ( 0x80+0x40+3);} if( K1num==7 ) { L--; if( L==-1 ) L=99; write_HL ( 7,L ); write_com ( 0x80+0x40+8);} } } } } ////////////////////////////////////////////////////// ///////// /*************initialization**************/ void init() { uchar num; //Define a variable num for initializing the following array// lcden=0; write_com ( 0x38 ); write_com ( 0x0c ); //Do not display the cursor write_com ( 0x06 ); //The cursor automatically moves to the right write_com ( 0x01 ); // clear screen shi=1; fen=00; miao=55; //xq=7; H=38; L=25; Z=1; Y=1; write_com ( 0x80 ); for( num=0; num<16; num++ ) { write_data( table[num] ); delay(5); } write_com ( 0x80+0x40 ); for( num=0; num<16; num++ ) { write_data( table1[num] ); delay(5); } ////////////////////////////////////////////////////// ///////// /********Timer initialization*************/ TMOD=0x01; //Define timer 0, working mode 1 TH0=(65536-50000)/256; //high 8 bits TL0=(65536-50000)%256; //low 8 bits EA=1; //Open global interrupt ET0=1; TR0=1; //Turn on the timer } ////////////////////////////////////////////////////// /////////////////// /***********Timer interrupt function************/ void timer0() interrupt 1 { TH0=(65536-50000)/256; TL0=(65536-50000)%256; count++; if( count==20 ) { count=0; miao++; if(miao==60) { miao=0; fen++; } if( fen==60 ) { fen=0; shi++; } if( shi==24 ) { shi=0; //xq++; //if( xq==8 ) { xq=1; } } } } //////////////////////////////////////////////// ////////////////////////////////////////////////////// //// ////////////////////////////////////////////////////// / /****************************************/ uchar code mytab[8]={0x0C,0x12,0x12,0x0C,0x00,0x00,0x00,0x00}; /* Set the display position /******************************************/ void lcd_pos(uchar pos) { write_com(pos | 0x80); //data pointer = 80 + address variable } //////////////////////////////////////////////// /*Write custom characters into CGRAM /************************************/ void writetab() { unsigned char i ; write_com(0x40); //Write CGRAM for (i = 0 ; i < 8 ; i++) write_data(mytab[ i ]) ; } ////////////////////////////////////////////////////// //////////////////// ////////////////////////////////////////////////////// ////////////////////// ////////////////////////////////////////////////////// ////////////////////// void wendu() /////Temperature display program///// { writetab(); //Write custom characters into CGRAM delay ( 5 ) ; lcd_pos ( 0x4e ) ; //display circle write_data( 0x00 ) ; //Display custom characters write_com ( 0x0c ); ReadTemperature(); write_com ( 0x80+0x40+10 ); write_data ( 0x30+temp/10 ); write_com ( 0x80+0x40+11 ); write_data ( 0x30+temp%10 ); write_com ( 0x80+0x40+13 ); write_data ( 0x30+xs%10 ); /////Time display address///// write_sfm( 0,shi ); write_sfm( 3,fen ); write_sfm( 6,miao ); // write_xq (11,xq ); } ////////////////////////////////////////////////////// ///////////// ////////////////////////////////////////////////////// ///////////// ////////////////////////////////////////////////////// ///////////// //////Temperature alarm program////// void baojing() { if(H==temp) { bi(500); } if(L==temp) { bi(500); } if((Z==shi)&(Y==fen)&(miao<10)) //Set the alarm to sound for 10 seconds. { { bi(500); } } } ////////////////////////////////////////////////////// ///////////// ////////////////////////////////////////////////////// ///////////// ////////////////////////////////////////////////////// ///////////// /***************Main function***************/ void main() { init(); while(1) { keycan(); wendu(); baojing(); } }
Previous article:ds18b20 temperature + digital tube display
Next article:51 MCU Series: Flashing Light
Recommended ReadingLatest update time:2024-11-16 23:35
- Popular Resources
- Popular amplifiers
- Single-chip microcomputer C language programming and simulation
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Principles and Applications of Single Chip Microcomputers and C51 Programming (3rd Edition) (Xie Weicheng, Yang Jiaguo)
- Teach you to learn 51 single chip microcomputer-C language version (Second Edition) (Song Xuefeng)
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
- Shenzhen becomes the "world's first 5G city": challenges and business opportunities for the PCB industry
- 【Ended】 R&S Live 【PCI Express Gen 3 Conformance Test】
- Live broadcast at 10 am today [Renesas Electronics Secure IoT Suite provides you with secure cloud connection solutions
- Xun developed Qt for Android for i.MX6ULL Terminator QT application
- Applications of RF Transformers
- RF FilterRF knowledge classics to understand
- 【NXP Rapid IoT Review】+Hello world!
- 5G miniaturized terminal and base station antenna technology
- DIY retro game console based on Raspberry Pi Zero
- Transistor static operating point