adc0809溫度
單片機(jī)源程序如下:
- #include "STC15F2K60S2.H"
- sbit SEL0=P2^0; //定義引腳
- sbit SEL1=P2^1;
- sbit SEL2=P2^2;
- sbit SEL3=P2^3;
- sbit beep=P3^4; //蜂鳴器引腳
- sbit s1=P4^1; //步進(jìn)電機(jī)IO口
- sbit s2=P4^2;
- sbit s3=P4^3;
- sbit s4=P4^4;
- sbit key=P3^2; //開(kāi)關(guān)按鍵
- #define uint unsigned int //宏定義
- #define ulint unsigned long
- uint value=0x00; //LED顯示
- uint count=0;
- uint i=1; //步進(jìn)電機(jī)
- bit flag=0; //開(kāi)關(guān)
- uint time=0; //延時(shí)
- uint t=0; //執(zhí)行溫度的次數(shù)
- ulint sumt=0; //溫度AD值的總和
- uint date_temp;
- int temp=0; //溫度值
- uint abs_temp; //溫度絕對(duì)值
- //設(shè)置用于顯示溫度的三個(gè)變量
- uint temp_bai=0; //百位
- uint temp_shi=0; //十位
- uint temp_ge =0; //個(gè)位
- //數(shù)碼管上顯示0-F
- char segtable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
- //溫度值對(duì)應(yīng)表
- int code tempdata[]={239,197,175,160,150,142,135,129,124,120,116,113,109,107,104,101,
- 99, 97, 95, 93, 91, 90, 88, 86, 85, 84, 82, 81, 80, 78, 77, 76,
- 75, 74, 73, 72, 71, 70, 69, 68, 67, 67, 66, 65, 64, 63, 63, 62,
- 61, 61, 60, 59, 58, 58, 57, 57, 56, 55, 55, 54, 54, 53, 52, 52,
- 51, 51, 50, 50, 49, 49, 48, 48, 47, 47, 46, 46, 45, 45, 44, 44,
- 43, 43, 42, 42, 41, 41, 41, 40, 40, 39, 39, 38, 38, 38, 37, 37,
- 36, 36, 36, 35, 35, 34, 34, 34, 33, 33, 32, 32, 32, 31, 31, 31,
- 30, 30, 29, 29, 29, 28, 28, 28, 27, 27, 27, 26, 26, 26, 25, 25,
- 24, 24, 24, 23, 23, 23, 22, 22, 22, 21, 21, 21, 20, 20, 20, 19,
- 19, 19, 18, 18, 18, 17, 17, 16, 16, 16, 15, 15, 15, 14, 14, 14,
- 13, 13, 13, 12, 12, 12, 11, 11, 11, 10, 10, 9, 9, 9, 8, 8, 8, 7,
- 7, 7, 6, 6,5, 5, 54,4, 3, 3,3, 2, 2, 1, 1, 1, 0, 0, -1, -1, -1,
- -2, -2, -3, -3, -4, -4, -5, -5, -6, -6, -7, -7, -8, -8, -9, -9,
- -10, -10, -11, -11, -12, -13, -13, -14, -14, -15, -16, -16, -17,
- -18, -19, -19, -20, -21, -22, -23, -24, -25, -26, -27, -28, -29,
- -30, -32, -33, -35, -36, -38, -40, -43, -46, -50, -55, -63, 361};
- void Delay(int n); //延時(shí)函數(shù)
- void weixuan(char i); //數(shù)碼管位的選擇
- void SEG_Display(); //顯示溫度
- void InitADC_temp(); //初始化溫度ADC
- void date_processtemp();
- void Delay(int n) //延時(shí)函數(shù)
- {
- while(n--);
- }
- void weixuan(char i) //數(shù)碼管位的選擇
- {
- SEL2=i/4;
- SEL1=i%4/2;
- SEL0=i%2;
- }
- void SEG_Display() //顯示溫度
- {
- //用于設(shè)置溫度的位數(shù)
- SEL3=0; //數(shù)碼管點(diǎn)亮
- if(temp<0)
- {
- P0=0;
- weixuan(5); //若溫度為負(fù),則首位顯示‘-‘
- P0=0x40;
- Delay(500);
- }
- else
- {
- P0=0;
- weixuan(5);
- P0=segtable[temp_bai];
- Delay(500);
- }
-
- P0=0;
- weixuan(6);
- P0=segtable[temp_shi];
- Delay(500);
-
- P0=0;
- weixuan(7);
- P0=segtable[temp_ge];
- Delay(500);
-
- if(key==0)
- {
- Delay(15000); //延時(shí)10ms,按鍵消抖
- if(key==0)
- {
- while(key==0);
- flag=~flag;
- }
- }
-
- if(temp>30&&flag==0)
- {
- beep=~beep;
-
- switch(i++) //控制步進(jìn)電機(jī)不同IO口脈沖電平
- {
- case 1: s1=1;s2=0;s3=0;s4=0;break;
- case 2: s1=0;s2=1;s3=0;s4=0;break;
- case 3: s1=0;s2=0;s3=1;s4=0;break;
- case 4: s1=0;s2=0;s3=0;s4=1;break;
- }
- if(i==5)
- i=1;
-
- count++;
- SEL3=1; //LED燈點(diǎn)亮
- P0=value; //LED發(fā)光
- Delay(500);
- if(count==30)
- {
- value=~value;
- count=0;
- }
- }
- }
- void T_L()
- {
- P0M1=0x00; //設(shè)置P0為推挽模式,點(diǎn)亮數(shù)碼管
- P0M0=0xff;
- P2M1=0x00;
- P2M0=0x08; //將P2^3設(shè)置為推挽模式,其余為準(zhǔn)雙向口模式
- P3M1=0x00;
- P3M0=0x10; //設(shè)置P3^4為推挽模式
- SEL3=0; //熄滅LED燈
-
- IE=0xa8; //EA=1打開(kāi)總中斷,EADC=1允許A/D轉(zhuǎn)化中斷,ET1=1允許T1中斷
- TMOD=0x10; //使用定時(shí)器1,16位不可重裝載模式,TH1、TL1全用
- TH1=(65535-40000)/256;//高8位賦初值,定時(shí)40000周期
- TL1=(65535-40000)%256;//低8位賦初值
- TR1=1; //啟動(dòng)定時(shí)器1
- beep=0;
-
- while(1)
- {
- SEG_Display();
- }
- }
- void InitADC_temp() //初始化溫度ADC
- {
- P1ASF=0xff; //將P1口作為模擬功能A/D使用
- ADC_RES=0; //寄存器ADC_RES和ADC_RESL保存A/D轉(zhuǎn)化結(jié)果
- ADC_RESL=0; //初始賦值0
- ADC_CONTR=0x8b; //ADC_POWER=1打開(kāi)A/D轉(zhuǎn)換器電源;ADC_START=1啟動(dòng)模擬轉(zhuǎn)換器ADC;CHS=011選擇P1^3作為A/D輸入使用
- CLK_DIV=0x20; //ADRJ=1:ADC_RES[1:0]存放高2位ADC結(jié)果,ADC_RESL[7:0]存放低8位ADC結(jié)果
- }
- //分別取出溫度的百位、十位、個(gè)位
- void date_processtemp()
- {
- if(temp<0)
- abs_temp=-temp;
- else
- abs_temp=temp;
- temp_bai=abs_temp%1000/100;
- temp_shi=abs_temp%100/10;
- temp_ge =abs_temp%10;
- }
- void Timer1_Routine() interrupt 3 //3為定時(shí)器1中斷編號(hào)
- {
- IE=0x00; //關(guān)閉總中斷
- TR1=0; //定時(shí)器1停止
- TH1=(65535-40000)/256;//重新賦值
- TL1=(65535-40000)%256;
- InitADC_temp(); //初始化溫度
- IE=0xa8; //打開(kāi)總中斷
- TR1=1; //啟動(dòng)定時(shí)器1
- }
- //AD中斷
- void adc_isr() interrupt 5 using 1
- {
- time++;
- ……………………
- …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
溫度.rar
(42.82 KB, 下載次數(shù): 28)
2017-6-3 16:23 上傳
點(diǎn)擊文件名下載附件
|