Arduino sprintf()Arduino sprintf

In one of our last post we started the series of Assembly language programming of Microchip PIC. This is the continuation of the previous series. In this article we are going to explain the use of the ADC of pic microcontroller. Specially pic16f based microcontrollers. Although this code is generic and could be adapted to any similar variant of the pic16 based microcontroller to read the ADC of pic microcontroller in assembly language programming but we are using the PIC16F887A microcontroller which is very similar to PIC16F877A microcontroller.

The ADC of pic microcontroller specially the PIC16F877A microcontroller ADC is 10 bit. Which means, you can read the value from 0-1023 in decimals. But because we are going to implement the ADC reading in assembly language and due to the nature of 8bit ( as PIC16 microcontroller’s are 8 bit microcontrollers) we have 8 bit registers to read in. But the 1023 value is 10 bit. So how to read it? one obvious answer is to read in two 8 bit registers which makes it 16 bit. So, if we are reading 16-bit we still left few bits untouched. So we have two options in this case, either we can shift right or shift left the ADC Readings and could read the bits accordingly.

One more thing to remember that there is only 1 ADC in the PIC16F887 microcontroller. But it have many channels which are associated with pics which could also be used as Digital IO pins rather than Analog input pins. So this is one more thing we need to configure before reading the sampled value from the ADC to specify the nature of the pin to be analog input.

For the example to be more objective oriented we used temperature sensor LM35 to read for this article but our main focus is to demonstrate how to use the ADC of PIC16 microcontrollers in assembly language. So let’s not waste more time and dive into the actual implementation part.

Components:

  • PIC16F877A microcontroller
  • LM35 temperature sensor
  • 10K ohm resistor
  • Breadboard
  • Jumper wires

Connections:

  • LM35: VCC to 5V, GND to GND, OUT to AN0
  • 10K ohm resistor: one end to AN0, the other end to GND

Key Concepts:

  • Analog-to-digital conversion
  • Temperature sensor interfacing
  • Assembly language programming

PIC Assembly Code for LM35 temperature Sensor

Here is the complete code which is used for reading the LM35 temperature sensor with the PIC16 microcontroller in assembly language.

LIST P=16F877A
INCLUDE<P16F877A.INC>

ORG 0
GOTO START

ORG 4
START:
BSF STATUS, RP0        ; Select bank 1
MOVLW b'00000000'      ; Configure AN0 as analog input
TRIS A
BSF ADCON0, ADON       ; Turn on ADC
BSF ADCON0, CHS0       ; Select AN0 channel
CLRF ADCON0            ; Start conversion
WAIT:
BTFSC ADCON0, GO_DONE  ; Wait for conversion to complete
GOTO WAIT
MOVLW 0x80             ; Set A/D conversion clock to FOSC/32
MOVWF ADCON1
BSF ADCON0, GO_DONE    ; Start conversion
WAIT1:
BTFSC ADCON0, GO_DONE  ; Wait for conversion to complete
GOTO WAIT1
MOVF ADRESH, W         ; Move result to W
MOVWF TEMP             ; Store result in variable TEMP
CALL DELAY             ; Delay function call
GOTO START

DELAY:
MOVLW .100             ; Delay for 100 ms
MOVWF COUNT
LOOP1:
MOVLW .250
MOVWF COUNT1
LOOP:
NOP
DECFSZ COUNT1, F
GOTO LOOP
DECFSZ COUNT, F
GOTO LOOP1
RETURN

TEMP EQU 20h

END
Code language: PHP (php)

Key Functions:

  • START: Sets up AN0 as analog input, turns on ADC, and starts conversion
  • WAIT: Waits for ADC conversion to complete
  • DELAY: Delays for 100ms between temperature readings
  • TEMP: Stores temperature reading

Tips:

  • Calibrate the LM35 sensor for accurate temperature readings.
  • Adjust the delay time as needed for your specific application.

This code-centric guide demonstrates how to create a PIC microcontroller-based temperature sensor using LM35 and assembly language programming. By following the provided code and connections, you can create a functional temperature sensor for various applications.

By Abdul Rehman

My name is Abdul Rehman and I love to do Reasearch in Embedded Systems, Artificial Intelligence, Computer Vision and Engineering related fields. With 10+ years of experience in Research and Development field in Embedded systems I touched lot of technologies including Web development, and Mobile Application development. Now with the help of Social Presence, I like to share my knowledge and to document everything I learned and still learning.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.