PIC32 Ports

Ports

portb
portw
setport
port
port()
 

Ports are fundamental to microcontroller use, they are the main method of communicating with the outside world, PIC32-Basic has two methods of doing this. The first method uses three keywords and is simple to implement, the second method is more cryptic but allows much more flexibility

setport
setport <port> as [output][input] [wpu]
setport "e2" as output
setport "b14" as in
setport "b0" as in wpu

 The purpose of this statement is to specify one bit or a port as either an input or output, the keywords 'input' and 'output' can be abbreviated to 'in' and 'out'. The port is specified as a port name followed by the bit value and as a string. The PIC32 in the BV513 has port names 'b' through to 'g' a number following this specifies the bit number of the port to set as either an input or an output.

Some ports have a weak pull up facility, if 'wpu' is added then the weak pull up is activated, if it is not added then the weak pull up is disabled. A warning is given if 'wpu' is used with a port that does not have this facility.

As an example

setport "b14"  as out

would set bit 14 of port b as an output. Constants and variables can be used so that the following would be perfectly acceptable:

constant LED$ "d4"
setport LED$ as output

port
port <port> [1][0]
port "e2" 1
port "b14" 0

This will write to the specified port and set its output to be either high (1) or low (0) or toggle(T) or (t). The toggle option will set the port to low if it was high and high if it was low.

port()
value=port(<port>)
a%=port("e2")

Returns a value (0 or 1) representing the current state of the bit on the designated port.

Method Two

The following two statement allows the flexibility of reading and writing to them in several different ways, yet only using two keywords.

portb 
portb “directive” bit_value
portb "eos" 3

 

portw
portw “directive” 32bit-word
portw "eos" 0xc3

Both keywords work in exactly the same way. The difference is in how the value is interpreted. Portb with either set or clear a bit at a given position and portw will set or clear the bits of the given value, i.e. several at a time. As an example to set bit 4 (00010000) on port 'e' using portb would be:

portb "eos" 4

To do the same using port w would be:

portw "eos" 0x10

The String Directive

This controls which port, what and how it is set. It is made up of three letters:

Port directive string

The three characters make up a string, for example “bts” or “dot”. Each character represents either the port, directive or action:

Port

The first character refers to the port letter usually between a and g. The PIC 32 has ports named b through g, there is no ‘a’ port on the 64 pin devices.

Action

The second character is the action to be carried out on the port and this can be a choice of:

t: Will set the port to either input or output
o: Will output to the port
i: Will input from the port - operation field ignored

The operation determines what is to be done with the port, the two obvious choices are input from the port to get data and output to the port to switch on devices. This is taken care of by ‘o’ and ‘i’. The less obvious action is ‘t’. At reset the ports of a microcontroller usually default to inputs. The ‘t’ will determine if the port, or more accurately which bits of the port are set to act as either input or outputs.

Operation

The third character or field is the operation and has the following options:

s: Set (set a bit to 1)
c: Clear (clear a bit to 0)
t: Toggle a bit (if the bit was 1 then set it to 0 and vice versa )

A port is usually 8, 16 or 32 bits wide and could have many devices connected to a single port. The purpose of the operation filed is to enable a bit to be set(1) or cleared(0) without effecting any of the other bits. This is highly convenient as otherwise the port would need to be read and at least one logical operation (and,or,xor) be performed before writing back. As previously mentioned portb operates on individual bits, only one bit can be set at a time but portw will operate on all of the bits in the port. When using portb a number represents the bit value starting at 0.

Port with options B and W

The above illustration represents the difference between the portb and the portw statement. The value used in portb will set, clear or toggle that bit as referenced by a number from 0. It will be appreciated that using this method will only allow 1 bit to be set, toggled or cleared at any one command. On the other hand the portw value is the binary value that is used to set, toggle or clear the port. In the example given 7 is represented in binary by the lowest 3 bits.

Examples

The port statement is simple but involved and so it may be better to look at several examples in order to get a feel of how to use it.

Turn the on board LED on and off

The on board LED is the one that indicates when data is being read or written to the SD-Card. It is connected to Port C bit 15 (RC15).

portb "ctc"  15 // set RC15 to an o/p
portb "cos" 15 // set RC15 high LED off
portb "coc" 15 // set RC15 low LED on

This can be put is a function to flash the LED on and off using toggle:

function fl(times)
    while times > 0
        portb "cot" 15  // toggle port
        wait 100         // wait 100 ms
        portb "cot" 15
        wait 100
        times=times-1  // reduce count
    wend
endf

To call this and flash the LED 10 times use:

fl(10)

Using Port b

Port b by default is set to be an analogue input, however from version 1.460 onwards if port b is selected using either setport or the other port words then the analogue function will be automatically disabled.

NOTE: Most of the PIC32 ports are 5V tolerant but port b is not, the voltage on this port must be restricted to 3v3.

dim a$="btc", x=5
portb a$ x
// OR
dim a$="btc", x=0x20
portw a$, x

In this example (which applies to all ports, not just b) bit 5 of port b has been set for output. This is because the first field specifies port b, the second field ‘t’ specifies that the port should be set for either input or output and the third field specifies that bit 5 (in this case) should be cleared. Clearing the bit using 't' will set an output, setting will be input. In the above both are equivalent as portb is used for one and portw is used for the other. when setting a port for either input or output the action is always 't' and the operation is either 'c' for output and 's' for input.

portb "btc" 5 // sets bit 5 to be an o/p
portb "bts" 5 // sets bit 5 to be in input
portw "btc" 3 // sets bits 0 and 1 to be o/p

Operation Value Existing Port Value New Port Value
set  01000000 10000011 11000011
clear 01000000 11000011 10000011
toggle 00000001 11000011 11000010

From this table the value column is the required value that needs to effect the bit, in the set row, bit 6 (bit 0 is far right) is ‘1’ so this will make bit 6 on the port also a 1 without effecting any other bits.

Inputting

portb “ci” a%

This will read the contents of port c into the variable a%. The first character of the directive is ‘c’, so port c is used and the second character is ‘i’ for input. No other characters (fields) are required. NOTE portb or portw can be used for an input command as it makes no difference. The value input into the variable is the whole width of the port.