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

signed int mult => unsigned result =>?

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



Joined: 09 Sep 2003
Posts: 67
Location: Winterthur, Switzerland

View user's profile Send private message

signed int mult => unsigned result =>?
PostPosted: Sun Sep 04, 2005 12:04 pm     Reply with quote

Hello

I have some problems with signed integer multiplication:
I multiply two signed ints together, to get a signed long, but the result looks like it's treatend unsigned internally:

Code:

#include "bootloader.h" // fuse settings, delay, etc

#use fast_IO(B)

signed int clicks;
signed int Kp = 3;


// clicks is counted in an isr

void main(void)
{
signed long P;
signed int error;
signed int setpoint = 15;
signed long duty = 0;

     setup_timer_2(T2_DIV_BY_4,255,1);
     setup_ccp1(CCP_PWM);
     set_tris_B(0B10000000);

while(TRUE)
{

clicks = 0;
enable_interrupts(INT_RB);
enable_interrupts(GLOBAL);
delay_ms(200);
disable_interrupts(GLOBAL);

error = setpoint - clicks;

P = (setpoint - clicks) * Kp; // This calc is wrong

duty = duty + P; // looks ok

printf("%d\t%ld\t%ld\n\r", error, P, duty);

if(duty > 255L) duty = 255L;
if(duty < 0L)   duty = 0L;

set_pwm1_duty((unsigned int)duty);
}

}

Yes, it is a P controller, perhaps a little bit too complicated, but im experimenting...

Some debugging output:
Code:

error    P        duty

-6      238     493
-7      235     490
-7      235     490
-6      238     493

P should be something like -21. duty looks fine, so I don't think it's just a display error.
Did I screw this up or does the compiler?

Compiler: 2.732
PIC: PIC16F876


Many thanks
Felix
mbge5amw



Joined: 13 Dec 2004
Posts: 15
Location: Yorkshire, UK

View user's profile Send private message MSN Messenger

PostPosted: Sun Sep 04, 2005 12:28 pm     Reply with quote

I think this might be that the compiler looks at the variables in your equation and sees that they are all ints so reckons it can get away with 8 bit arithmetic.

This would be in keeping with -21 coming out as 235 (256-21 = 235)

There are 2 options,

1. Cast. precede your variables with (unsigned long) to tell the compiler what sort of arithmetic todo with these numbers.
2. spread the equation over a few lines of C code:

Code:


P = setpoint; // store the value of setpoint in a 16 bit variable so compiler uses 16 bit arithmetic
P -= clicks;
P *= Kp;



The

HTH

Andy
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

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

PostPosted: Sun Sep 04, 2005 8:58 pm     Reply with quote

The compiler is pretty old. Probably just an error with the printf function.
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