基于單片機的溫度測量方案
三、電路圖
我在這里用的是芯片PIC18F2550,它可以與任何其他規(guī)模較小的PIC微控制器實現I2C通信。溫度由TC74傳感器讀取并顯示在LCD字符。別忘 了把兩個上拉電阻(1K)連接在SDA和SCL線I2C總線上。在本實驗中采用PIC18F2550單片機PIC板來自于StartUSB for PIC board.。
圖4 電路圖
圖5 實物圖
圖6 TC74溫度傳感器
四、軟件
PIC18F2550固件是在C使用MikroC Pro for PIC編譯開發(fā)的。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
MCLR Enabled
*/
// Define LCD module connections.
sbit LCD_RS at RC6_bit;
sbit LCD_EN at RC7_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISC6_bit;
sbit LCD_EN_Direction at TRISC7_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connection definition
unsigned char Temp;
unsigned short num;
const int TC74A0 = 0x90;
void check_device(unsigned short dev_address){ I2C1_Start();
if (I2C1_Wr(dev_address)){ Lcd_Out(1,1,"Device not found");
}
else Lcd_Out(1,1,"TC74 device");
I2C1_Stop();
}
unsigned short Read_Temp(){
unsigned short 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;
}
評論