- /**************************************************************************************
- * 溫度傳感器實(shí)驗(yàn) *
- 實(shí)現(xiàn)現(xiàn)象: 具體接線操作請看視頻。
- 下載程序后,在溫度傳感器接口處,按照絲印方向插好溫度傳感器,數(shù)碼管就會顯示
- 檢測的溫度值,
- 注意事項(xiàng):
- ***************************************************************************************/
- #include <reg52.h> //此文件中定義了單片機(jī)的一些特殊功能寄存器
- #include <stdio.h>
- #include <eeprom.h>
- typedef unsigned int u16; //對數(shù)據(jù)類型進(jìn)行聲明定義
- typedef unsigned char u8;
- #define WRITE24C02 0xA0 //10100000,器件地址,寫操作
- #define READ24C02 0xA1 //10100001,器件地址,讀操作
- sbit LSA=P2^2;
- sbit LSB=P2^3;
- sbit LSC=P2^4;
- sbit key1=P1^0;
- sbit key2=P1^1;
- sbit zongkong=P2^7;
- sbit output=P2^6;
- sbit SO=P3^2;
- sbit CS=P3^0;
- sbit SCK=P3^1;
- char num=0;
- u8 xw[8],sdwd,a=101,sjwd;
- u8 code smgduan[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- void display0 ( te );
- void display1(h);
- #define uchar unsigned char
- #define uint unsigned int //--------------熱電偶數(shù)字轉(zhuǎn)換器MAX6675
- /*******************************************************************************
- * 函 數(shù) 名 : delay_ms(uchar ntimes)
- * 函數(shù)功能 :
- * 輸 入 : temp
- * 輸 出 : 無
- *******************************************************************************/
- void delay(u16 i)
- {
- while(i--);
- }
- /********************************
- //讀取MAX6675
- *********************************/
- int Read_6675()
- { uchar i;
- uint dat_temp;
- uint dat_6675;
- i=0;
- dat_temp=0;
- dat_6675=0;
- //ready
- CS=0;
- //delay_us(10);
- SCK=0;
- //delay_us(10);
- //get D15-D0 from 6675
- for(i=0;i<16;i++)
- {
- SCK=1;
- dat_temp=dat_temp<<1;
- if(SO==1)
- dat_temp=dat_temp|0x01;
- SCK=0;
- //delay_us(10);
- }
- CS=1;
- dat_temp=dat_temp<<1;
- dat_temp=dat_temp>>4;
- dat_temp=dat_temp/4;
- dat_6675 = dat_temp;
- return dat_6675;
- }
- /*******************************************************************************
- * 函 數(shù) 名 : datapros()
- * 函數(shù)功能 : 溫度讀取處理轉(zhuǎn)換函數(shù)
- * 輸 入 : temp
- * 輸 出 : 無
- *******************************************************************************/
- void datapros(int temp)
- {
- float tp;
- if(temp< 0) //當(dāng)溫度值為負(fù)數(shù)
- {
- xw[2] = 0x40; // -
- //因?yàn)樽x取的溫度是實(shí)際溫度的補(bǔ)碼,所以減1,再取反求出原碼
- temp=temp-1;
- temp=~temp;
- tp=temp;
- temp=tp*0.0025*100+0.5;
- //留兩個(gè)小數(shù)點(diǎn)就*100,+0.5是四舍五入,因?yàn)镃語言浮點(diǎn)數(shù)轉(zhuǎn)換為整型的時(shí)候把小數(shù)點(diǎn)
- //后面的數(shù)自動去掉,不管是否大于0.5,而+0.5之后大于0.5的就是進(jìn)1了,小于0.5的就
- //算加上0.5,還是在小數(shù)點(diǎn)后面。
- }
- else
- {
- xw[0] = 0x00;
- tp=temp;//因?yàn)閿?shù)據(jù)處理有小數(shù)點(diǎn)所以將溫度賦給一個(gè)浮點(diǎn)型變量
- //如果溫度是正的那么,那么正數(shù)的原碼就是補(bǔ)碼它本身
- temp=tp*0.0025*100+0.5;
- //留兩個(gè)小數(shù)點(diǎn)就*100,+0.5是四舍五入,因?yàn)镃語言浮點(diǎn)數(shù)轉(zhuǎn)換為整型的時(shí)候把小數(shù)點(diǎn)
- //后面的數(shù)自動去掉,不管是否大于0.5,而+0.5之后大于0.5的就是進(jìn)1了,小于0.5的就
- //算加上0.5,還是在小數(shù)點(diǎn)后面。
- }
- xw[4] = smgduan[temp % 10000 / 1000];
- xw[5] = smgduan[temp % 1000 / 100] | 0x80;
- xw[6] = smgduan[temp % 100 / 10];
- xw[7] = smgduan[temp % 10];
- sjwd=temp % 10000 / 1000*10+temp % 1000 / 100;
- }
- /*******************************************************************************
- * 函數(shù)名 :DigDisplay()
- * 函數(shù)功能 :數(shù)碼管顯示函數(shù)
- * 輸入 : 無
- * 輸出 : 無
- *******************************************************************************/
- void DDP()
- {
- u8 i;
- for(i=0;i<8;i++)
- {
- switch(i) //位選,選擇點(diǎn)亮的數(shù)碼管,
- {
- case(0):
- LSA=0;LSB=0;LSC=0; break;//顯示第0位
- case(1):
- LSA=1;LSB=0;LSC=0; break;//顯示第1位
- case(2):
- LSA=0;LSB=1;LSC=0; break;//顯示第2位
- case(3):
- LSA=1;LSB=1;LSC=0; break;//顯示第3位
- case(4):
- LSA=0;LSB=0;LSC=1; break;//顯示第4位
- case(5):
- LSA=1;LSB=0;LSC=1; break;//顯示第5位
- case(6):
- LSA=0;LSB=1;LSC=1; break;//顯示第6位
- case(7):
- LSA=1;LSB=1;LSC=1; break;//顯示第7位
- }
- P0=xw[i];//發(fā)送數(shù)據(jù)
- delay(200);
- P0=0x00;//消隱
- }
- }
- /*******************************************************************************
- * 函 數(shù) 名 : Initialization
- * 函數(shù)功能 : 初始化函數(shù)
- * 輸 入 : 無
- * 輸 出 : 無
- *******************************************************************************/
- void Initialization()
- {
- uchar i=0;
- P0=0x00;
- P1=0x00;
- P2=0XFF;
- P3=0XFF;
- sdwd=30;
- for(i=0;i++;i<8)
- {
- xw[i]=0x08;
- }
- readeeprom();
- datapros(Read_6675() );
- }
- /*******************************************************************************
- * 函 數(shù) 名 : main
- * 函數(shù)功能 : 主函數(shù)
- * 輸 入 : 無
- * 輸 出 : 無
- *******************************************************************************/
- void main()
- {
- Initialization();
- delay(50);
- while(1)
- {
- if(zongkong==1)
- {
- while(a>100)
- {
- a=0;
- datapros(Read_6675() );
-
- }
- a++;
- xw[0]=smgduan[sdwd/10%10];
- xw[1]=smgduan[sdwd%10];
- xw[2]=0x08;
- xw[3]=0x08;
- DDP();
- if(key1==1)
- {
- while(key1==1)DDP();
- sdwd++;
- if(sdwd>50)sdwd=50;
- }
- if(key2==1)
- {
- while(key2==1)DDP();
- sdwd--;
- if(sdwd<10)sdwd=10;
- }
- if(sdwd>sjwd+2)
- {
- output=0;
- }
- if(sdwd<sjwd+1)
- {
- output=1;
- }
- }
- if(zongkong==0)
- {
- while(a>100)
- {
- a=0;
- datapros(Read_6675() );
-
- }
- a++;
- xw[0]=smgduan[sdwd/10%10];
- xw[1]=smgduan[sdwd%10];
- xw[2]=0x08;
- xw[3]=0x08;
- DDP();
- if(key1==1)
- {
- while(key1==1)DDP();
- sdwd++;
- if(sdwd>50)sdwd=50;
- }
- if(key2==1)
- {
- while(key2==1)DDP();
- sdwd--;
- if(sdwd<10)sdwd=10;
- }
- output=0;
- }
- }
- }
- #include "reg52.h" //包含頭文件
- #include"intrins.h" //_nop_();延時(shí)函數(shù)用
- #include<eeprom.h>
- #define uchar unsigned char
- #define uint unsigned int
- void Delay1ms(uint y)
- {
- uint x;
- for( ; y>0; y--)
- {
- for(x=110; x>0; x--);
- }
- }
- //**************************************************************************************************
- //啟動(SCL為高,SDA由高變?yōu)榈褪且粋(gè)開始條件)
- //**************************************************************************************************
- void start()
- {
- sda=1; //數(shù)據(jù)線置高,
- _nop_(); //延時(shí)
- scl=1; //時(shí)鐘線置高
- _nop_(); //延時(shí)
- sda=0; //數(shù)據(jù)線置低,由高變低
- _nop_(); //延時(shí)
- scl=0; //時(shí)鐘線置低,準(zhǔn)備發(fā)送或接收數(shù)據(jù),總線進(jìn)入忙狀態(tài)(I2C總線在空閑狀態(tài)時(shí),SDA與SCL均被置高)
- _nop_(); //延時(shí)
- }
- //**************************************************************************************************
- //停止(SCL為高,SDA由低變?yōu)楦呤且粋(gè)結(jié)束條件)
- //**************************************************************************************************
- void stop()
- {
- sda=0; //數(shù)據(jù)線置低
- _nop_(); //延時(shí)
- scl=1; //時(shí)鐘線置高
- _nop_(); //延時(shí)
- sda=1; //數(shù)據(jù)線置高,由低變高
- _nop_(); //延時(shí)
- }
- //**************************************************************************************************
- //檢測應(yīng)答(所有的地址和數(shù)據(jù)字都是以8bit,在第9個(gè)時(shí)鐘周期,從器件發(fā)出"0"信號來作為收到一個(gè)字的應(yīng)答信號)
- //**************************************************************************************************
- void checkACK() //主器件檢測從器件是否返回應(yīng)答
- {
- scl=1; //時(shí)鐘線置高
- _nop_(); //延時(shí)
- while(sda==1); //等待第9個(gè)時(shí)鐘周期器件發(fā)出的響應(yīng)信號"0"
- scl=0; //時(shí)鐘線置低
- _nop_(); //延時(shí)
- }
- //**************************************************************************************************
- //發(fā)送應(yīng)答(發(fā)送方為主器件,接收方為從器件,控制器作為從器件接收完1數(shù)據(jù)時(shí),發(fā)送應(yīng)答信號
- //**************************************************************************************************
- void sendACK(bit ACK)
- {
- if(ACK)sda=1; //如果i位為1則發(fā)送1,即發(fā)送"非應(yīng)答信號"
- else sda=0; //如果i位為0則發(fā)送0,即發(fā)送"應(yīng)答信號"
- scl=1; //時(shí)鐘線置高,給一個(gè)脈沖
- _nop_(); //延時(shí)
- scl=0; //時(shí)鐘線置低
- _nop_(); //延時(shí)
- }
- //**************************************************************************************************
- //寫一字節(jié)
- //**************************************************************************************************
- void send_byte(uchar date) //寫一個(gè)8位字
- {
- uchar i,temp; //定義局部變量
- temp=date; //待發(fā)8位數(shù)據(jù)賦予temp
- for(i=0;i<8;i++) //循環(huán)8次,每次寫入1位,從最高位開始發(fā)送
- {
- if(temp&0x80)sda=1; //如果temp最高位為1則發(fā)送1
- else sda=0; //如果temp最高位為0則發(fā)送0
- _nop_(); //延時(shí)
- scl=1; //給一個(gè)脈沖,發(fā)送sda當(dāng)前這位數(shù)據(jù)
- _nop_(); //延時(shí),需大于4us(參考數(shù)據(jù)手冊時(shí)序圖)
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- scl=0; //時(shí)鐘線置低,準(zhǔn)備下一脈沖
- _nop_(); //延時(shí),需大于4.7us(參考數(shù)據(jù)手冊時(shí)序圖)
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- temp=temp<<1; //左移1位,準(zhǔn)備好下1位待發(fā)送的數(shù)據(jù)
- }
- checkACK(); //查詢是否返回應(yīng)答信號
- }
- //**************************************************************************************************
- //讀一字節(jié)
- //**************************************************************************************************
- uchar receive_byte() //讀一個(gè)8位字
- {
- uchar i,temp; //定義局部變量
- sda=1; //設(shè)置數(shù)據(jù)線為輸入
- _nop_(); //延時(shí)
- for(i=0;i<8;i++) //循環(huán)8次,每次讀取1位,從最高位開始接收
- {
- scl=1; //給一脈沖,準(zhǔn)備發(fā)送1位數(shù)據(jù)
- _nop_(); //延時(shí),需大于4us(參考數(shù)據(jù)手冊時(shí)序圖)
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- temp=(temp<<1)|sda; //讀取1位數(shù)據(jù),放在temp最低位
- scl=0; //準(zhǔn)備給下1個(gè)脈沖
- _nop_(); //延時(shí),需大于4.7us(參考數(shù)據(jù)手冊時(shí)序圖)
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- }
- return temp; //返回讀取的8位數(shù)據(jù)
- }
- //**************************************************************************************************
- //向某I2C器件的某字地址寫一字節(jié)數(shù)據(jù)
- //**************************************************************************************************
- void write_word(uchar device_add,uchar word_add,uchar date) //寫進(jìn)去一個(gè)存儲數(shù)據(jù)
- {
- start(); //啟動
- send_byte(device_add); //選擇從器件地址,RW位為0,即選擇寫命令
- send_byte(word_add); //寫字地址
- send_byte(date); //寫數(shù)據(jù)
- stop(); //停止
- }
- //**************************************************************************************************
- //向某I2C器件的某字地址讀一字節(jié)數(shù)據(jù)
- //**************************************************************************************************
- uchar read_word(uchar device_add,uchar word_add) //讀出一個(gè)存儲的數(shù)據(jù)
- {
- uchar date;
- start(); //啟動
- send_byte(device_add); //選擇從器件地址,RW位為0,即選擇寫命令
- send_byte(word_add); //寫字地址
- start(); //啟動
- send_byte(device_add+1); //選擇從器件地址,RW位為1,即選擇讀命令
- date=receive_byte(); //讀數(shù)據(jù)
- sendACK(1); //發(fā)送非應(yīng)答信號
- stop(); //停止
- return date; //返回讀取結(jié)果數(shù)據(jù)
- }
- //**************************************************************************************************
- //主函數(shù)
- //**************************************************************************************************
- int readeeprom()
- {
- uchar temp; //定義中間變量
- temp=read_word(0xa0,0); //從I2C器件a0的第0個(gè)地址讀出數(shù)據(jù)賦予temp
- write_word(0xa0,0,temp+1); //向I2C器件a0的第0個(gè)地址寫etmp+1
- Delay1ms(50);
- while(1) //死循環(huán)
- {
- temp=read_word(0xa0,0); //從第0個(gè)地址讀出一個(gè)數(shù)據(jù)
- Delay1ms (10);
- P1=~temp; //送數(shù)據(jù)到P1口顯示
- }
- }
復(fù)制代碼
|