Timing to do multiplication and divide

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Timing to do multiplication and divide

跳至解决方案
1,061 次查看
New_to_HC08
Contributor I

Hi

 

Hardware: MS9SO8GT60 with 32.768 kHZ xtal

programmer: Cyclone Pro HC08 - HCS08

IDE: Freescale Codewarrior 5.9.0

 

 

I seem to be having a timing issue with how long it takes to perform the calculation, and then assigning the computed value.

I assumed it wouldnt move to the next step, before the calculation had finished, but when I step through the code, it looks like it assigns 'tempunit' to activeArray before the value is finished calculating. and therefore, assigns '0'.

 

so all this is doing is a 6 digit number (date) and putting it into a 3 byte array (activeArray)

so for example 

 

divisor = 10/06/02

 

then activeArray should be [ 10, 6, 2 ];

 

but it ends up being [0, 6, 2]

 

 

  for (i=0;i<=2;i++){        tempunit = (BYTE)(divisor%10);    divisor /= 10;    //new value to mod by 10        temptens =  (BYTE)(divisor%10);    temptens *= 10;    temptens += tempunit;    activeArray[j] = tempunit;    j--;    divisor /= 10;    //new value to mod by 10      }

 

is this possible?

标签 (1)
0 项奖励
回复
1 解答
617 次查看
bigmac
Specialist III

Hello,

 

Assuming the initial value of the variable divisor is the unsigned 16-bit value 100602 (not entirely clear from your post), the initial value of variable j is 2, and that you complete the loop three times, rather than twice, I would expect to get the result  0, 6, 2.

 

I think your problem is with the line activeArray[j] = tempunit;

It would seem that the expression should be activeArray[j] = temptens;

 

Perhaps the code might be simplified, as follows

:

for (j = 2; j; j--) {  // Loop twice only  activeArray[j] = (byte)(divisor % 100);  divisor /= 100;      // new value to mod by 100}activeArray[0] = (byte)divisor;

 

Regards,

Mac

 

在原帖中查看解决方案

0 项奖励
回复
2 回复数
618 次查看
bigmac
Specialist III

Hello,

 

Assuming the initial value of the variable divisor is the unsigned 16-bit value 100602 (not entirely clear from your post), the initial value of variable j is 2, and that you complete the loop three times, rather than twice, I would expect to get the result  0, 6, 2.

 

I think your problem is with the line activeArray[j] = tempunit;

It would seem that the expression should be activeArray[j] = temptens;

 

Perhaps the code might be simplified, as follows

:

for (j = 2; j; j--) {  // Loop twice only  activeArray[j] = (byte)(divisor % 100);  divisor /= 100;      // new value to mod by 100}activeArray[0] = (byte)divisor;

 

Regards,

Mac

 

0 项奖励
回复
617 次查看
New_to_HC08
Contributor I

Wow. I am a retard.
I ended up just coding a new loop anyway, coz I often find, when I cant see the bug, it helps to start from scratch...is it just me that has this problem?
it is very similar to your solution

   for (i=0;i<=2;i++){     activeArray[j] =  (BYTE)(divisor%100);      divisor /= 100;      if (j>0) j--;         }

 


Thanks anyway!

 

0 项奖励
回复