The C2C compiler provides a number of functions which can be used to interact with the PIC hardware. Many of these are not functions as such, but end up as single PIC instructions. However, you will write your code as if they were functions since they will work in exactly the same way.
Note that if you turn on source code highlighting (which I strongly advise you to do) you will find that built in C2C components will be displayed in red.
I have used a mix of these functions in the example programs, depending on the precise requirements.
There are number of functions for manipulating individual bits in registers. You can refer to a register by name or use a built in function specifically assigned to a particular register.
On the right you can see number of the functions in use in the VPIC. Note that this program would not work like this on a real PIC, but this should illustrate how the functions are used.
There are six functions which deal directly with output bits in the PIC ports:
output_high_port_a (n) ; output_high_port_b (n) ; output_high_port_c (n) ; output_low_port_a (n) ; output_low_port_b (n) ; output_low_port_c (n) ;
The single parameter is the number which identifies the bit in the port to be set. Remember that the first port is number 0. In the VPIC you can see the appropriate functions used to set and clear a bit in PORTB.
Another way you can set and clear bits in registers is to use the set_bit and clear_bit functions:
clear_bit ( portno, bit ) ; set_bit ( portno, bit ) ;
The first parameter gives the name of the register to be worked on (all the register and bit names which we referred to in the PIC reference are also available in C2C). The second parameter gives the number of the bit to be changed.
You can use these functions on variables which you create, provided that you remember they only work on 8 bits arguments, i.e. on chars but not on integers). In the VPIC program I use these functions on PORTB with the expected results.
The final way you can change the contents of a register is to perform an assignment directly to it. In the VPIC program above we assign a value to PORTB and all the bits change instantly. Note that the C2C compiler provides functions for PORTC which is present in other PICs, but not in the one we are using.
The C2C compiler provides functions which can be used to test the values of bits. They will return non-zero (i.e. true) values when the bit is set.
input_pin_port_a (n) ; input_pin_port_b (n) ; input_pin_port_c (n) ;
As with the first set of functions above the parameter is the number of the bit to be tested. In the VPIC the least significant bit of PORTB, bit number 0, is tested. You can actually change the direction of the program by setting or clearing this bit.
The second way to test bits is to read the entire register and use it in a comparison. This is how the code in the second conditional statement works. Note that if you want to test for a particular bit you will need to use a mask and the & operator. For example the code below tests the same pin as input_pin_port_b (0):
if ( PORTB & 0x01 ) {
/* get here if the bottom */
/* bit is set */
}
This method is the only one which is available if you want to test particular bits in registers other than PORTA, B or C.