• No results found

Pins,฀Ports฀and฀Input/Output

Since฀every฀useful฀program฀must฀read฀from฀or฀write฀to฀a฀PIC’s฀input/output฀pins,฀let’s฀summarize฀how฀

MBasic฀handles฀pins฀and฀ports.฀It฀can฀be฀confusing฀because฀some฀pins฀have฀triple฀or฀quadruple฀or฀even฀more฀

duties฀and฀because฀MBasic฀provides฀several฀ways฀to฀address฀any฀given฀pin.฀And,฀the฀word฀pin฀itself฀has฀dual฀

usage,฀as฀it฀refers฀to฀the฀physical฀packaging฀(an฀8-pin฀DIP฀package฀for฀example)฀and฀to฀those฀physical฀pins฀

that฀may฀be฀used฀for฀various฀purposes.฀To฀simplify฀our฀discussion฀we฀will฀limit฀ourselves฀to฀PICs฀that฀are฀

supported฀by฀MBasic฀and฀plug฀into฀either฀the฀0818฀or฀2840฀development฀boards.

PICs฀communicate฀with฀external฀circuitry฀through฀intermediary฀“ports.”฀Ports฀are฀treated฀internally฀by฀the฀

PIC’s฀CPU,฀and฀by฀MBasic,฀as฀byte฀(8-bit)฀variables฀with฀each฀bit฀corresponding฀to฀a฀particular฀pin.฀For฀

example,฀the฀most฀significant฀bit฀in฀PortB’s฀byte฀value฀corresponds฀to฀pin฀RB7,฀while฀the฀least฀significant฀bit฀

corresponds฀to฀RB0.฀(In฀some฀PICs,฀not฀all฀bits฀of฀each฀port฀variable฀have฀physical฀pin฀assignments.) Letters฀from฀A…E฀identify฀ports,฀except฀in฀DIP8฀packaged฀PICs฀which฀have฀only฀one฀port,฀called฀GPIO฀

(general฀purpose฀input/output).฀Thus,฀we฀have฀GPIO,฀PortA,฀PortB…PortE฀as฀predeclared฀variables฀in฀

MBasic.฀(Port฀identifiers฀are฀written฀without฀a฀space,฀for฀example,฀PortA,฀not฀Port฀A.)฀Of฀course,฀not฀all฀

PICs฀physically฀support฀all฀of฀these฀ports,฀and฀in฀some฀cases฀not฀

all฀eight฀bits฀of฀a฀port฀have฀associated฀pins.฀For฀example,฀the฀

PIC16F876฀has฀PortA฀(but฀only฀bits฀0…5฀are฀mapped฀to฀pins),฀

PortB฀and฀PortC.฀MBasic’s฀configuration฀files,฀fortunately,฀ensure฀

that฀only฀legitimate฀port฀variables฀are฀predeclared฀for฀the฀particular฀

PIC฀being฀programmed.

Figure฀2-7฀illustrates,฀as฀an฀example,฀Port฀B฀and฀its฀pin฀assign-ments.฀Each฀general-purpose฀I/O฀pin฀is฀identified฀with฀a฀consistent฀

naming฀convention.฀For฀example,฀RB0฀is฀PortB,฀bit฀0.฀The฀“R”฀

in฀RB฀stands฀for฀“register,”฀which฀is฀synonymous฀with฀“file”฀or฀

“variable.”฀MBasic฀also฀pre-defines฀constants฀associated฀with฀each฀ Figure฀2-7:฀PortB฀to฀pin฀assignments.

of฀these฀pins,฀so฀we฀have฀constants฀B0,฀B1…B7฀available฀to฀us.฀MBasic฀gives฀us฀a฀second฀way฀to฀reference฀

pins฀through฀a฀sequential฀numbering฀system,฀for฀example,฀A0฀=฀0,฀A1฀=฀1฀up฀through฀E7฀=฀39.฀Finally,฀to฀

provide฀backwards฀compatibility฀with฀the฀Basic฀Stamp™,฀MBasic฀includes฀the฀dedicated฀functions฀INxx,฀

OUTxx฀and฀DIRxx฀where฀xx฀is฀the฀bit,฀nibble,฀byte฀or฀word฀identifier฀as฀reflected฀in฀Table฀2-2.

