CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

for (;n!=0;n--)

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
rvwills



Joined: 20 Dec 2014
Posts: 10

View user's profile Send private message

for (;n!=0;n--)
PostPosted: Tue Aug 30, 2016 10:31 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Tue Aug 30, 2016 10:49 am     Reply with quote

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

View user's profile Send private message

for (;n!=0;n--)
PostPosted: Tue Aug 30, 2016 11:07 am     Reply with quote

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

View user's profile Send private message

for (;n!=0;n--)
PostPosted: Tue Aug 30, 2016 11:15 am     Reply with quote

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

View user's profile Send private message AIM Address

PostPosted: Tue Aug 30, 2016 11:24 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Aug 30, 2016 11:37 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Tue Aug 30, 2016 11:43 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Aug 30, 2016 12:06 pm     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Tue Aug 30, 2016 12:10 pm     Reply with quote

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

View user's profile Send private message AIM Address

PostPosted: Tue Aug 30, 2016 12:16 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Aug 30, 2016 12:31 pm     Reply with quote

m4rcdbk:
asmboy:

Thanks again...
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group