;------------------------------------------------------------------------------- ; FILE: Flashing LED ; AUTH: Ed's Projects ; DATE: 04/02/2017 ; DESC: Flashing LED on PortA0, pin2. Input on PortB that allows the LED to flash or not. ;------------------------------------------------------------------------------- LIST P=18F4520, R=DEC ; All numbers Decimal format unless otherwise stated. include ; Define configurations, registers, etc. CONFIG OSC = INTIO67 ; 8 MHz internal oscillator CONFIG FCMEN = OFF ; Failsafe clock monitor off CONFIG IESO = OFF ; Internal / External oscillator switchover off CONFIG PWRT = ON ; Power up timer on CONFIG BOREN = OFF ; Brown out reset off CONFIG WDT = OFF ; Watchdog timer off CONFIG MCLRE = OFF ; Master reset pin off CONFIG LPT1OSC = OFF ; Low power timer 1 off CONFIG PBADEN = OFF ; PortB 4 to 0 digital on reset CONFIG LVP = OFF ; Low voltage programming off CONFIG XINST = ON ; Extended instruction set enabled ;------------------------------------------------------------------------------- ;-------------------------------- Variables ---------------------------------- ;------------------------------------------------------------------------------- cblock 0x000 b0_temp b0_temp2 b0_temp3 endc ;------------------------------------------------------------------------------- ;------------------------------- Program Code -------------------------------- ;------------------------------------------------------------------------------- ORG 0x0 ; Reset Vector ORG 0x0008 ; High Priority Interrupt Vector ORG 0x0018 ; Low Priority Interrupt Vector ;------------------------------------------------------------------------------- ;-------------------------- Initialisation of Ports -------------------------- ;------------------------------------------------------------------------------- Init movlb d'15' ; Set BSR to bank 15 clrf ADCON0 ; Turn off analogue module movlw 00001111b movwf ADCON1 ; Configure all pins as digital movlw 00000111b movwf CMCON ; Turn off comparators movlw 00000010b movwf TRISA ; Set PORTA 1 as input movlw d'255' movwf TRISB clrf TRISC clrf TRISD ; Set all ports as outputs clrf TRISE movlw 01110000b movwf OSCCON ; 8MHz Internal oscillator movlw 01000000b movwf OSCTUNE ; Enable PLL ; 32MHz with PLL ;------------------------------------------------------------------------------- ;------------------------------- Main Program -------------------------------- ;------------------------------------------------------------------------------- Main movlw 10100101b cpfseq PORTB, 1 ; is PortB equal to working register goto Main ; if no go to Main ; if yes then continue btg LATA, 0, 1 ; toggle portA0, BSR pointer call delay ; 250ns goto Main ; 125ns per instruction at 32MHz clock delay movlw d'100' ; 125ns movwf b0_temp3, 0 ; 125ns nop ; 125ns label_3 movlw d'40' ; 125ns movwf b0_temp2, 0 ; 125ns nop ; 125ns nop ; 125ns label_1 movlw d'249' ; 125ns movwf b0_temp, 0 ; 125ns nop ; 125ns nop ; 125ns label_2 nop ; 125ns nop ; 125ns nop ; 125ns nop ; 125ns nop ; 125ns decfsz b0_temp, f, 0 ; 125ns goto label_2 ; 250ns (1us total inner loop) ; 125ns decfsz b0_temp2, f, 0 ; 125ns goto label_1 ; 250ns ; 125ns decfsz b0_temp3, f, 0 ; 125ns goto label_3 ; 250ns ; 1us x 249 = 249us ; 249 + 1 = 250us , 250us x 40 = 10ms ; 10ms + 1us = 1001us, 10001us x 100 = 1000100us, or 1.0001s ; 125ns return ; 250ns ; Our final delay 1.0001s + 750ns + 250ns (original call) ; 1.000101s end