Table฀2-2:฀Port฀and฀bit฀I/O฀variables,฀constants฀and฀dedicated฀functions.

Variables Constants Dedicated฀Functions

Port฀at฀a฀

Time

Nibble฀at฀

a฀฀Time

Bit฀at฀a฀

Time

INS/OUTS/DIRS

Port฀฀

Variable Nibble Port.Bit Pin฀

Constant Pin฀

Value Bit Nibble Byte Word

PortA

PortA.

Nib0 or PortA.

LowNib

PortA.Bit0 RA0 0 IN0/OUT0/DIR0

INA/OUTA/

DIRA

INL/OUTL/

DIRL

INS/

OUTS/

DIRS

PortA.Bit1 RA1 1 IN1/OUT1/DIR1

PortA.Bit2 RA2 2 IN2/OUT2/DIR2

PortA.Bit3 RA3 3 IN3/OUT3/DIR3

PortA.

Nib1 or PortA.

HighNib

PortA.Bit4 RA4 4 IN4/OUT4/DIR4

INB/OUTB/

DIRB

PortA.Bit5 RA5 5 IN5/OUT5/DIR5

PortA.Bit6 RA6 6 IN6/OUT6/DIR6

PortA.Bit7 RA7 7 IN7/OUT7/DIR7

PortB

PortB.Nib0 or PortB.

LowNib

PortB.Bit0 RB0 8 IN8/OUT8/DIR8

INC/OUTC/

DIRC

INHOUTH/

DIRH

PortB.Bit1 RB1 9 IN9/OUT9/DIR9

PortB.Bit2 RB2 10 IN10/OUT10/

DIR10

PortB.Bit3 RB3 11 IN11/OUT11/

DIR11

PortB.Nib1 or PortB.

HighNib

PortB.Bit4 RB4 12 IN12/OUT12/

DIR12

IND/OUTD/

DIRD

PortB.Bit5 RB5 13 IN13/OUT13/

DIR13

PortB.Bit6 RB6 14 IN14/OUT14/

DIR14

PortB.Bit7 RB7 15 IN15/OUT15/

DIR15

PortC

PortC.

Nib0 or PortC.

LowNib

PortC.Bit0 RC0 16

PortC.Bit1 RC1 17

PortC.Bit2 RC2 18

PortC.Bit3 RC3 19

PortC.

Nib1 or PortC.

HighNib

PortC.Bit4 RC4 20 INxx:฀Read฀status,฀whether฀in฀input฀or฀

output฀mode

PortC.Bit5 RC5 21 OUTxx:฀Write฀value฀

PortC.Bit6 RC6 22 DIRxx:฀Set฀direction฀

1=input,฀0=output

PortC.Bit7 RC7 23

(continued)

Table฀2-2:฀Port฀and฀bit฀I/O฀variables,฀constants฀and฀dedicated฀functions.

Variables Constants Dedicated฀Functions

Port฀at฀a฀

Time

Nibble฀at฀

a฀฀Time

Bit฀at฀a฀

Time

INS/OUTS/DIRS

Port฀฀

Variable Nibble Port.Bit Pin฀

Constant Pin฀

Value Bit Nibble Byte Word

PortD

PortD.

Nib0 or PortD.

LowNib

PortD.Bit0 RD0 24 xx฀as฀appropriate฀for฀bit,฀nibble,฀byte฀

or฀word

PortD.Bit1 RD1 25

PortD.Bit2 RD2 26 Note:฀DIRxx฀command฀is฀reversed฀

from฀Basic฀Stamp

PortD.Bit3 RD3 27

PortD.

Nib1 or PortD.

HighNib

PortD.Bit4 RD4 28

PortD.Bit5 RD5 29

PortD.Bit6 RD6 30

PortD.Bit7 RD7 31

PortE

PortE.Nib0 or PortE.

LowNib

PortE.Bit0 RE0 32

PortE.Bit1 RE1 33

PortE.Bit2 RE2 34

PortE.Bit3 RE3 35

PortE.Nib1 or PortE.

HighNib

PortE.Bit4 RE4 36

PortE.Bit5 RE5 37

PortE.Bit6 RE6 38

PortE.Bit7 RE7 39

