Although
gives an error "can't generate code for this expression"
can you please tell me why I got error for &= I am using MPLAB IDE v8.91 with High Tech C compiler.
Please pardon me, it has been a while since I last utilized a Baseline PIC16F series device.
Alright, I believe I have determined the source of that particular compiler error, however before we cover that topic, lets review the purpose and affects of the &= operator.
Which is equivalent to:
A bitwise AND (&) used with a bitmask (0xDF in this case), clears only those bits in OPTION which correspond to 0's in the bitmask, while leaving the other bits of OPTION unchanged.
Notice in the long form:
The contents of the OPTION register must first be READ, then bitwise AND with the bitmask value of 0xDF, before being WRITTEN back to the OPTION register.
Reference: PIC12F508/509/16F505 Datasheet, Section: 4.5 OPTION Register, Page: 24
4.5 OPTION Register
The OPTION register is a 8-bit wide, write-only register,
which contains various control bits to configure the
Timer0/WDT prescaler and Timer0.
By executing the OPTION instruction, the contents of
the W register will be transferred to the OPTION regis-
ter. A Reset sets the OPTION<7:0> bits.
The above section of the devices datasheet indicate the OPTION register is WRITE only, therefore statements like the following:
or
Will produce a compiler error as they require the contents of the OPTION register be READ acted upon before being WRITTEN back to the OPTION register.
Obviously, as the OPTION register is WRITE ONLY, any attempts to READ it will result in an error.
It should also be noted, the reason for the OPTION register being WRITE ONLY is the register is not memory mapped as indicated in the Address column of the table below:
As such, Microchip has provide special instructions to deal with these WRITE ONLY registers, such as OPTION, which copies the contents of the W register to the OPTION register.
Therefore the following C code:
Actually translates into assembly code as the following:
The WRITE ONLY registers, TRISB and TRISC are handled in a similar fashion with the TRIS instruction.
I should also point out the statement:
Does not function as expected, due to the fact T0CS is not mapped to a bit position of the OPTION register, but actual a numerical value representing the bits position within the OPTION register.
Normally you would use it as such:
Code:
OPTION = nGPWU | nGPPU | T0CS | T0SE | PSA | PS2 |PS1 |PS0; // Sets all the bits of the OPTION register
Or
Code:
OPTION = nGPWU | nGPPU | T0SE | PSA | PS2 |PS1 |PS0; // Sets all the bits of the OPTION register except the T0CS which is cleared
BigDog