|
电路原理图如下:
单片机源程序如下:
- #include"reg52.h"
- #include"intrins.h"
- #include"common.h"
- #include"Key.h"
- #include"lcd1602.h"
- //************************************
- #define Nack_counter 10
- //mlx90614 端口定义
- sbit SCL=P3^6;//时钟线
- sbit SDA=P3^7;//数据线
- //************数据定义****************
- bdata uchar flag;//可位寻址数据
- sbit bit_out=flag^7;
- sbit bit_in=flag^0;
- bit start_flag;
- uchar DataH,DataL,Pecreg;
- uchar flag1;
- uchar dis0[] = "Life Is Movement";
- uchar dis1[] = " T: 000.00 C ";
- uchar dis2[] = " X: S: ";
- uint Max_Tem = 3800;//最大温度,100倍
- uint Min_Tem = 2000;//最小温度
- uint Tem;
- uchar status = 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 receive_bit(void); //MLX90614 接收位子程序
- void delay(uint);
- uint memread(void); //读温度数据
- void display(uint Tem); //显示子程序
- //*************主函数*******************************************
- void main()
- {
- uint i,count;
- //函数部分
- SCL=1;
- SDA=1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- SCL=0;
- delay(1000);
- SCL=1;
- dis1[11] =0xdf;
-
- LCD_init(); //lcd初始化
- start_flag = 0;
- delay_ms(2000);
- while(1)
- {
- count++;
- if(count<500)
- {
- Tem=memread(); //读取温度
- delay_ms(5000);
- key_det(); //按键扫描
- display(Tem); //显示温度
- delay_ms(5000);
- for(i=0;i<16;i++)
- {
- LCD_write_char(i,0,dis1[i]);
- LCD_write_char(i,1,dis2[i]);
- }
- delay_ms(500);
- count=0;
- }
- }
- }
- //*********输入转换并装载*********
- void display(uint Tem)
- {
- uint T,a,b,min,max;
- T=Tem*2;
- if(T>=27315)
- {
- T=T-27315;
- dis1[5]=' '; // 符号位
- }
- else
- {
- T=27315-T;
- dis1[5]='-'; // 符号位
- }
- a=T/100;
- b=T-a*100;
- dis1[5]=a/100+0x30; // 百位
- dis1[6]=(a%100)/10+0x30; // 十位
- dis1[7]=a%10+0x30; // 个位
- dis1[9]=b/10+0x30; //十分位
- dis1[10]=b%10+0x30; //百分位
- if(Min_Tem<0)
- {
- dis2[3]='-';
- min=-Min_Tem/100;
- }
- else
- {
- dis2[3]=' ';
- min=Min_Tem/100;
- }
- if(Max_Tem<0)
- {
- dis2[10]='-';
- max=-Max_Tem/100;
- }
- else
- {
- dis2[10]=' ';
- max=Max_Tem/100;
- }
-
- dis2[4] = min/100+0x30; //Min
- dis2[5] = (min%100)/10+0x30;
- dis2[6] = min%10+0x30;
- dis2[11]= max/100+0x30; //Max
- dis2[12]= (max%100)/10+0x30;
- dis2[13]= max%10+0x30;
- if(status==0)
- {
- dis2[0] = ' ';
- dis2[7] = ' ';
- }
- if(status==1)
- {
- dis2[0] = '>';
- dis2[7] = ' ';
- }
-
- if(status==2)
- {
- dis2[0] = ' ';
- dis2[7] = '>';
- }
-
- }
- 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;
- }
- //---------发送一个字节---------
- 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:
- ;
- }
- //-----------发送一个位---------
- 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_();
- }
- //----------接收一个字节--------
- 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;
- }
- //----------接收一个位----------
- void receive_bit(void)
- {
- SDA=1;
- bit_in=1;
- SCL=1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- bit_in=SDA;
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei附件下载:
程序与原理图.7z
(583.09 KB, 下载次数: 27)
|
评分
-
查看全部评分
|