Introduction
In industrial control or consumer electronic product, measuring the temperature is usually required. A thermometer used for measuring the temperature comes with many interfaces, analog and digital interface.
For digital interface there are many options. In this example, I use ds1621 digital thermometer manufactured by Maxim Integrated.
This temperature sensor comes with 8 pin integrated circuit. It is also available in DIP package, a good choice for bread board prototyping and hobbyist. The communication interface is inter integrated circuit.
If we use degree Celsius it could sample the temperature between -55 to +125 degree Celsius with a 0.5 degree Celsius step. Using the Fahrenheit unit equivalence, the temperature sampling ranges from -67 to 257 degree Fahrenheit.
The supply voltage ranges from 2.7 to 5.5 V. In usual prototyping, it's normally supplied at 5 V DC.
Pin diagram of ds1621 |
Programming interface
Currently of microcontrollers come with a rich of peripherals including the communication interfaces like inter integrated circuit (I2C). For some old age microcontrollers like PIC16F628A, the I2C is absent.
With CCS PICC compiler, the I2C interface could be implemented using I2C module inside the MCU. Anyway, without the I2C module any MCU with sufficient program memory could emulate this interface using an I2C library that use bit banging method.
In this example, the MCU read the temperature data from ds1621 thermometer. Temperature data is displayed on a 16x2 lines character LCD. Timer 0 schedule the temperature reading and displaying for every one minute.
The PICC source code:
#include <16F628A.h>
#fuses INTRC
#use delay(clock=4000000)
//i2c pins
#define DAL_SCL PIN_A0
#define DAL_SDA PIN_A1
//LCD pins
#define LCD_RS_PIN PIN_B0
#define LCD_RW_PIN PIN_B1
#define LCD_ENABLE_PIN PIN_B2
#define LCD_DATA4 PIN_B4
#define LCD_DATA5 PIN_B5
#define LCD_DATA6 PIN_B6
#define LCD_DATA7 PIN_B7
#include <ds1621.c>
#include<lcd.c>
BYTE value;
BYTE cntInterrupts=0;
float celsius;
/*this function activates
every one second
*/
void displayResult(void){
value = read_temp();
/*convert to degree Celsius*/
celsius = 5.0*(value-32)/9;
lcd_gotoxy(1,2);
printf(LCD_PUTC,"%.1f degree C\r\n",celsius);
}
void main() {
/*timer 0 is prescaled to 1:256*/
setup_timer_0( RTCC_DIV_256);
/*turn on timer 0 interrupt*/
enable_interrupts(INT_TIMER0);
/*turn on global interrupt control*/
enable_interrupts(GLOBAL);
/*clear timer 0 interrupt flage*/
clear_interrupt(INT_TIMER0);
/*initialize the ds1621 function*/
init_temp();
/*initialize the LCD function*/
lcd_init();
lcd_gotoxy(1,1);
printf(LCD_PUTC,"Temperature: ");
while(1){
/*if it's one second*/
if(cntInterrupts>=15){
displayResult();
cntInterrupts=0;
}
}
}
#INT_TIMER0
void overflowISR(void){
/*Each interrupt occurs every 65 mS*/
cntInterrupts++;
/*Clear flag*/
clear_interrupt(INT_TIMER0);
}
A test program read 27.7 degree Celsius from ds1621 thermometers. |
No comments:
Post a Comment