Saturday, May 16, 2020

PIC12F683 reading LM35 analog temperature sensor with 74HC595 driven seven-segment displays

PIC12F685 is an 8-pin DIP PIC microcontroller. However, it has a lot peripherals such as 10-bit ADC, timer, large program memory and EEPROM.

It has up to 6 inputs/outputs, if we internal RC oscillator, and disabling the master clear reset (/MCLR) pin.

Since it has only 6 inputs/outputs, we need more output expanding. I use six 74HC595 shift register to drive 6 digits seven-segment display. 

The overall 74HC595 combination interface to the PIC12F685 with only three pins:

  1. serial clock

  1. serial data

  1. and latch pin

The PIC12F685 shifts data to the display system serially using these three pins.

LM35 is an analog temperature sensor, comes with three pins:

  1. DC supply voltage
  2. analog temperature data out
  3. DC supply ground     

The DC supply voltage ranges from +4 V to +30 V. But it is typically supplied at +5 V.

The analog temperature output is +10 mV per degree Celsius. With a 10-bit ADC module of PIC12F685 could precisely convert the analog temperature.

In this application, I use the temperature sensor to condition a DC fan. The fan will turn on if the temperature exceed 30 degree Celsius. 


PIC12F683 reading LM35 analog temperature sensor with 74HC595 driven seven-segment displays
Schematic diagram,
PIC12F683 CPU supplied at +5 V DC, clocked at 4 MHz
internal RC oscillator.
The display system driven by the 74HC595 shift register,
implementing only three MCU pins.
LM35 supplied at +5 V DC, fed the analog temperature
data to the MCU's pin GP4.
A DC fan output controlled by MCU's pin GP5.
It will turn on at the temperature above +30 degree
Celsius.


In this situation the system could only read the non negative temperature data, due to insufficient analog configuration pins.

The source code is written in CCS PICC compiler. The compiler has a lot of libraries. The 74HC595 is implemented with the compiler ready-to-use library.


Source Code:
#include<12F683.h>
//set the ADC to 10-bit
#device adc=10
#fuses INTRC_IO,NOWDT,NOMCLR
#use delay(clock=4M)
#define EXP_OUT_ENABLE  PIN_A2
#define EXP_OUT_CLOCK   PIN_A0
#define EXP_OUT_DO      PIN_A1
#define NUMBER_OF_74595 6
#include<74595.c>
#define FAN PIN_A5
void main(){
   unsigned char shiftData[6];
   unsigned char ssd[16]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,
0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71}; 
   unsigned int16 converter;
   long adcResult;
   float temperature;
   
   setup_oscillator(OSC_4MHz);
   
   output_low(FAN);
   setup_adc_ports(sAN0);
   setup_adc(ADC_CLOCK_INTERNAL);
   set_adc_channel(3);
   delay_ms(10);
     
   read_adc(adc_start_only);
   
   while(1){
      adcResult=read_adc();
      while(!adc_done());
      
      //convert the analog voltage to temperature
      temperature = (5.0*adcResult/1024)*1000;
      
      converter=(int16)temperature;
      
      //display the temperature data
      shiftData[0]=ssd[converter/1000];
      if(converter<1000) shiftData[0]=0;
      shiftData[1]=ssd[(converter%1000)/100];
      if(converter<100) shiftData[1]=0;
      shiftData[2]=ssd[(converter%100)/10]|0x80;
      shiftData[3]=ssd[converter%10];
      shiftData[4]=0b01100011;
      shiftData[5]=0b00111001;
      write_expanded_outputs(&shiftData);
      
      //conditioning the fan
      if((temperature/10)>30) output_high(FAN);
      else output_low(FAN);
      
      delay_ms(1000);
   }
}

The overall program require 44% of the 2048 word of the PIC12F683 program memory.


PIC12F683 reading the temperature from LM35
Simulation screen shot,
The temperature reading is 29.7 degree Celsius.
The DC fan is turned off.

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)