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 CCS Technical Support

CCS C Compiler PIC16F877A

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



Joined: 06 Nov 2021
Posts: 92

View user's profile Send private message

CCS C Compiler PIC16F877A
PostPosted: Sat Nov 06, 2021 6:26 am     Reply with quote

Hello I am new at CCS C. I tried to "When I press a button F-A counter starts run on 7 SEGMENT DISPLAY. When I press the button second time A-F counter starts run on 7 SEGMENT DISPLAY. Third time I press the button again F-A ...(F-A,A-F,F-A...)" this application here is the code that I wrote:
Code:

const int FA[6]={0x71,0x79,0x5E,0x39,0x7C,0x77};
int i;
int a=2;

void main()
{   
     output_a(0x02);
     
     while(true)
     {
       if(input(button) && a%2==0)
        {
          for(int i=0;i<=5;i++)
          {
             delay_ms(300);
             output_d(FA[i]);
          }   
         
          a++;
        }
       
        else if(input(button) && a%2==1)
        {
            for(int i=5;i>=0;i--)
          {
             delay_ms(300);
             output_d(FA[i]);
          }   
          a++;
        } 
       
       
     }
 }

It goes first time F-A, second time A-F and then program does not work. I don't know why. At which point I made mistakes? If someone could enlighten me, I'll appreciate it. Thanks.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Nov 06, 2021 12:09 pm     Reply with quote

You got a warning on this one:
Quote:
for(int i=5;i>=0;i--) // Warning: Condition always TRUE

That's the reason. Find the problem and fix it.
Hint: With the PCM compiler, an 'int' is unsigned.
Hint: What does your loop count down to ?

Also, here's something I don't like:

You declare 'i' here for your loops:
Quote:

const int FA[6]={0x71,0x79,0x5E,0x39,0x7C,0x77};
int i;
int a=2;


Then you declare it again inside your loops:
Quote:
for(int i=0;i<=5;i++)
for(int i=5;i>=0;i--)

In my opinion, get rid of the declarations inside your loops and
just have the one declaration at the top.
Khansokhua



Joined: 06 Nov 2021
Posts: 92

View user's profile Send private message

PostPosted: Sat Nov 06, 2021 2:50 pm     Reply with quote

Code:
const int FA[6]={0x71,0x79,0x5E,0x39,0x7C,0x77};
int i;
int a=2;

void main()
{   
     output_a(0x02);
     
     while(true)
     {
       if(input(button) && a%2==0)
        {
          for( i=0;i<=5;i++)
          {
             delay_ms(300);
             output_d(FA[i]);
          }   
         
          a++;
        }
       
        else if(input(button) && a%2==1)
        {
            for( i=5;i>0;i--)
          {
             delay_ms(300);
             output_d(FA[i]);
          }   
          a--;
        }
       
       
     }
 }

So, it counts F to A and then A to E .But how it could be always true? What makes it? I can't see any reason.
for(int i=5;i>=0;i--) // Warning: Condition always TRUE
Also, how do I can understand the reason of be taken warnings?I just see redline.
Hint: With the PCM compiler, an 'int' is unsigned.
I really didn't understand it, pardon me sir. Thanks for your time. I’m absolutely delighted.
jeremiah



Joined: 20 Jul 2010
Posts: 1358

View user's profile Send private message

PostPosted: Sat Nov 06, 2021 5:03 pm     Reply with quote

Khansokhua wrote:

So, it counts F to A and then A to E .But how it could be always true? What makes it? I can't see any reason.
for(int i=5;i>=0;i--) // Warning: Condition always TRUE


Well you didn't specify that the int i was "signed" so it was defaulted to unsigned. An unsigned number can never be less than 0, so the check "i>=0" is always true as the variable "i" can never be negative (it just wraps back around to max positive).

One of our coding standard rules is to always declare an integer based type with "signed" or "unsigned" specified. We found out the hard way once before when the compiler changed a default in a newer version.
Khansokhua



Joined: 06 Nov 2021
Posts: 92

View user's profile Send private message

PostPosted: Sun Nov 07, 2021 7:41 am     Reply with quote

Thank you so much. I clearly understood.
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