C语言代码
MLX90615和单片机STC12C5604AD的SMBus通信
begin:
start_bit(); //发送起始位 TX_byte(SLA); //发送受控地址,写命令 if(slave_ack()==0) {
stop_bit(); goto begin; }
TX_byte(cmdW); //发送命令 if(slave_ack()==0) {
stop_bit(); goto begin; }
TX_byte(DataL); //发送数据低位字节 if(slave_ack()==0) {
stop_bit(); goto begin; }
TX_byte(DataH); //发送数据高位字节 if(slave_ack()==0) {
stop_bit(); goto begin; }
TX_byte(Pecreg); //发送PEC码 if(slave_ack()==0) {
stop_bit(); goto begin; }
stop_bit(); //发送终止位 Delay(200); //等候5ms }
//----------------------------------------------------------------------------------------------------------------------------------------//
//----------------------------------------------------------------------------------------------------------------------------------------//
子文件Delay.c
//----------------------------------------------------------------------------------------------------------------------------------------//
//----------------------------------------------------------------------------------------------------------------------------------------// 延迟函数
//----------------------------------------------------------------------------------------------------------------------------------------//
#include
//----------------------------------------------------------------------------------------------------------------------------------------//
11 / 19
C语言代码
MLX90615和单片机STC12C5604AD的SMBus通信
函数名: Delay
功能: 延迟一定的时间
参数: N表示延迟时间为MCU机器周期的N倍
注解: 机器周期是时钟周期(1/Fosc)的12倍,对于STC12C5604AD, Fosc=11.0592MHz
//----------------------------------------------------------------------------------------------------------------------------------------// void Delay(unsigned int N) {
unsigned int i; for(i=0;i //----------------------------------------------------------------------------------------------------------------------------------------// //----------------------------------------------------------------------------------------------------------------------------------------// 子文件dec2hex.c //----------------------------------------------------------------------------------------------------------------------------------------// //----------------------------------------------------------------------------------------------------------------------------------------// 十进制转换为十六进制 //----------------------------------------------------------------------------------------------------------------------------------------// #include \ //头文件中引用自身 #include \//----------------------------------------------------------------------------------------------------------------------------------------// 函数名: dec2hex 功能: 将十进制代码转换为十六进制代码 参数: float e (发射率数值) 返回值: unsigned int c c是数组c[4]的首地址 //----------------------------------------------------------------------------------------------------------------------------------------// unsigned int *dec2hex(float e) { long int a=(e*65535)+0.5; int j,m=0,i=0; unsigned int b[16]; unsigned int c[4],t; b[15]=0x0f; b[14]=0x0e; b[13]=0x0d; b[12]=0x0c; b[11]=0x0b; b[10]=0x0a; b[9]=0x09; b[8]=0x08; b[7]=0x07; b[6]=0x06; b[5]=0x05; b[4]=0x04; b[3]=0x03; b[2]=0x02; b[1]=0x01; b[0]=0; 12 / 19 C语言代码 MLX90615和单片机STC12C5604AD的SMBus通信 while(a!=0) { j=a; //求模运算 c[i++]=b[j]; a=a/16; //求余运算 m++; } for(i=0;i //----------------------------------------------------------------------------------------------------------------------------------------// //----------------------------------------------------------------------------------------------------------------------------------------// 子文件CalTem.c //----------------------------------------------------------------------------------------------------------------------------------------// //----------------------------------------------------------------------------------------------------------------------------------------// 根据十六进制数值计算温度 //----------------------------------------------------------------------------------------------------------------------------------------// #include \ //头文件中引用自身 //----------------------------------------------------------------------------------------------------------------------------------------// 函数名: CALTEMP 功能: 计算温度 参数: unsigned long int TEMP (由MLX90615中读到的数据) 返回值: unsigned int mah mah是数组mah[5]的首地址 注解: 将十六进制代码转换为温度数据的公式为T=(Data)*0.02-273.15 //----------------------------------------------------------------------------------------------------------------------------------------// unsigned int *CALTEMP(unsigned long int TEMP) { unsigned long int T; unsigned int a,b; unsigned int A4,A5,A6,A7,A8; unsigned int 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=a0; A5=a/10; a=a; 13 / 19 C语言代码 MLX90615和单片机STC12C5604AD的SMBus通信 A6=a; } else if(a>=10) { A4=0; A5=a/10; a=a; A6=a; } else { A4=0; A5=0; A6=a; } if(b>=10) { A7=b/10; b=b; 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; A6=a; } else { A5=0; A6=a; } if(b>=10) { A7=b/10; b=b; A8=b; } else { A7=0; 14 / 19 C语言代码 MLX90615和单片机STC12C5604AD的SMBus通信 A8=b; } } mah[4]=A4; mah[3]=A5; mah[2]=A6; mah[1]=A7; mah[0]=A8; return mah; } //----------------------------------------------------------------------------------------------------------------------------------------// //----------------------------------------------------------------------------------------------------------------------------------------// 子文件digitalLED.c //----------------------------------------------------------------------------------------------------------------------------------------// //----------------------------------------------------------------------------------------------------------------------------------------// 根据计算所得数据在数字式LEDs上显示 //----------------------------------------------------------------------------------------------------------------------------------------// #include unsigned char yanshicon; unsigned char weicon; unsigned char code LED_tab[10]={ 0xFC, //0 0x60, //1 0xDA, //2 0xF2, //3 0x66, //4 0xB6, //5 0xBE, //6 0xE0, //7 0xFE, //8 15 / 19