Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
;Blinky VR1 requires the use of VR1 (DIP switch VR1 = ON)
LIST p=16F88
INCLUDE "p16f88.inc"
ERRORLEVEL 0, -302
__CONFIG _CONFIG1, 0x2F30
__CONFIG _CONFIG2, 0x3FFC
movlf macro x,y ;MACRO movlf <literal>, <register>
movlw x ;W = literal
banksel y ;make sure it's in the right bank
movwf y ;register = W
endm
cblock 0x20 ;start of general purpose registers
Delay_Count ;delay loop counter
endc
org 0x000 ;this is where the program starts running
Init movlf 0x07, CMCON ;turn comparators off (make it like a 16F84)
movlf b'00000000', ADCON1 ;
movlf b'00000010', ANS ;select RA1/AN1 as analog input
movlf b'11001101', ADCON0 ;AD enabled, RC clock, AN1 selected (VR1) & begin conversion
Red1 movlf b'01000000', PORTA ;LED1 RED
movlf b'10111110', TRISA
call Delay
Red2 movlf b'10000000', PORTA ;LED2 RED
movlf b'00111111', TRISA
call Delay
Red3 movlf b'00000001', PORTA ;LED3 RED
movlf b'01111110', TRISA
call Delay
Green3 movlf b'10000000', PORTA ;LED3 GREEN
movlf b'01111110', TRISA
call Delay
Green2 movlf b'01000000', PORTA ;LED2 GREEN
movlf b'00111111', TRISA
call Delay
Green1 movlf b'00000001', PORTA ;LED1 GREEN
movlf b'10111110', TRISA
call Delay
goto Red1 ;repeat forever
Delay
banksel ADRESH ;
movf ADRESH, W ;W = VR1 value 0x00-0xFF
banksel ADCON0 ;
bsf ADCON0,0x2 ;start next conversion
banksel Delay_Count ;
movwf Delay_Count ;Delay_Count = W
addlw 0x00 ;used to set Zero flag
btfsc STATUS, Z ;if ADRESH = 0 (Z flag is set)
return ;then return early (no need to delay)
Loop
nop ;32KHz Clock (default internal OSC speed)
decfsz Delay_Count, f ;
goto Loop ;
return ;return
END
;###############################################################################
; Title: ADC, Servo Demo
; Author: Mike Webb
; Date: 21 Nov 2006
;################################################################################
include "p16f88.inc"
errorlevel -302 ; stop the annoying " Register in operand not in bank 0." error.
__config _CONFIG1, _CP_OFF & _CCP1_RB0 & _DEBUG_ON & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_ON & _PWRTE_ON & _WDT_OFF & _INTRC_IO
__config _CONFIG2, _IESO_OFF & _FCMEN_OFF
; Setup Variables
cblock 0x20
Count
OnTime
endc
org 0h
nop ; required for ICD2
bcf STATUS,RP1; bank 0 or 2
bsf STATUS,RP0; select bank 1
movlw (0<<NOT_RBPU|0<<INTEDG|0<<T0CS|0<<T0SE|0<<PSA|B'000'<<PS0)
movwf OPTION_REG; setup option reg
bcf TRISB,3 ; make b3 output
movlw B'111'<<IRCF0; =b'01110000'
movwf OSCCON; select 8MHz clock
movlw b'00001000'; Select bit 3
movwf ANSEL; ADC on RA3
bcf STATUS,RP0; back to bank 0
Loop Call ReadADC; read A3
movwf OnTime; Store it for later
movlw d'250'; 250*8 = 2000 cycles = 1mS
bsf PORTB,3; Turn output on
call DelayW; delay the 1mS
movfw OnTime; Get value from ADC
btfss STATUS,Z; if it's zero don't do any delay
call DelayW; delay W*8 = 0 to 255 = 0 to 1.03 mS -- near enough
bcf PORTB,3; Turn output off - pulse is complete
comf OnTime,W; Complement ADC value
addlw 1; com and inc = negate.
btfsc STATUS,Z; Don't do a delay of zero
call DelayW; delay (256-ADC value)*8 = time required to get to 2mS
movlw d'18'; We need to delay another 18mS
movwf Count; so setup a counter
TimeLoop movlw d'250'; 250*8 = 2000 clock cycles = 1mS
call DelayW; do the delay
decfsz Count,F; repeat it?
goto TimeLoop; yes, go around again
goto Loop; Go back and do it all again
DelayW
; will delay W*8 cycles including the call/return
addlw 0xff; add -1 to W 1
btfsc STATUS,Z; =zero? 1/2
goto DoneDelay; yes, better get out of here 2
nop; delay a bit 1
goto $+1; delay a bit more 2
goto DelayW; go around again 2
DoneDelay return ; 2
ReadADC movlw b'01'<<ADCS0|b'011'<<CHS0|b'0'<<GO|b'1'<<ADON
movwf ADCON0; select pin A3 and 16 TOSC
bsf STATUS,RP0; bank 1
movlw b'0'<<ADFM|b'1'<<ADCS2|b'00'<<VCFG0
movwf ADCON1; Left justify
bcf STATUS,RP0; back to bank 0
movlw 5; wait 5*8 = 40 cycles
call DelayW; for ADC capacitor to charge
bsf ADCON0,GO; Start the conversion
WaitADC btfsc ADCON0,GO; Conversion complete?
goto WaitADC; No, so keep waiting
movfw ADRESH; return with result in W
return
END
alexis_ezekiel said:Hello,
I am really new at assembly language programming, and I'm trying to learn how to program a pic to read ADC. can someone send me a sample code/guide me thru how a pic can read the ADC?
alexis_ezekiel said:Hello,
I am really new at assembly language programming, and I'm trying to learn how to program a pic to read ADC. can someone send me a sample code/guide me thru how a pic can read the ADC?