View previous topic :: View next topic |
Author |
Message |
rvwills
Joined: 20 Dec 2014 Posts: 10
|
for (;n!=0;n--) |
Posted: Tue Aug 30, 2016 10:31 am |
|
|
Programmers:
I am a CCS C newbe and have been following the discussion regarding enabling "delay_in_seconds delay_seconds".
Here is the code I copied and am using with an 18f4520 PIC.
I have also edited this (copied) code by mixing "delay_in_seconds" with "delay_ms"
Code: |
#include <18f4520.h>
#device ICD=TRUE
#fuses HS,NOLVP,NOWDT
#use delay (clock=10000000)
#define GREEN_LED PIN_A5
#define YELLOW_LED PIN_B4
#define RED_LED PIN_B5
#define delay_in_seconds delay_seconds
void delay_seconds(int n) {
for (;n!=0;n--) {
delay_ms (1000);
}
}
void main () {
while (TRUE) {
output_low (GREEN_LED);
delay_in_seconds (2);
output_high (GREEN_LED);
delay_ms (1000);
output_low (YELLOW_LED);
delay_ms (2000);
output_high (YELLOW_LED);
delay_in_seconds (1);
output_low (RED_LED);
delay_in_seconds (2);
output_high (RED_LED);
delay_ms (3000);
}
} |
It's my understanding that the "for loop statement" contains,
"for ( Variable initialization; Conditional expression; Increment/decrement)"
My question regards the "for" statement. Specifically, where is the "Variable initialization" obtained for the following.
Code: |
void delay_seconds (int n) {
for (;n!=0;n--) {
|
Thanks in advance for your assistance.
RV Wills |
|
|
m4rcdbk
Joined: 29 Aug 2016 Posts: 10 Location: Michigan, USA
|
|
Posted: Tue Aug 30, 2016 10:49 am |
|
|
Your understanding of a for loop is correct;
The line in question means (or could be thought of) as
Quote: | instead of initializing n to a value, simply use the supplied number given to the function and decrement it until not zero, running the code under until the conditions are met. |
Bounds checks as well as some sanity checks should exist, but for the gist it is what it is. |
|
|
rvwills
Joined: 20 Dec 2014 Posts: 10
|
for (;n!=0;n--) |
Posted: Tue Aug 30, 2016 11:07 am |
|
|
m4rcdbk:
Am I correct then with the following?
the "Variable initialization" obtains "2" from "delay_in_seconds (2);
Bob |
|
|
rvwills
Joined: 20 Dec 2014 Posts: 10
|
for (;n!=0;n--) |
Posted: Tue Aug 30, 2016 11:15 am |
|
|
m4rcdbk:
Am I correct then with the following?
the "Variable initialization" obtains "2" from "delay_in_seconds (2);
and "variable initialization" obtains "1" from "delay_in_seconds (1);
etc.?
Thanks,
Bob |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Tue Aug 30, 2016 11:24 am |
|
|
this is a horrible way to make that delay- using a timer
would be much better.
the following traps zero seconds correctly too
Code: |
void delay_seconds(int n) {
while(n){ delay_ms (1000); --n;}
}
|
game over |
|
|
rvwills
Joined: 20 Dec 2014 Posts: 10
|
|
Posted: Tue Aug 30, 2016 11:37 am |
|
|
asmboy:
Thanks for your input with "void delay_seconds(int n) {
while(n){ delay_ms (1000); --n;}
} " |
|
|
m4rcdbk
Joined: 29 Aug 2016 Posts: 10 Location: Michigan, USA
|
|
Posted: Tue Aug 30, 2016 11:43 am |
|
|
rvwills, logically you're correct; it will obtain a value of "2" from delay_in_seconds and then run 2 loops decrementing the counter to 0, delaying by the constant value each time.
However, as asmboy mentions, is a/are better mechanism(s). That can be used. |
|
|
rvwills
Joined: 20 Dec 2014 Posts: 10
|
|
Posted: Tue Aug 30, 2016 12:06 pm |
|
|
m4rvdbk:
asmboy:
Thanks for your continued patience as I ask questions. I should probably offer an apology for not quickly understanding the best code for this consideration. My many questions likely is a "hang over" from my tutoring process with college math students.
Thanks again...
my game is just beginning |
|
|
m4rcdbk
Joined: 29 Aug 2016 Posts: 10 Location: Michigan, USA
|
|
Posted: Tue Aug 30, 2016 12:10 pm |
|
|
No problem; sometimes "what works" and "what are best" are not always the same. I often find it best when I have other people I work with print up their code, and review it to say "Okay. It works. Let's see how we can make this quicker/less error prone", so (I at least) don't see any issues; especially while you're learning. |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Tue Aug 30, 2016 12:16 pm |
|
|
correctly handling zero as an accidental input argument is
a common problem for new PIC programmers.
I only aim to offer simpler -robust code.
the same wrapper with multiplication could be used for timer polled overflow
rollovers - to get the same result -without the CCS delay_MS() function
use it and prosper. |
|
|
rvwills
Joined: 20 Dec 2014 Posts: 10
|
|
Posted: Tue Aug 30, 2016 12:31 pm |
|
|
m4rcdbk:
asmboy:
Thanks again... |
|
|
|