MBasic฀permits฀us฀to฀reference฀a฀port฀or฀a฀pin฀as฀an฀address฀or฀as฀a฀variable.฀As฀an฀address,฀the฀port฀or฀pin฀

is฀an฀argument฀to฀certain฀functions.฀As฀a฀variable,฀the฀value฀of฀the฀port฀(either฀in฀reading฀or฀writing)฀can฀be฀

used฀like฀any฀other฀variable.฀There฀are฀also฀the฀dedicated฀functions฀identified฀in฀Table฀2-2฀that฀operate฀on฀

specific฀ports฀or฀pins฀without฀an฀explicit฀port฀or฀pin฀reference,฀such฀as฀IN0.฀We฀must฀remember฀that฀MBasic฀

automatically฀initializes฀all฀I/O฀pins฀as฀inputs฀and฀that฀before฀reading฀from฀or฀writing฀to฀a฀port฀or฀a฀pin฀we฀

must฀follow฀some฀simple฀rules:

First,฀set฀the฀direction฀of฀the฀port฀or฀pin฀to฀be฀either฀an฀input฀or฀output;฀

Second,฀read฀the฀port฀or฀pin฀if฀an฀input,฀or฀write฀to฀the฀port฀or฀pin฀if฀an฀output;

or,

Read฀from฀or฀write฀to฀a฀port฀or฀pin฀with฀a฀procedure฀that฀automatically฀sets฀the฀direction.

Output฀Mode

Let’s฀see฀how฀many฀different฀ways฀we฀can฀assign฀a฀pin฀to฀an฀output฀and฀to฀make฀its฀value฀0.฀We’ll฀use฀pin฀

RB0฀as฀our฀example.

;Direct฀Pin฀Addressing

;---Output฀B0฀ ;฀First฀make฀it฀an฀output.฀B0฀is฀a฀constant PortB.Bit0฀=฀0฀฀ ;฀PortB.Bit0฀is฀a฀variable

Dir8฀=฀0฀ ;฀Special฀purpose฀function,฀DIR8฀is฀for฀pin฀B0 Out8฀=฀0฀ ;฀Likewise฀for฀Out8

Low฀B0฀ ฀ ;฀Low฀function฀automatically฀makes฀it฀an฀output

;฀no฀need฀to฀separately฀make฀it฀into฀an฀output Output฀8฀ ;฀B0฀is฀an฀alias฀for฀8฀so฀can฀use฀8฀directly PortB.Bit0฀=฀0฀฀ ;฀Make฀the฀variable฀assignment

Low฀8฀ ฀ ;฀B0฀is฀an฀alias฀for฀8฀so฀can฀use฀8฀directly

;฀LOW฀switches฀to฀output฀mode฀and฀outputs฀0 TRISB.Bit0฀=฀0฀฀ ;฀TRISB฀variable฀controls฀PortB฀I/O฀direction,฀

;฀0=output฀&฀1=input.฀

PortB.Bit0฀=฀0฀฀ ;฀PortB.Bit0฀is฀a฀variable

;Byte฀at฀a฀time฀addressing฀to฀deal฀with฀multiple฀pins

;in฀one฀instruction

;---TRISB฀=฀%00000000฀ ;฀Sets฀all฀8฀pins฀to฀0,฀i.e.,฀output PortB฀=฀%00000000฀฀ ;฀Assign฀all฀8฀bits฀(pins)฀to฀0.฀

DIRH฀=฀%00000000฀฀฀ ;฀Make฀all฀8฀Pins฀in฀PortB฀output OUTH฀=฀%00000000฀฀฀ ;฀Set฀all฀8฀bits฀(pins)฀to฀0

Input฀Mode

To฀make฀RB0฀an฀input฀and฀read฀its฀value,฀we฀have฀the฀choice฀of฀a฀similar฀set฀of฀options:

;Direct฀Pin฀Addressing

;฀Assume฀we฀have฀already฀declared:

;฀ BitVar฀฀Var฀฀ Bit

;฀ ByteVar฀Var฀ Byte

;฀to฀hold฀the฀value฀being฀read

;---Input฀B0฀ ;฀First฀make฀it฀an฀input.฀B0฀is฀a฀constant BitVar฀=฀PortB.Bit0฀฀ ;฀PortB.Bit0฀is฀a฀variable

Dir8฀=฀1฀ ;฀Special฀purpose฀function,฀DIR8฀is฀for฀pin฀B0 BitVar฀=฀In8฀ ;฀Likewise฀for฀IN8

Input฀8฀฀ ;฀B0฀is฀an฀alias฀for฀8฀so฀can฀use฀8฀directly BitVar฀=฀PortB.Bit0฀฀ ;฀Make฀the฀variable฀assignment

TRISB.Bit0฀=฀1฀฀ ;฀TRISB฀variable฀controls฀PortB฀I/O฀

฀฀฀ ;฀direction,0=output฀&฀1=input.฀

BitVar฀=฀PortB.Bit0฀฀ ;฀PortB.Bit0฀is฀a฀variable

;Byte฀at฀a฀time฀addressing฀to฀deal฀with฀multiple฀pins

;in฀one฀instruction

;---TRISB฀=฀%11111111฀ ;฀Sets฀all฀8฀pins฀to฀1,฀i.e.,฀input ByteVar฀=฀PortB฀฀฀฀ ;฀Read฀all฀8฀bits฀(pins)฀into฀ByteVar.฀

DIRH฀=฀%11111111฀฀฀ ;฀Make฀all฀8฀Pins฀in฀PortB฀input ByteVar฀=฀INH฀฀฀฀฀฀ ;฀Read฀all฀8฀bits฀(pins)฀into฀ByteVar

Pin฀Variables฀vs.฀Addresses

One฀common฀error฀by฀beginners฀is฀confusing฀pin฀variables฀with฀pin฀addresses.฀The฀functions฀Output,฀Low฀

and฀Input฀require฀a฀pin฀address฀as฀their฀argument.฀The฀pin฀address฀may฀be฀one฀of฀MBasic’s฀pre-defined฀

constants,฀for฀example,฀B0,฀or฀its฀equivalent฀numerical฀value,฀8.฀The฀pin฀address฀may฀also฀be฀the฀value฀of฀a฀

variable,฀such฀as:

For฀I฀=฀B0฀to฀B7฀ ;฀I฀goes฀from฀8฀(B0)฀to฀15฀(B7)

Low฀I฀ ฀ ;฀Makes฀B0฀low,฀then฀B1฀through฀B7฀sequentially Next

In฀the฀Low฀I฀statement,฀Low฀operates฀on฀the฀value฀of฀I,฀which฀it฀interprets฀as฀the฀address฀of฀a฀pin.฀When฀

I฀is฀8,฀for฀example,฀Low฀operates฀on฀pin฀RB0.฀Thus,฀the฀above฀code฀fragment฀is฀identical฀with:

For฀I฀=฀8฀to฀15฀

Low฀I฀

Next

Pin฀variables฀are฀used฀to฀read฀the฀value฀of฀a฀pin฀or฀of฀a฀port฀and฀to฀write฀to฀a฀pin฀or฀port฀via฀an฀assignment฀

(the฀“=”฀sign).฀Thus฀we฀have฀ByteVar฀=฀PortB,฀or฀PortB฀=฀$FF.฀We฀also฀may฀use฀PortB฀like฀any฀other฀

byte฀variable,฀such฀as฀x=2*PortB.

If฀we฀try฀to฀read฀the฀value฀of฀pin฀B0฀as฀an฀input฀with฀the฀statement฀BitVar฀=฀B0,฀the฀compiler฀will฀produce฀

no฀error,฀but฀BitVar฀will฀not฀hold฀the฀desired฀result.฀Rather,฀this฀statement฀is฀identical฀with฀BitVar฀=฀8.฀If฀

testing฀for฀a฀pin฀value฀condition฀in฀a฀loop฀statement,฀it’s฀important฀that฀the฀variable฀construct฀be฀used.

;To฀test฀Pin฀B0฀

;---If฀PortB.Bit0฀=฀1฀Then

;฀execute฀code฀goes฀here EndIf

;The฀following฀code฀will฀compile฀but฀won’t฀work

;since฀it’s฀the฀same฀as฀writing฀If฀(8฀=฀1),฀which฀is฀always฀false If฀B0฀=฀1฀Then

;execute฀code฀goes฀here EndIf

Finally฀it’s฀possible฀to฀read฀from฀a฀pin฀or฀port฀that฀is฀set฀for฀output฀in฀whole฀or฀in฀part,฀and฀to฀write฀to฀a฀pin฀

or฀port฀that฀is฀set฀for฀input฀in฀whole฀or฀in฀part.฀No฀error฀message฀will฀be฀generated.฀If฀you฀are฀experiencing฀

strange฀or฀unstable฀results฀reading฀or฀writing฀to฀pins฀or฀ports,฀check฀to฀ensure฀the฀correct฀direction฀is฀set฀and฀

that฀you฀are฀correctly฀using฀pin฀variables฀and฀pin฀constants.

PICs฀equipped฀with฀analog-to-digital฀converters฀apply฀the฀designators฀AN0,฀AN1…฀to฀pins฀that฀also฀have฀an฀

analog฀function.฀Thus,฀a฀16F876฀pin฀name฀RA0/AN0฀indicates฀that฀the฀pin฀has฀three฀possible฀uses:฀digital฀

output,฀digital฀input฀(RA0)฀and฀analog฀(AN0)฀input.฀The฀process฀of฀assigning฀a฀pin฀to฀be฀an฀analog฀input฀is฀

discussed฀in฀detail฀in฀Chapter฀11.

Run฀Time฀vs.฀Program฀Time฀Pin฀Assignments

All฀the฀pin฀assignments฀we฀have฀discussed฀to฀this฀point฀are฀run฀time฀alterable,฀i.e.,฀their฀status฀may฀be฀altered฀

by฀the฀program฀on฀the฀fly.฀In฀one฀part฀of฀your฀program฀a฀pin฀may฀be฀an฀input฀and฀later฀in฀the฀program฀the฀

same฀pin฀may฀be฀an฀output.฀However,฀in฀some฀PICs—most฀often฀those฀in฀the฀8฀and฀18-pin฀packages—cer-tain฀pin฀configurations฀may฀only฀be฀established฀at฀program฀time,฀a฀task฀usually฀accomplished฀via฀an฀option฀

dialog฀box฀in฀MBasic฀before฀compiling฀your฀code.฀(This฀permits฀Microchip฀to฀make฀their฀smaller฀package฀

devices฀more฀flexible,฀but฀at฀the฀cost฀of฀confusion฀to฀beginning฀programmers.)฀Then,฀depending฀upon฀the฀

program฀time฀configuration,฀further฀run฀time฀changes฀may฀be฀possible.฀Program฀time฀pin฀setup฀is฀highly฀

device฀specific฀and฀reference฀to฀the฀data฀sheet฀for฀your฀specific฀PIC฀will฀be฀beneficial.

We’ll฀explore฀the฀difference฀between฀run฀time฀and฀program฀time฀alterable฀pins฀in฀the฀context฀of฀the฀12F629,฀

which฀has฀3฀pins฀that฀must฀be฀configured฀at฀program฀time:

12F629฀Example฀of฀Pins฀Configured฀at฀Program฀Time

Pin฀Name Program฀Time฀Configuration Run฀Time฀Configuration

GP3/MCLR/Vpp GP3฀(general-purpose฀I/O) GP3:฀Input

GP3:฀Output MCLR/Vpp฀(master฀clear฀/฀Vprogram) None

GP4/T1G/OSC2/CLKOUT

GP4฀(general-purpose฀I/O)

GP4:฀Input GP4:฀Output T1G฀(timer฀1฀gate) OSC2฀(second฀resonator฀connection) None

CLKOUT฀(clock฀out) None

Figure฀ 2-8:฀ Program฀ time฀ pin฀ options฀

for฀12F629.

12F629฀Example฀of฀Pins฀Configured฀at฀Program฀Time

Pin฀Name Program฀Time฀Configuration Run฀Time฀Configuration

GP5/T1CKI/OSC1/CLKIN

GP5฀(general-purpose฀I/O)

GP5:฀Input GP5:฀Output T1CKI฀(Timer฀1฀clock฀in)

CLKIN฀(external฀clock฀input) None

OSC1฀(first฀resonator฀connection) None

If฀the฀GP3/MCLR/Vpp฀pin฀is฀defined฀at฀program฀time฀to฀be฀a฀gener-al-purpose฀I/O฀pin,฀it฀may฀be฀used฀for฀input฀or฀output฀exactly฀as฀we฀

have฀earlier฀discussed,฀and฀changed฀from฀input฀to฀output฀under฀pro-gram฀control.฀However,฀if฀at฀program฀time฀it฀is฀defined฀as฀MCLR,฀it฀

is฀unavailable฀for฀any฀other฀purpose.฀This฀selection฀is฀accomplished฀

with฀the฀MCLR฀check฀box฀found฀in฀MBasic’s฀Configuration฀dialog฀

box,฀as฀shown฀in฀Figure฀2-8.฀

The฀two฀oscillator฀pins฀also฀must฀be฀defined฀at฀program฀time,฀but฀

are฀linked.฀If฀you฀plan฀to฀use฀an฀external฀resonator฀or฀crystal,฀the฀

OSC1฀and฀OSC2฀pin฀configuration฀must฀be฀active.฀If฀you฀plan฀to฀

use฀an฀external฀clock฀source,฀then฀the฀CLKIN฀option฀must฀be฀active.฀

If฀you฀wish฀to฀use฀the฀internal฀RC฀oscillator,฀then฀the฀CLKOUT฀pin฀

may฀either฀be฀GP5฀or฀OSCOUT.฀If฀an฀external฀RC฀oscillator฀is฀used,฀

the฀RC฀network฀must฀connect฀to฀the฀OSC1฀pin.฀Table฀2-3฀shows฀

how฀these฀options฀interact฀and฀how฀they฀are฀selected฀in฀the฀MBasic฀

configuration฀box฀of฀Figure฀2-8.฀(The฀MBasic฀configuration฀options฀

correspond฀to฀the฀first฀column฀in฀Table฀2-3.)

Table฀2-3:฀Configuration฀Dialog฀for฀12F629฀–฀Oscillator฀Configuration.

Configuration฀

Dialog฀Box

Oscillator฀

Configuration฀ GP4/T1G/OSC2/CLKOUT฀Function GP5/T1CKI/OSC1/CLKIN฀Function

LowPower LP OSC2—crystal฀connection OSC1—crystal฀connection฀

External XT OSC2—crystal฀connection OSC1—crystal฀connection

HighSpeed HS OSC2—crystal฀connection OSC2—crystal฀connection

ExternalClk EC GP4฀(general-purpose฀I/O) CLKIN฀(external฀clock฀input) IntRC฀on฀GP4 INTOSC GP4฀(general-purpose฀I/O) GP5฀(general-purpose฀I/O) IntRC฀on฀ClkOut INTOSC CLKOUT฀(clock฀waveform฀output) GP5฀(general-purpose฀I/O)

ExtRC฀on฀GP4 RC GP4฀(general-purpose฀I/O) CLKIN-RC฀circuit฀on฀pin

ExtRC฀on฀ClkOut RC CLKOUT฀(clock฀waveform฀output) CLKIN-RC฀circuit฀on฀pin

Further฀complicating฀an฀already฀complex฀matter,฀an฀external฀clock฀source฀may฀be฀used฀in฀the฀LP,฀XT฀and฀

HS฀modes฀by฀feeding฀it฀into฀OSC1,฀in฀which฀case,฀OSC2฀is฀unused.฀The฀LP,฀XT฀and฀HS฀modes฀set฀internal฀

parameters฀in฀the฀oscillator฀section฀of฀the฀12F629฀and฀establish฀the฀maximum฀resonator฀or฀crystal฀frequency฀

and฀associated฀capacitor฀values.฀Section฀9฀of฀Microchip’s฀PIC12F629/675฀Data฀Sheet฀should฀be฀consulted฀

for฀specifics.

LVP฀Programming฀Pin฀Selection฀

One฀compile฀time฀feature฀shared฀by฀16F876/877฀chips฀(including฀the฀“A”฀versions),฀along฀with฀many฀other฀

flash฀program฀memory฀PICs,฀is฀the฀low฀voltage฀programming฀(LVP)฀option.฀Historically,฀flash฀memory฀

has฀required฀application฀of฀a฀programming฀voltage฀two฀or฀three฀times฀that฀of฀the฀normal฀operating฀voltage,฀

typically฀12V฀for฀a฀PIC฀operating฀with฀VDD฀of฀5V,฀known฀as฀high฀voltage฀programming฀(HVP).฀Newer฀PICs,฀

such฀as฀the฀16F876/877/876A/877A฀may฀be฀optionally฀programmed฀in฀LVP฀mode,฀using฀only฀+5V.฀Whether฀

a฀PIC฀that฀supports฀LVP฀actually฀has฀LVP฀enabled฀is฀determined฀by฀a฀configuration฀bit,฀the฀value฀of฀which฀is฀

keyed฀to฀the฀LVP฀checkbox฀in฀MBasic’s฀configuration฀setup฀dialog฀box฀seen฀in฀Figure฀2-8.฀If฀the฀chip฀does฀

not฀support฀LVP,฀as฀is฀true฀in฀Figure฀2-8,฀the฀LVP฀check฀box฀is฀grayed฀out.฀

To฀ensure฀maximum฀flexibility฀when฀programming฀both฀older฀and฀newer฀model฀PICs,฀Basic฀Micro’s฀ISP-PRO฀and฀its฀0818฀and฀2840฀Development฀Boards฀use฀high฀voltage฀programming฀and฀for฀the฀programs฀in฀this฀

book฀you฀should฀not฀select฀LVP฀mode฀in฀MBasic’s฀programming฀options.฀Microchip฀enables฀LVP฀as฀part฀of฀

the฀manufacturing฀process,฀so฀when฀programming฀a฀new฀PIC฀for฀the฀first฀time฀you฀will฀find฀it฀necessary฀to฀

clear฀the฀LVP฀selection฀box฀in฀MBasic’s฀configuration฀menu,฀if฀that฀model฀PIC฀has฀LVP฀functionality.฀

Basic฀Micro’s฀ISP-PRO฀does฀not฀support฀LVP฀and฀programs฀only฀using฀HVP฀mode.฀But,฀since฀a฀PIC฀with฀

LVP฀enabled฀is฀still฀programmable฀via฀HVP,฀you฀can,฀nonetheless,฀select฀LVP฀and฀program฀16F876/877/

876A/877A฀chips฀with฀MBasic฀and฀the฀ISP-PRO.฀However,฀if฀you฀do฀so,฀pin฀RB3฀becomes฀the฀LVP฀control฀

pin฀and฀is฀no฀longer฀available฀as฀a฀general-purpose฀I/O฀pin.฀(The฀specific฀pin฀used฀for฀LVP฀control฀varies;฀for฀

example,฀a฀16F628฀uses฀RB4.)฀Not฀every฀LVP-capable฀model฀PIC฀behaves฀so฀nicely฀and฀you฀may฀find฀some฀

model฀devices฀refuse฀to฀program฀if฀you฀inadvertently฀leave฀the฀LVP฀option฀selected.฀I’ve฀even฀seen฀different฀

samples฀of฀the฀same฀model฀PIC฀behave฀differently฀with฀the฀LVP฀option฀selected.฀In฀this฀case,฀clearing฀the฀

LVP฀check฀box,฀followed฀by฀several฀cycles฀of฀MBasic’s฀“erase”฀function฀usually฀restores฀programmability.

Weak฀Pull-Up

One฀last฀remark฀and฀we฀may฀leave฀this฀overly฀long฀discussion฀of฀pins.฀Many฀PICs฀have฀built-in฀“weak”฀pull-up฀

resistors฀for฀Port฀B,฀usable฀when฀set฀to฀be฀an฀input.฀We’ll฀deal฀with฀floating฀input฀gates฀and฀the฀need฀for฀pull-up฀resistors฀in฀Chapter฀4,฀but฀MBasic’s฀procedure฀for฀controlling฀Port฀B’s฀internal฀pull฀ups฀is฀SetPullUps฀

<mode>฀where฀mode฀is฀one฀of฀two฀pre-defined฀constants,฀Pu_Off฀or฀Pu_On฀for฀de-activating฀or฀activating,฀

respectively,฀internal฀pull-up฀resistors.฀Pull-up฀resistors฀for฀all฀eight฀pins฀of฀Port฀B฀are฀activated฀or฀deactivated฀

by฀this฀command,฀not฀individual฀pins.฀For฀Port฀B฀pins฀that฀are฀set฀to฀be฀outputs,฀SetPullUps฀has฀no฀effect.