![]() | ![]() | ![]() |
| |||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
![]() |
| | Tools |
| | #1 |
|
I received my PicKit2 a few weeks ago and I've been playing around with it. I'm having fun and learning alot. I should be able to complete one of my projects soon. However, I have a question, and be gentle, I'm new to this. One of the tutorials that came with the kit had the following program to make an LED blink: #include <p16F690.inc> __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF) cblock 0x20 Delay1 ; Define two file registers for the Delay2 ; delay loop endc org 0 Start: bsf STATUS,RP0 ; select Register Page 1 bcf TRISC,0 ; make IO Pin B.0 an output bcf STATUS,RP0 ; back to Register Page 0 MainLoop: bsf PORTC,0 ; turn on LED C0 OndelayLoop: decfsz Delay1,f ; Waste time. goto OndelayLoop ; The Inner loop takes 3 instructions per loop * 256 loopss = 768 instructions decfsz Delay2,f ; The outer loop takes and additional 3 instructions per lap * 256 loops goto OndelayLoop ; (768+3) * 256 = 197376 instructions / 1M instructions per second = 0.197 sec. ; call it a two-tenths of a second. bcf PORTC,0 ; Turn off LED C0 OffDelayLoop: decfsz Delay1,f ; same delay as above goto OffDelayLoop decfsz Delay2,f goto OffDelayLoop goto MainLoop ; Do it again... end My question is concerning Delay1 and Delay2. No value was specified for them in the program, yet it says that the inner and outer loops occur 256 times each. Is it assumed that if no value is specified that the value will be 256? From Nigel's tutorials I know that one can specify values and change the delay, but this program doesn't do that. | |
| |
| | #2 |
|
The valure 256 is the maximum value because when you decrement zero it goes to 255 and so the decfsz instruction gets repeated 256 times. If you want to make the delay shorter then insert 2 lines before the On/OffdelayLoop line with, Code: movlw .128 movwf Delay2 | |
| |
| | #3 |
|
So if a value is not specified, it defaults to the maximum value?
| |
| |
| | #4 | |
| Quote:
Mike. | ||
| |
| | #5 |
|
Alright, I think I got it. So when it is powered on, it may not blink with the time advertised at first, but once it goes through one cycle, the delays go to maximum. Thanks | |
| |
| | #6 |
|
Lets imagine that you have just powered on your PIC. The delay variables are in volatile RAM so will be 0. Your code performs the first decrement, so zero minus 1 equals 255 or 0xFF , remember it is a byte value so can only contain numbers from 0x00 to 0xFF. Alternatively if one of your delay variables contained 0xFF if you were to perform an increment the contents of the variable would "roll over" to 0x00. What your DECFSZ is doing is looking for when a decrement happens which sets the variable to 0, if the result of the decrement is 0 it sets a ZERO status flag, if DECFSZ finds this it performs the skip. So in essence your loop program starts at 0x00, 0xFF, 0xFE, 0xFD ..... until it goes 0x1, 0x0 = at this stage the ZERO flag is set and you code takes the alternate path. Here's a question for you, if the delay starts at 0x00 why doesnt the loop break immediately? Good Luck Mark | |
| |
| | #7 | |
| Quote:
| ||
| |
| | #8 |
|
UTMonkey, As well as the error pointed out by Nigel, you have one other, the decfsz instruction does not set the zero flag in the status register. It does skip the next instruction if the result is zero but it doesn't set the zero flag. While this may seem picky, assuming things like this can cause very frustrating bugs. I know because it has caused problems for myself in the past. Mike. | |
| |
| | #9 |
|
Nigel does a good impression of Jim Trott! I bow to everyone elses experience. Out of interest though, what does set the ZERO flag? | |
| |
| | #10 | |
| Quote:
Code: SUBFWB Subtract f from W with Borrow
Syntax: SUBFWB f {,d {,a}}
Operands: 0 ≤ f ≤ 255
d ∈ [0,1]
a ∈ [0,1]
Operation: (W) – (f) – (C) → dest
Status Affected: N, OV, C, DC, Z
__________________ ========================= Futz's Microcontrollers & Robotics ========================= Last edited by futz; 11th December 2007 at 04:57 PM. | ||
| |
| | #11 | |
| Quote:
| ||
| |
|
| Tags |
| delay, loop, question |
| Thread Tools | |
| Display Modes | |
| |
Similar | ||||
| Title | Starter | Forum | Replies | Latest |
| PIC Christmas Light Project | amdkicksass | Micro Controllers | 28 | 9th July 2007 08:54 PM |
| Motor Controllers and serial comm? | RedCore | Micro Controllers | 18 | 3rd July 2007 02:27 AM |
| calculator | sara_6252 | Micro Controllers | 8 | 1st June 2007 03:29 AM |
| Delay routine not working | gregmcc | Micro Controllers | 6 | 18th September 2005 06:23 PM |
| Effects | stephenpic | Micro Controllers | 6 | 19th January 2004 12:57 PM |