I know this should be reallllly simple, but it is driving me insane...
could someone please add comments to make the understanding of the code so simple that even I could understand it...? I know it should be as simple as 1-1=0 but I am still struggling with it...
Delay
movlw 20h ; .32 in W register
movwf 21h ; placing .32 from W register into F (21h)
Loop1 ; subroutine name
decfsz 21h,1 ; decrementing 21h x 1 per loop (32,31,30....0 take 2 steps forward)
goto loop1 ; counting down from 32
decfsz 22h,1 ; count down from 35 x 255 times(32+3;including instruction cycles)
goto loop1
return
By my calculation this should countdown 8925 cycles which is still to fast to see with the eye (using 4mhz crystal), but the LEd is flashing (by my estimate) at 1/4 second...
Thanks for the help, i really appreciate the responses...
As you've probably figured out, i'm very much a newbie having a play...I've been working through the various tutorials, but this delay thing has just had me stumped for a long time! the code I'm using is
********************************************************
; Tutorial for Intro Operators
LIST p=16F628a ;tell assembler what chip we are using
include "P16F628a.inc" ;include the defaults for the chip
ERRORLEVEL -224 ; ignore this error during build
ERRORLEVEL -302 ; ignore this error during build
org 0000h ;org sets the origin, 0x0000 for the 16F628a,
;this is where the program starts running
; Turn CMCON (comparators RA3 & 4) OFF and makes more like 16f84a
; movlw b'0111' ;store bits to turn comparators OFF and enable I/O
; movwf 1fh ;send comparator bits to CMCON
;------------------------------------------------------------------------
; leave data manipulation mode
; go into pic configure mode
; Prepare I and/or O
; and return to data manipulation mode
bsf 03h,5 ;open pic configure mode (bank1) by setting (BitSetF) bank0, bit#5
; bit#'s 76543210
movlw b'00000000' ;store I/O configuration - 0=output 1=input
movwf 85h ;send configuration to TRISA (porta)
movwf 86h ;send configuration to TRISB (portb)
bcf 03h,5 ;return to pic control mode (bank0) by clearing (BitClearF) bank0, bit#5
start
movlw b'10000000'
movwf 06h
call Delay ;this waits for a while!
movlw b'01000000'
movwf 06h
call Delay ;this waits for a while!
movlw b'00100000'
movwf 06h
call Delay ;this waits for a while!
movlw b'00010000'
movwf 06h
call Delay ;this waits for a while!
movlw b'00001000'
movwf 06h
call Delay ;this waits for a while!
movlw b'00000100'
movwf 06h
call Delay ;this waits for a while!
movlw b'00000010'
movwf 06h
call Delay ;this waits for a while!
movlw b'00000001'
movwf 06h
call Delay ;this waits for a while!
movlw b'00000010'
movwf 06h
call Delay ;this waits for a while!
movlw b'00000100'
movwf 06h
call Delay ;this waits for a while!
movlw b'00001000'
movwf 06h
call Delay ;this waits for a while!
movlw b'00010000'
movwf 06h
call Delay ;this waits for a while!
movlw b'00100000'
movwf 06h
call Delay ;this waits for a while!
movlw b'01000000'
movwf 06h
call Delay ;this waits for a while!
goto start ;go back and do it again
********************************************************
I understand your saying the address needs to be initialised...but I have no idea why the led's blink at 1/4 second rate...?
I've also tried modifying the movlw 20h to 30h but it doesn't seem to change any on/off timing...?
in my experimenting; if I add an additional loop (red coloured) it extends the time to about 20-30sec (haven't actually timed it but that is how long it seems)
By my calculation this should countdown 8925 cycles which is still to fast to see with the eye (using 4mhz crystal), but the LEd is flashing (by my estimate) at 1/4 second...
You didn't take into account all the instructions PIC is executing. Also you should know that while most instructions take 1 cycle to execute, some take 2 cycles (jumping ones: goto, call, ..)
According to my calculations (if there's no mistake) the whole delay takes total of 72963 cycles. With 4Mhz crystal this is the same amount microseconds or 72.962ms. LED blinking with 73ms on, 73ms off (period is then 146ms) should already be visible to eye as separate blinks.
Code:
Delay
movlw 20h ; 1 cycle
movwf 21h ; 1 cycle
; total so far: 2 cycles
loop1:
decfsz 21h, F ; takes 1 cycle default, 2 cycles when skipping (21h == 0)
goto loop1 ; takes always 2 cycles
; TOTAL: 32x3-1 = 95 cycles
decfsz 22h,1 ; assume 22h is zero at the beginning, takes 1 cycle unless register skipping, then takes 2
goto loop1 ; takes always 2 cycles
return ; takes always 2 cycles
; TOTAL: 256x3x95-1+2+2 = 72 963
I have had a lot of success using Nigel Goodwin's tutorials. There is a led flashing tut and code for delays. I would go there for more knowledge on coding for delays and other functions. Good luck!
This allows you to put a name on the F files starting at 20h. You can name them and then use them later. So instead of writing "movwf 20h" you can write "movwf timecounter" This way later when you are use the decfsz command you know exactly which file you're playing with and it's easy to go look what value you put in it. Also since you used the include file you can use the names of the places you want to send things to, like the TRISA or GPIO.......you can search with your computer to find the .inc file and it has all the names of the registers of the pic.