Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Simple Simon Delay Loop

Status
Not open for further replies.

Nelg

New Member
Hi All,

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...:eek:

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 heaps
 
I assumed 255...? i haven't given it a value and to my, very basic knowledge of programming, thought it was defaulted to 255...?
 
RAM variables need to be initialized, they can contain anything on powerup.

PS run your code through the MPLAB simulator, there's a stopwatch function too.
 
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

__config B'11110100011000'
;sets the configuration bits (SEE P98 OF DATASHEET)
; bit#'s 13 12 11 10 9 8 7 6 5 4 3 2 1
; code - - - - LVP | MCLR | WDTE | | |
; prot. BOREN PWRT OSC select

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

;------------------------------------------------------------------------

movlw 00h
movwf 06h

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



;
Delay
movlw 20h
movwf 21h

loop1 decfsz 21h,1
goto loop1
decfsz 22h,1
goto loop1
decfsz 23h,1
goto loop1

return

End

********************************************************
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)

Thanks again
 
I've just gone through the mplab simulator and the code loops between

loop1 decfsz 21h,1
goto loop1

more than 32 times...its looping more times than I care to keep pressing the mouse key...?
 
glen_cqu@yahoo.com said:
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!

http://www.winpicprog.co.uk

fiveten
 
some good advice I got here one day was to use a C block.
Code:
    list P=12f675
#include <p12f675.inc>
    __config _INTRC_OSC_NOCLKOUT & _BODEN_OFF & _WDT_OFF & _PWRTE_ON & _MCLRE_OFF & _CPD_OFF & _CP_OFF 
    ERRORLEVEL -302
    
[COLOR=Red]     cblock        20h
    irpulse, datagap, min
    endc[/COLOR]
    
    bsf    STATUS,RP0        ;bank 1
    movlw    0x34
    movwf    ANSEL
    movlw    0x00
    movwf    TRISIO        ;set I/O
    bcf    STATUS,RP0
    movlw    0x07        ;turn off comparitors
    movwf    CMCON
    movlw    0x09
    movwf    ADCON0

start

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.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top