Hi Guys, as someone who drops in and out of Flowcode I often forget things so I started to write some of it down. Working with registers is something that took a little while for me to get my head around, so the following may help others who are new on the path to knowledge! Please point out my errors, bound to be a few ( as I've smashing this out whilst waiting in the airport)
!Sorry, Had to use lots of code blocks to keep the correct spacing!
(Simply) A register is a storage location on the PIC. These predefined memory slots are used to hold the settings and options for the various features of the PIC. Using the info below you should be able to read, write and modify registers
!! Get the Microchip Datasheet for your PIC. !! Towards the end of each section they list all of the relevant registers.
For some of examples I'm use the PIC18F4455 INTRUPT registers but it can be applied to any register.
Reading a Register
For example INTCON2 the “INTERRUPT CONTROL REGISTER 2”
Create a Byte variable in FC, for example MyReg1
In C component load the register value into the FC variable
Code: Select all
FCV_MYREG1 = INTCON2;
Back in FC, simply display the value of MyReg1 on an LCD.
Masking a Register
You may only be interested in the state of a single bit, say bit 5. So Mask the value to find the bit state.
Code: Select all
Bit Number 7 6 5 4 3 2 1 0
Binary Value 128 64 32 16 8 4 2 1
In a FC calculation component, Bitshift the value by 5 then Bitwise AND by 1.
Code: Select all
Bit5State = ( MyReg1 >> 5 ) & 1
Its the same for any bit...for example, For bit 6 I would Bitshift by 6 and then bitwise AND by 1.
Code: Select all
Bit6State = ( MyReg1 >> 6 ) & 1
Read the PICs register document and determine what state you need for each bit.
For example,
Code: Select all
Bit Number 7 6 5 4 3 2 1 0
Bit Value 0 0 1 1 0 0 0 1 (Note this is my desired bit settings)
Hex Value 3 1 (and convert into HEX)
Code: Select all
INTCON2 = 0x31
In this example we'll take a source Byte and force bit2 to logic 1 using Bitwise OR.
Note the use of the | (pipe) symbol for Bitwise OR.
Code: Select all
Bit Number 7 6 5 4 3 2 1 0
Binary Value 128 64 32 16 8 4 2 1
Source value 0 0 1 1 0 0 0 1
Bit to Set 0 0 0 0 0 1 0 0
OR them
Result 0 0 1 1 0 1 0 1
So in FC, in a calculation component;
Code: Select all
MyReg1 = MyReg1 | 0x04
Alternatively,I find showing my values a binary can be more readable so I could have done..
Code: Select all
MyReg1 = MyReg1 | 0b00000100
This method is good because you don't have to work out the mask, just know the bit number.
Register = Register | (1<< BitToClear)
Say we want to set Bit 7.
First a mask is made for you by Bit shifting the value 1 right by the bit number.
Bitwise | (Or) the Register with this, to set the specified bit.
Code: Select all
MyReg1 = MyReg1 | (1<<7)
Clearing a Specific Bit in a Register
In this example we'll take a source Byte and clear bit4 to logic 0, using Bitwise AND
Note the use of the & (Ampersand) symbol for Bitwise AND.
Code: Select all
Bit Number 7 6 5 4 3 2 1 0
Binary Value 128 64 32 16 8 4 2 1
Source value 0 0 1 1 0 0 0 1
Bit to Clear 1 1 1 0 1 1 1 1 (Note the inverted mask in selecting the bit)
AND them
Result 0 0 1 0 0 1 0 1
So in FC, in a calculation component;
Code: Select all
MyReg1 = MyReg1 & 0xEF
Code: Select all
MyReg1 = MyReg1 & 0b11101111
This method is good because you don't have to work out the mask, just know the bit number.
Register = Register & ~(1<< BitToClear)
Say we want to clear Bit 5.
First a mask is made for you by;
Bit shifting the value 1 right by the bit number.
Inverting this byte to give the Mask.
Bitwise & (And) as before to clear the specified bit.
Code: Select all
MyReg1 = MyReg1 & ~(1<<5)
Toggling a Specific Bit in a Register
Sometimes you want to toggle a bits state, this can be neater than repeatedly setting and clearing a specific bit in a program.
In this example we'll take a source Byte and toggle bit3, using Bitwise XOR.
Note the use of the ^ (Caret) symbol for Bitwise XOR.
Code: Select all
Bit Number 7 6 5 4 3 2 1 0
Binary Value 128 64 32 16 8 4 2 1
Source value 0 0 1 1 0 0 0 1
Bit to toggle 0 0 0 0 1 0 0 0
XOR them
Result 0 0 1 1 1 0 0 1
Repeating this will toggle that specific bit back, using exactly the same method.
Code: Select all
Bit Number 7 6 5 4 3 2 1 0
Binary Value 128 64 32 16 8 4 2 1
Prev Result 0 0 1 1 1 0 0 1 (Note the Previous Result of the last operation)
Bit to toggle 0 0 0 0 1 0 0 0 (Note bit mask stays the same)
XOR them
Result 0 0 1 1 0 0 0 1
In a FC calculation component,
Code: Select all
MyReg1 = MyReg1 ^ 0x08
Code: Select all
MyReg1 = MyReg1 ^ 0b00001000
In some situations you may wish to check a specific bit in a register and react based on that bits state.
A good example is when data is received into a buffer and sets a 'flag' bit. By checking for this flag, you can read the buffer only when there is a reason to do so.
By using an Decision block, then bitwise & the Register with a relevant Binary value, you can create two options for you program.
Code: Select all
Bit Number 7 6 5 4 3 2 1 0
Binary Value 128 64 32 16 8 4 2 1
Register & 32 (For reacting to bit 5 being set (Logic 1) )