各位大佬,我的单片机一直不能跟MLX90614t通讯,希望大家可以帮忙解决,有点急,麻烦各位了。
- #include"HT66F2390.h"
- #include"oled.h"(这是oled的程序文件,不在下面)
- #ifndef HW_H
- #define HW_H
- #define uchar unsigned char
- #define Nack 10
- //*******端口定义***********
- //mlx90614
- #define SDA _pf4
- #define SCL _pg0
- #define RamAccess 0x00
- #define AbmientTempAddr 0x03 //周围温度
- #define IR1Addr 0x04
- #define IR2Addr 0x05
- #define LineAbmientTempAddr 0x06 //环境温度
- #define LineObj1TempAddr 0x07 //目标温度,红外温度
- #define EepomAccess 0x20
- #define Mode 0x60
- #define ExitMode 0x61
- #define ReadFlag 0xf0
- #define LineObj2TempAddr 0x08 //eepom address EEPROM地址
- int out=0, in=0;
- //*******函数声明**********
- void start_bit(); //MLX90614 发起始位子程序
- void stop_bit(); //MLX90614 发结束位子程序
- uchar rx_byte(void); //MLX90614 接收字节子程序
- void send_bit(void); //MLX90614 发送位子程序
- void tx_byte(uchar dat_byte); //MLX90614 接收字节子程序
- void jieshou(); //MLX90614 接收位子程序 //延时程序
- void delay_h(unsigned int k); //延时
- unsigned int memread(void); //读温度数据
- void display_h(unsigned int Tem);
- //*********输入转换并显示*********
- void display_h(unsigned int Tem)
- {
- unsigned int T,a,b;
- uchar c;
- T=Tem*2;
- if(T>=27315)
- {
- T=T-27315;
- a=T/100;
- b=T-a*100;
-
- if(a>=100)
- {
- c=a/100;
- write_wx1(c,1,6);//oled 显示程序函数
- a=a%100;
- c=a/10;
- write_wx1(c,7,6) ;
- c=a%10;
- write_wx1(c,13,6) ;
- }
- else if(a>=10)
- {
- c=a/10;
- write_wx1(11,1,6);
- write_wx1(c,7,6) ;
- c=a%10;
- write_wx1(c,13,6) ;
- }
- else
- {
- write_wx1(11,1,6);
- write_wx1(11,7,6) ;
- c=a%10;
- write_wx1(c,13,6) ;
- }
- write_wx1(2,19,6) ;
- //分隔开
-
-
- if(b>=10)
- {
- b=b/10;
- write_wx1(b,25,6);
- }
- else
- {
- write_wx1(0,25,6) ;
- }
-
- }
-
-
- else
- {
- T=27315-T;
- a=T/100;
- b=T-a*100;
- if(a>=10)
- {
- c=a/10;
- write_wx1(c,7,6) ;
- c=a%10;
- write_wx1(c,13,6) ;
- }
- else
- {
- c=a%10;
- write_wx1(c,7,6) ;
- }
- write_wx1(2,19,6) ;
- //分隔开
- if(b>=10)
- {
- c=b/10;
- write_wx1(c,25,6) ;
- }
- else
- {
- write_wx1(c,25,6) ;
- }
- }
- }
- void delay_h(unsigned int k)
- {
- while(k--);
-
-
- }
- //*********开始触发*******
- void start_bit(void)
- {
- SDA=1;
- delay(5);
- SCL=1;
- delay(5);
- SDA=0;
- delay(5);
- SCL=0;
- delay(5);
- }
- //*********结束*******
- void stop_bit(void)
- {
- SCL=0;
- delay(5);
- SDA=0;
- delay(5);
- SCL=1;
- delay(5);
- SDA=1;
- }
- //---------发送一个字节---------
- void tx_byte(uchar dat_byte)
- {
- uchar i,n,dat;
- n=Nack;
-
- TX_again:
- dat=dat_byte;
-
- for(i=0;i<8;i++)
- {
- if(dat&0x80)
- out=1;
- else
- {
- out=0;
- }
- send_bit();
-
- dat=dat<<1;
- }
-
- jieshou();//
-
- if(in==1)//?
- {
- stop_bit();
- if(n!=0)
- {
- n--;
- goto Repeat;
- }
-
- else
- goto exit;
- }
-
- else
- {
- goto exit;
- }
-
- Repeat:
- start_bit();
- goto TX_again;
-
- exit: ;
- }
- //*********发送一个位*******
- void send_bit(void)
- {
- if(out==1)
- SDA=1;
- else
- {
- SDA=0;
- }
- delay(5);
- SCL=1;
- delay(8);
- SCL=0;
- delay(8);
- }
- //----------接收一个字节--------
- uchar rx_byte(void)
- {
- uchar i,dat;
- dat=0;
- for(i=0;i<8;i++)
- {
- dat=dat<<1;
- jieshou();//
- if(in==1)
- dat=dat+1;
- }
- SDA=0;
- send_bit();//?
-
- return dat;
- }
- //----------接收一个位----------
- void jieshou()
- {
- SDA=1;
-
- in=1;
- SCL=1;
- delay(8);
- in=SDA;
- delay(1);
- SCL=0;
- delay(8);
- }
- //-----------读温度--------------
- unsigned int memread(void)
- {
- uchar DataH,DataL,Pecreg;
-
- start_bit();
-
- tx_byte(0x00); //Send SlaveAddress
-
- tx_byte(0x07); //Send Command
- start_bit();
-
- tx_byte(0x1);
-
- out=0;
- DataL=rx_byte();
- out=0;
- DataH=rx_byte();
- out=1;//
- Pecreg=rx_byte();
- stop_bit();
-
- return(DataH*256+DataL);
- }
- #endif
- 主函数:
- #include "HT66F2390.h"
- #include"hw.h"
- #include"oled.h"
- void main()
- {
- unsigned int tem;
-
- _wdtc = 0xa8;
- _pec = 0x00;
- _scc = 0x00;
- _pfc = 0x00;
- _hirc0 =0;
- _hirc1 =0;
-
- Init();
- qp();
- write_x(5,10);
-
- SCL=1;
- SDA=1;
- delay(4);
- SCL=0;
- delay(1000);
- SCL=1;
- while(1)
- {
-
-
- tem =memread();
- display_h(tem);
- delay(20);
- }
复制代码
|