Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Thread Tools Display Modes
Old 9th May 2008, 02:49 AM   (permalink)
New Member
Nelg is on a distinguished road
Default Simple Simon Delay Loop

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

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
Nelg is offline   Reply With Quote
Old 9th May 2008, 03:23 AM   (permalink)
Experienced Member
 
bananasiong is a glorious beacon of lightbananasiong is a glorious beacon of lightbananasiong is a glorious beacon of lightbananasiong is a glorious beacon of light
Default

Hi,
What value is in 22h?
__________________
Superman Returns..
http://www.supermanhomepage.com/imag...an-returns.gif

My friend created this account for me.. LOL
http://bananasiong.myminicity.com
oh.. somemore
http://bananasiong.myminicity.com/env
bananasiong is online now   Reply With Quote
Old 9th May 2008, 03:34 AM   (permalink)
New Member
Nelg is on a distinguished road
Default

I assumed 255...? i haven't given it a value and to my, very basic knowledge of programming, thought it was defaulted to 255...?
Nelg is offline   Reply With Quote
Old 9th May 2008, 03:53 AM   (permalink)
Experienced Member
 
Blog Entries: 4
blueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to behold
Send a message via Skype™ to blueroomelectronics
Default

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.
__________________
Bill
Smart Kits build Smart People

http://www.blueroomelectronics.com
blueroomelectronics is online now   Reply With Quote
Old 9th May 2008, 04:16 AM   (permalink)
New Member
Nelg is on a distinguished road
Default

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
Nelg is offline   Reply With Quote
Old 9th May 2008, 04:24 AM   (permalink)
New Member
Nelg is on a distinguished road
Default

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...?
Nelg is offline   Reply With Quote
Old 9th May 2008, 08:08 AM   (permalink)
New Member
 
qratman is on a distinguished road
Default

Quote:
Originally Posted by glen_cqu@yahoo.com

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
qratman is offline   Reply With Quote
Old 12th May 2008, 06:02 PM   (permalink)
Experienced Member
fiveten is on a distinguished road
Default

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
fiveten is offline   Reply With Quote
Old 12th May 2008, 11:48 PM   (permalink)
Experienced Member
 
jeremygaughan is on a distinguished road
Default

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
    
     cblock        20h
    irpulse, datagap, min
    endc
    
    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.
__________________
jeremy

Last edited by jeremygaughan; 12th May 2008 at 11:50 PM.
jeremygaughan is offline   Reply With Quote
Old 13th May 2008, 01:24 AM   (permalink)
Experienced Member
 
futz is a name known to allfutz is a name known to allfutz is a name known to allfutz is a name known to allfutz is a name known to allfutz is a name known to all
Default

Quote:
Originally Posted by jeremygaughan
you can search with your computer to find the .inc file and it has all the names of the registers of the pic.
Better yet, just look at the datasheet.
__________________
=========================
Futz's Microcontrollers & Robotics
=========================
futz is online now   Reply With Quote
Old 13th May 2008, 03:21 AM   (permalink)
Experienced Member
 
Blog Entries: 4
blueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to behold
Send a message via Skype™ to blueroomelectronics
Default

Quote:
Originally Posted by futz
Better yet, just look at the datasheet.
Often it's much easier to find in register names in the .inf file, and sometimes the names don't quite match in the datasheet.
__________________
Bill
Smart Kits build Smart People

http://www.blueroomelectronics.com
blueroomelectronics is online now   Reply With Quote
Old 13th May 2008, 03:41 AM   (permalink)
Experienced Member
 
futz is a name known to allfutz is a name known to allfutz is a name known to allfutz is a name known to allfutz is a name known to allfutz is a name known to all
Default

Quote:
Originally Posted by blueroomelectronics
and sometimes the names don't quite match in the datasheet.
That's bloody annoying! When they don't match I edit the include file. The 16F88 include had a bunch of mismatches.
__________________
=========================
Futz's Microcontrollers & Robotics
=========================
futz is online now   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Latest
Atmel, assembly:displaying on lcd Haidy Micro Controllers 13 11th February 2008 08:12 PM
PIC Christmas Light Project amdkicksass Micro Controllers 28 9th July 2007 07:54 PM
calculator sara_6252 Micro Controllers 8 1st June 2007 02:28 AM
LCD Alarm clock with PIC16F877 ( some pics included ) pasanlaksiri Micro Controllers 10 22nd May 2007 10:36 PM
Modified LED Blinker jonathan Micro Controllers 12 6th October 2003 01:42 PM



All times are GMT. The time now is 02:39 AM.


Electronic Circuits  |  Radio Controlled
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.