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

variable problem

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



Joined: 25 Oct 2016
Posts: 62

View user's profile Send private message

variable problem
PostPosted: Tue Mar 07, 2017 10:58 am     Reply with quote

Hi
I am currently developing some bargraph (8 LEDs) made with a shift register and, in order to test it i have developed to following code.
The problem I have encountered and that I need help with is regarding the variable "a". I have noticed that the value it takes isn't changing correctly.The values it is taking are between 0 and 1 and I don't know why .
Does anyone know why this variable "a" is taking this weird values?

Code:

int8 z,a,i;

for (i=1; i<250; i++)
    {
     a=(i / 200)*8);
     for(z=0; z<=a; z++){
        output_high(pin_c1);
        output_high(pin_c2);
        bargraph_noD(z);
       }
    }


thanks
jaume
newguy



Joined: 24 Jun 2004
Posts: 1900

View user's profile Send private message

PostPosted: Tue Mar 07, 2017 11:11 am     Reply with quote

a is an int8 and i is also an int8.

Work through your math. Let's start with i = 1:
a = (1 / 200) * 8.

Remember, this is integer math we're dealing with. 1 / 200, using integer math, is 0. Therefore 0 * 8 = 0.

Let's skip ahead to i = 199:
a = (199 / 200) * 8.

Again, using integer math, 200 goes into 199 zero times. 199 / 200 is still 0. Therefore 0 * 8 = 0.

What about 200? 200 / 200 is 1, and a is then 1 * 8 = 8.

Think about what you want to do while keeping in mind what happens when integer math is performed. Hint: you'll want to use 16 bit math, and you'll want to do the multiplication BEFORE you do the division.
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Tue Mar 07, 2017 12:24 pm     Reply with quote

and this can be done very easily once you _understand_ what newguy has pointed out:

a=(i*8L)/200;

The single letter 'L' tells the compiler that the constant '8' is a 'long' (int16).
jamalavedra



Joined: 25 Oct 2016
Posts: 62

View user's profile Send private message

PostPosted: Thu Mar 16, 2017 11:03 am     Reply with quote

Great, it worked perfectly. Thanks guys.
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