Analogue input programming activity
Create the Input program
The entire INPUT.ASM program is shown below. INPUT reads the state of two of the CHRP pushbuttons and outputs a unique pattern for each button on the LEDs. Start a new project in MPLAB, copy the INPUT.ASM code into the project, and build the program.
;ANALOGUE.ASM v2.0 Last modified on August 3, 2010 ;=============================================================================== ;Description: Displays analogue potentiometer position on the LEDs. ;Start of MPLAB and processor configuration. include "p16f676.inc" ;Include processor definitions __config _CPD_OFF & _CP_OFF & _BODEN_OFF & _MCLRE_ON & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT ;End of MPLAB and processor configuration. ;Start of hardware equates. U2Q equ 2 ;U2 Phototransistor Port A bit position P1 equ 4 ;Potentiometer P1 Port A bit position U2LED equ 5 ;U2 LED Port A bit position ;Start of program. org 3FFh ;Oscillator calibration location oscillator org 00h ;Start of program memory bsf STATUS,RP0 ;Select memory register page 1 call oscillator ;Store pre-programmed oscillator calibration movwf OSCCAL ;constant in OSCCAL register goto init ;Start program after Interrupt vector org 05h ;Continue after interrupt vector init ;Initialize ports A and C for digital and analogue I/O bcf STATUS,RP0 movlw 7 ;Disable comparator and make movwf CMCON ;RA0-2 digital I/O movlw 00001100b ;Set A-D VDD reference, ch. 3, movwf ADCON0 ;left justified result banksel ANSEL ;Switch to register bank 1 movlw 11010111b ;Disable Port A pull-ups, set TMR0 to movwf OPTION_REG ;internal clock with prescaler 256 movlw 00001000b ;Set up AN3 (potentiometer) as an movwf ANSEL ;analogue input, others as digital I/O movlw 00010000b ;Set A-D FOSC/8 clock divider movwf ADCON1 movlw 11011111b ;Set RA5 as output and all movwf TRISA ;other PORTA pins as inputs clrf TRISC ;Set all PORTC pins as outputs banksel PORTC ;Return to PORTC register bank clrf PORTA ;Turn off photosensor LED clrf PORTC ;Turn all LEDs off bsf ADCON0,ADON ;Turn A-D converter on adConvert bsf ADCON0,GO_DONE ;Start A/D conversion adConvLoop btfsc ADCON0,GO_DONE ;Continue checking for end of conversion goto adConvLoop ;by waiting for GO_DONE to go low display bcf STATUS,C ;Clear carry before shifting rrf ADRESH,f ;A-D result one position right bcf STATUS,C ;Clear carry again for next shift rrf ADRESH,W ;to reduce result to 6 bits movwf PORTC ;Display result on LEDs goto adConvert ;Get the next conversion and repeat end
Download the program into the CHRP and verify its operation.

