|
|
View previous topic :: View next topic |
Author |
Message |
fiasgardone
Joined: 19 Jan 2010 Posts: 71
|
Problem with function |
Posted: Sat Aug 31, 2013 8:38 am |
|
|
hello friends
I have a problem in this code I can not solve, I need your help!
When the key C3 = 0 increment the variable (i), then the key and C1 = 0 (i == 0) makes the code 1
if the key C1 = 0 and (i == 1) makes the code 2, the problem is that I never make the code 1. where is the problem?
Code: |
Void mon_day (void)
{
if((input(PIN_C3)==0)//
{
if(i==2)
{
i=0;
}
i=i+1;
delay_ms (100);
}
if((input(PIN_C1)==0)&&(i==0)) //code 1
{
sprintf(d_day,"%02d",day);
glcd_text57(62,3,d_day,1,off);
if(day<=24)
{
day=day+1;
}
if(day==24)
{
day=0;
}
sprintf(d_day,"%02d",day);
glcd_text57(62,3,d_day,1,on);
delay_ms(200);
}
if((input(PIN_C1)==0)&&(i==1)) //code 2
{
sprintf(m_mon,"%02d",mon);
glcd_text57(87,3,m_mon,1,off);
if(mon<=12)
{
mon=mon+1;
}
if(mon==12)
{
mon=0;
}
sprintf(m_mon,"%02d",mon);
glcd_text57(87,3,m_mon,1,on);
delay_ms(200);
}
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Sat Aug 31, 2013 9:21 am |
|
|
Start by being logical in laying out your code:
Code: |
Void mon_day (void)
{
if((input(PIN_C3)==0)//
{
if(i==2)
{
i=0;
}
i=i+1;
delay_ms (100);
}
|
Now look at it and think.
If i==2, what is i going to proceed with?.
Is it ever going to come out as '0'?......
Best Wishes |
|
|
fiasgardone
Joined: 19 Jan 2010 Posts: 71
|
|
Posted: Sat Aug 31, 2013 1:08 pm |
|
|
Hello Ttelmah thank you for answering my questions.
I've done several tests, but without any result.
The variable (i) is a global variable this initialized to (0) when (C3 = 0) to variable (i) is incremented to 1 (i = i +1) so as to have variable = 1, and why does the code 2.
As I have done various tests but still not working.
I think the problem is here, but do not know how to solve!
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Sat Aug 31, 2013 2:54 pm |
|
|
The i=i+1, is done after you have set it to zero.
So i is then going to be '1'. It is never going to come out as zero. |
|
|
fiasgardone
Joined: 19 Jan 2010 Posts: 71
|
|
Posted: Sat Aug 31, 2013 4:29 pm |
|
|
Hello Ttelmah
Yes, the problem is how the variable (i) remains at zero when C3 = 0, the first time so you can enter the code 1.
Unfortunately I am not able to operate with the knowledge that I have!
Thank you for your attention |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Sun Sep 01, 2013 12:10 am |
|
|
This is not a 'C primer' forum. It is for problems/operations with the CCS compiler....
You really need to get a basic textbook.
However 'clue'. Look at the keyword 'else'.
You have two situations. One where you want to increment, and the other where the number wants to be set to zero.
As a comment though, consider 'style' in all your coding. It makes things much easier to understand for other people, or _you_ when you come back in a few months.
If you have two pieces of code you are calling '1' and '2', then have the numbers to select them 'be' '1' or '2'. Make the names have a relation to what is happening. Otherwise if you start to use a debugger for example, you have to remember that '0' selects code block '1'. Much simpler to either call the block '0', or make '1' be the number to select it....
Also do you want just two switch between two code blocks, or do you want three cases with the third not doing anything?. If the former, look at an 'int1'. This can only be '0' or '1'. val=!val, will toggle between these. No need to increment, and the number can never be anything but 0 or 1.
Also, with 'i' as it currently is, what would happen if it accidentally got set to 3 somewhere else?. Could the code recover?.
Final comment, why is 'i' global?. Are you using it elsewhere?. If not, then declare it as a local static variable. The fewer variables that are 'global', the less chance of other things interfering with your code. |
|
|
fiasgardone
Joined: 19 Jan 2010 Posts: 71
|
|
Posted: Sun Sep 01, 2013 3:16 am |
|
|
Hi! Ttelmah thanks for helping me!!
I have some books on C programming, but logic programming is that I have had some difficulties in solving this as well!
The function is to configure the DS1302, it is to set the clock and calendar, this code will be longer, and how I'm doing testing and Correcting mistakes, but in this case I do not succeed!
As I did not include the comments to be a function test, and hence be easily
I have various research here in the forum, but still could not solve, have tested with (switch, for (;;) else) but without success!
I always like to solve the problems I own, but have only situations that even with help from others, so here I am trying to get more information!
thank you |
|
|
|
|
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
|