Problems in C

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi there,

I' m very new in C programming but very old in assembly, basic and pascal. I desided to learn C. I use the IAR C Compiler.

Problem 1:
My program has a lot of modules, each one with it's own .h file and .ext file.
I have defined the unsigned char TimeFlags as an external variable.
I have name the 1st bit us TimerInterruptFlag.
I have extern the definitions in order to be known from the other modules in the project.
When it interrupts I set this bit using a macro:
SetBit (TimeFlags, TimerInterruptFlag);
In another module waiting for the interrupt finished:
void Wait_Timer_Interrupt()}
ClrBit (TimeFlags, TimerInterruptFlag);
while ((TimeFlags & 0x01) == 0x01);
}
Then I found that the programs is always loop at the last command (while).
In disasembler I see that in the while the program poles r16 and not TimeFlags. I see that r16 has the value of TimeFlags just before the interrupt.

Then I changed the variable as char (and not unsigned char) and everything works OK.

Why this?

Problem 2:
I read the C language and I am a little bit comfused with the pointers. Does anybody can tell me the diferrences between:
a) unsigned char *p, *pk;
unsigned char k=100;

p = &k;
and
*p = &k;

b) p = pk;
and
*p = *pk;

thank you.

Michael.

User of:
IAR Embedded Workbench C/C++ Compiler
Altium Designer

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Umm this is the GCC forum? - but what the hell!

1) A flag set in an ISR and read in the main app code MUST be "volatile" - is yours?

2) p = &k will set p to hold the address where the k variable is stored

*p = &k will set the location whose address is currently contained in p to hold the address where k variable is stored (as the source is 16 bits and the destination is just 8 bits there will be some truncation)

p = pk will just copy the address of an unsigned char variable currently held in pk to p

*p = *pk will take the contents of the variable whose address is currently stored in pk and assign it to the contents of the unsigned char variable whose address is stored in p

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok, thanks a lot

Sory, Which is the right forum for IAR C programmers?

Michael.

User of:
IAR Embedded Workbench C/C++ Compiler
Altium Designer

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AVR forum