'
Blinks LED using HIGH and LOW commands to control specified pin.
Symbol LED = 2 ' LED is pin 2 on PORTB
loop: High LED ' Turn on LED
Pause 1000 ' Delay 1 second
Low LED ' Turn off LED
Pause 1000 ' Delay 1 second
Goto loop ' Go back to loop and blink LED forever
'
Access PIC16C71 A/D using PEEK and POKE instructions.
Symbol ADCON0 = 8 ' A/D Configuration Register 0 address
Symbol ADRES = 9 ' A/D Result address
Symbol ADCON1 = $88 ' A/D Configuration Register 1 address
Symbol SO = 0 ' Serial output pin
Poke ADCON1, 0 ' Set PORTA 0-3 to analog inputs
Poke ADCON0, %01000001 ' Set A/D to Fosc/8, Channel 0, On
Pause 1 ' Wait 1ms for setup
loop: Poke ADCON0, %01000101 ' Start conversion
Pause 1 ' Wait 1ms for conversion to complete
Peek ADRES, B0 ' Get result to variable B0
Serout SO, N2400, (#B0, 10) ' Send variable to serial out
Goto loop
'
Read and write PORTA using PEEK and POKE instructions.
Symbol PORTA = 5 ' PORTA address
Symbol TRISA = $85 ' PORTA data direction register
Symbol SO = 0 ' Serial output
Poke TRISA, 0 ' Set PORTA to all output
Poke PORTA, 31 ' Send a value to PORTA
Poke TRISA, 255 ' Set PORTA to all input
Peek PORTA, B0 ' Get PORTA inputs to variable B0
Serout SO, N2400, (#B0, 10) ' Send variable to serial out
end
'
Read and write hardware USART using Peek and Poke
' Define the USART registers
Symbol PIR1 = $0C ' Peripheral Interrupt Flag register
Symbol RCSTA = $18 ' Receive Status and Control register
Symbol TXREG = $19 ' Transmit Data register
Symbol RCREG = $1A ' Receive Data register
Symbol TRISC = $87 ' PortC Data Direction register
Symbol TXSTA = $98 ' Transmit Status and Control register
Symbol SPBRG = $99 ' Baud Rate Generator register
' Initialize USART
Poke TRISC,%10111111 ' Set TX (PortC.6) to out, rest in
Poke SPBRG,25 ' Set baud rate to 2400
Poke RCSTA,%10010000 ' Enable serial port and continuous receive
Poke TXSTA,%00100000 ' Enable transmit and asynchronous mode
' Echo received characters in infinite loop
loop: Gosub charin ' Get a character from serial input, if any
If B1 = 0 Then loop ' No character yet
Gosub charout ' Send character to serial output
Goto loop ' Do it forever
' Subroutine to get a character from USART receiver
charin: B1 = 0 ' Preset to no character received
Peek PIR1,B0 ' Get Flag register to B0
If Bit5 = 0 Then ciret ' If no receive flag then exit
Peek RCREG,B1 ' Else get received character to B1
ciret: Return ' Go back to caller
' Subroutine to send a character to USART transmitter
charout: Peek PIR1,B0 ' Get flag register to B0
If Bit4 = 0 Then charout ' Wait for transmit register empty
Poke TXREG,B1 ' Send character to transmit register
Return ' Go back to caller
'
PicBasic program to demonstrate operation of an LCD in 4-bit mode
'
' LCD should be connected as follows:
' LCD PICmicro
' DB4 PortA.0
' DB5 PORTA.1
' DB6 PORTA.2
' DB7 PORTA.3
' RS PORTA.4 (add 4.7K pullup resistor to 5 volts)
' E PORTB.3
' RW Ground
' Vdd 5 volts
' Vss Ground
' Vo 20K potentiometer (or ground)
' DB0-3 No connect
Symbol PORTA = 5 'PORTA is PICmicro register 5
Symbol TRISA = $85 'PORTA data direction is PICmicro register hexadecimal
85
Symbol Lcde = 3 'lcd enable line is Pin3
Poke TRISA,0 'set all PORTA lines to output
Low Lcde 'start with lcd enable low
Gosub lcdinit 'initialize the lcd
loop: Gosub lcdclr 'clear lcd screen
For B4 = 0 to 4 'send string to lcd one letter at a time
Lookup B4,("Hello"),B2 'get letter from string
Gosub lcddata 'send letter in B2 to lcd
Next B4
Pause 500 'wait .5 second
Gosub lcdclr 'clear lcd screen
For B4 = 0 to 4 'send string to lcd one letter at a time
Lookup B4,("world"),B2 'get letter from string
Gosub lcddata 'send letter in B2 to lcd
Next B4
Pause 500 'wait .5 second
Goto loop 'do it forever
' subroutine to initialize the lcd - uses B2 and B3
lcdinit: Pause 15 'wait at least 15ms
Poke PORTA,3 'initialize the lcd
Gosub lcdtog 'toggle the lcd enable line
Pause 5 'wait at least 4.1ms
Poke PORTA,3 'initialize the lcd
Gosub lcdtog 'toggle the lcd enable line
Pause 1 'wait at least 100us
Poke PORTA,3 'initialize the lcd
Gosub lcdtog 'toggle the lcd enable line
Pause 1 'wait once more for good luck
Poke PORTA,2 'put lcd into 4 bit mode
Gosub lcdtog 'toggle the lcd enable line
B2 = $28 '4 bit mode, 2 lines, 5x7 font
Gosub lcdcom 'send B2 to lcd
B2 = $0c 'lcd display on, no cursor, no blink
Gosub lcdcom 'send B2 to lcd
B2 = $06 'lcd entry mode set, increment, no shift
Goto lcdcom 'exit through send lcd command
' subroutine to clear the lcd screen - uses B2 and B3
lcdclr: B2 = 1 'set B2 to clear command and fall through to lcdcom
' subroutine to send a command to the lcd - uses B2 and B3
lcdcom: B3 = B2 / 16 'shift top 4 bits down to bottom 4 bits
Poke PORTA,B3 'send upper 4 bits to lcd
Gosub lcdtog 'toggle the lcd enable line
B3 = B2 & 15 'isolate bottom 4 bits
Poke PORTA,B3 'send lower 4 bits to lcd
Gosub lcdtog 'toggle the lcd enable line
Pause 1 'wait 1ms for write to complete
Return
' subroutine to send data to the lcd - uses B2 and B3
lcddata: B3 = B2 / 16 'shift top 4 bits down to bottom 4 bits
B3 = B3 + 16 'add in register select to indicate data
Poke PORTA,B3 'send upper 4 bits to lcd
Gosub lcdtog 'toggle the lcd enable line
B3 = B2 & 15 'isolate bottom 4 bits
B3 = B3 + 16 'add in register select to indicate data
Poke PORTA,B3 'send lower 4 bits to lcd
Gosub lcdtog 'toggle the lcd enable line
Pause 1 'wait 1ms for write to complete
Return
' subroutine to toggle the lcd enable line
lcdtog: High Lcde 'set lcd enable line high
Low Lcde 'set lcd enable line low
Return
'
Simulate BS2 Shiftin and Shiftout in PicBasic
Symbol DDIR = Dir0 ' Shift data pin direction is Dir0
Symbol DPIN = Pin0 ' Shift data pin is 0
Symbol CPIN = 1 ' Shift clock pin is 1
Symbol I = B2 ' Loop counter
' Shift in some data
Low CPIN ' Start shift clock low
Gosub shiftin ' Shift in some data
' Shift out some data
Low CPIN ' Start shift clock low
B0 = 100 ' Data to shift out
Gosub shiftout ' Go do it
End
' Subroutine to synchronously shift in one byte
shiftin: DDIR = 0 ' Set data pin direction to input
For I = 1 to 8 ' 8 bits to a byte
B0 = B0 * 2 ' Shift result 1 bit to the left
Toggle CPIN ' Toggle shift clock
Bit0 = DPIN ' Move data into LSB
Toggle CPIN ' Toggle shift clock once more
Next I ' Loop
Return ' Go back to caller
' Subroutine to synchronously shift out one byte
shiftout: DDIR = 1 ' Set data pin direction to output
For I = 1 to 8 ' 8 bits to a byte
DPIN = Bit0 ' Data out is LSB
Toggle CPIN ' Toggle shift clock
B0 = B0 / 2 ' Shift byte 1 bit to the right
Toggle CPIN ' Toggle shift clock once more
Next I ' Loop
Return ' Go back to caller
'
Demo program of table use past 2K in PicBasic
' Must be done in 2 stages:
' First compile using -s switch to suppress assembly.
' Then delete second include "pbl.inc" from end of .src file.
' Finally execute PM to finish the process.
Symbol i = B2
loop: For i = 0 to 10 ' 11 chars in this table
Gosub gettable1 ' Get char i from table 1
Serout 0, N2400, (B0) ' Send char from table to serial pin
Next i ' Move to next char
Serout 0, N2400, (13) ' Send carriage return to serial pin
Goto loop ' Do it forever
gettable1:
Asm ' The following must be in assembler
movlw table1/256 ; Get page address of table
movwf PCLATH ; Set pc latch high
call table1 ; Do long call
clrf PCLATH ; Reset pc latch high to page 0
movwf _B0 ; Save char in B0
goto done ; Return to caller via done
include "pbl.inc" ; Include the PBC library here
org 800H ; Bump to next 2K
table1 movf _i, W ; Get char index to W
addwf PCL
retw "Hello world" ; Return char i in W
Endasm ' That's it for assembler
|