bhadmanathan
Junior Member level 1
Hi ,
Still i dont understand volatile keyword in c.I read that
A variable should be declared volatile whenever its value could change unexpectedly.
Ex: Memory-mapped peripheral registers
Embedded systems contain real hardware, usually with sophisticated peripherals. These peripherals contain registers whose values may change asynchronously to the program flow. As a very simple example, consider an 8-bit status register at address 0x1234. It is required that you poll the status register until it becomes non-zero. The nave and incorrect implementation is as follows:
This will almost certainly fail as soon as you turn the optimizer on, since the compiler will generate assembly language that looks something like this:
The rationale of the optimizer is quite simple: having already read the variable's value into the accumulator (on the second line), there is no need to reread it, since the value will always be the same. Thus, in the third line, we end up with an infinite loop. To force the compiler to do what we want, we modify the declaration to:
The assembly language now looks like this:
The desired behavior is achieved.
But,i done the following code ..
Timer0 in pic16f877a is memory mapped at 0x01. I created a non volatile pointer variable to point that address.
I expected that LCD will display "BEFORE WHILE" and while(*p<254); become infinite loop. But the above code is working fine without volatile and it displays "AFTER WHILE".
What i have done wrong??
Thanks in advance
Still i dont understand volatile keyword in c.I read that
A variable should be declared volatile whenever its value could change unexpectedly.
Ex: Memory-mapped peripheral registers
Embedded systems contain real hardware, usually with sophisticated peripherals. These peripherals contain registers whose values may change asynchronously to the program flow. As a very simple example, consider an 8-bit status register at address 0x1234. It is required that you poll the status register until it becomes non-zero. The nave and incorrect implementation is as follows:
Code:
UINT1 * ptr = (UINT1 *) 0x1234;
// Wait for register to become non-zero.
while (*ptr == 0);
// Do something else.
This will almost certainly fail as soon as you turn the optimizer on, since the compiler will generate assembly language that looks something like this:
Code:
mov ptr, #0x1234
mov a, @ptr loop
bz loop
The rationale of the optimizer is quite simple: having already read the variable's value into the accumulator (on the second line), there is no need to reread it, since the value will always be the same. Thus, in the third line, we end up with an infinite loop. To force the compiler to do what we want, we modify the declaration to:
Code:
UINT1 volatile * ptr =
(UINT1 volatile *) 0x1234;
The assembly language now looks like this:
Code:
mov ptr, #0x1234
loop mov a, @ptr
bz loop
The desired behavior is achieved.
But,i done the following code ..
Timer0 in pic16f877a is memory mapped at 0x01. I created a non volatile pointer variable to point that address.
Code:
void main()
{
unsigned char *p=0x01;
Lcd8_Init();
OPTION=0x07;//turn on timer0
while(1)
{
Lcd8_Decimal3(0x80,*p);
Lcd8_Display("BEFORE WHILE");
while(*p<254);
Lcd8_Display("AFTER WHILE");
while(1);
}
}
I expected that LCD will display "BEFORE WHILE" and while(*p<254); become infinite loop. But the above code is working fine without volatile and it displays "AFTER WHILE".
What i have done wrong??
Thanks in advance
Last edited by a moderator: