32 bits Math Operations HCS08

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

32 bits Math Operations HCS08

ソリューションへジャンプ
877件の閲覧回数
adrianodangelo
Contributor I

Hello All,

 

 

I am new on Codewarrior and in my current project(9S08PA8) I need a 32 bits variable to receive the result of a math operation.

This variable will receive a value from a single  multiply operation(eg. ADC_result x 87).

Below a snippet of my code:

 

 

uint8_t Mult=87;

uint32_t temp=0;

.

.

.

temp=(uint32_t)(adc_result0*Mult);

 

 

What happens is that if the value of adc_result0 is below or equal to 753 (the result ocupy only 2 bytes) everything is ok, but if the adc_result0 is higher than 753 (the result does not fill in 2 bytes) the compiler casts the result to 2 bytes. My question is can the HCS08 compiler operate in 32 bits variables? If not, what workaround can I use to solve this problem? (I tried to declare the variable as dword, unsigned long, long, without success).

 

Thanks,

ラベル(1)
0 件の賞賛
返信
1 解決策
667件の閲覧回数
kef2
Senior Contributor V

This is how C works. 16 bits operation gives 16 bits result. You need to implicitly cast at least one of multipliers to 32 bits to get 32 bits product.

temp=(uint32_t)( (uint32_t)adc_result0*Mult);

Regards,

Edward

元の投稿で解決策を見る

0 件の賞賛
返信
2 返答(返信)
668件の閲覧回数
kef2
Senior Contributor V

This is how C works. 16 bits operation gives 16 bits result. You need to implicitly cast at least one of multipliers to 32 bits to get 32 bits product.

temp=(uint32_t)( (uint32_t)adc_result0*Mult);

Regards,

Edward

0 件の賞賛
返信
667件の閲覧回数
adrianodangelo
Contributor I

Thank you Eduards,

Perfect! You solved my problem and saved me a lot of time.

0 件の賞賛
返信