Datasheet

Page 14
Page 15
Time has come to do some coding. First thing
we need to do is to disable analog function of
PORTBpins,sotheyactasdigitalonly:
Now we have to initialize PORTB to act as
digital output. TRISB register, associated with
PORTB, is used to set whether each pin acts
as input or output.
LATB register is used instead of PORTB for
digital output. We can now initialize it with
logiczerosoneverypin:
Finally, in a while() loop we will toggle the
PORTB value, and put a 1000 ms delay, so
theblinkingisnottoofast(seeFigure 4-1).
program LedBlinking
main:
‘ Congure analog pins as digital I/O
ADPCFG = 0xFFFF
‘ set PORTB to be digital output
TRISB = 0
‘ Turn OFF LEDs on PORTB
LATB = 0
while TRUE
‘ Toggle LEDs on PORTB
LATB = not LATB
‘ Delay 1000 ms
Delay_ms(1000)
wend
end.
‘ set PORTB to be digital output
TRISB = 0
‘ Congure AN pins as digital I/O
ADPCFG = 0xFFFF
‘ Turn OFF LEDs on PORTB
LATB = 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
LedBlinking.mbas - source code
4. Code Example
Figure 4-1: Complete source code of the PORTB LED blinking