標(biāo)題:
MLX90614紅外測溫
[打印本頁]
作者:
孫文超
時(shí)間:
2021-2-7 15:17
標(biāo)題:
MLX90614紅外測溫
#include"at89x52.h"
#include"intrins.h"
//************************************
#define uint unsigned int
#define uchar unsigned char
#define Nack_counter 10
//************** 端口定義**************
//LCD 控制線接口
uchar flag1;
sbit RS=P0^7;
sbit RW=P0^6;
sbit LCDE=P0^5;
//mlx90614 端口定義
sbit SCL=P1^6;// 時(shí)鐘線
sbit SDA=P1^7;// 數(shù)據(jù)線
//************ 數(shù)據(jù)定義****************
bdata uchar flag;//可位尋址數(shù)據(jù)
sbit bit_out=flag^7;
sbit bit_in=flag^0;
uchar DataH,DataL,Pecreg;
//************ 函數(shù)聲明*****************************************
void start_bit(); //MLX90614 發(fā)起始位子程序
void stop_bit(); //MLX90614發(fā)結(jié)束位子程序
uchar rx_byte(void); //MLX90614 接收字節(jié)子程序
void send_bit(void); //MLX90614發(fā)送位子程序
void tx_byte(uchar dat_byte); //MLX90614 接收字節(jié)子程序
void receive_bit(void); //MLX90614接收位子程序
void delay(uint N); //延時(shí)程序
uint memread(void); // 讀溫度數(shù)據(jù)
void init1602(void); //LCD 初始化子程序
void chk_busy_flg(void); //LCD 判斷忙子程序
void dis_cmd_wrt(uchar cmd); //LCD 寫命令子程序
void dis_dat_wrt(uchar dat); //LCD 寫數(shù)據(jù)子程序
void display(uint Tem); // 顯示子程序
//*************主函數(shù)*******************************************
void main()
{
uint Tem;
//函數(shù)部分
SCL=1;SDA=1;_nop_();
_nop_();_nop_();_nop_();
SCL=0;
delay(1000);
SCL=1;
init1602();
while(1)
{
Tem=memread();
display(Tem);
delay(20);
}
}
//*********輸入轉(zhuǎn)換并顯示*********
void display(uint Tem)
{
uint T,a,b;
T=Tem*2;
dis_cmd_wrt(0x01);//清屏
if(T>=27315)
{
T=T-27315;
a=T/100;
b=T-a*100;
//---------------------------
if(a>=100)
{
dis_dat_wrt(0x30+a/100);
a=a%100;
dis_dat_wrt(0x30+a/10);
a=a%10;
dis_dat_wrt(0x30+a);
}
else if(a>=10)
{
dis_dat_wrt(0x30+a/10);
a=a%10;
dis_dat_wrt(0x30+a);
}
else
{
dis_dat_wrt(0x30+a);
}
dis_dat_wrt(0x2e);// 顯示點(diǎn)
//---------------------------
if(b>=10)
{
dis_dat_wrt(0x30+b/10);
// b=b%10;
// dis_dat_wrt(0x30+b);
}
else
{
dis_dat_wrt(0x30);
// dis_dat_wrt(0x30+b);
}
}
//==========
else
{
T=27315-T;
a=T/100;
b=T-a*100;
dis_dat_wrt(0x2d);
//--------------------------
if(a>=10)
{
dis_dat_wrt(0x30+a/10);
a=a%10;
dis_dat_wrt(0x30+a);
}
else
{
dis_dat_wrt(0x30+a);
}
dis_dat_wrt(0x2e);//顯示點(diǎn)
//--------------------------
if(b>=10)
{
dis_dat_wrt(0x30+b/10);
b=b%10;
dis_dat_wrt(0x30+b);
}
else
{
dis_dat_wrt(0x30);
dis_dat_wrt(0x30+b);
}
}
}
//************************************
void start_bit(void)
{
SDA=1;
_nop_();_nop_();_nop_();_nop_();_nop_();
SCL=1;
_nop_();_nop_();_nop_();_nop_();_nop_();
SDA=0;
_nop_();_nop_();_nop_();_nop_();_nop_();
SCL=0;
_nop_();_nop_();_nop_();_nop_();_nop_();
}
//------------------------------
void stop_bit(void)
{
SCL=0;
_nop_();_nop_();_nop_();_nop_();_nop_();
SDA=0;
_nop_();_nop_();_nop_();_nop_();_nop_();
SCL=1;
_nop_();_nop_();_nop_();_nop_();_nop_();
SDA=1;
}
//--------- 發(fā)送一個(gè)字節(jié)---------
void tx_byte(uchar dat_byte)
{
char i,n,dat;
n=Nack_counter;
TX_again:
dat=dat_byte;
for(i=0;i<8;i++)
{
if(dat&0x80)
bit_out=1;
else
bit_out=0;
send_bit();
dat=dat<<1;
}
receive_bit();
if(bit_in==1)
{
stop_bit();
if(n!=0)
{n--;goto Repeat;}
else
goto exit;
}
else
goto exit;
Repeat:
start_bit();
goto TX_again;
exit: ;
}
//-----------發(fā)送一個(gè)位---------
void send_bit(void)
{
if(bit_out==0)
SDA=0;
else
SDA=1;
_nop_();
SCL=1;
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
SCL=0;
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
}
//---------- 接收一個(gè)字節(jié)--------
uchar rx_byte(void)
{
uchar i,dat;
dat=0;
for(i=0;i<8;i++)
{
dat=dat<<1;
receive_bit();
if(bit_in==1)
dat=dat+1;
}
send_bit();
return dat;
}
//---------- 接收一個(gè)位----------
void receive_bit(void)
{
SDA=1;bit_in=1;
SCL=1;
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
bit_in=SDA;
_nop_();
SCL=0;
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
}
//------------ 延時(shí)--------------
void delay(uint N)
{
uint i;
for(i=0;i<N;i++)
_nop_();
}
//------------------------------
uint memread(void)
{
start_bit();
tx_byte(0xB4); //Send SlaveAddress ==============================
//tx_byte(0x00);
tx_byte(0x07); //Send Command
//------------
start_bit();
tx_byte(0x01);
bit_out=0;
DataL=rx_byte();
bit_out=0;
DataH=rx_byte();
bit_out=1;
Pecreg=rx_byte();
stop_bit();
return(DataH*256+DataL);
}
//******************LCD 顯示子函數(shù)***********************
void init1602(void) // 初始化LCD
{
dis_cmd_wrt(0x01);
dis_cmd_wrt(0x0c);
dis_cmd_wrt(0x06);
dis_cmd_wrt(0x38);
}
void chk_busy_flg(void) //LCD 忙標(biāo)志判斷
{
flag1=0x80;
while(flag1&0x80)
{
P2=0xff;
RS=0;
RW=1;
LCDE=1;
flag1=P2;
LCDE=0;
}
}
void dis_cmd_wrt(uchar cmd) // 寫命令子函數(shù)
{
chk_busy_flg();
P2=cmd;
RS=0;
RW=0;
LCDE=1;
LCDE=0;
}
void dis_dat_wrt(uchar dat) // 寫數(shù)據(jù)子函數(shù)
{
chk_busy_flg();
if(flag1==16)
{
P2=0XC0;
RS=0;
RW=0;
LCDE=1;
LCDE=0;
}
P2=dat;
RS=1;
RW=0;
LCDE=1;
LCDE=0;
}
復(fù)制代碼
作者:
xxl是菜雞
時(shí)間:
2021-5-18 12:51
樓主在嗎 我能請教一些問題嗎
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1