|
MLX90614程序
#ifndef MLX_2018_6_6
#define MLX_2018_6_6
#include <intrins.h>
#include "LCD1602.h"
#define Nack_number 10
bdata unsigned char flag1; //可位尋址數(shù)據(jù)
sbit bit_out=flag1^7;
sbit bit_in=flag1^0;
unsigned char temp2=0; //設(shè)全局變量
unsigned char temp1=0;
unsigned char err=0;
//float m90614 =0;
//*************************************************************************
sbit SCK=P3^7; //時(shí)鐘線
sbit SDA=P3^6; //數(shù)據(jù)線
void start_mlx();
void read_bit(void);
unsigned char ReadByte(void);
unsigned int change(unsigned int tem);
void send_bit(void);
//------------延時(shí)--------------
void delay1(unsigned int n)
{
unsigned int j;
for(j=0;j<n;j++)
{
_nop_();
}
}
//開(kāi)始********************************************************
void start(void) //停止條件是 SCK=1時(shí),SDA由1到0
{
SDA=1; delay1(4);
SCK=1; delay1(4);
SDA=0; delay1(4);
SCK=0; delay1(4);
}
//結(jié)束********************************************************
void stop(void) //停止條件是 SCK=1時(shí),SDA由0到1
{
SCK=0; delay1(4);
SDA=0; delay1(4);
SCK=1; delay1(4);
SDA=1;
}
//發(fā)送一個(gè)字節(jié)********************************************************
void SendByte(unsigned char number)
{
unsigned char i,n,dat;
n=Nack_number; //可以重發(fā)次數(shù)
Send_again:
dat=number;
for(i=0;i<8;i++) //8位依次發(fā)送
{
if(dat&0x80) //取最高位
{
bit_out=1; //發(fā)1
}
else
{
bit_out=0; //發(fā)0
}
send_bit(); //發(fā)送一個(gè)位
dat=dat<<1; //左移一位
}
read_bit(); //接收1位應(yīng)答信號(hào)
if(bit_in==1) //無(wú)應(yīng)答時(shí)重發(fā)
{
stop();
if(n!=0)
{
n--; //可以重發(fā)Nack_number=10次
goto Repeat; //重發(fā)
}
else
{
goto exit; //退出
}
}
else
{
goto exit;
}
Repeat:
start(); //重新開(kāi)始
goto Send_again; //重發(fā)
exit: ; //退出
}
//-----------發(fā)送一個(gè)位---------
void send_bit(void)
{
if(bit_out==1)
{
SDA=1; //發(fā)1
}
else
{
SDA=0; //發(fā)0
}
// _nop_();
SCK=1; //上升沿
delay1(4); delay1(4);
SCK=0;
delay1(4); delay1(4);
}
//----------接收一個(gè)字節(jié)--------
unsigned char ReadByte(void)
{
unsigned char i,dat;
dat=0; //初值為0
for(i=0;i<8;i++)
{
dat=dat<<1; //左移
read_bit(); //接收一位
if(bit_in==1)
{
dat=dat+1; //為1時(shí)對(duì)應(yīng)位加1
}
}
SDA=0; //發(fā)送應(yīng)答信號(hào)0
send_bit();
return dat; //帶回接收數(shù)據(jù)
}
//----------接收一個(gè)位----------
void read_bit(void)
{
SDA=1; //數(shù)據(jù)端先置1
bit_in=1;
SCK=1; //上升沿
delay1(4);
delay1(4);
bit_in=SDA; //讀數(shù)據(jù)
_nop_();
SCK=0;
delay1(4);
delay1(4);
}
unsigned int readtemp(void)
{
SCK=0;
start(); //開(kāi)始條件
SendByte(0x00); //發(fā)送從地址00
SendByte(0x07); //發(fā)送命令
start(); //開(kāi)始條件
SendByte(0x01); //讀從地址00
bit_out=0;
temp2=ReadByte(); //讀數(shù)據(jù)低字節(jié)
bit_out=0;
temp1=ReadByte(); //讀數(shù)據(jù)高字節(jié)
bit_out=1;
err=ReadByte(); //讀錯(cuò)誤信息碼
stop(); //停止條件
return(temp1*256+temp2);
}
//溫度轉(zhuǎn)換************************************************************************88
unsigned int change(unsigned int tem)
{
float mlx_t ,mlx_k ,a =0,b =0;
float mlx_back;
mlx_t = tem*2;
if(mlx_t >27315)
{
mlx_k = mlx_t - 27315;
a = mlx_k/100;
b = mlx_k - a*100;
mlx_back = a+(b/100);
}
else
{
mlx_k = 27315 - mlx_t;
a = mlx_k/100;
b = mlx_k - a*100;
mlx_back = a+b;
}
return mlx_back;
}
void start_mlx()
{
SCK=1;
SDA=1;
delay1(4);
SCK=0;
delay1(100);
SCK=1;
}
#endif
主函數(shù)
/*******************************************
程序功能:利用單線制DS18B20溫度傳感器
LED數(shù)碼管顯示當(dāng)前的溫度值
********************************************/
#include <reg51.h>
#include "LCD1602.h"
#include "DS18B20.h"
#include "BUZZER.h"
#include "90614.h"
#include "stdio.h"
sbit keys= P3^5;
float T=0;
void Delay1ms(unsigned int count)
{
unsigned int i,j;
for(i=0;i<count;i++)
for(j=0;j<113;j++);
}
//**************************** //主程序
main()
{
unsigned int value = 0;
unsigned char buffer[40];
LCD_Initial();
while (1)
{
start_mlx();
if(keys == 0)
{
GotoXY(0,0);
Print("DS18B20 Temper");
ReadTemperature() ;
GotoXY(3,1);
sprintf(buffer,"+%6.3f",temper);
Print(buffer);
Print(" ^C");
buzze(temper);
Delay1ms(200);
}
else
{
GotoXY(0,0);
Print(" IR Temper");
T = readtemp();
T = change(T);
GotoXY(3,1);
sprintf(buffer,"%6.4f",T);
Print(buffer);
Print("^C");
buzze(T);
Delay1ms(200);
}
}
}
|
|