;-------------------------------------------------------------------------------------------- ; FILE: Flashing LED - 20MHz Clock ; AUTH: Ed's Projects ; DATE: 20/06/2016 ; DESC: When a button is held down on Pin 1 (RA1) an LED will flash on pin 10 (RB4) until released. ;-------------------------------------------------------------------------------------------- LIST P=16F628A, R=DEC ; All numbers Decimal format unless otherwise stated. include ; Define configurations, registers, etc. __config _CP_OFF & _CPD_OFF & _LVP_OFF & _BOREN_ON & _MCLRE_OFF & _PWRTE_ON & _WDTE_OFF & _HS_OSC ; _LP_OSC ; Low power crystal - 32kHz - 200kHz ; _XT_OSC ; Crystal / resonator - 100kHz - 4MHz ; _HS_OSC ; High speed crystal - 8MHz - 20MHz ; _RC_OSC ; External RC oscillator ; _RC_OSC_NOCLKOUT ; External RC oscillator without clockout pin, allows RA6 to be used as an I/O ; _INTOSC ; Internal oscillator - 4MHz ; _INTOSC_OSC_NOCLKOUT ; Internal oscillator - 4MHz without clockout pin ; _EC_OSC ; External clock ;-------------------------------------------------------------------------------------------- ;------------------------------------- Port Definitions ----------------------------------- ;-------------------------------------------------------------------------------------------- ; PortA - RA2 - Pin 1 - Input button ; ; PortB - RB4 - Pin 10 - Output for LED ;-------------------------------------------------------------------------------------------- ;--------------------------------------- Variables ---------------------------------------- ;-------------------------------------------------------------------------------------------- cblock h'20' ; 1st part of General Purpose Register I can use. delay1 ; Variable 1 delay2 ; Variable 2 endc ;-------------------------------------------------------------------------------------------- ;------------------------------------- Program Code --------------------------------------- ;-------------------------------------------------------------------------------------------- ORG 0 ; Start assembly here Reset vector. . ;-------------------------------------------------------------------------------------------- ;-------------- Initialisation - Set up all ports. Make unused ports outputs. ------------ ;-------------------------------------------------------------------------------------------- Init bsf STATUS,RP0 ; Set RP0 to 1 bcf STATUS,RP1 ; Set RP1 to 0 ; Bank 1 Selected clrf VRCON ; Set Vref to off clrf TRISB ; Set all PortB as Output movlw b'00000100' ; Load 00000100 to working register movwf TRISA ; Set bit 2 as input on PortA bcf STATUS,RP0 ; Set RP0 to 0 ; Bank 0 Selected movlw d'7' ; Load working register with "7" movwf CMCON ; Turn off comparators clrf PORTA clrf PORTB clrf delay1 clrf delay2 ; Clear variables and output ports ;-------------------------------------------------------------------------------------------- ;-------------------------------- Main part of program ------------------------------------ ;-------------------------------------------------------------------------------------------- Start btfss PORTA,2 ; Test bit PortA 2, skip if set goto Start ; Loop back to start label_1 movlw b'00010000' ; Load working register with 00010000 movwf PORTB ; Move working register contents to PortB call pause200ms ; call subroutine "pause200ms" clrf PORTB ; Clear contents PortB, all pins low call pause200ms ; call subroutine "pause200ms" goto Start ; Loop back to start pause200ms movlw d'199' ; load working register with "199" movwf delay1 ; move working register to variable "delay1" delay_1b movlw d'200' ; load woring register with "200" movwf delay2 ; move working register to variable "delay2" delay_1a nop ; No operation - waste time - 1us nop ; No operation - waste time - 1us decfsz delay2,f ; Decrement value of variable "delay2" by 1, skip next instruction when zero goto delay_1a ; Loop to label delay_1a delay_1c decfsz delay1,f ; Decrement value of variable "delay1" by 1, skip next instruction when zero goto delay_1b ; Loop to label delay_1b ; total of 200ms return ; Return subroutine call END ;Stop assembling here ;--------------------------------------------------------------------------------------------