Upload
ngodieu
View
224
Download
1
Embed Size (px)
Citation preview
' PicBasic Pro program to display result of
' 10-bit A/D conversion on LCD
'
' Connect analog input to channel-0 (RA0)
' Define LCD registers and bits
Define LCD_DREG PORTD
Define LCD_DBIT 4
Define LCD_RSREG PORTE
Define LCD_RSBIT 0
Define LCD_EREG PORTE
Define LCD_EBIT 1
adval var word ' Create adval to store result
TRISA = %11111111 ' Set PORTA to all input
ADCON1 = %10000010 ' Set PORTA analog and RIGHT justify result
ADCON0 = %11000001 ' Configure and turn on A/D Module
Pause 500 ' Wait .5 second
loop: ADCON0.2 = 1 ' Start Conversion
notdone: Pause 5
If ADCON0.2 = 1 Then notdone ' Wait for low on bit-2 of ADCON0,
conversion finished
adval.highbyte = ADRESH ' Move HIGH byte of result to adval
adval.lowbyte = ADRESL ' Move LOW byte of result to adval
Lcdout $fe, 1 ' Clear screen
Lcdout "Value: ", DEC adval ' Display the decimal value
Pause 100 ' Wait .1 second
Goto loop ' Do it forever
End
' PicBasic Pro program to display result of
' 8-bit A/D conversion on LCD
'
' Connect analog input to channel-0 (RA0)
' Define LCD registers and bits
Define LCD_DREG PORTD
Define LCD_DBIT 4
Define LCD_RSREG PORTE
Define LCD_RSBIT 0
Define LCD_EREG PORTE
Define LCD_EBIT 1
adval var byte ' Create adval to store result
TRISA = %11111111 ' Set PORTA to all input
ADCON1 = %00000010 ' Set PORTA analog and LEFT justify result
ADCON0 = %11000001 ' Configure and turn on A/D Module
Pause 500 ' Wait .5 second
loop: ADCON0.2 = 1 ' Start Conversion
notdone: Pause 5
If ADCON0.2 = 1 Then notdone ' Wait for low on bit-2 of ADCON0,
conversion finished
adval = ADRESH ' Move high byte of result to adval
Lcdout $fe, 1 ' Clear screen
Lcdout "Value: ", DEC adval ' Display the decimal value
Pause 100 ' Wait .1 second
Goto loop ' Do it forever
End
' Access PIC16C7x A/D
Include "modedefs.bas" ' Include serial modes
SO con 0 ' Define serial output pin
ADCON1 = 0 ' Set PortA 0-3 to analog inputs
ADCON0 = $41 ' Set A/D to Fosc/8, Channel 0, On
Pause 1 ' Wait for channel to setup
loop: ADCON0 = $45 ' Start conversion
Pause 1 ' Wait for conversion
Serout SO,N2400,[#ADRES,10] ' Send variable to serial out
Goto loop
' PicBasic Pro program to display result of
' 10-bit A/D conversion on LCD
'
' Connect analog input to channel-0 (RA0)
' Define LCD registers and bits
Define LCD_DREG PORTD
Define LCD_DBIT 4
Define LCD_RSREG PORTE
Define LCD_RSBIT 0
Define LCD_EREG PORTE
Define LCD_EBIT 1
' Define ADCIN parameters
Define ADC_BITS 10 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uS
adval var word ' Create adval to store result
TRISA = %11111111 ' Set PORTA to all input
ADCON1 = %10000010 ' Set PORTA analog and right justify result
Low PORTE.2 ' LCD R/W line low (W)
Pause 500 ' Wait .5 second
loop: ADCIN 0, adval ' Read channel 0 to adval
Lcdout $fe, 1 ' Clear LCD
Lcdout "Value: ", DEC adval ' Display the decimal value
Pause 100 ' Wait .1 second
Goto loop ' Do it forever
End
' PicBasic Pro program to display result of
' 8-bit A/D conversion on LCD
'
' Connect analog input to channel-0 (RA0)
' Define LCD registers and bits
Define LCD_DREG PORTD
Define LCD_DBIT 4
Define LCD_RSREG PORTE
Define LCD_RSBIT 0
Define LCD_EREG PORTE
Define LCD_EBIT 1
' Define ADCIN parameters
Define ADC_BITS 8 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uS
adval var byte ' Create adval to store result
TRISA = %11111111 ' Set PORTA to all input
ADCON1 = %00000010 ' Set PORTA analog
Low PORTE.2 ' LCD R/W line low (W)
Pause 500 ' Wait .5 second
loop: ADCIN 0, adval ' Read channel 0 to adval
Lcdout $fe, 1 ' Clear LCD
Lcdout "Value: ", DEC adval ' Display the decimal value
Pause 100 ' Wait .1 second
Goto loop ' Do it forever
End
' Interrupts in assembly language
' Turn LED on. Interrupt on PORTB.0 (INTE) turns LED off.
' Program waits .5 seconds and turns LED back on.
led var PORTB.7
wsave var byte $20 system
'wsave1 var byte $a0 system ' Necessary for devices with RAM in bank1
'wsave2 var byte $120 system ' Necessary for devices with RAM in bank2
'wsave3 var byte $1a0 system ' Necessary for devices with RAM in bank3
ssave var byte bank0 system
psave var byte bank0 system
Goto start ' Skip around interrupt handler
' Define interrupt handler
define INTHAND myint
' Assembly language interrupt handler
asm
; Save W, STATUS and PCLATH registers, if not done previously
myint movwf wsave
swapf STATUS, W
clrf STATUS
movwf ssave
movf PCLATH, W
movwf psave
; Insert interrupt code here
; Save and restore FSR and any other registers used
bcf _led ; If interrupt, turn off LED
bcf INTCON, 1 ; Clear interrupt flag
; Restore saved registers
movf psave, W
movwf PCLATH
swapf ssave, W
movwf STATUS
swapf wsave, F
swapf wsave, W
retfie ; Return from interrupt
endasm
start: TRISB = $7f ' LED out, rest in
OPTION_REG = $7f ' Enable PORTB pullups
INTCON = $90 ' Enable INTE interrupt
led = 1 ' Turn LED on
loop: If led = 1 Then loop ' Wait here while LED is still on
' If we get here, LED is off
Pause 500 ' Wait .5 seconds
Goto start ' Start over (turn LED back on)
' Sample program to show assembler interrupt handler for PIC17Cxxx
Define INTHAND8 myint ' For first interrupt vector
psave var byte bank0 system
ssave var byte bank0 system
wsave var byte bank0 system
Goto start ' Skip interrupt handler
asm
myint ; ALUSTA, BSR, PCLATH and W saved already
; Put assembler interrupt code here
movfp psave, PCLATH ; Restore registers
movfp wsave, WREG
movfp ssave, ALUSTA
movfp TBLPTRH, BSR
retfie ; Interrupts over
endasm
start: Goto start ' Basic program goes here
' PicBasic Pro program to demonstrate conversion to and from BCD.
pinout VAR PORTC.6 ' Define serial output pin
pinin VAR PORTC.7 ' Define serial input pin
bcdin VAR BYTE ' BCD data in
bcdout VAR BYTE ' BCD data out
binhold VAR BYTE ' Binary data
addamt CON 12 ' Set value to add
ADCON1 = 7 ' Set ports A and E to digital
loop:
Serout2 pinout,396,["Enter 2-digit decimal number:",10,13] ' Send
prompt to user
Serin2 pinin,396,[HEX2 bcdin] ' Receive 2 characters as BCD data with the
HEX2 modifier
binhold = ((bcdin >> 4) * 10) + (bcdin & $0f) ' Convert bcdin to binary
binhold = binhold + addamt ' Add addamt to binary value
If binhold>99 Then errmess ' Make sure result is less than 100
bcdout = ((binhold / 10) << 4) + (binhold // 10) ' Convert binhold
to BCD
' Send data as BCD with the HEX2 modifier
Serout2 pinout,396,[HEX2 bcdin, " + ", DEC addamt, " = ", HEX2 bcdout,10,13]
Goto loop ' Do it forever
errmess: ' Send error message
Serout2 pinout,396,[10,13,"ERROR: Result greater than 99",10,13,10,13]
Goto loop ' Begin again
End
' Example program from manual to blink an LED connected to PORTB.0 about once a second
loop: High 0 ' Turn on LED connected to PORTB.0
Pause 500 ' Delay for .5 seconds
Low 0 ' Turn off LED connected to PORTB.0
Pause 500 ' Delay for .5 seconds
Goto loop ' Go back to loop and blink LED forever
End
' BUTTON Command
'
' Demonstate BUTTON command. Prints working variable (in this case B0) to
' show how auto-repeat works. Prints "PRESS" when button is pressed or
' repeated.
Include "modedefs.bas" ' Include serial modes
SO con 0 ' Define serial output pin
B con 5 ' Define Button input pin
B0 var byte
B0 = 0 ' Zero Button Working Buffer
loop: Button B,1,10,5,B0,0,notp ' Check Button (Skip if Not Pressed)
Serout SO,N2400,["Press",13,10] ' Indicate Button Pressed
notp: Serout SO,N2400,[#B0,13,10] ' Show Working Variable
Pause 100 ' Visual Pause
Goto loop ' Forever
' EEPROM, READ and WRITE Commands
'
' Demonstate commands for EEPROM. Works on PIC16F(C)84 targets only!!!
' Initialized address 0..5 and 9. Writes 10..63. This leaves addesses
' 6..8 undefined (assuming your programmer doesn't unconditionally
' program all EEPROM locations).
Include "modedefs.bas" ' Include serial modes
SO con 0 ' Define serial output pin
B0 var byte
B1 var byte
B2 var byte
EEPROM ["vwxyz"] ' EEPROM[0..4] = 118..122
EEPROM 9,[100] ' EEPROM[9] = 100
' I2CREAD and I2WRITE Commands
'
' Write to the first 16 locations of internal I2C EEPROM
' Read first 16 locations back and send to serial out repeatedly
' Note: for PIC12CE67x MCU's
Include "modedefs.bas" ' Include serial modes
Define I2C_INTERNAL 1
ADCON1 = 7 ' Set ADC pins to digital operation
SO con 1 ' Define serial output pin
DPIN var GPIO.6 ' Data line to internal EEPROM
CPIN var GPIO.7 ' Clock line to internal EEPROM
B0 var byte
B1 var byte
B2 var byte
For B0 = 0 To 15 ' Loop 16 times
I2CWRITE DPIN,CPIN,$A0,B0,[B0] ' Write each location's address to
itself
Pause 10 ' Delay 10ms after each write
Next B0
loop: For B0 = 0 To 15 step 2 ' Loop 8 times
I2CREAD DPIN,CPIN,$A0,B0,[B1,B2] ' Read 2 locations in a row
Serout SO,T2400,[#B1," ",#B2," "] ' Print 2 locations
Next B0
Serout SO,T2400,[10] ' Print linefeed
Goto loop
' I2CREAD and I2WRITE Commands
'
' Write to the first 16 locations of internal I2C EEPROM
' Read first 16 locations back and send to serial out repeatedly
' Note: for PIC16CE62x MCU's
Include "modedefs.bas" ' Include serial modes
Define I2C_INTERNAL 1
SO con 0 ' Define serial output pin
DPIN var EEINTF.1 ' Data line to internal EEPROM
CPIN var EEINTF.2 ' Clock line to internal EEPROM
B0 var byte
B1 var byte
B2 var byte
For B0 = 0 To 15 ' Loop 16 times
I2CWRITE DPIN,CPIN,$A0,B0,[B0] ' Write each location's address to
itself
Pause 10 ' Delay 10ms after each write
Next B0
loop: For B0 = 0 To 15 step 2 ' Loop 8 times
I2CREAD DPIN,CPIN,$A0,B0,[B1,B2] ' Read 2 locations in a row
Serout SO,N2400,[#B1," ",#B2," "] ' Print 2 locations
Next B0
Serout SO,N2400,[10] ' Print linefeed
Goto loop
' FOR..NEXT Command
'
' Prints series of numbers showing STEP facility.
Include "modedefs.bas" ' Include serial modes
SO con 0 ' Define serial output pin
B0 var byte
B1 var byte
loop: For B0 = 3 To 1 Step -1 ' Countdown step size
For B1 = 0 To 10 Step B0 ' Count to 10 (or so)
Serout SO,N2400,[#B1," "]
Next B1
Serout SO,N2400,[13,10] ' End of line
Pause 100 ' Delay
Next B0
Goto loop
' PicBasic Pro Program to demonstrate hardware PWM.
' Output is a 1Khz signal with duty cycle sweeping
' from 20% to 80% once per second
' Note: PWM output will be on the CCP1 pin. Register
' names are for the PIC16F87x devices.
' PR2 = Timer2 period register, controls PWM period.
' CCPR1L and CCP1CON<5:4>bits control the duty cycle,
' and should be treated as a 10-bit word.
' Fosc = Clock Frequency (4MHz)
' PS = Timer2 Prescale Value (T2CON<1:0>)
' Freq = PWM output frequency
' Duty% = Duty cycle (20% = 0.2)
' formulas:
' PR2=(Fosc/(4*PS*Freq))-1
' CCPR1L:CCP1CON<5:4>=(PR2+1)*4*Duty%
duty VAR WORD ' Duty cycle value (CCPR1L:CCP1CON<5:4>)
TRISC.2 = 0 ' Set PORTC.2 (CCP1) to output
CCP1CON = %00001100 ' Set CCP1 to PWM
T2CON = %00000101 ' Turn on Timer2, Prescale=4
' Use formula to determine PR2 value for a 1KHz signal,
' 4MHz clock, and prescale=16. (4E6/(4*4*1E3))-1=249
PR2 = 249 ' Set PR2 to get 1KHz out
' Use formula to determine CCPR1L:CCP1CON<5:4> value for
' ends of range 20% to 80%. (249+1)*4*0.2=200 (20% value)
' (249+1)*4*0.8=800 (80% value)
duty = 200 ' Set duty cycle to 20%
loop: CCP1CON.4 = duty.0 ' Store duty to registers as
CCP1CON.5 = duty.1 ' a 10-bit word
CCPR1L = DUTY >> 2
duty = duty + 10 ' Increase duty cycle
' Since the total sweep of duty is 600 (800-200) and
' we are adding 10 for each loop, that results in 60
' steps min to max. 1 second divided by 60 = 16.67mS
Pause 17 ' Pause 1/60 of second
If (duty < 800) Then loop ' Do it again unless 80% duty cycle
duty = 200 ' Reset to 20% duty cycle
Goto loop ' Do it forever
' PicBasic Pro program to send and receive from the hardware serial port
char var byte ' Storage for serial character
start: Hserout ["Hello World", 13, 10] ' Send text followed by carriage return and
linefeed
loop: Hserin 10000, start, [char] ' Get a char from serial port
Hserout [char] ' Send char out serial port
Goto loop ' Do it all over again
End
' I2CREAD and I2WRITE Commands
'
' Write to the first 16 locations of an external serial EEPROM
' Read first 16 locations back and send to serial out repeatedly
' Note: for EEPROMS with byte-sized address
Include "modedefs.bas" ' Include serial modes
SO con 0 ' Define serial output pin
DPIN var PORTA.0 ' I2C data pin
CPIN var PORTA.1 ' I2C clock pin
B0 var byte
B1 var byte
B2 var byte
For B0 = 0 To 15 ' Loop 16 times
I2CWRITE DPIN,CPIN,$A0,B0,[B0] ' Write each location's address to
itself
Pause 10 ' Delay 10ms after each write
Next B0
loop: For B0 = 0 To 15 step 2 ' Loop 8 times
I2CREAD DPIN,CPIN,$A0,B0,[B1,B2] ' Read 2 locations in a row
Serout SO,N2400,[#B1," ",#B2," "] ' Print 2 locations
Next B0
Serout SO,N2400,[10] ' Print linefeed
Goto loop
' PicBasic program to demonstrate operation of an LCD in 4-bit mode
'
' LCD should be connected as follows:
' LCD PIC
' 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
Pause 500 ' Wait for LCD to startup
loop: Lcdout $fe, 1 ' Clear LCD screen
Lcdout "Hello" ' Display Hello
Pause 500 ' Wait .5 second
Lcdout $fe, 1 ' Clear LCD screen
Lcdout "World"
Pause 500 ' Wait .5 second
Goto loop ' Do it forever
' Display Truth Table for Binary Logical Operators
Include "modedefs.bas" ' Include serial modes
SO con 0 ' Define serial output pin
B0 var byte
B1 var byte
B2 var byte
loop: Serout SO,N2400,[" & &/ | |/ ^ ^/",10,13]
For B0 = 0 To 1
For B1 = 0 To 1
Serout SO,N2400,[#B1," ",#B0," : "]
B2 = B1 & B0 : Gosub disp
B2 = B1 &/ B0 : Gosub disp
B2 = B1 | B0 : Gosub disp
B2 = B1 |/ B0 : Gosub disp
B2 = B1 ^ B0 : Gosub disp
B2 = B1 ^/ B0 : Gosub disp
Serout SO,N2400,[10,13]
Next B1
Next B0
Serout SO,N2400,[10,13]
Goto loop
disp: B2 = B2 & 1
Serout SO,N2400,[#B2," "]
Return
' LOOKDOWN Command
'
' Convert ASCII Hexadecimal characters to numeric equivalents.
Include "modedefs.bas" ' Include serial modes
SO con 0 ' Define serial out pin
SI con 1 ' Define serial in pin
B0 var byte
B1 var byte
loop: Serin SI,N2400,B0 ' B0 = Next Character
B1 = 255 ' B1 = Convert to Hex (255 if Fail)
Lookdown B0,["0123456789ABCDEF"],B1
If B1 = 255 Then loop ' Skip Non-Hex Characters
Serout SO,N2400,[#B1,13,10] ' Output Decimal Equivalent
Goto loop ' Forever
' LOOKUP & RANDOM Commands
'
' Convert numeric value to ASCII hexadecimal equivalents.
Include "bs1defs.bas" ' Include BS1 variable and serial
definitions
SO con 0 ' Define serial out
loop: W6 = W0 : Gosub HexW ' Print W0
Serout SO,N2400,[13,10] ' Print end of line
Random W0 ' Randomize W0
Goto loop ' Do this forever!!!
' Send W6 as XXXX (Uses W5)
HexW: Gosub HexB13 ' Print MSB
B13 = B12 ' Print LSB
HexB13: B11 = B13 / 16 ' Print MSN
Gosub HexB11
B11 = B13 & 15
HexB11: Lookup B11,["0123456789ABCDEF"],B10 ' B10 = HEX$(B11)
Serout SO,N2400,[B10]
Return
' MAX/MIN Operators
'
' Use MAX and MIN operators to Bound [0..9] to [3..7]
Include "modedefs.bas" ' Include serial modes
SO con 0 ' Define serial out pins
B0 var byte
B1 var byte
loop: For B0 = 0 To 9 ' B0 = 0..9
B1 = B0 Max 3 Min 7 ' B1 = B0 Bounded to [3..7]
' Display Results
Serout 0,N2400,[#B0," ",#B1,10,13]
Next B0
Serout 0,N2400,[10] ' Line Break
Goto loop ' Forever
' On Interrupt - Interrupts in BASIC
' Turn LED on. Interrupt on PORTB.0 (INTE) turns LED off.
' Program waits .5 seconds and turns LED back on.
led var PORTB.7
OPTION_REG = $7f ' Enable PORTB pullups
On Interrupt Goto myint ' Define interrupt handler
INTCON = $90 ' Enable INTE interrupt
loop: High led ' Turn LED on
Goto loop ' Do it forever
' Interrupt handler
Disable ' No interrupts past this point
myint: Low led ' If we get here, turn LED off
Pause 500 ' Wait .5 seconds
INTCON.1 = 0 ' Clear interrupt flag
Resume ' Return to main program
Enable
' PULSIN Command
'
' Serial Pulse Width Meter
Include "modedefs.bas" ' Include serial modes
SO con 0 ' Define serial out pin
FI con 4 ' Define frequency input pin
W0 var word
loop: Pulsin FI,0,W0 ' Measure pulse (in 10 uSec)
If W0 = 0 Then disp ' If non-zero, display
Serout SO,N2400,[#W0]
disp: Serout SO,N2400,["0 uSec",13,10] ' Display trailer
Goto loop ' Forever
' PULSOUT Command
'
' Variable Pulse Generator. Two buttons adjust from off to 10mSec in
' 10 uSec steps.
Include "bs1defs.bas" ' Include BS1 variables and serial modes
DN con 5 ' Define frequency down button
UP con 4 ' Define frequency up button
PO con 3 ' Define pulse output pin
W0 = 0
Low PO ' Initialize output polarity
inc: If (Pin4 = 1) Or (W0 = 1000) Then decr ' Increment freq on button
W0 = W0 + 1
decr: If (Pin5 = 1) Or (W0 = 0) Then pulse ' Decrement freq on button
W0 = W0 - 1
pulse: Pulsout 3,W0 ' Generate pulse
Pause 10 ' Intra-pulse delay
Goto inc ' Forever
' PicBasic Pro Code that demonstrates the use of modifiers
' with the Serin2 and Serout2 commands.
testword VAR WORD ' Define word variable
testbyte VAR BYTE ' Define byte variable
test8 VAR BYTE[8] ' Define array variable with 8 locations
pinin VAR PORTC.7 ' Define pinin as PORTC.7
pinout VAR PORTC.6 ' Define pinout as PORTC.6
' For these examples, assume that the following string
' is being received continuously at 2400 baud on PORTC.7:
' "X-1011Y-546Z-F7ZZ-0001"
loop:
Serin2 pinin,396,[WAIT("X"),testbyte]
Serout2 pinout,396,["1: ",testbyte,13,10] ' 1: -
' Waits for ascii "X", then reads the next byte without a modifier.
' Numeric value of testbyte is 45, the ascii code for "-".
Serin2 pinin,396,[WAIT("Y"),DEC testword]
Serout2 pinout,396,["2: ",SDEC testword,13,10] ' 2: -546
' Waits for ascii "Y", then looks for an ascii string that could represent
' a decimal number. It finds the string "-546", which it converts to a
' signed integer. Since the value of testword is signed, we use SDEC to
' send it.
Serin2 pinin,396,[WAIT("Y-"),DEC testword]
Serout2 pinout,396,["3: ",DEC testword,13,10] ' 3: 546
' Same as the above example, but we change the WAIT parameter so that the
' ascii "-" is ignored. The DEC modifier then finds the first decimal
' sting, "546".
Serin2 pinin,396,[WAIT("X"),BIN testbyte]
Serout2 pinout,396,["4: ",ISBIN testbyte,13,10] ' 4: %-1011
' Waits for ascii "X", then looks for an ascii string that could represent
' a binary number. It finds the string "-1011", which it converts to a
' signed integer. Since the value of testbyte is signed, we use the S
' prefix to send it. The I prefix inserts the ascii "%" to denote binary.
Serin2 pinin,396,[WAIT("X"),BIN testbyte]
Serout2 pinout,396,["5: ",BIN testbyte,13,10] ' 5: 11110101
' Same as example 4, but shows what is sent when the I and S prefixes
' are omitted from the SerOut2.
Serin2 pinin,396,[WAIT("ZZ-"),BIN testbyte]
Serout2 pinout,396,["6: ",IBIN4 testbyte,13,10] ' 6: %0001
' Waits for ascii string "ZZ-", then looks for a string that could represent
' a binary number. It finds "0001", which it converts to a signed integer.
' We use the IBIN4 modifier, which inserts the "%" denoting binary, and
' sends 4 digits. With only IBIN, the result is: 6: %1
Serin2 pinin,396,[WAIT("X-"),HEX testbyte]
Serout2 pinout,396,["7: ",IHEX testbyte,13,10] ' 7: $11
' Waits for ascii string "X-", then looks for a string that could represent
' a hexadecimal number. It finds "1011" which it tries to store in the
' testbyte variable. Since the value of $1011 is too large for a single
' byte, it only stores the least significant 8 bits.
Serin2 pinin,396,[WAIT("X-"),HEX2 testbyte]
Serout2 pinout,396,["8: ",IHEX testbyte,13,10] ' 8: $10
' Same as example 7, but we have used HEX2 in the SerIn2 statement.
' This causes the compiler to collect the string "10" and store it
' in testbyte.
Serin2 pinin,396,[WAIT("Z-"),HEX testbyte]
Serout2 pinout,396,["9: ",IHEX testbyte,13,10] ' 9: $F7
' Since this example waits for the string "Z-", it ignores the "1011"
' string. The first string it finds that could be hex data is "F7".
Serin2 pinin,396,[WAIT("Y-"),DEC testword,testbyte]
Serout2 pinout,396,["10: ",DEC testword,",",testbyte,13,10] ' 10:
546,-
' Waits for ascii string "Y-", then looks for a string that could represent a
' decimal number. It finds "546" and stores the value in testword. Since we
' have a second item after testword, it stores the next charater "-" in
' testbyte.
Serin2 pinin,396,[WAIT("Y-"),DEC testword,WAIT("-"),HEX testbyte]
Serout2 pinout,396,["11: ",IDEC testword,",",IHEX testbyte,13,10]
' 11: #546,$F7
' Waits for the string "Y-", then collects a string that could represent
' a decimal number ("546"). It then waits again for the string "-". After
' that it collects the next string that looks like hex data, "F7". We use
' the I prefix to send both numbers to differentiate between decimal and hex.
Serin2 pinin,396,[WAIT("Y-"),DEC2 testword,testbyte]
Serout2 pinout,396,["12: ",DEC testword,",",testbyte,13,10] ' 12:
54,6
' In this example, we use the DEC2 modifier to collect the only 2 decimal
' digits after the wait-string "Y-" is received. This results in "54" being
' stored to testword. The next character is "6", which is stored as ascii
' in testbyte.
Serin2 pinin,396,[WAIT("Y"),SKIP 2,DEC2 testword,testbyte]
Serout2 pinout,396,["13: ",DEC testword,",",testbyte,13,10] ' 13:
46,Z
' Waits for the string "Y", then skips the next 2 characters "-5". It then
' collects a 2-digit decimal number ("46") and stores it to testword. The
' next byte received is "Z", which is stored as ascii to testbyte.
Serin2 pinin,396,[WAIT("F"), STR test8¥8]
Serout2 pinout,396,["14: ",STR test8¥8,13,10] ' 15: 7ZZ-0001
' Waits for the string "F", then collects the next 8 characters. These are
' stored as ascii in 8 locations of the array variable test8. The SerOut2
' statement uses the same modifier to send all 8 locations of the array
' variable as an ascii string.
Serin2 pinin,396,[WAIT("Z",45), STR test8¥8¥"0"]
Serout2 pinout,396,["15: ",STR test8¥8,13,10] ' 16: F7ZZ-
' This example demonstrates how you can put multiple characters in the
' WAIT. It waits for the string "Z-", since the ascii code for "-" is
' 45. The STR item is the same as above except we have added the stop
' character "0". When it encounters "0" at the sixth character, it
' replaces it and fills the rest of the test8 array with null characters.
Pause 2000
Goto loop
' SERIN & SEROUT Commands
'
' Upper case serial filter.
Include "modedefs.bas" ' Include serial modes
SO con 0 ' Define serial out pin
SI con 1 ' Define serial in pin
B0 var byte
loop: Serin SI,N2400,B0 ' B0 = input character
If (B0 < "a") or (B0 > "z") Then print ' If lower case, convert to upper
B0 = B0 - $20
print: Serout SO,N2400,[B0] ' Send character
Goto loop ' Forever
' SERIN Command w/ Qualifiers
'
' "Crude" serial filter for C++ Style Comments
Include "modedefs.bas" ' Include serial modes
SO con 0 ' Define serial out pin
SI con 1 ' Define serial in pin
B0 var byte
wait1: Serin SI,N2400,["//"],B0 ' B0 = first char on comment
loop: Serout SO,N2400,[B0] ' Print character
If B0 = 13 Then wait1 ' Continue til CR
Serin SI,N2400,B0 ' Get next character
Goto loop ' Forever
' SHIFTIN and SHIFTOUT Commands
Include "modedefs.bas" ' Include shift modes
D1PIN var PORTA.0 ' Shift data pin 1
C1PIN var PORTA.1 ' Shift clock pin 1
D2PIN var PORTB.0 ' Shift data pin 2
C2PIN var PORTB.1 ' Shift clock pin 2
bvar var byte
wvar var word
' Shift in 10 bits of data
Shiftin D1PIN, C1PIN, MSBPRE, [wvar¥10]
bvar = wvar
' Shift out 8 bits of data onto other pins
Shiftout D2PIN, C2PIN, MSBFIRST,[bvar]
End
' SLEEP Command
'
' Slowly Blink LED Using Low Power Mode Delay
LED con 2 ' LED Pin
loop: Toggle LED ' Toggle LED
Sleep 10 ' Sleep for 10 Seconds (or so)
Goto loop ' Forever
' SOUND Command
'
' Make random computer-like noises. More refinement might make sound effects
' realistic enough to convince your boss you're working when you're really
' just playing Doom!!!
Include "bs1defs.bas" ' Include BS1 variables
SND con 0 ' Define speaker pin
loop: Random W0 ' Randomize W0
B2 = (B0 & 31) + 64 ' Generate notes [64..95]
If B2 >= 68 Then beep ' Make [64..68] silence
B2 = 0
beep: Sound SND,[B2,4] ' Generate sound
Goto loop ' Forever
' Read and write hardware USART
B1 var byte
' Initialize USART
TRISC = %10111111 ' Set TX (PortC.6) to out, rest in
SPBRG = 25 ' Set baud rate to 2400
RCSTA = %10010000 ' Enable serial port and continuous receive
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
If PIR1.5 = 1 Then ' If receive flag then...
B1 = RCREG ' ...get received character to B1
Endif
ciret: Return ' Go back to caller
' Subroutine to send a character to USART transmitter
charout: If PIR1.4 = 0 Then charout ' Wait for transmit register empty
TXREG = B1 ' Send character to transmit register
Return ' Go back to caller