Tuesday, August 25, 2020

Reading And Displaying The Temperature From DS18S20 Using PIC16F630

The PIC16F630

PIC16F630 is an 8-bit PICMicro released a decade a go. I bought a dozen of it for a simple project at a company at that time. So fa, I still have them left in my components storage box.


It's a 14-pin device with a DIP package enable good choice for hobbyist prototyping. The device typically supplied from 2.0 V to 5.5 V, normally 5 V is preferred for most peripheral device interfacing.


The program memory is 1024 words, with 64 bytes of internal SRAM. The auxiliary non-volatile memory EEPROM is 128 bytes.


It could clock up to 20 MHz. However with ease of prototyping and spacing saving, we can use it's internal RC oscillator that could clock up to 4 MHz with 1% error.


In this example, reading a temperature from one-wire device and just making a display, selecting this device is more economics.

The DS18S20 1-Wire Digital Temperature Sensor

DS18S20 is digital thermometer come with only three pins, two supply pin and one bi-directional data pin.


Reading And Displaying The Temperature From DS18S20 Using PIC16F630
DS18S20 sample picture an pin diagram

The device comes with both TO-92 and SMD package. A TO-92 is preferred for most student and hobbyist prototyping.


Temperature resolution is 9-bit. The temperature conversion ranges from -55 to +125 degree Celsius. The result includes one bit of fraction with the fraction number of 0.5 degree Celsius.


It needs a DC supply voltage from 3.0 V to 5.5 V. The data DQ pin is open-drain, thus requires a pull up resistor with the resistance from 4.7 to 10 kOhm depending on the wire length. 


One-wire protocol made by the manufacturer requires a good embedded programming skill. It needs a precise timing, and there a lot of device command such as a Skip ROM and a Read Scratchpad command.


For a single device connection, the reading process is simpler. A single DS18S20 device temperature reading flow list below.


A screen shot from device datasheet. Here there's a single DS18S20 connected on the bus.

For timing diagram and data transfer criteria, please check its datasheet on the web.

CCS PICC Programming And Interfacing

CCS PICC includes a driver for one-wire device reading. It works with DS1820, DS18S20 and DS18B20 1-wire device.

CCS PICC 1-Wire Driver

The touch.c contains many function to work with 1-wire device.

  • data = touch_read_bit()     Reads one bit from a touch device                                                                   
  • data = touch_read_BYTE()    Reads one byte from a touch device.                                                           
  • ok = touch_write_bit(data)  Writes one bit to a touch device and returns true if all went ok. A false indicates a collision with   another device.                                                                                                                                                                                 
  • ok = touch_write_byte(data) - Writes one byte to a touch device and returns true if all went ok. A false indicates a collision with  another device.                                                                                                                                                                              
  • present = touch_present()  - Issues a reset and returns true if the touch device is there.                                                
  • reset_pulse() - Issues a reset and waits for a present pulse.   

Programming And Circuit Simulation

I planned the circuit requirement as per in the schematic.

Reading And Displaying The Temperature From DS18S20 Using PIC16F630
Schematic Diagram

CCS PICC program lists below.



#include<16f630.h>
#fuses INTRC_IO,NOMCLR
#use delay(clock=4000000)

#define TOUCH_PIN  PIN_A5

#include<touch.c>

#define LCD_ENABLE_PIN  PIN_C1                                    
#define LCD_RS_PIN      PIN_A0                                    
#define LCD_RW_PIN      PIN_C0                                    
#define LCD_DATA4       PIN_C2                                    
#define LCD_DATA5       PIN_C3                                    
#define LCD_DATA6       PIN_C4                                    
#define LCD_DATA7       PIN_C5     

#include<LCD.c>

byte buffer[2];
byte tempDecimal,tempFraction;

void temperatureRead(void){
   if(touch_present()) {
         //Skip ROM
        touch_write_byte(0xCC);
        //Convert T
        touch_write_byte (0x44);
        output_high(TOUCH_PIN);
        delay_ms(1000);

        touch_present();
        //Skip ROM
        touch_write_byte(0xCC);
        //Read Scratchpad
        touch_write_byte (0xBE);
        //LSB
        buffer[0] = touch_read_byte();
        //MSB
        buffer[1] = touch_read_byte();
    
     }
}

void main() {
    
   setup_comparator(NC_NC_NC_NC);
   lcd_init();
  
   lcd_gotoxy(1,1);
   printf(LCD_PUTC,"  Temperature:  ");
   temperatureRead();
   delay_ms(1000);
   while (TRUE) {
     
     temperatureRead();
     tempDecimal=buffer[0]>>1;
     tempFraction=buffer[0]&0x01;
       
     if(buffer[1]==0xFF) tempDecimal=(~buffer[0]>>1)+1;
     if(tempFraction==0x01) tempFraction=5;
     else tempFraction=0;
 
     lcd_gotoxy(1,2);
     printf(LCD_PUTC,"  %c%d.%d %cC ",buffer[1]?'-':' ',tempDecimal,tempFraction,223);
  }
}
The simulation screen shot lists below.

Reading And Displaying The Temperature From DS18S20 Using PIC16F630
Simulation Screen Shot

Example archive could be download here.





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 (54) 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 (46) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (66) 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 (2) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (30) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)