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

Modulo division problem

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







Modulo division problem
PostPosted: Mon Jul 08, 2002 1:06 pm     Reply with quote

I'm working on some code that will take a 16-bit unsigned integer with a max value of 34000 and produce a string with the decimal equivalent in ASCII of twice the value in the integer. I have compiled the code with gcc and ran it on my PC. This works as intened, so I know my algorithm is correct.

However, when I compile it with PCM (Linux, v3.091), it only works to convert an 8-bit variable. When given a 16-bit variable, even with a value that will fit within 8-bits, it only outputs the most significant digit. Each following digit is set to the value zero, and not the ASCII '0', but just 0.

Here is the code:

<PRE>#include <stdio.h>

// For PCM, change the following 2 lines to:
// void writeDecVal(char *str, long val) {
// char loop;

void writeDecVal(char *str, int val) {
unsigned char loop;

str[4] = (val \% 5) * 2 + '0';
val /= 5;
for (loop = 3; loop != 0xFF; loop--) {
if (val > 0) {
str[loop] = (val \% 10) + '0';
val /= 10;
}
else {
str[loop] = ' ';
}
}
}

void showit(char *str) {
printf("Result: \%s, \%02X \%02X \%02X \%02X \%02X \%02X\n",
str, str[0], str[1], str[2], str[3], str[4], str[5]);
}

void main() {
char str[10];
int val;

memset(str, 0, 10);
val = 34000;
writeDecVal(str, val);
showit(str);
memset(str, 0, 10);
val = 1;
writeDecVal(str, val);
showit(str);
}
</PRE>
Any idea why this isn't working on the PIC?
___________________________
This message was ported from CCS's old forum
Original Post ID: 5414
Albert Holman
Guest







Re: Modulo division problem
PostPosted: Thu Jul 18, 2002 3:20 am     Reply with quote

:=I'm working on some code that will take a 16-bit unsigned integer with a max value of 34000 and produce a string with the decimal equivalent in ASCII of twice the value in the integer. I have compiled the code with gcc and ran it on my PC. This works as intened, so I know my algorithm is correct.
:=
:=However, when I compile it with PCM (Linux, v3.091), it only works to convert an 8-bit variable. When given a 16-bit variable, even with a value that will fit within 8-bits, it only outputs the most significant digit. Each following digit is set to the value zero, and not the ASCII '0', but just 0.
:=
:=Here is the code:
:=
:=<PRE>#include <stdio.h>
:=
:=// For PCM, change the following 2 lines to:
:=// void writeDecVal(char *str, long val) {
:=// char loop;
:=
:=void writeDecVal(char *str, int val) {
:= unsigned char loop;
:=
:= str[4] = (val \% 5) * 2 + '0';
:= val /= 5;
:= for (loop = 3; loop != 0xFF; loop--) {
:= if (val > 0) {
:= str[loop] = (val \% 10) + '0';
:= val /= 10;
:= }
:= else {
:= str[loop] = ' ';
:= }
:= }
:=}
:=
:=void showit(char *str) {
:= printf("Result: \%s, \%02X \%02X \%02X \%02X \%02X \%02X\n",
:= str, str[0], str[1], str[2], str[3], str[4], str[5]);
:=}
:=
:=void main() {
:= char str[10];
:= int val;
:=
:= memset(str, 0, 10);
:= val = 34000;
:= writeDecVal(str, val);
:= showit(str);
:= memset(str, 0, 10);
:= val = 1;
:= writeDecVal(str, val);
:= showit(str);
:=}
:=</PRE>
:=Any idea why this isn't working on the PIC?

Hello Jack,

The problem lies most likely in the size of the int variable.
The size is 8 bit unsigned in the CCS compiler version I use and for other C compilers like you used to test the algorithm, it is probably 16 bit signed or more.

If you change all int declarations to long (size is then 16 bit unsigned) then it should work. At least it worked when I tried it.

good luck
___________________________
This message was ported from CCS's old forum
Original Post ID: 5642
Al



Joined: 13 Nov 2003
Posts: 28
Location: Belfast

View user's profile Send private message

Modulo division problem
PostPosted: Fri Jan 30, 2004 8:40 am     Reply with quote

You can actually define a 16 bit integer as int16 using CCS compiler. This should solve your problem. Wink
_________________
Alan Murray
Mark



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

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

PostPosted: Fri Jan 30, 2004 9:40 am     Reply with quote

Dude....that post is over 1.5 years old!!!!!!!!! I don't think he is still waiting.
Al



Joined: 13 Nov 2003
Posts: 28
Location: Belfast

View user's profile Send private message

PostPosted: Fri Jan 30, 2004 10:08 am     Reply with quote

Thanks for pointing that out, the processor in my head must have generated an integer division error giving me 0.5 years. Laughing
_________________
Alan Murray
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