看該模塊的功能,不僅同時(shí)具備模數(shù)和數(shù)模轉(zhuǎn)換功能,比單一的模數(shù)或數(shù)模轉(zhuǎn)換功能強(qiáng)大,而且還能精確測(cè)光照強(qiáng)度和溫度,可謂功能豐富,實(shí)在令人欣喜,怎么多功能怎么利用?我是從基本的模數(shù)和數(shù)模轉(zhuǎn)換入手,然后再嘗試光照和溫度的測(cè)定。
在完成基本的模數(shù)和數(shù)模轉(zhuǎn)換程序后,將輸出與輸入通道0用杜邦線短接,然后輸出一個(gè)數(shù)值的電壓,再通過(guò)通道0測(cè)量電壓值,與輸出的電壓值接近相等,證明模數(shù)數(shù)模轉(zhuǎn)換功能正確實(shí)現(xiàn),通過(guò)斷開、連接P4、P5、P6再對(duì)四個(gè)輸入通道的數(shù)據(jù)變化情況進(jìn)行測(cè)試,發(fā)現(xiàn)P6對(duì)應(yīng)通道0,P5對(duì)應(yīng)通道1,P4對(duì)應(yīng)通道2。同時(shí)發(fā)現(xiàn),不需要用杜邦線把輸出端與輸入端短接,只要向輸出端輸出一個(gè)數(shù)值的電壓,四個(gè)輸入端都自行得到這個(gè)輸出端的電壓。
下面是本人的實(shí)驗(yàn)單片機(jī)程序,
- #include <reg52.h>
- #include "codetab.h"
- #include "LQ12864.h"
- #define uchar unsigned char
- #define uint unsigned int
- sbit sda=P2^6;
- sbit scl=P2^7;
- void delayms(int z){int x,y;for(x=z;x>0;x--)for(y=110;y>0;y--);}
- //-------------------------------------------------------
- void start(){sda=1;scl=1;sda=0;}
- void stop(){sda=0;scl=1;sda=1;}
- void respons(){int i;scl=1;while((sda==1)&&(i<250))i++;scl=0;}
- void init(){sda=1;scl=1;}
- void write_byte(uchar dat)
- {uchar i,temp;
- temp=dat;
- for(i=0;i<8;i++){temp=temp<<1;scl=0;sda=CY;scl=1;}
- scl=0;sda=1;
- }
- //--------------------------------------------------------------------------------
- uchar read_byte()
- {uchar i,k;
- scl=0;sda=1;
- for(i=0;i<8;i++){scl=1;k=(k<<1)|sda;scl=0;}
- return k;
- }
- //##################################################################
- void write_data(uchar chip,uchar address,uchar dat)
- {start();
- write_byte(chip);respons();write_byte(address);respons();
- write_byte(dat);respons();stop();
- }
- //----------------------------------------------
- void SetChanel(uchar chip,uchar ch)
- {start();
- write_byte(chip);respons();
- write_byte(ch);respons();
- stop();
- }
- //----------------------------------------------
- uchar read_data(uchar chip)
- {uchar dat;
- start(); write_byte(chip+1);respons();dat=read_byte();stop();
- return dat;
- }
- //==================================================================
- Putint(int x,int y,unsigned char ci)
- {unsigned char str[3];
- if((ci/16)<10)str[0]=(ci/16)+0x30;else str[0]=(ci/16)+0x37;
- if((ci%16)<10)str[1]=(ci%16)+0x30;else str[1]=(ci%16)+0x37;
- OLED_P8x16Str(x,y,str);
- }
- Showchandata(char chan,char x,char y,char *str)
- {unsigned char c;
- SetChanel(0x90,chan);
- c=read_data(0x90);
- OLED_P8x16Str(x,y,str);
- Putint(x+32,y,c);
- }
- //######################################################################
- main()
- {int i,j;
- uchar c;
- char str0[]="Begin ";;
- init();
- OLED_Init();write_data(0x90,0x40,0x99);
- for(i=0;i<50;i++)
- {str0[5]=i/10+0x30;str0[6]=i%10+0x30;OLED_P8x16Str(32,0,str0);
- Showchandata(0x40,2,2,"0P6= ");//通道0,0-5V可調(diào)電壓接入電路,通過(guò)P6接入
- Showchandata(0x41,64,2,"1P5= ");//通道1,光敏電阻,通過(guò)P5接入
- Showchandata(0x42,2,4,"2P4= ");//通道2,熱敏電阻,通過(guò)P4接入
- Showchandata(0x43,64,4,"ch3= ");//通道3,輸入電壓,反映輸出端的電壓高低。
- delayms(500);
- }
- OLED_P8x16Str(32,0,"Test...");i=0;j=0;
- while(1)
- {write_data(0x90,0x40+i%4,i);
- Showchandata(0x40,2,2,"0P6= ");//通道0,0-5V可調(diào)電壓接入電路,通過(guò)P6接入
- Showchandata(0x41,64,2,"1P5= ");//通道1,光敏電阻,通過(guò)P5接入
- Showchandata(0x42,2,4,"2P4= ");//通道2,熱敏電阻,通過(guò)P4接入
- Showchandata(0x43,64,4,"ch3= ");//通道3,輸入電壓,反映輸出端的電壓高低。
- i+=5;if(i>=255)i=0;j++;if(j>15)j=0;delayms(500);
- };
- }
復(fù)制代碼
程序分兩個(gè)過(guò)程,第一個(gè)過(guò)程先對(duì)四個(gè)輸入端進(jìn)行采樣測(cè)量,然后數(shù)值顯示出來(lái),為了方便顯示,本次實(shí)驗(yàn)用OLED 128x64顯示器,每隔1秒測(cè)試一遍,第二個(gè)過(guò)程,先向輸出端口輸出0,再測(cè)試四個(gè)通道的數(shù)值,然后將輸出端口的輸出電壓遞增5個(gè)整數(shù)值,然后再測(cè)量四個(gè)通道的數(shù)值。從實(shí)驗(yàn)中可看到,通道0反映電位器的狀態(tài),整個(gè)過(guò)程數(shù)值沒變,通道1反映實(shí)驗(yàn)時(shí)的光照,數(shù)值隨光照不同而改變,通道2數(shù)值始終是0xff,按設(shè)備說(shuō)明應(yīng)是反映環(huán)境溫度,但不知是熱敏電阻壞還是本人的程序有哪里出錯(cuò),不管環(huán)境溫度多少,通道2數(shù)值都不變,用手摸熱敏電阻,通道2數(shù)值也不變,看來(lái)測(cè)溫度的功能是沒實(shí)現(xiàn)啦,不知原因在哪,希望懂的人指教。
下面是程序運(yùn)行實(shí)況:
b6.jpg (158.38 KB, 下載次數(shù): 72)
下載附件
2019-7-20 07:42 上傳
b12.jpg (149.79 KB, 下載次數(shù): 76)
下載附件
2019-7-20 07:43 上傳
b28.jpg (131.35 KB, 下載次數(shù): 83)
下載附件
2019-7-20 07:43 上傳
b35.jpg (125.68 KB, 下載次數(shù): 72)
下載附件
2019-7-20 07:43 上傳
第一階段,對(duì)四個(gè)通道采樣測(cè)量50次,數(shù)值基本不變。
t2.jpg (163.53 KB, 下載次數(shù): 65)
下載附件
2019-7-20 07:47 上傳
t3.jpg (159.35 KB, 下載次數(shù): 69)
下載附件
2019-7-20 07:47 上傳
第二階段,以5為間隔遞增輸出,通道3數(shù)值不斷改變,其他通道基本不變。