embedded system/16bit integer overflowing

Status
Not open for further replies.

Blaze

Seniorius Lurkius
20
I have a small-embedded system project that giving me trouble. It is small robot based on the Motorola 68HC12 (16 bit) microcontroller. Here is the deal:

//#### Define ####//
#define PWPER_01 *(int volatile *)(0x4C) //Channel 0/1
#define PWPER_23 *(int volatile *)(0x4E) //Channel 2/3
#define PWDTY_01 *(int volatile *)(0x50) //Channel 0/1
#define PWDTY_23 *(int volatile *)(0x52) //Channel 2/3


PWDTY_01 = (PWPER_01 - (PWPER_01/100)*(motor_DC));


"PWDTY_01" and "PWPER_01" are both 16 bit internal registers. "motor_DC" is a variable use in calculations. "PWPER_01" is about 40000 so is fairly close to the upper limit of a 16bit integer. "motor_DC" is an integer less that 100.
"PWDTY_01" is not coming out right. I think something exceeding the 16bit-interger space. I tried to order my operation to avoid overflowing the 16bit-interger space, but something is up.

Any ideas?

I'm developing the software in C.

geoff
 
Can you give some test values?
Given the code:
PWDTY_01 = (PWPER_01 - (PWPER_01/100)*(motor_DC));
with
motor_DC = 99
PWPER_01 = 40000

it generally follows
PWDTY_01 = (40000 - (40000/100)*(99));
PWDTY_01 = (40000 - (400)*(99));
PWDTY_01 = (40000 - 39600);
PWDTY_01 = (400);
PWDTY_01 = 400;

What values are you getting? What are you expecting? And out of curiousity why are you doing it? (I did a robotics project for college around 5years ago).
 

Blaze

Seniorius Lurkius
20
I figured it out (well actually had had a little help from a professor): A 16bit signed integer (int) has only 15bits of positive integer space. Using an unsigned integer (unsigned int) solves the problem.

Lyme- I'm building a small robot for my senior project. I am a Mechanical Engineering at Cal Poly, SLO. This little bit of code is part of the PID motion control for the drive wheels.


geoff
 
Status
Not open for further replies.