Thursday, March 11, 2021

PIC16F887 ADC interfaces to LM35 thermometer

 Introduction LM35

LM35 is thermometer that converter surrounding temperature to analog voltage. Its analog voltage voltage is proportional to temperature that is 10mV per degree Celsius. It's supplied from a DC voltage source ranging from 4 to 30V. Conversion temperature is between -55 to 150 degree Celsius. 

PIC16F887 ADC interfaces to LM35 thermometer
Sample of this example program
 

Dual-In-Line Package of TO-92 is very popular for hobbyist prototyping of this kind of sensor. It's a three-pin transistor-like electronics device. These three pins are VDD, VOUT, and GND.

PIC16F887 ADC interfaces to LM35 thermometer
LM35DZ TO-92 transistor-like package

 From electronics component store this device costs below one USD.

Interfacing and Programming using MikroC

Using ADC module inside PIC16F887, temperature reading could be done easily and precise due to 10-bit resolution inside ADC module. Floating point calculation in program C allow result acquisition calculated within a line of C code.

PIC16F887 ADC interfaces to LM35 thermometer
Circuit Diagram
In this programming example, AN0 of PIC16F887 reads voltage value that represent temperature data. Conversion temperature in only positive since voltage reference of ADC here set to positive rail only.

Temperature data will show on multiplexed display driven by PortB. Device supplied from stabilized 5V DC voltage with 8MHz internal oscillator.



MikroC source code:

  1. #define STEP_SIZE 0.0048
  2. #define ONE_SECOND 30
  3. #define Volt_Per_Celcius 0.01
  4. #define RATE 1
  5. #define DIGIT1 PORTA.RA4
  6. #define DIGIT2 PORTA.RA5
  7. #define DIGIT3 PORTA.RA6
  8. #define DIGIT4 PORTA.RA7
  9.  
  10. unsigned ADC_Value;
  11. float Voltage,temp;
  12. char int_count,Temp_Data;
  13. char LED[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
  14. bit ADC_RUN;
  15.  
  16. void interrupt() {
  17. if(INTCON.T0IF) {
  18. int_count++;
  19. if(int_count==ONE_SECOND)
  20. { int_count=0; ADC_RUN=1; }
  21. }
  22. INTCON.T0IF=0;
  23. }
  24.  
  25. void PORT_SETUP() {
  26. PORTA=0x00;
  27. PORTB=0x00;
  28. TRISA=0x01; // RA0 IS ANALOG IN PUT
  29. TRISB=0x00; // PORTB is OUTPUT
  30. ANSEL=0x01; // SELECT AN0
  31. ANSELH=0x00; // OTHERS ARE DIGITAL IO
  32. OSCCON|=0x07; // SELECT 8MHz INT RC
  33. }
  34.  
  35. void Timer0_Setup(){
  36. OPTION_REG.T0CS=0; // SELECT FOSC/4
  37. OPTION_REG.PSA=0; // SELECT TIMER0 PRESCALER
  38. OPTION_REG|=0x07; // SELECT 1:256 PRESCALER
  39. }
  40.  
  41. void interrupt_setup() {
  42. INTCON.GIE=1;
  43. INTCON.T0IE=1;
  44. INTCON.T0IF=0;
  45. }
  46.  
  47. void Read_Temp () {
  48. if(ADC_RUN=1) {
  49. ADC_Value=ADC_Get_Sample(0); // READ AN0
  50. delay_ms(10);
  51. Voltage=ADC_Value*STEP_SIZE; // Convert to Decimal [Volt]
  52. }
  53. ADC_RUN=0;
  54. }
  55.  
  56. void SSD() {
  57. Temp_Data=Voltage/Volt_Per_Celcius; // convert Voltage to integer
  58.  
  59. PORTB=LED[Temp_Data/10]; //10's
  60. DIGIT1=1;
  61. delay_ms(RATE);
  62. PORTA=0x00;
  63.  
  64. PORTB=LED[Temp_Data%10]; // 1's
  65. DIGIT2=1;
  66. delay_ms(RATE);
  67. PORTA=0x00;
  68.  
  69. PORTB=0x63; // o
  70. DIGIT3=1;
  71. delay_ms(RATE);
  72. PORTA=0x00;
  73.  
  74. PORTB=0x39; // C
  75. DIGIT4=1;
  76. delay_ms(RATE);
  77. PORTA=0x00;
  78. }
  79.  
  80. void main() {
  81. ADC_Value=0;
  82. int_count=0;
  83. ADC_RUN=0;
  84. PORT_SETUP();
  85. ADC_Init();
  86. Timer0_Setup();
  87. Interrupt_Setup();
  88. while(1) {
  89. Read_Temp();
  90. SSD();
  91. }
  92. }

Timer0 is added to this example program. It's used for creating timer ticks that schedule ADC reading, and activating each digit of multiplexing display.

Click here to download this example.



No comments:

Post a Comment

Labels

ADC (10) Analog (14) Arduino (12) Atmega16 (19) Audio (2) AVR (20) Charger (1) Cortex-M0 (1) Counter (10) CPLD (25) Digital I/O (22) Display (34) EEPROM (2) Environment Sensor (1) esp8266 (2) Experiment Board (10) I2C (4) Interrupt (7) LCD (1) LDmicro (29) measurement and instrumentation (7) Microchip Studio (3) MikroC (1) One-Shot (3) OpAmp (1) PCB (31) PIC16 Microcontrollers (16) PIC16F877A (2) PIC16F887 MikroC (22) PLC (35) PWM (11) Regulator (1) RTC (2) Sensor (8) Shift Registers (5) SPI (5) Timer (34) UART (2) ultra-sonic sensor (1) USB (1) VHDL (21) xc8 (1) XC95108 (9) XC9536 (15) XC9572 (1) Xilinx (23) Xilinx ISE (22)