图6 TC74温度传感器
四、软件
PIC18F2550固件是在C使用MikroC Profor PIC编译开发的。MikroC Pro for PIC编译器提供的内置支持I2C库。单片机读取TC74内部温度寄存器温度字并显示在LCD。下面的程序显示,TC74的整个工作范围内在40至125°C。
程序:
/*Project: Using TC74 with PIC microcontroller
for temperature measurement
MCU: PIC18F2550 on-board StartUSB for PIC
Clock 48.0 MHz using HS + PLL
MCLREnabled
*/
//Define LCD module connec
tions.
***itLCD_RS at RC6_bit;
***itLCD_EN at RC7_bit;
***itLCD_D4 at RB4_bit;
***itLCD_D5 at RB5_bit;
***itLCD_D6 at RB6_bit;
***itLCD_D7 at RB7_bit;
***itLCD_RS_Direction at TRISC6_bit;
***itLCD_EN_Direction at TRISC7_bit;
***itLCD_D4_Direction at TRISB4_bit;
***itLCD_D5_Direction at TRISB5_bit;
***itLCD_D6_Direction at TRISB6_bit;
***itLCD_D7_Direction at TRISB7_bit;
// EndLCD module connection definition
unsignedchar Temp;
unsignedshort num;
constint TC74A0 = 0x90;
voidcheck_device(unsigned short dev_address){ I2C1_Start();
if(I2C1_Wr(dev_address)){ Lcd_Out(1,1,"Device not found");
}
elseLcd_Out(1,1,"TC74 device");
I2C1_Stop();
}
unsignedshort Read_Temp(){
unsignedshort result; I2C1_Start(); // Issue start signal
I2C1_Wr(TC74A0);// Address + Write bit
I2C1_Wr(0x00);// Read Temp
I2C1_Repeated_Start();// Issue start signal
I2C1_Wr(TC74A0+1);// Address + Read bit
result= I2C1_Rd(0u); return result;
}
chartemperature[] = " 000 C";
voidmain() { CMCON = 0x07; // Disable comparators
ADCON1= 0x0F; // Disable Analog functions
TRISA= 0x00;
TRISC= 0x00;
TRISB= 0x00;
I2C1_Init(100000);// Initiate I2C
Lcd_Init();// Initialize LCD
Lcd_Cmd(_LCD_CLEAR);// CLEAR display
Lcd_Cmd(_LCD_CURSOR_OFF);// Cursor off
Lcd_Out(1,1,"TestingTC74");
Lcd_Out(2,1,"Thermalsensor");
Delay_ms(1000);
Lcd_Cmd(_LCD_CLEAR);
do {check_device(TC74A0);
num =Read_Temp();
//Check for negative temperature
if(num > 127) {
temperature[0]= '-';
num =~num +1;
}
elsetemperature[0] = '+';
temperature[1]= num/100 + 48;
temperature[2]= (num/10)%10 + 48;
temperature[3]= num%10 + 48;
temperature[5]= 223;
//eliminate 0s at beginning
if(temperature[1] == '0') {
temperature[1]= ' ';
if(temperature[2] == '0') temperature[2] = ' ';
}
Lcd_Out(2,4,temperature);
Delay_ms(500);} while(1);
}
五、输出
下面的照片显示装置显示正面和负面的温度。一种热焊棒尖端饱和的传感器在127°C。测试负温度读数的时候,该装置放进冰箱。
图7 测量室内的温度
图8 最大的测量温度
图9 放在冰箱中的温度显示
六、总结
本实验对微芯片的TC74传感器与PIC单片机测量环境温度的接口技术进行了探讨,并且证明方案是可以成功的。与TC74传感器的通信是通过PIC18F2550的一个I2C总线接口完成的。8位温词是从内部温度寄存器读取和经过PIC18F2550单片机在液晶屏上显示的。再者,借助虹科
电子的便利的编译器MikroC Profor PIC,也让刚方案显得更加容易,更加稳定。
注:本文由广州虹科电子Andy译自网络,若有差错,麻烦指出!
`