I am trying to write my first program in assembler for PIC16F886. The program is supposed to do the following:
- Read ADC value on channel AN0
- If the value is smaller than 128 - turn LED off
- Otherwise - turn LED on
The schematic is rather simple:
And here is my assembly code.
list p=16f886 ; list directive to define processor #include <p16f886.inc> ; processor specific variable definitions __CONFIG _CONFIG1, _LVP_OFF & _FCMEN_ON & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_ON & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT __CONFIG _CONFIG2, _WRT_OFF & _BOR21V org 00 ;start at 0x00 VALUE EQU 0x80 ;value to compare adc results cblock 0x20 RESULTLO ;ADC 8-bit result endc port_config movlw b'11111110' ;set RB0 on PORTB to output movwf TRISB start bsf PORTB, 0 ;turn LED on adc_config BANKSEL ADCON1 movlw 0x00 ;left justify MOVWF ADCON1 ;Vdd and Vss as Vref BANKSEL TRISA ; BSF TRISA,0 ;Set RA0 to input BANKSEL ANSEL ; BSF ANSEL,0 ;Set RA0 to analog adc_sample BANKSEL ADCON0; MOVLW b'11000001' ;ADC Frc clock, MOVWF ADCON0 ;AN0, On BSF ADCON0,GO ;Start conversion BTFSC ADCON0,GO ;Is conversion done? GOTO $-1 ;No, test again BANKSEL ADRESH MOVF ADRESH,W ;Read lower 8 bits MOVWF RESULTLO ;Store in GPR space ; if RESULT <= VALUE movf RESULTLO,w addlw 255 - VALUE skpnc goto led_off ; if RESULT > VALUE movf VALUE,w addlw 255 - RESULTLO + 1 skpnc goto led_on ;loop goto adc_sample led_on bsf PORTB, 0 goto adc_sample led_off bcf PORTB, 0 goto adc_sample end
I expect the led to turn on/off when I change the resistance of the pot but instead the led constantly stays on.
Update: I suspect it has something to do with the way I compare
VALUE since I get the following warning during compilation:
Argument out of range. Least significant bits used.
Update: As I've mention in comments, the schematic is taken directly from Proteus. On the real device the controller is powered from 5V source, there should be a limiting resistor in series with a led (unless I drive the mcu from a smaller voltage) and nMCLR is tied to +5V through a 5k resistor.