Topic : Absolute address problem - hbit

Forum : ST7/STM8

Original Post
Post Information Post
May 26, 2011 - 10:04am
Guest

Hi,

I have been trying to add bit definitions for PA_ODR in STM8S105C_S.h. As it's written in ST7/STM8 C Compiler (*8 + ), I multiply base address (which in this case 0x5000) by 8 and add bit number (for PA_ODR.0, it's 0x28000).

If I write exact address, it's ok:

at 0x28000 hbit LATA0;
(Disasm: BSET PA_ODR, #0)

If I write something like below, there won't be any warnings or errors but when I use that bit, it has no effect:

at (((0x5000)*8)+0) hbit LATA0;
(Disasm: BSET 0F000h, #0)

Testing on simulator under Ride7 with the code below:

#include "STM8S105C6.h"

void main()
{
PA_ODR=0xF8;
//PA_ODR.0=1;
LATA0=1;
while(1);
}

Ride7 patch: v7.30.10.0169
RKit-STM8 for Ride7: v2.32.10.0307

Replies
Post Information Post
+1
0
-1
May 26, 2011 - 10:37am
Raisonance Support Team

Hi Quiss,

Thanks for your report. We have succesfully reproduced your issue.

The problem lies in the way you use the C preprocessor: "0x28000" is handled as a long value (remember we are on a 16-bit compiler). In the second form, "0x5000" is handled as an integer value (16-bit). When multiplied by 8, the binary operator identifies a multiplication of 2 16-bit values (0x5000 and 8) hence performs a 16-bit multiplication, which truncates the binary result "0x28000" and looses the upper part of the 16-bit value.

The solution is to qualify one of the operands of the multiplication as an unsigned long constant, and the result will be ok. This is a working solution:

at (((0x5000)*8UL)+0) hbit LATA0;   // The 8UL is unsigned long => multiplication keeps all resulting bits

Best Regards,

+1
0
-1
May 26, 2011 - 10:56am
Guest

Hi Bruno,

It's working, thank you.