51 single-Chip microcomputer course design: alcohol concentration alarm based on MQ-3
This program is used to convert the analog signal read on the MQ-3 to the corresponding digital signal, after 51 single-chip microcomputer processing, in the digital tube display, at the same time has the alarm function, when the detection value is higher than the warning value, buzzer alarm. In addition to the detection of MQ-3 alcohol concentration module ad value, but also applicable to the MQ series of other modules, the principle is the same, are read to the ad value converted to digital signal, the program modified, if the wiring method is correct, can be in Jilin Agricultural University 51 Development Board perfect operation, related engineering documents see the bottom of the annex.
/************************************* Description *********************************** This program only collects the module signal size needs to be converted to the corresponding value Also need to convert formula connection way see below definition about wiring mq7 connect Power then AO Connect to school board a/d analog signal input This routine can be used for MQ series modules, Convert the corresponding analog signal to the corresponding digital signal display (for Ginon 51 Development Board) 2016-12-29---------------- -------sworld****************************************************************************/#include < reg52.h> //header File #include <intrins.h> #define uchar unsigned char //macros define unsigned character types #define uint unsigned int //macros define unsigned integer #define gpio_dig p0//and Segment-selected data output port definitions Unsigned char code dig_code[10]={ 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //Display segment Code Digital tube Word with unsigned The char displaydata[8];//is used to store the value of the 8 digits to be displayed sbit cs=p3^5;//the adcs adrd of the single-chip microcomputer of the school adwr sbit rd=p3^6;sbit wr=p3^7;sbit clk=p3^3; //Clock Pulse sbit duc=p2^2;// duc signal sbit wec=p2^3;//wec bit selection signal sbit bell= p3^4;//connecting buzzer sbit setwarn=p2^7; //determine sbit add=p2^6;//value increase sbit sub=p2^5;// Numeric reduction uint z,x,c,v,ad0809,date,ok;//define data type uint num; //default alarm value/******************* Letter Number name : delay10ms* function function : delay function, delay 10ms* input : no * out : No ****************************************************************** /void delay10ms (unsigned int c) //error 0us{ unsigned char a, b;//--c has already been assigned at the time of delivery, so the first sentence of the For statement is not assigned to a value--// for (; c>0;c--) { for (b=38;b>0; b--) { for (a=130;a>0;a--); } } }unsigned char key_scan () { unsigned char keyvalue = 0 , i; //Save key value //--Detect button 1--// if (setwarn != 1)//Detect button K1 whether to press { delay10ms (1);//Remove jitter if (setwarn != 1)//Check again if the button is pressed { keyvalue = 3; i = 0; while ((I<50) && (setwarn != 1)) //detect if the button is released { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;DELAY10MS (1); i++; } }}//--detection Key 1--//if (add != 1)//Detect button K1 whether to press {delay10ms (1);//Remove Jitter if ( add!= 1)//Check again if the button is pressed {keyvalue = 1;i = 0;while (i<50) && ( add != 1)) //detect if the button is released {delay10ms (1); i++;}}} --detection key 1--//if (sub != 1)//Detection button K1 whether to press {delay10ms (1);//Remove jitter if (sub!= 1)//Check again if the button is pressed {keyvalue = 2;i = 0;while ((i<50) && (sub != 1)) //detect if the button is released {delay10ms (1); i++;}}} return keyvalue; //reads the value of the key value back to}/********************************************************* Letter Number name : digdisplay* function : Use digital tube display * input : no * out : No *******************************************************************************/void digdisplay () {unsigned char i;unsigned int j;for (i=0;i<8;i++) {WEC=1;DUC=0;switch (i ) //, select the lit digital tube, {case (0): gpio_dig=0x7f; break;//shows the No. 0 case (1): gpio_dig=0xbf; break;//shows the 1th case (2 ): gpio_dig=0xdf; break;//Display 2nd case (3): gpio_dig=0xef; break;//display 3rd bit CASE (4): gpio_dig=0xf7; break;//Show 4th case (5): gpio_dig=0xfb; break;//Show 5th case (6):gpio_dig=0xfd; break;//shows the 6th case (7): gpio_dig=0xfe; break;//shows the 7th bit}wec=0;duc=1; gpio_dig=displaydata[i];//Send segment Code j=15; //scan interval Time setting while (j--); gpio_dig=0x00;//Hidden}}/********************************************************************** Digital Tube Dynamic scanning ************************* /void xianshi () //display function { if (ad0809>250) ad0809=250 ;d Ate=ad0809;/******************************data to read the ad value, through the conversion formula, you can convert data to the corresponding alcohol concentration to show that you need to check the data to modify **************** /if (date>num) {bell=0;} else{bell=1;} uint z,x,c,v; z=date/1000; //ask thousand x=date%1000/100; //to beg hundred c= date%100/10; //10-bit v=date%10; //displaydata[6]=dig_code[z];D isplaydata[5]=dig_code[x] ;D Isplaydata[4]=dig_code[c];D isplaydata[3]=dig_CODE[V];D igdisplay (); } /******************************************************************** &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CLK Oscillation Signal ************* /void timer0 ( ) interrupt 1 //Timer 0 Working mode 1{th0= (65536-2)/256; //reload count initial value tl0= (65536-2)%256; //reload count initial value clk=! clk; //take anti-} /************************************************************************* Main function ***************************** /void main () { uint xx=0;num=100; tmod=0x01; //timer interrupt 0bell=1; clk=0; //pulse Signal Initial value is 0th0= (65536-2)/256; //timing time high eight-bit initial value tl0= (65536-2)%256; //timing Time Low eight-bit initial value ea=1; //Open CPU Interrupt et0=1; //on T/C0 interrupt Tr0=1;cs=0;ok=0;while (1) {if (ok==1) //infinite loop {if ( XX==20) {wr=0; _nop_ ();wr=1; _nop_ (); _nop_ (); Rd=0; _nop_ (); ad0809=p1; //read data rd=1; xx=0;} else{xx++;} Xianshi ();//Digital tube Display function}else//The following sets the alarm value code {switch (Key_scan ()) {Case 3:ok=1;break;case 1:num-=2;break;case 2:num+=2;break;} z=num/1000; //seek thousands x=num%1000/100; //hundred c=num%100/10; //ask for 10 bit v=num%10; //seek digit Displaydata[6]=dig _CODE[Z];D isplaydata[5]=dig_code[x];D isplaydata[4]=dig_code[c];D isplaydata[3]=dig_code[v];D igdisplay ();}}}
This article is from the "World is the same" blog, please be sure to keep this source http://970076933.blog.51cto.com/9767314/1887487
51 single-Chip microcomputer course design: alcohol concentration alarm based on MQ-3