<<Previous

Next>>

PEEK

PEEK Address,Var

Read the microcontroller register at the specified Address and stores the result in Var. Special PICmicro features such as A/D converters and additional I/O ports may be read using PEEK.

If Address is a constant, the contents of this register number are placed into Var. If Address is the name of a special function register, e. g. PORTA, the contents of this register will be placed into Var. If Address is a RAM location, the value of the RAM location will first be read, then the contents of the register specified by that value will be placed into Var.

However, all of the PICmicro registers can be and should be accessed without using PEEK and POKE. All of the PICmicro registers are considered 8-bit variables by PICBASIC PRO™ and may be used as you would any other byte-sized variable. They can be read directly or used directly in equations.

	B0 = PORTA		' Get current PORTA pin states to B0

 

PEEKCODE

    PEEKCODE Address,Var

Read a value from the code space at the specified Address and store the result in Var.

PEEKCODE can be used to read data stored in the code space of a PICmicro MCU. It executes a call to the specified Address and places the returned value in Var. The specified location should contain a retlw and the data value. POKECODE may be used to store this value at the time the device is programmed.

PEEKCODE $3ff, OSCCAL   ‘ Get OSCCAL value for PIC12C671/12CE673
PEEKCODE $7ff, OSCCAL   ‘ Get OSCCAL value for PIC12C672/12CE674

 POKE

POKE Address,Value

Write Value to the microcontroller register at the specified Address. Special PICmicro features such as A/D converters and additional I/O ports may be written using POKE.

If Address is a constant, Value is placed into this register number. If Address is the name of a special function register, e. g. PORTA, Value will be placed into this register. If Address is a RAM location, the contents of the RAM location will first be read, then Value is placed into the register specified by those contents.

However, all of the PICmicro registers can be and should be accessed without using PEEK and POKE. All of the PICmicro registers are considered 8-bit variables by PICBASIC PRO™ and may be used as you would any other byte-sized variable. They can be written directly or used directly in equations.

	TRISA = 0	' Set PORTA to all outputs
	PORTA.0 = 1	' Set PORTA bit 0 high

POKECODE

    POKECODE Value{,Value...}

Store Values to the code space at the current program address at the time the microcontroller is programmed.

POKECODE can be used to generate tables in the code space of the PICmicro MCU. It generates a return with the data in W. This data can be accessed using the PEEKCODE instruction.

An assembler Org can be used to set the origin location for data storage. If it is not specified, data storage will be located immediately after the last program instruction written. 

To avoid interruption of program flow, POKECODE should be the last line of your program. It should be place after the END or STOP command.

	POKECODE 10, 20, 30 	‘ Store 10, 20, and 30 in code space
Generates:
retlw 10
retlw 20
retlw 30
@ 	org 	7ffh 	‘ Set origin to $7ff
	POKECODE $94	‘ Set OSCCAL value for PIC12C672/12CE674
Generates:
org 	7ffh
retlw 	94h

 POT

POT Pin,Scale,Var

Reads a potentiometer (or some other resistive device) on Pin. Pin may be a constant, 0 - 15, or a variable that contains a number 0 - 15 (e.g. B0) or a pin name (e.g. PORTA.0).

The resistance is measured by timing the discharge of a capacitor through the resistor (typically 5K to 50K). Scale is used to adjust for varying RC constants. For larger RC constants, Scale should be set low (a minimum value of one). For smaller RC constants, Scale should be set to its maximum value (255). If Scale is set correctly, Var should be zero near minimum resistance and 255 near maximum resistance.

Unfortunately, Scale must be determined experimentally. To do so, set the device under measure to maximum resistance and read it with Scale set to 127. Adjust Scale until the Pot command returns 254. If 255, decrease the scale. If 253 or lower, increase the scale. (NOTE: This is the same type of process performed by the Alt-P option of the BS1 environment).

Use the following code to automate the process. Make sure that you set the pot to maximum resistance.

	B0 Var Byte 
	scale Var Byte
	For scale = 1 To 255
	    POT 0, scale, B0 
	    If (B0 > 253) Then calibrated
	Next scale 

	Serout 2,0,[" Increase R or C.", 10,13]
	Stop
calibrated: 
	Serout 2,0,[" Scale= ",# scale, 10,13]

PULSIN

PULSIN Pin,State,Var

Measures pulse width on Pin. If State is zero, the width of a low pulse is measured. If State is one, the width of a high pulse is measured. The measured width is placed in Var. If the pulse edge never happens or the width of the pulse is too great to measure, Var is set to zero. If an 8-bit variable is used, only the LSB of the 16-bit measurement is returned.

Pin is automatically made an input. Pin may be a constant, 0 -15, or a variable that contains a number 0 -15 (e. g. B0) or a pin name (e. g. PORTA. 0).

The resolution of PULSIN is dependent upon the oscillator frequency. If a 4MHz oscillator is used, the pulse width is returned in 10us increments. If a 20MHz oscillator is used, the pulse width will have a 2us resolution. Defining an OSC value has no effect on PULSIN. The resolution always changes with the actual oscillator speed.

PULSIN normally waits a maximum of 65535 counts before it determines there is no pulse. If it is desired to wait fewer counts before it stops looking for a pulse or the end of a pulse, a DEFINE can be added:

DEFINE PULSIN_ MAX 1000 

This DEFINE also affects RCTIME in the same manner.

	' Measure high pulse on Pin4 stored in W3
	PULSIN PORTB.4,1,W3

 PULSOUT

PULSOUT Pin,Period

Generates a pulse on Pin of specified Period. The pulse is generated by toggling the pin twice, thus the initial state of the pin determines the polarity of the pulse. Pin is automatically made an output. Pin may be a constant, 0 - 15, or a variable that contains a number 0 - 15 (e.g. B0) or a pin name (e.g. PORTA.0).

The resolution of PULSOUT is dependent upon the oscillator frequency. If a 4MHz oscillator is used, the Period of the generated pulse will be in 10us increments. If a 20MHz oscillator is used, Period will have a 2us resolution. Defining an OSC value has no effect on PULSOUT. The resolution always changes with the actual oscillator speed.

	' Send a pulse 1mSec long (at 4MHz) to Pin5
	PULSOUT PORTB.5,100

 PWM

PWM Pin,Duty,Cycle

Outputs a pulse width modulated pulse train on Pin. Each cycle of PWM consists of 256 steps. The Duty cycle for each PWM cycle ranges from 0 (0%) to 255 (100%). This PWM cycle is repeated Cycle times. Pin may be a constant, 0 - 15, or a variable that contains a number 0 - 15 (e.g. B0) or a pin name (e.g. PORTA.0).

The Cycle time of PWM is dependent upon the oscillator frequency. If a 4MHz oscillator is used, each Cycle would be about 5ms long. If a 20MHz oscillator is used, each Cycle would be about 1ms in length. Defining an OSC value has no effect on PWM. The Cycle time always changes with the actual oscillator speed.

If you want continuous PWM output and the PICmicro MCU has PWM hardware, HPWM may be used instead of PWM.

Pin is made an output just prior to pulse generation and reverts to an input after generation stops. The PWM output on a pin looks like so much garbage, not a beautiful series of square waves. A filter of some sort is necessary to turn the signal into something useful. An RC circuit can be used as a simple D/A converter:

	PWM PORTB.7,127,100	' Send a 50% duty cycle PWM signal out Pin7 for 100 cycles
<<Previous

Next>>