熱門: 51單片機(jī) | 24小時(shí)必答區(qū) | 單片機(jī)教程 | 單片機(jī)DIY制作 | STM32 | Cortex M3 | 模數(shù)電子 | 電子DIY制作 | 音響/功放 | 拆機(jī)樂(lè)園 | Arduino | 嵌入式OS | 程序設(shè)計(jì)
![]() |
發(fā)布時(shí)間: 2019-6-12 15:02
正文摘要:設(shè)計(jì)的是一種以酒精濃度傳感器、AT89C51單片機(jī)和A/D轉(zhuǎn)換器為主的,檢測(cè)駕駛員呼出氣體的酒精濃度的,而且還帶有聲光報(bào)警功能的酒精濃度檢測(cè)儀。這種檢測(cè)儀不僅可以檢測(cè)出空氣環(huán)境中酒精濃度值,還可以由不同的環(huán)境來(lái) ... |
有仿真圖嗎 |
這個(gè)原理圖不清晰呀,還有beep的端口是干啥用的呀 |
gagalonghun 發(fā)表于 2019-6-18 15:18 樓主負(fù)責(zé)又細(xì)心 |
是這個(gè),這個(gè)是正確的 #include <reg51.h> #include <intrins.h> #include <stdio.h> #define uint unsigned int #define uchar unsigned char /**********************定義全局變量***************/ //unsigned char dat=0x00 ; //AD值 unsigned char count = 0x00; //定時(shí)器計(jì)數(shù) unsigned char channel; //通道變量 //ADC0832的引腳 sbit ADCS =P1^2; //ADC0832 chip seclect sbit ADDI =P1^1; //ADC0832 k in sbit ADDO =P1^1; //ADC0832 k out sbit ADCLK =P1^0; //ADC0832 clock signal unsigned char dispbuf[3]; uint temp; uint alc;//酒精濃度 uchar dat; //獲取ADC轉(zhuǎn)換回來(lái)的值 uchar *aa="M=T.TTmg/L"; uchar *cc="alarm"; uchar *dd=" "; uint sheding=60; uchar bb[16]={'A','l','c','o','h','o','l','=',' ','.',' ',' ','m','g','/','L'}; sbit RS = P2^0; //定義端口 sbit RW = P2^1; sbit EN = P2^2; sbit beep=P3^7; sbit k1=P1^3;//+鍵 sbit k2=P1^4;//-鍵 #define RS_CLR RS=0 #define RS_SET RS=1 #define RW_CLR RW=0 #define RW_SET RW=1 #define EN_CLR EN=0 #define EN_SET EN=1 void DelayUs2x(unsigned char t); void DelayMs(unsigned char t); void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s); void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data); void init(); void write_com(unsigned char com); void write_data(unsigned char date); void DelayUs2x(unsigned char t) { while(--t); } void DelayMs(unsigned char t) { while(t--) { //大致延時(shí)1mS DelayUs2x(245); DelayUs2x(245); } } /*********************************lcd1602各子函數(shù)**************************************/ void write_com(unsigned char com) //寫(xiě)命令 { RS_CLR; RW_CLR; P0=com; DelayMs(5); EN_SET; DelayMs(5); EN_CLR; } void write_data(unsigned char date) //寫(xiě)一個(gè)字符 { RS_SET; RW_CLR; P0=date; DelayMs(5); EN_SET; DelayMs(5); EN_CLR; } void initlcd() //初始化 { write_com(0x38); write_com(0x0c); write_com(0x06); write_com(0x01); } /*------------------------------------------------ 寫(xiě)入字符串函數(shù) ------------------------------------------------*/ void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s) { if (y == 0) { write_com(0x80 + x); } else { write_com(0xC0 + x); } while (*s) { write_data( *s); s ++; } } /*------------------------------------------------ 寫(xiě)入字符函數(shù) ------------------------------------------------*/ void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data) { if (y == 0) { write_com(0x80 + x); } else { write_com(0xC0 + x); } write_data(Data); } /**************************************************************************** 函數(shù)功能:AD轉(zhuǎn)換子程序 入口參數(shù):CH 出口參數(shù):dat ****************************************************************************/ //采集并返回 unsigned char Adc0832(unsigned char channel) //AD轉(zhuǎn)換,返回結(jié)果 { uchar i=0; uchar j; uchar dat=0; uchar ndat=0; if(channel==0)channel=2; if(channel==1)channel=3; ADDI=1; _nop_(); _nop_(); ADCS=0;//拉低CS端 _nop_(); _nop_(); ADCLK=1;//拉高CLK端 _nop_(); _nop_(); ADCLK=0;//拉低CLK端,形成下降沿1 _nop_(); _nop_(); ADCLK=1;//拉高CLK端 ADDI=channel&0x1; _nop_(); _nop_(); ADCLK=0;//拉低CLK端,形成下降沿2 _nop_(); _nop_(); ADCLK=1;//拉高CLK端 ADDI=(channel>>1)&0x1; _nop_(); _nop_(); ADCLK=0;//拉低CLK端,形成下降沿3 ADDI=1;//控制命令結(jié)束 _nop_(); _nop_(); dat=0; for(i=0;i<8;i++) { dat|=ADDO;//收數(shù)據(jù) ADCLK=1; _nop_(); _nop_(); ADCLK=0;//形成一次時(shí)鐘脈沖 _nop_(); _nop_(); dat<<=1; if(i==7)dat|=ADDO; } for(i=0;i<8;i++) { j=0; j=j|ADDO;//收數(shù)據(jù) ADCLK=1; _nop_(); _nop_(); ADCLK=0;//形成一次時(shí)鐘脈沖 _nop_(); _nop_(); j=j<<7; ndat=ndat|j; if(i<7)ndat>>=1; } ADCS=1;//拉低CS端 ADCLK=0;//拉低CLK端 ADDO=1;//拉高數(shù)據(jù)端,回到初始狀態(tài) dat<<=8; dat|=ndat; return(dat);//return ad k } /**************************************************************************** 函數(shù)功能:定時(shí)器中斷延時(shí)程序 這一段的作用時(shí)隔一段時(shí)間抽樣一次 否側(cè)顯示的最后一位會(huì)不穩(wěn)定 入口參數(shù): 出口參數(shù): ****************************************************************************/ void timer0(void) interrupt 1 { TMOD = 0x01; TH0 = 0x00; TL0 = 0x00; IE = 0x82; TR0 = 01; count++; if (count == 0x0A) { count = 0x00; dat= Adc0832(channel); } } /*void format_data(uint dat) { uint change_dat=dat; uchar ptr[3]; change_dat=(change_dat<<8)+(change_dat<<7)+(change_dat<<6)+(change_dat<<5)+(change_dat<<4)+(change_dat<<2);//乘500 change_dat=change_dat>>8;//除以256 ptr[0]=change_dat/100; //得到個(gè)位數(shù)字 ptr[1]=(change_dat-(100*ptr[0]))/10; //得到十分位數(shù)字 ptr[2]=change_dat-(100*ptr[0])-(10*ptr[1]); //得到百分位數(shù)字 }*/ void keyscan() { if(!k1) { DelayMs(5); if(!k1) { while(!k1); sheding+=5; LCD_Write_Char(2,1,sheding/100+'0'); LCD_Write_Char(4,1,sheding%100/10+'0'); LCD_Write_Char(5,1,sheding%10+'0'); } } if(!k2) { DelayMs(5); if(!k2) { while(!k2); sheding-=5; LCD_Write_Char(2,1,sheding/100+'0'); LCD_Write_Char(4,1,sheding%100/10+'0'); LCD_Write_Char(5,1,sheding%10+'0'); } } } /**************************************** 函數(shù)功能:主程序 入口參數(shù): 出口參數(shù): ****************************************/ void main(void) { uchar i; P2=0xff; //端口初始化 P0=0xff; P3=0xff; channel=0x00; //channel=0 TMOD = 0x01; TH0 = 0x00; TL0 = 0x00; IE = 0x82; TR0 = 01; initlcd(); LCD_Write_String(0,0,bb); LCD_Write_String(0,1,aa); //LCD_Write_String(11,1,cc); LCD_Write_Char(2,1,sheding/100+'0'); LCD_Write_Char(4,1,sheding%100/10+'0'); LCD_Write_Char(5,1,sheding%10+'0'); while(1) { temp=dat*1.0/255*500; //電壓值轉(zhuǎn)換,5V做為參考電壓,分成256份。 if(temp<187) { alc=100*(temp-8)/895; } else { alc=100*(temp-133)/272; } dispbuf[0]=alc%10+'0'; //個(gè)位 dispbuf[1]=alc/10%10+'0'; //十位 dispbuf[2]=alc/100%10+'0'; //百位 //dispbuf[0]=temp%10+'0'; //個(gè)位 //dispbuf[1]=temp/10%10+'0'; //十位 //dispbuf[2]=temp/100%10+'0'; //百位 LCD_Write_Char(8,0,dispbuf[2]); LCD_Write_Char(10,0,dispbuf[1]); LCD_Write_Char(11,0,dispbuf[0]); keyscan(); if(alc>sheding) { //beep=0; for(i=0;i<10;i--) { beep=0; DelayMs(1); beep=1; } LCD_Write_String(11,1,cc); } else { LCD_Write_String(11,1,dd); } } } |
錯(cuò)誤較多,我發(fā)源碼給你 #include<reg51.h> #include"delay.h" #include"lcd602.h" #include"ds18b20.h" //**************端口定義******************* sbit LED = P1^0; sbit k1 = P1^4; sbit k2 = P1^5; sbit k3 = P1^6; //**************數(shù)組定義******************* uchar code tab2[]={"Temp: "}; //5 uchar code tab3[]={" H "}; //3 uchar code tab4[]={"L "}; //8 //**************變量定義******************* int temp1 = 400; //上限 int temp3 = 100; //下限 bit flag = 0; //上下限選擇標(biāo)志 int temp4; //當(dāng)上限減到負(fù)數(shù)時(shí) uchar temp5 = 0; /*******************按鍵********************/ void key() { if(flag == 0) //上限 { write_zifu(2,0,'*'); write_zifu(2,8,' '); if(k1 == 0) { delayxms(5); while(k1 == 0); temp1 += 10; } if(k2 == 0) { delayxms(5); while(k2 == 0); temp1 -= 10; } } if(flag == 1) //下限 { write_zifu(2,0,' '); write_zifu(2,8,'*'); if(k1 == 0) { delayxms(5); while(k1 == 0); temp3 += 10; } if(k2 == 0) { delayxms(5); while(k2 == 0); temp3 -= 10; } } if(k3 == 0) //上下限選擇標(biāo)志 { delayxms(5); while(k3 == 0); flag = ~flag; } } /*******************定時(shí)器T1初始化********************/ void T1_init() { TMOD = TMOD | 0x10; //設(shè)T1為方式1 TH1 = (65535-20000)/256; //計(jì)數(shù)20000個(gè) TL1 = (65535-20000)%256; EA = 1; //開(kāi)啟總中斷 ET1 = 1; //允許T1中斷 TR1 = 1; //啟動(dòng) } /*******************主函數(shù)********************/ void main() { chushihua();//lcd1602初始化 write_string(1,0,tab2); write_string(2,0,tab3); write_string(2,9,tab4); T1_init(); while(1) { key(); ds1820disp(); /*溫度上限*/ if(temp1 < 0) { write_zifu(2,2,'-'); temp4 = temp1; temp4 = ~temp4; temp4 += 1; write_num3(2,3,temp4); write_zifu(2,7,' '); } else { write_zifu(2,2,'+'); if(temp1>=1000) { write_num4(2,3,temp1); } else { write_num3(2,3,temp1); } } /*溫度下限*/ if(temp3 < 0) { write_zifu(2,10,'-'); write_num3(2,11,-(temp3)); write_zifu(2,15,' '); } else { write_zifu(2,10,'+'); if(temp3>=1000) {write_num4(2,11,temp3);} else {write_num3(2,11,temp3);} } /*報(bào)警*/ if(flag_temp == 1) //溫度小于零 { if((-t)>temp1 || (-t)<temp3) {LED = 0;} else {LED = 1;} } else //溫度大于零 { if(t>temp1 || t<temp3) {LED = 0;} else {LED = 1;} } } } /**************************定時(shí)器T1中斷服務(wù)函數(shù)************************************/ void T1_time() interrupt 3 { TH1 = (65535-20000)/256; //計(jì)數(shù)20000個(gè) TL1 = (65535-20000)%256; temp5 += 1; if(temp5 >= 25) { temp5 = 0; TR1 = 0; ReadTemperature(); TR1 = 1; } } |
第15行的unsigned chardispbuf[3];改為unsigned char dispbuf[3];//char后打一個(gè)空格就行了 |
怎么改呢 |
MAIN.C(16): error C129: missing ';' before 'temp' |
Powered by 單片機(jī)教程網(wǎng)