Library_io
GPIO
// #include "http://www.byvac.com//mBlib/flb/Library/2016/lib_io.bas"
The GPIO for the PIC has mainly three elements, ADC, I/O and Interrupts. The ports are designated A through G but not all processors have all of the ports:
- MX170(28 pin) uses PORTA through PORTB
- MX170(44 pin) uses PORTA through PORTC
- MX370(64 pin) used PORTB through PORTG
As an example of how to use the GPIO constants, suppose we have a reset on PORTB pin 6 and a chip enable of PORTC pin 2
constant RST {PORTB(PORT),6}
constant CE {PORTC(PORT),2}
or alternatively:
constant RST {RB6(PPORT),RB6(PVAL)}
constant RST {RC2(PPORT),RB6(PVAL)}
To set the pins:
io_pinRole(*RST(),OUT,WOFF)
io_pinRole(*CE(),OUT,WOFF)
it is possible to refer to the pins directly, e.g:
io_pinRole(*RB6(),OUT,WOFF)
io_pinRole(*RC2(),OUT,WOFF)
GPIO Functions
The following can be used for:
inorout - either IN, OUT (must be upper case)
pull - WOFF of no resistor, WPU for weak pull up resistor, WPD for wak pull down resistor. These refer to input only but WOFF is still needed in specificing an output as it keep the functions for in and out consistant.
value - In general this is 0 for low, 1 for high and 2 for toggle
- io_pinRole(*pin(),ionorout,pull) Prefered use over pinMode
- io_pinSet(*pin(),value) Sets pin to high, low or toggle [1]
- io_pinGet(*pin()) Returns value of a pin, this will be either 0 or 1 [2]
- io_getPort(*port()) Returns the value of a whole port, port is *PORTB(),*PORTC() etc. [3]
- io_setPort(*port(),theBits) Sets the value of a whole port [4]
- io_setPortH(*port(),theBits) Sets the value of only those bits specified in theBits [5]
- io_setPortL(*port(),theBits) Clears the bits of only those bits specified in theBits [6]
- io_ir(*ir(), pri, func$[40], pin) Sets the interrupt on change for a port pin [7]
io_pinSet(*port(), value)
NOTE: To use this the pin must have been set to an output first
io_pinSet(*RB5(),0) Sets Pin 5 on PORT B low
io_pinSet(*RB5(),1) Sets Pin 5 on PORT B high
io_pinSet(*RB5(),2) Toggle Pin 5 on PORT B
Get pin on port [2]
io_pinGet(*port())
NOTE: To use this the pin must have been set to an input first
io_pinGet(*RB5()) Sets Pin 5 on PORT B low
Gets the whole port as a 32 bit number [3]
x = io_getPort(port)
Example: x = io_gePort(*PORTB()) // gets all pins on PORTB
Sets the whole port [4]
io_setPort(port, value)
Example io_setPort(*PORTB(), 3) // sets pins 0,1 to high, the rest low
This will set only those pins on the port that have the high bit set on the value [5]
io_setPortH(port,value)
In the following example only bits 0 and 1 are set, to 1 the other bits on the port are left alone
Example io_setPortH(*PORTB(), 3)
This will clear only those pins on the port that have the high bit set on the value [6]
io_setPortL(port,value)
In the following example only bits 0 and 1 are cleared, to 0 the other bits on the port are left alone
Example io_setPortL(*PORTB(), 3)
Interrupt on Change
ir_io(*port(), pri, func$[40], pin)
This is the function that will associate a pin change with a user defined function:
*port() is PORTA, PORTB, INT0 or PORTC
pri is the priority 0 to 7 but 3 should be used
function_name$ is the name of a user defined function, see the example
pin is the pin that will change to cause the interrupt
Example: Make hello world print out every time pin RB6 changes
1) create a function to be called
function hello()
io_pinGet(*RB6()) // reading port clears interrupt
print "\nHello world"
endf
2) Set the pin to be an input and use a weak pull up so that it will only respond when we connect it to ground
io_pinRole(*RB6(),IN,WPU)
3) create an entry into the interrupt table
ir_io(*PORTB(),3,"hello",6) Note PORT and pin are specified separately
Each time pin RB6 is shorted to ground an interrupt will occur that will output Hello World
HOT TIP
When using ports and pins it is better to describe them for the system concerned. As an example suppose we had a switch connected to port B1 and an LED connected to C3, rather than using the port names, use the actula names as follows:
constant SWITCH {PORTB(PORT),1}
constant LED {PORTC(PORT),3}
io_pinRole(*SWITCH(),IN,WPU)
io_pinRole(*LED(),OUT,WOFF)