<<Previous

Next>>

5.22. PEEK

PEEK Address, Var

Reads the PICmicro MCU register at the specified Address and stores the result in Var. Special PICmicro MCU features such as A/D converters and additional I/O ports may be read using PEEK. PEEK is a PICBASIC™ Compiler statement and is not supported on the BASIC Stamp.

PEEK and POKE allow direct access to all of the registers on a PICmicro MCU including PORTA, PORTB, PORTC, PORTD, PORTE and their associated data direction (TRIS) registers. PEEK and POKE operate on all of the bits, i.e. the entire byte, of the particular register at once. When you POKE data to PORTA, the entire port is updated, not just one individual bit.

If substantial individual bit manipulation is required, it is recommended that those I/O functions be assigned to a pin on PORTB and less demanding byte access be left to the other I/O ports. Alternatively variable B0 allows manipulation of its individual bits. It can first be set up as desired and then POKEd to PORTA, for example, to ease bit manipulation. Following are examples of this technique. This technique may be used with any PICmicro MCU register.

' Read PortA bits using intermediate variable B0

 

Symbol PortA = 5   'Define PortA register location
Symbol TrisA = $85 'Define PortA direction register location
Poke TrisA,255 'Make all PortA pins inputs

 

loop: Peek PortA,B0 'Get current PortA pin states to variable B0
If Bit0 = 1 Then zerohigh 'Jump to label zerohigh if Bit0 (RA0) is high
If Bit1 = 0 Then onelow 'Jump to label onelow if Bit1 (RA1) is low
Goto loop 'Go check some more

 

zerohigh: High 0

'Set Pin0 (RB0) high

Goto loop 'Go do some more

 

onelow: Low 1

'Set Pin1 (RB1) low

Goto loop 'Go do some more
End

 

' Set PortA bits using intermediate variable B0

 

Symbol PortA = 5 'Define PortA register location
Symbol TrisA = $85 'Define PortA direction register location
Poke TrisA,0 'Make all PortA pins outputs

 

Peek PortA,B0 'Get current PortA pin states to variable B0
Bit1 = 1 'Set Bit1 in B0 which will become PortA pin 1 high
Bit3 = 0 'Set Bit3 in B0 which will become PortA pin 3 low
'Bit0, 2 and 4 will remain unchanged
Poke PortA,B0

'Send the new byte to PortA to complete the change

End

5.23. POKE

POKE Address, Value

Writes Value to the PICmicro MCU register at the specified Address. Special PICmicro MCU features such as A/D converters and additional I/O ports may be written using POKE. POKE is a PICBASIC™ Compiler statement and is not supported on the BASIC Stamp. (See PEEK for more information.)

Poke $85,0 'Write 0 to register hexadecimal 85 (Sets PortA to all outputs)

<<Previous

Next>>