51单片机连接红外测温模块无法进行串口调试打印问题
时间:10-02
整理:3721RD
点击:
- #include<reg52.h>
- #include <stdio.h>
- #include<intrins.h>
- #include <string.h>
- #define uint unsigned int
- #define uchar unsigned char
- typedef unsigned int u16; //对数据类型进行声明定义
- typedef unsigned char u8;
- unsigned char flag,i,a;
- uchar tempH,tempL,err;
- sbit SCL=P2^7; // 时钟线
- sbit SDA=P2^6;
- //函数声明
- void SMBusStart();
- void Delay100us();
- unsigned char SMBusReadByte();
- unsigned MLX90614ReadTemp(void);
- uint CALTEMP(uint TEMP);
- uchar final;
- void delay(u16 i);
- void init();
- //uchar code table[]="I get ";
- //主函数
- void main()
- {
- u16 TEM;
- init();
- while(1)
- {
-
- if(flag == 1)
- {
- flag = 0;
- ES = 0;
- TI = 1;
- MLX90614ReadTemp();
- CALTEMP(TEM);
- printf("temp is %c",TEM);
- while(!TI);
- TI = 0;
- ES = 1;
- }
- delay(1000);
- }
- }
- void init() //中断初始化
- {
- TMOD = 0x20;
- TH1 = 0xfd;
- TL1 = 0xfd;
- TR1 = 1;
- SM0 = 0;
- SM1 = 1;
- REN = 1;
- EA = 1;
- ES = 1;
- }
- void ser() interrupt 4
- {
- if(RI)
- {
- RI = 0;
- a = SBUF;
- flag = 1;
- }
- if(TI)
- {}
-
- }
- //大延时函数
- void delay(u16 i)
- {
- while(i--);
- }
- //小延时函数
- void Delay100us(void)
- {
- unsigned char a,b;
- for(b=1;b>0;b--)
- for(a=47;a>0;a--);
- }
- void SMBusStart() //开始
- {
- SDA = 1;
- Delay100us();
- SCL = 1;
- Delay100us();
- SDA = 0;
- Delay100us();
- SCL = 0;
- }
- void SMBusStop()//结束
- {
- SDA = 0;
- Delay100us();
- SCL = 1;
- Delay100us();
- SDA = 1;
- Delay100us();
- }
- unsigned char SMBusSendByte(unsigned char dat) //发送字节
- {
- unsigned char a=0,b=0;
- for(a = 0; a < 8; a ++)
- {
- SDA = dat>>7;
- dat = dat<<1;
- Delay100us();
- SCL = 1;
- Delay100us();
- SCL = 0;
- Delay100us();
- }
- SDA = 1;
- Delay100us();
- SCL = 1;
- while(SDA)
- {
- b ++;
- if(b > 200)
- {
- SCL = 0;
- Delay100us();
- return 0;
- }
- }
- SCL = 0;
- Delay100us();
- return 1;
- }
- unsigned char SMBusReadByte() //读取字节
- {
- unsigned char a=0,dat=0;
- SDA = 1;
- Delay100us();
- for(a = 0; a < 8; a ++)
- {
- SCL = 1;
- Delay100us();
- dat <<= 1;
- dat |= SDA;
- Delay100us();
- SCL = 0;
- Delay100us();
- }
- return dat;
- }
- unsigned MLX90614ReadTemp(void) //读取温度数据 8bit
- {
- SCL = 0;
- SMBusStart();
- SMBusSendByte(0x00);
- SMBusSendByte(0x07);
- SMBusStart();
- SMBusSendByte(0x01);
- tempL = SMBusReadByte();
- tempH = SMBusReadByte();
- err = SMBusReadByte();
- SMBusStop();
- return(tempH*256+tempL);
- }
- uint CALTEMP(uint TEMP)//温度数据计算处理
- {
- uint T;
- uint a,b;
- uchar A4,A5,A6,A7,A8;
- uchar mah[5];
- T=TEMP*2;
- if(T>=27315) //温度为正值
- {
- T=T-27315;
- a=T/100;
- b=T-a*100;
- if(a>=100)
- {
- A4=a/100; //百
- a=a%100;
- A5=a/10; //十
- a=a%10;
- A6=a; //个
- }
- else if(a>=10)
- {
- A4=0;
- A5=a/10;
- a=a%10;
- A6=a;
- }
- else
- {
- A4=0;
- A5=0;
- A6=a;
- }
- if(b>=10)
- {
- A7=b/10;
- b=b%10;
- A8=b;
- }
- else
- {
- A7=0;
- A8=b;
- }
- }
- else //温度为负值
- {
- T=27315-T;
- a=T/100;
- b=T-a*100;
- A4=9;
- if(a>=10)
- {
- A5=a/10;
- a=a%10;
- A6=a;
- }
- else
- {
- A5=0;
- A6=a;
- }
- if(b>=10)
- {
- A7=b/10;
- b=b%10;
- A8=b;
- }
- else
- {
- A7=0;
- A8=b;
- }
- }
- mah[4]=A4;
- mah[3]=A5;
- mah[2]=A6;
- mah[1]=A7;
- mah[0]=A8;
- return mah; /////////////////////////////////////////////////////////
-
- }