View previous topic :: View next topic |
Author |
Message |
zeyad
Joined: 24 Feb 2015 Posts: 22
|
condition always true for loop |
Posted: Wed Feb 25, 2015 1:59 pm |
|
|
Code: |
#include <16f877a.h>
#fuses HS,NOLVP,NOWDT,NOPROTECT
#use delay(clock=20M)
void s1()
{
unsigned short table[4]={3,6,12,9};
int i;
for (i=0;i<200;i++)
{
output_b(table[i]);
}
}
void s2()
{
unsigned short table[4]={3,6,12,9};
int i;
for (i=0;i<600;i++) // ? condition always true ?
{
output_b(table[i]);
}
}
void s3()
{
output_b(3);
delay_ms(50);
output_b(6);
delay_ms(50);
output_b(12);
delay_ms(50);
output_b(9);
delay_ms(50);output_b(3);
delay_ms(50);
output_b(6);
delay_ms(50);
output_b(12);
delay_ms(50);
output_b(9);
delay_ms(50);output_b(3);
delay_ms(50);
output_b(6);
delay_ms(50);
output_b(12);
delay_ms(50);
output_b(9);
delay_ms(50);output_b(3);
delay_ms(50);
output_b(6);
delay_ms(50);
output_b(12);
delay_ms(50);
output_b(9);
delay_ms(50);output_b(3);
delay_ms(50);
output_b(6);
delay_ms(50);
output_b(12);
delay_ms(50);
output_b(9);
delay_ms(50);output_b(3);
delay_ms(50);
output_b(6);
delay_ms(50);
output_b(12);
delay_ms(50);
output_b(9);
delay_ms(50);output_b(3);
delay_ms(50);
output_b(6);
delay_ms(50);
output_b(12);
delay_ms(50);
output_b(9);
delay_ms(50);output_b(3);
delay_ms(50);
output_b(6);
delay_ms(50);
output_b(12);
delay_ms(50);
output_b(9);
delay_ms(50);
}
void s4()
{
output_b(3);
delay_ms(50);
output_b(6);
delay_ms(50);
output_b(12);
delay_ms(50);
output_b(9);
delay_ms(50);output_b(3);
delay_ms(50);
output_b(6);
delay_ms(50);
output_b(12);
delay_ms(50);
output_b(9);
delay_ms(50);output_b(3);
delay_ms(50);
output_b(6);
delay_ms(50);
output_b(12);
delay_ms(50);
output_b(9);
delay_ms(50);output_b(3);
delay_ms(50);
output_b(6);
delay_ms(50);
output_b(12);
delay_ms(50);
output_b(9);
delay_ms(50);output_b(3);
delay_ms(50);
output_b(6);
delay_ms(50);
output_b(12);
delay_ms(50);
output_b(9);
delay_ms(50);output_b(3);
delay_ms(50);
output_b(6);
delay_ms(50);
output_b(12);
delay_ms(50);
output_b(9);
delay_ms(50);output_b(3);
delay_ms(50);
output_b(6);
delay_ms(50);
output_b(12);
delay_ms(50);
output_b(9);
delay_ms(50);output_b(3);
delay_ms(50);
output_b(6);
delay_ms(50);
output_b(12);
delay_ms(50);
output_b(9);
delay_ms(50);output_b(3);
delay_ms(50);
output_b(6);
delay_ms(50);
output_b(12);
delay_ms(50);
output_b(9);
delay_ms(50);output_b(3);
delay_ms(50);
output_b(6);
delay_ms(50);
output_b(12);
delay_ms(50);
output_b(9);
delay_ms(50);output_b(3);
delay_ms(50);
output_b(6);
delay_ms(50);
output_b(12);
delay_ms(50);
output_b(9);
delay_ms(50);output_b(3);
delay_ms(50);
output_b(6);
delay_ms(50);
output_b(12);
delay_ms(50);
output_b(9);
delay_ms(50);
}
void main()
{
setup_adc_ports(NO_ANALOGS);
set_tris_d(0xff);
//set_tris_b(0x00);
while(true)
{
if (input(pin_d0))
{
s1();
}
if (input(pin_d1))
{
s2();
}
if (input(pin_d2))
{
s3();
}
if (input(pin_d3))
{
s4();
}
}
} |
When i am compiling this code condition always true is coming at the message box. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Wed Feb 25, 2015 2:01 pm |
|
|
yes....
while(true) ...
{
..
...
..
}
this condition( the while( true))
is always true....
jay |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Feb 25, 2015 2:05 pm |
|
|
Quote: | void s2()
{
unsigned short table[4]={3,6,12,9};
int i;
for (i=0;i<600;i++) // ? condition always true ?
{
output_b(table[i]);
}
} |
Look in the CCS manual, in the Type Specifiers section. What is an 'int'
equivalent to (int8), and what is the maximum size number that can fit
in an int8 ?
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Wed Feb 25, 2015 4:51 pm |
|
|
function s3() and s4() are very interesting.
I am trying to think of a useful purpose for them as they evaluate to basically
a port pins change (reiterated to no purpose)
and a (ultimate ) delay.
and
s1() which handily over runs the index delimit of the array it pesters.
what do you believe is happening when these little code gems execute??
is this a Proteus tail chasing program ?
Last edited by asmboy on Thu Feb 26, 2015 9:02 am; edited 1 time in total |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Feb 25, 2015 11:03 pm |
|
|
We like to see the short example programs. Why post all your 205 lines?
This takes us a lot of time to figure out what your problem is.
We like short programs like: Code: | #include <16f877a.h>
#fuses HS,NOLVP,NOWDT,NOPROTECT
#use delay(clock=20M)
void main()
{
unsigned short table[4]={3,6,12,9};
int i;
for (i=0;i<600;i++) // ? condition always true ?
{
output_b(table[i]);
}
} |
Please, next time make your program small as possible to show your problem. It is polite to us and is also a good way of debugging; in removing all the excess code you often will find the solution yourself.
Besides all the other problems already mentioned: There is a bug in the array: Code: | unsigned short table[4]={3,6,12,9}; | In the CCS compiler a 'short int' == 'int1'. So, can only hold 1 bit (0 or 1). To avoid these type of problems it is best to never use types like int, short or long. Better use int1, int8, int16.
Did I already say your code makes no sense at all?
- You have only 4 values in the array but then you want to output 600 values?
- No comments in the code about what it is supposed to do.
- Function names that say nothing about what the function is supposed to do like s1, s2?
It will be a long night before your homework is finished... |
|
|
zeyad
Joined: 24 Feb 2015 Posts: 22
|
thank you pcm programmer. it works. but the code still didnt |
Posted: Thu Feb 26, 2015 4:00 am |
|
|
PCM programmer wrote: | Quote: | void s2()
{
unsigned short table[4]={3,6,12,9};
int i;
for (i=0;i<600;i++) // ? condition always true ?
{
output_b(table[i]);
}
} |
Look in the CCS manual, in the Type Specifiers section. What is an 'int'
equivalent to (int8), and what is the maximum size number that can fit
in an int8 ?
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf |
thank you pcm programmer. it works. but the code still don't work |
|
|
zeyad
Joined: 24 Feb 2015 Posts: 22
|
still didn't work |
Posted: Thu Feb 26, 2015 4:04 am |
|
|
ckielstra wrote: | We like to see the short example programs. Why post all your 205 lines?
This takes us a lot of time to figure out what your problem is.
We like short programs like: Code: | #include <16f877a.h>
#fuses HS,NOLVP,NOWDT,NOPROTECT
#use delay(clock=20M)
void main()
{
unsigned short table[4]={3,6,12,9};
int i;
for (i=0;i<600;i++) // ? condition always true ?
{
output_b(table[i]);
}
} |
Please, next time make your program small as possible to show your problem. It is polite to us and is also a good way of debugging; in removing all the excess code you often will find the solution yourself.
Besides all the other problems already mentioned: There is a bug in the array: Code: | unsigned short table[4]={3,6,12,9}; | In the CCS compiler a 'short int' == 'int1'. So, can only hold 1 bit (0 or 1). To avoid these type of problems it is best to never use types like int, short or long. Better use int1, int8, int16.
Did I already say your code makes no sense at all?
- You have only 4 values in the array but then you want to output 600 values?
- No comments in the code about what it is supposed to do.
- Function names that say nothing about what the function is supposed to do like s1, s2?
It will be a long night before your homework is finished... |
void s2()
{
unsigned long table[4]={3,6,12,9};
unsigned long i;
for (i=0;i<600;i++)
{
output_b(table[i]);
}
}
still code is not working |
|
|
zeyad
Joined: 24 Feb 2015 Posts: 22
|
|
Posted: Thu Feb 26, 2015 4:07 am |
|
|
asmboy wrote: | function s3() and s4() are very interesting.
I am trying to think of a useful purpose for them as they evaluate to basically
a port pins change change
and a (ultimate ) delay.
and
s1() which handily over runs the index delimit of the array it pesters.
what do you believe is happening when these little code gems execute??
is this a Proteus tail chasing program ? |
yes it is a proteus tail chasing program . |
|
|
oxo
Joined: 13 Nov 2012 Posts: 219 Location: France
|
Re: still didn't work |
Posted: Thu Feb 26, 2015 4:47 am |
|
|
zeyad wrote: |
Code: | void s2()
{
unsigned long table[4]={3,6,12,9};
unsigned long i;
for (i=0;i<600;i++)
{
output_b(table[i]);
}
} |
still code is not working |
600 values of index, but table is only 4 items..
Maybe programming is not for you. |
|
|
ELCouz
Joined: 18 Jul 2007 Posts: 427 Location: Montreal,Quebec
|
|
Posted: Thu Feb 26, 2015 4:07 pm |
|
|
zeyad wrote: | asmboy wrote: | function s3() and s4() are very interesting.
I am trying to think of a useful purpose for them as they evaluate to basically
a port pins change change
and a (ultimate ) delay.
and
s1() which handily over runs the index delimit of the array it pesters.
what do you believe is happening when these little code gems execute??
is this a Proteus tail chasing program ? |
yes it is a proteus tail chasing program . |
Proteus topic should be locked up!
They are in no way representative to a real setup! _________________ Regards,
Laurent
-----------
Here's my first visual theme for the CCS C Compiler. Enjoy! |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
Re: still didn't work |
Posted: Thu Feb 26, 2015 5:52 pm |
|
|
zeyad wrote: | ......
.....
still code is not working | You've been told why it's not working.
Also read the forum guidelines, and search for references to Proteus/ISIS.
Mike |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Fri Feb 27, 2015 2:17 pm |
|
|
When I tell you it is best to use variable types like 'int8' and 'int16' but you choose to use 'unsigned long' instead, then I feel like you don't respect me. Still, your program isn't working and you want us to help you?
Sorry, here on this forum we are not easy to students. We are not going to do your homework. You will have to put in some effort and up till now you did not enough. Your problem is easy, something for a first programming lesson. Just reading any book on the C language will be enough to fix your program.
You say your program isn't working, but you haven't told us what it is supposed to do. We can't read your mind, so give us as much information as possible. We asked you this before but you didn't answer that question.
From your other thread I get an idea of what you want but instead of guessing I want you to tell us.
I also suggest you read this thread again. One major reason for your program failing has been answered two times. Perhaps you don't understand all answers, but then say so and we can explain better. Just ignoring the answers and then asking the same question again is not polite and makes you look stupid.
ckielstra wrote: | It will be a long night before your homework is finished... | I was wrong. For you it will take a week to finish this problem. |
|
|
ELCouz
Joined: 18 Jul 2007 Posts: 427 Location: Montreal,Quebec
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19518
|
|
Posted: Fri Feb 27, 2015 3:18 pm |
|
|
More constructive than your comment.....
The point is that the core reasons the code fails, have been listed by at least two posters, and ignored, also the problems are 'basic C', which is _not_ what this forum is for.
The original poster needs to get a C textbook, and start with some very basic exercises. |
|
|
|