; Tutorial3.asm - A/D delayed LED Blink
; Copyright (C) 2007 www.pic18f.com
;
; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program. If not, see .
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#include
CONFIG WDT=OFF; disable watchdog timer
CONFIG MCLRE = ON; MCLEAR Pin on
CONFIG DEBUG = ON; Enable Debug Mode
CONFIG LVP = OFF; Low-Voltage programming disabled (necessary for debugging)
CONFIG FOSC = INTOSCIO_EC;Internal oscillator, port function on RA6
org 0; start code at 0
Delay1 res 1;reserve 1 byte for the variable Delay1
Delay2 res 1;reserve 1 byte for the variable Delay2
Start:
CLRF PORTD ;clear PORT D
CLRF TRISD ;set PORT D to input
CLRF Delay1
CLRF Delay2
MOVLW B'00001110';ADCON1 setup: VSS,VDD ref. AN0 analog only
MOVWF ADCON1
CLRF ADCON0 ;clear ADCON0 to select channel 0 (AN0)
;ADCON2 setup: Left justified, Tacq=2Tad, Tad=2*Tosc (or Fosc/2)
MOVLW B'00001000'
MOVWF ADCON2
BSF ADCON0,ADON ;Enable A/D Conversion Module
MainLoop:
BSF ADCON0,GO_DONE ;Start A/D Conversion
BTFSC ADCON0,GO_DONE ;Loop here until A/D conversion completes
GOTO $-2
BTG PORTD,RD1 ;Toggle PORT D PIN 1 (20)
CALL DelayAD ;Call A/D result-based Delay Function
goto MainLoop ;Restart
DelayAD: ;Delay function based on A/D result
MOVFF ADRESH,Delay2 ;move 8 MSb from A/D result to Delay2
;Make sure no overflows occur
INFSNZ Delay2,F
DECF Delay2,F
Delay:
DECFSZ Delay1,1 ;Decrement Delay1 by 1, skip next instruction if Delay1 is 0
GOTO Delay
DECFSZ Delay2,1
GOTO Delay
RETURN
end