Created
October 3, 2017 10:30
-
-
Save Xhendos/7c5df784dda7905dcc26a95dfc0d991d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
; ; | |
; Created by : Youri Klaassens ; | |
; Date : 30 september, 2017 ; | |
; Description : Blink 2 LEDs every 0,5s ; | |
; ; | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
#include <p16f1829.inc> | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
; ; | |
; Configuration bits ; | |
; ; | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
__CONFIG _CONFIG1, (_FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_ON & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF); | |
__CONFIG _CONFIG2, (_WRT_OFF & _PLLEN_OFF & _STVREN_OFF & _LVP_OFF); | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
; ; | |
; Common RAM starts at 0x70 and ends at location 0x7F ; | |
; -> 0x70 = 7 * 16 = 112 ; | |
; -> 0x7F = (7 * 16) + 16 = 128 ; | |
; The difference is 16 which means we have 16 bytes common RAM available ; | |
; ; | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
cblock 0x70 ;First location address where common RAM starts | |
__delay1 ;Define first variable which is used to decrement | |
__delay2 ;Define second variable which is used to decrement | |
endc | |
org 0 ;Lets start at the begin 0x0000 | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
; ; | |
; INIT ; | |
; ; | |
; Initialise the microcontroller so the program can execute exactly what we want to do. ; | |
; Set all directions of the TRISC register to '0' (OUTPUT) ; | |
; Set all logic levels of the LATC register to '0' (LOW) ; | |
; ; | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
INIT: | |
banksel TRISC ;Select the bank where TRISC sits in (Bank1) | |
clrf TRISC ;Set all pins in the TRISC register to OUTPUT | |
;OSCCON bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 | |
; SPLLEN IRCF<3;0> ---- SCS<1;0> | |
movlw b'00111000' ;Set the correct bit pattern to the working register | |
movwf OSCCON ;Copy the bits in the working register to the OSCCON register (Sets internal oscillator to 500 kHz) | |
banksel LATC ;Select the bank where LATC sits in (Bank 2) | |
clrf LATC ;Set all pins in the LATC register to LOW | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
; ; | |
; SETDELAY1 ; | |
; ; | |
; Move the correct values to the correct RAM location. ; | |
; __delay1 (inner loop) will get the decimal number 83 ; | |
; __delay2 (outer loop) will get the decimal number 247 ; | |
; ; | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
SETDELAY1: | |
movlw 0x53 ;Export value 83 to the working register | |
movwf __delay1 ;Export value 83 to the common RAM (0x70) | |
movlw 0xF7 ;Export value 247 to the working register | |
movwf __delay2 ;Export value 247 to the common RAM (0x71) | |
nop ;So there are 62 500 - 9 = 61 491 instructions between the 2 loops. | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
; ; | |
; TURNDS1ONDS4OFF ; | |
; ; | |
; Set LED DS1 logic HIGH and set LED DS4 logic LOW. ; | |
; Before we do this we use loops to create a delay. ; | |
; ; | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
TURNDS1ONDS4OFF: | |
decfsz __delay1, f ;Decrement the value which is stored in location __delay1 by 1. If 0 we skip the next line. | |
bra TURNDS1ONDS4OFF ;Go to label TURNDS1ONDS4OFF | |
movlw 0x53 ;Export value 83 to the working register | |
movwf __delay1 ;Export value 83 to the common RAM (0x70) | |
decfsz __delay2, f ;Decrement the value which is stored in locatioon __delay2 by 1. If 0 we skip the next line. | |
bra TURNDS1ONDS4OFF ;Go to label TURNDS1ONDS4OFF | |
bcf LATC, 3 ;Turn LED DS4 OFF | |
bsf LATC, 0 ;Turn LED DS1 ON | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
; ; | |
; RESETDELAY1 ; | |
; ; | |
; Move the correct values to the correct RAM location. ; | |
; __delay1 (inner loop) will get the decimal number 83 ; | |
; __delay2 (outer loop) will get the decimal number 247 ; | |
; Execute a few nops so there are enough instructions between the the 2 loops ; | |
; ; | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
RESETDELAY1: | |
movlw 0x53 ;Export value 83 to the working register | |
movwf __delay1 ;Export value 83 to the common RAM (0x70) | |
movlw 0xF7 ;Export value 247 to the working register | |
movwf __delay2 ;Export value 247 to the common RAM (0x71) | |
nop ;A no operation so we have 8 instructions between the loops | |
nop ;A no operation so we have 8 instructions between the loops. | |
nop ;So there are 62 500 - 9 = 61 491 instructions between the 2 loops. | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
; ; | |
; TURNDS1OFFDS4ON ; | |
; ; | |
; Set LED DS1 logic LOW and set LED DS4 logic HIGH. ; | |
; Before we do this we use loops to create a delay ; | |
; Go to label SETDELAY1 to start the proces again (this causes the blink effect) ; | |
; ; | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
TURNDS1OFFDS4ON: | |
decfsz __delay1, f ;Decrement the value which is stored in location __delay1 by 1. | |
bra TURNDS1OFFDS4ON ;Go to label TURNDS1OFFDS4ON | |
movlw 0x53 ;Export value 83 to the working register | |
movwf __delay1 ;Export value 83 to the common RAM (0x70) | |
decfsz __delay2, f ;Decrement the value which is stored in locatioon __delay2 by 1. | |
bra TURNDS1OFFDS4ON ;Go to label TURNDS1OFFDS4ON | |
bsf LATC, 3 ;Turn LED DS4 ON | |
bcf LATC, 0 ;Turn LED DS1 OFF | |
bra SETDELAY1 ;go to label SETDELAY1 (So the whole proces starts again) | |
end ;End the program (we never reach this) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
; ; | |
; ~Calculations~ ; | |
; ; | |
; Instruction time = 1 / (frequency / 4) ; | |
; x = 1 / (500 000 / 4) ; | |
; { x is here equal to 0,000008 } ; | |
; 1 instruction takes 8 microseconds. ; | |
; ; | |
; inner_loop = (((((__delay1 - 1) * 3) + 2) + 2) + 3) ; | |
; outer_loop = (inner_loop * __delay2) - 1 ; | |
; ; | |
; The first loop we created takes 3 instructions except if the value in common RAM is 0. ; | |
; If the value in common RAM is 0 the PIC needs 2 instructions which is the instruction 'dezfsz' ; ; | |
; We multiply the amount of instructions minus 1 by 3, because 1 cycle of the loop needds just 2 instructions ; | |
; The most usual case in the loop: ; | |
; -> (decfsz) 1 instruction ; | |
; -> (bra) 2 instructions ; | |
; ; | |
; The exception in the loop because decfsz skips the next line if value is 0: ; | |
; -> (decfsz) 2 instructions ; | |
; ; | |
; We add 2 by the result because we use the next 2 instructions in the loop: ; | |
; -> (movlw) 1 instruction ; | |
; -> (movwf) 1 instruction ; | |
; ; | |
; We add another 2 by the result because the exception of the loop (decfsz skips the next line) ; | |
; -> (decfsz) 2 instructions ; | |
; ; | |
; We add 3 to the result because the inner loop executes 3 instructions from the outer loop ; | |
; -> (decfsz) 1 instruction ; | |
; -> (bra) 2 instructions ; | |
; ; | |
; ; | |
; The outer_loop is all the instructions from the inner_loop multiplied by the amount of times inner_loop runs ; | |
; The minus 1 at the end is because the last cycle in the loop in is just 2 instructions ; | |
; -> (decfsz) 2 instructions ; | |
; The plus 3 from the inner loop needs to be substracted by 1 for just 1 time. ; | |
; ; | |
; ; | |
; We search a value between 1 and 255 for __delay1. ; | |
; since the outer_loop is the inner_loop multiplied by a value between 1 and 255, the maximum value is ; | |
; (62 491 / 255) = 245. This means that inner_loop must be a value between 245 and 255. ; | |
; ; | |
; if __delay1 is 83 we can calculate inner_loop by using the formula in line 153 ; | |
; -> inner_loop = (((((83 - 1) * 3) + 2) + 2) + 3) ; | |
; { inner_loop is here equal to 253 } ; | |
; ; | |
; To calculate the outer_loop we use the formula which is described in line 154 ; | |
; -> outer_loop = (253 * __delay2) - 1 ; | |
; -> 62 491 = (253 * __delay2) - 1 ; | |
; -> 62 492 = (253 * __delay2) ; | |
; { Use for __delay2 = 247 } ; | |
; -> instructions = 253 * 247 ; | |
; { instructions is here 62 491, but we need 62 492. We use 1 extra assembly operations which takes 1 cycle (nop) } ; | |
; ; | |
; ; | |
; __delay1 is 83 ; | |
; __delay2 is 247 ; | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment