Wednesday, May 13, 2020

A Temperature Controlled Lamp By PIC16F84A Using ds18B20

PIC16F84A was a popular embedded controller in the last decades. It comes with only basic MCU components without ADC/DAC, hardware PWM etc.

Some temperature sensor such as LM35 or PTC have only analog output, need an ADC module of MCU to the convert temperature data.

DS18B20 is a digital temperature sensor, interface to the MCU only one digital pin. But the communication protocol is such complex.

ds18B20 TO-92 package and long wire types


CCS PICC compiler has a built in library for DS1920 digital thermometer.  The ds1920 has a similar data format with DS18B20.

In this case, I use ds18B20 which nearly identical to ds18S20. But ds18B20 has more precise floating point number.


The driver file touch.c is a ready to use library come with CCS PICC after the installation. It has reset(), read(), and write() function to

process the one-wire bus data.


ds18B20 with PIC16F84A and 16x2 Character LCD
Schematic diagram
PIC16F84A reads the temperature data from ds18B20 one-wire
thermometer, displays the data to the LCD.
The relay controls the ON/OFF lamp from PIN_A0.
The relay works around 30 degree Celsius.

Source Code written in CCS PICC, could be downloaded here:


#include <16F84A.h>
#fuses XT
#use delay(clock=4000000)

#define LCD_ENABLE_PIN  PIN_B3                                       
#define LCD_RS_PIN      PIN_B1                                        
#define LCD_RW_PIN      PIN_B2                                       
#define LCD_DATA4       PIN_B4                                       
#define LCD_DATA5       PIN_B5                                      
#define LCD_DATA6       PIN_B6                                      
#define LCD_DATA7       PIN_B7

#include <lcd.c>
#include <touch.c>
#include <string.h>
#include <stdlib.h>

#define RELAY PIN_A0
#define TEMP 30
void main() {
   byte buffer[2];
   lcd_init();
   set_tris_a(0x00);
   output_a(0x00);
  
   while (TRUE) {
     if(touch_present()) {
        touch_write_byte(0xCC);
        touch_write_byte (0x44);
        output_high(TOUCH_PIN);
        delay_ms(2000);

        touch_present();
        touch_write_byte(0xCC);
        touch_write_byte (0xBE);
        buffer[0] = touch_read_byte();
        buffer[1] = touch_read_byte();
       
        buffer[0]=(buffer[1]<<5)|(buffer[0]>>3);
       
        lcd_gotoxy(1,1);
        LCD_PUTC("TEMPERATURE:");
       
        lcd_gotoxy(1,2);
        printf(LCD_PUTC,"%c",(buffer[1]&0b11111000)?'-':' ');
        if(buffer[1]&0b11111000) buffer[0]=-buffer[0];
        lcd_gotoxy(2,2);
        printf(LCD_PUTC,"%d.%c C",buffer[0]/2,((buffer[0])&0x01)?'5':'0');
        delay_ms (500);
        lcd_putc("          ");
       
        if((buffer[0]/2)>TEMP) output_high(RELAY);
        else output_low(RELAY);
     }
  }
}

No comments:

Post a Comment

Search This Blog

Labels

25AA010A (1) 8051 (7) 93AA46B (1) ADC (30) Analog Comparator (1) Arduino (15) ARM (6) AT89C52 (7) ATMega32 (56) AVR (57) CCS PICC (28) DAC (1) DHT11 (2) Display (105) Distance Sensor (3) DS18B20 (3) dsPIC (2) dsPIC30F1010 (2) EEPROM (5) Environment Sensor (4) esp8266 (1) I2C (29) Input/Output (67) Interrupt (19) Keil (5) Keypad (10) LCD (47) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (71) Nokia 5110 LCD (3) OLED (2) One-Wire (6) Oscillator (8) PCB (6) PCD8544 (3) PCF8574 (5) PIC (107) PIC12F (2) PIC16F628A (2) PIC16F630 (1) PIC16F716 (3) PIC16F818 (10) PIC16F818/819 (2) PIC16F84A (15) PIC16F876A (1) PIC16F877A (9) PIC16F88 (1) PIC16F887 (60) PIC18 (19) PIC18F1220 (4) PIC18F2550 (3) PIC18F4550 (12) PWM (11) RTC (8) Sensor (10) SH1106 (1) Shift Register (11) Shift Registers (3) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (31) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)