Photosensor 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.
;INPUT-POT.ASM v2.0 Last modified on August 3, 2010 ;=============================================================================== ;Description: Senses phototransistor input digitally. ;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 I/O bcf STATUS,RP0 movlw 7 ;Disable comparator and make movwf CMCON ;RA0-2 digital I/O 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 clrf ANSEL ;Set all PORTA pins to digital 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 PORTC ;Turn all LEDs off bsf PORTA,U2LED ;Turn photosensor LED on checkQ btfsc PORTA,U2Q ;Check phototransistor state goto qOff ;If high, turn LED off movlw 00000001b ;If low, turn LED on movwf PORTC goto checkQ ;Check phototransistor again qOff clrf PORTC ;Turn all LEDs off goto checkQ ;Check phototransistor again end
Download the program into the CHRP and verify its operation.

