PinClear
Syntax
PinClear Pin Number
Overview
Pull a Port’s pin low using a variable as the pin’s number, but does not set it as an output.
Parameter
Pin Number can be a variable or constant or expression that holds a value from 0 to the amount of I/O pins on the device. A value of 0 will be PORTA.0, if present, 1 will be PORTA.1, 8 will be PORTB.0 etc…
Example
' Clear then Set each pin of PORTB
Device = 16F1829
' Select the device to compile for
Declare Xtal = 16 ' Tell the compiler the device will be operating at 16MHz
Dim PinNumber as Byte
High PORTB
' Make PORTB output high before we start
Do
' Create a loop
For PinNumber = 8 to 16
' Create a loop for 8 pins
PinClear PinNumber
' Clear each pin of PORTB
DelayMs 100
' Slow things down to see what's happening
Next
' Close the loop
For PinNumber = 8 to 16
' Create a loop for 8 pins
PinSet PinNumber
' Set each pin of PORTB
DelayMs 100
' Slow things down to see what's happening
Next
' Close the loop
Loop
' Do it forever
Notes.
There are many ways to pull a pin of an I/O port low, however, each method requires a certain amount of manipulation, either with rotates, or alternatively, the use of indirect addressing. Each method has its merits, but requires a certain amount of knowledge to accomplish the task correctly. The PinClear command makes this task extremely simple using a variable as the pin number, however, this is not necessarily the quickest method, or the smallest, but it is the easiest. For speed and size optimisation, there is no shortcut to experience.
To clear a known constant pin number of a port, access the pin directly using the Low command
Low PORTA.1
Each pin number has a designated name. These are Pin_A0…Pin_A7, Pin_B0…Pin_B7, Pin_C0…Pin_C7, Pin_D0…Pin_D7 to Pin_L7 etc… Each of the names has a relevant constant value, for example, Pin_A0 has the value 0, Pin_B0 has the value 8, up to Pin_L7, which has the value 87. For 8-pin devices, the pins can be referenced as Pin_IO0...Pin_IO5. The pin names can be found within the device’s .def file, within the compiler’s directory.
These can be used to pass a relevant pin number to a subroutine. For example:
'
' Flash an LED attached to PORTB.0 via a procedure
' Then flash an LED attached to PORTB.1 via the same procedure
'
Device = 18F25K40
' Select the device to compile for
Declare Xtal = 16 ' Tell the compiler the device will be operating at 16MHz
Dim PinNumber As Byte
' Holds the pin number to set high and low
Do
' Create an infinite loop
PinNumber = Pin_B0
' Give the pin number to flash (PORTB.0)
FlashPin()
' Call the procedure to flash the pin
PinNumber = Pin_B1
' Give the pin number to flash (PORTB.1)
FlashPin()
' Call the procedure to flash the pin
Loop
' Do it forever
'
' Make a pin high then low for 500ms using a variable as the pin to adjust
'
Proc FlashPin()
Output PinNumber
' Make the pin an output
PinSet PinNumber
' Bring the pin high
DelayMs 500
' Wait for 500 milliseconds
PinClear PinNumber
' Bring the pin low
DelayMs 500
' Wait for 500 milliseconds
EndProc
Example 2
' Clear then Set each pin of PORTC
Device = 18F25K40
' Select the device to compile for
Declare Xtal = 16 ' Tell the compiler the device will be operating at 16MHz
Dim PinNumber as Byte
High PORTC
' Make PORTC output high before we start
Do
' Create a loop
For PinNumber = Pin_C0 to Pin_C7 ' Create a loop for 8 pins
PinClear PinNumber
' Clear each pin of PORTC
DelayMs 100
' Slow things down to see what's happening
Next
' Close the loop
For PinNumber = Pin_C0 to Pin_C7 ' Create a loop for 8 pins
PinSet PinNumber
' Set each pin of PORTC
DelayMs 100
' Slow things down to see what's happening
Next
' Close the loop
Loop
' Do it forever