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; //可位寻址数据
sbit bit_out=flag1^7;
sbit bit_in=flag1^0;
unsigned char temp2=0; //设全局变量
unsigned char temp1=0;
unsigned char err=0;
//float m90614 =0;
//*************************************************************************
sbit SCK=P3^7; //时钟线
sbit SDA=P3^6; //数据线
void start_mlx();
void read_bit(void);
unsigned char ReadByte(void);
unsigned int change(unsigned int tem);
void send_bit(void);
//------------延时--------------
void delay1(unsigned int n)
{
unsigned int j;
for(j=0;j<n;j++)
{
_nop_();
}
}
//开始********************************************************
void start(void) //停止条件是 SCK=1时,SDA由1到0
{
SDA=1; delay1(4);
SCK=1; delay1(4);
SDA=0; delay1(4);
SCK=0; delay1(4);
}
//结束********************************************************
void stop(void) //停止条件是 SCK=1时,SDA由0到1
{
SCK=0; delay1(4);
SDA=0; delay1(4);
SCK=1; delay1(4);
SDA=1;
}
//发送一个字节********************************************************
void SendByte(unsigned char number)
{
unsigned char i,n,dat;
n=Nack_number; //可以重发次数
Send_again:
dat=number;
for(i=0;i<8;i++) //8位依次发送
{
if(dat&0x80) //取最高位
{
bit_out=1; //发1
}
else
{
bit_out=0; //发0
}
send_bit(); //发送一个位
dat=dat<<1; //左移一位
}
read_bit(); //接收1位应答信号
if(bit_in==1) //无应答时重发
{
stop();
if(n!=0)
{
n--; //可以重发Nack_number=10次
goto Repeat; //重发
}
else
{
goto exit; //退出
}
}
else
{
goto exit;
}
Repeat:
start(); //重新开始
goto Send_again; //重发
exit: ; //退出
}
//-----------发送一个位---------
void send_bit(void)
{
if(bit_out==1)
{
SDA=1; //发1
}
else
{
SDA=0; //发0
}
// _nop_();
SCK=1; //上升沿
delay1(4); delay1(4);
SCK=0;
delay1(4); delay1(4);
}
//----------接收一个字节--------
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时对应位加1
}
}
SDA=0; //发送应答信号0
send_bit();
return dat; //带回接收数据
}
//----------接收一个位----------
void read_bit(void)
{
SDA=1; //数据端先置1
bit_in=1;
SCK=1; //上升沿
delay1(4);
delay1(4);
bit_in=SDA; //读数据
_nop_();
SCK=0;
delay1(4);
delay1(4);
}
unsigned int readtemp(void)
{
SCK=0;
start(); //开始条件
SendByte(0x00); //发送从地址00
SendByte(0x07); //发送命令
start(); //开始条件
SendByte(0x01); //读从地址00
bit_out=0;
temp2=ReadByte(); //读数据低字节
bit_out=0;
temp1=ReadByte(); //读数据高字节
bit_out=1;
err=ReadByte(); //读错误信息码
stop(); //停止条件
return(temp1*256+temp2);
}
//温度转换************************************************************************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
主函数
/*******************************************
程序功能:利用单线制DS18B20温度传感器
LED数码管显示当前的温度值
********************************************/
#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);
}
}
}
|