list p=18F1320
include <p18f1320.inc>
CONFIG OSC=INTIO2,WDT=OFF,MCLRE=ON,LVP=OFF
cblock 0x00
buff2,buff3,char,numr,bitnum,loop1,loop2,temp1,temp2
endc
org 0x0000
goto init
hello db 0x0d,0x0a,"Hello World!",0x0d,0x0a,0x00
pa_val db 0x0d,0x0a,"PortA = ",0x00
pb_val db 0x0d,0x0a,"PortB = ",0x00
string3 db 0x0d,0x0a,"PORTA cleared",0x0d,0x00
string4 db 0x0d,0x0a,"PORTB cleared",0x0d,0x00
string5 db 0x0d,0x0a,"RA1 toggled",0x0d,0x00
string6 db 0x0d,0x0a,"RA4 toggled",0x0d,0x00
string7 db 0x0d,0x0a,"RA6 toggled",0x0d,0x00
string8 db 0x0d,0x0a,"RA7 toggled",0x0d,0x00
string9 db 0x0d,0x0a,"RB0 toggled",0x0d,0x00
stringa db 0x0d,0x0a,"RB2 toggled",0x0d,0x00
stringb db 0x0d,0x0a,"RB3 toggled",0x0d,0x00
stringc db 0x0d,0x0a,"RB5 toggled",0x0d,0x00
stringd db 0x0d,0x0a,"LEDs flashed",0x0d,0x00 ;[b]
stringe db 0x0d,0x0a,"RA3 toggled",0x0d,0x00 ;[/b]
menu db 0x0d,0x0a,0x0d,0x0a,"Junebug PIC18F1320",0x0d,0x0a,0x0d,0x0a
db " A - PORTA Status",0x0d,0x0a
db " B - PORTB Status",0x0d,0x0a
db " C - Clear PORTA bits 1,2,3,4,6,7",0x0d,0x0a
db " D - Clear PORTB bits 0,5",0x0d,0x0a
db " H - Hello World!",0x0d,0x0a
db " L - Flash LEDs",0x0d,0x0a
db "1,2,3,4,6,7 - Toggle PORTA pins ",0x0d,0x0a
db "0,5 - Toggle PORTB pins ",0x0d,0x0a
db "<ENTER> - Display menu",0x0d,0x0a
db "Modded by Jason aka AtomSoft, thx futz!",0x0d,0x0a,0x0d,0x0a,0x00
init bsf OSCCON,IRCF2 ;set to 8MHz clock
bsf OSCCON,IRCF1
bsf OSCCON,IRCF0
setf ADCON1 ;set PortA to all digital
clrf TRISA ;set PortA pins to outputs
clrf LATA ;turn off LEDs
movlw b'00010010' ;set PORTB data directions
movwf TRISB
call rs_init ;init rs232
movlw menu ;send menu
call strsend
;************************************************************
;* main - poll rs232 for menu commands and branch accordingly
loop call rs_recv ;wait for a char
movwf numr ;save as numeral
andlw b'01011111' ;convert to upper case
movwf char ;save as uppercase char
movlw "L" ;is it 'L'?
cpfseq char
goto cont01 ;no, continue
L2 call flash ;yes, flash leds
movlw stringd
call strsend
call showa
goto loop ;go again
cont01 movlw "H" ;is it 'H'?
cpfseq char
goto cont02
H2 movlw hello ;yes, send "Hello World!"
call strsend
goto loop ;go again
cont02 movlw "A" ;is it 'A'?
cpfseq char
goto cont03 ;no, continue
A2 call showa
goto loop ;go again
cont03 movlw "B" ;is it 'B'?
cpfseq char ; [b]
goto cont03a ;no, continue [/b]
B2 call showb
goto loop ; [b]
cont03a movlw "Q" ;is it 'Q'?
cpfseq char
goto cont04 ;no, continue
Q2 call showq
goto loop ;go again [/b]
cont04 movlw "C" ;is it 'C'?
cpfseq char
goto cont05
C2 movlw b'00101101' ;clear PORTA bits 1,4,6,7
andwf LATA
movlw string3
call strsend
call showa ;display PORTA status
goto loop ;go again
cont05 movlw "D" ;is it 'D'?
cpfseq char
goto cont06
D2 movlw b'11010010' ;clear PORTB
andwf LATB
movlw string4
call strsend
call showb ;display PORTB status
goto loop ;go again
cont06 movlw 0x0d ;is it <ENTER>?
cpfseq char
goto cont07 ;no, continue
movlw menu ;yes, display menu again
call strsend
goto loop ;go again
cont07 movlw "0" ;is it '0'?
cpfseq numr
goto cont08 ;no, continue
;movlw b'00000001'
;xorwf PORTB
BTG PORTB, 0
movlw string9
call strsend
call showb
goto loop ;go again
cont08 movlw "2" ;is it '2'?
cpfseq numr
goto cont09 ;no, continue
;movlw b'00000100'
;xorwf PORTA
BTG PORTA, 2
movlw stringa
call strsend
call showa
goto loop ;go again [b]
cont09 movlw "3" ;is it '3'?
cpfseq numr
goto cont10 ;no, continue
CheckA3 MOVLW 0x01
CPFSEQ temp2
GOTO CheckB3
GOTO PORTA3
CheckB3 MOVLW 0x02
CPFSEQ temp2
goto loop ; Tell them to select port Q[A/B]
PORTB3 BTG PORTB, 3
movlw stringb
goto Contin3
PORTA3 BTG PORTA, 3
movlw stringe
Contin3
call strsend
goto loop ;go again [/b]
cont10 movlw "5" ;is it '5'?
cpfseq numr
goto cont11 ;no, continue
;movlw b'00100000'
;xorwf PORTB
BTG PORTB, 5
movlw stringc
call strsend
call showb
goto loop ;go again
cont11 movlw "1" ;is it '1'?
cpfseq numr
goto cont12 ;no, continue
;movlw b'00000010'
;xorwf PORTA
BTG PORTA, 1
movlw string5
call strsend
call showa
goto loop ;go again
cont12 movlw "4" ;is it '4'?
cpfseq numr
goto cont13 ;no, continue
;movlw b'00010000'
;xorwf PORTA
BTG PORTA, 4
movlw string6
call strsend
call showa
goto loop ;go again
cont13 movlw "6" ;is it '6'?
cpfseq numr
goto cont14 ;no, continue
;movlw b'01000000'
;xorwf PORTA
BTG PORTA, 6
movlw string7
call strsend
call showa
goto loop ;go again
cont14 movlw "7" ;is it '7'?
cpfseq numr
goto cont15 ;no, continue
;movlw b'10000000'
;xorwf PORTA
BTG PORTA, 7
movlw string8
call strsend
call showa
cont15 goto loop
;************************ [b]
;* Get Second char
showq call rs_recv ;wait for a char
movwf numr ;save as numeral
andlw b'01011111' ;convert to upper case
movwf char ;save as uppercase char
MyPortA movlw "A" ;is it 'A'?
cpfseq char
goto MyPortB ;no, continue
MOVLW 0x01
MOVWF temp2
return
;A2 call showa
MyPortB
movlw "B" ;is it 'B'?
cpfseq char
goto NoPort ;no, continue
MOVLW 0x02
MOVWF temp2
return
NoPort
MOVLW 0x00
MOVWF temp2
return
; [/b]
;***********************
;* rs_send PORTA status
showa movlw pa_val ;send "PORTA = "
call strsend
movlw 0x08 ;set bitnum counter variable
movwf bitnum
movff PORTA,temp1 ;save PORTA value in temp1
a_loop rlcf temp1 ;rotate bit into carry
bnc a_30 ;is the bit set?
movlw 0x31 ;yes, rs_send $31
call rs_send
goto a_bump
a_30 movlw 0x30 ;rs_send $30
call rs_send
a_bump decfsz bitnum ;decrement counter
goto a_loop ;counter not zero, go again
movlw 0x0d ;rs_send CR
call rs_send
movlw 0x0a ;rs_send NL
call rs_send
return
;***********************
;* rs_send PORTB status
showb movlw pb_val ;send "PORTB = "
call strsend
movlw 0x08 ;set counter
movwf bitnum
movff PORTB,temp1 ;save PORTB value in temp1
b_loop rlcf temp1 ;rotate next bit into position
bnc b_30 ;is bit 7 set?
movlw 0x31 ;yes, rs_send $31
call rs_send
goto b_bump
b_30 movlw 0x30 ;rs_send $30
call rs_send
b_bump decfsz bitnum ;decrement counter
goto b_loop ;counter not zero, go again
movlw 0x0d ;rs_send CR
call rs_send
movlw 0x0a ;rs_send NL
call rs_send
return ;counter=0, done
;**************************************
;* send null-terminated string to rs232
;* pass string pointer in W
strsend clrf TBLPTRH ;set up table pointer
movwf TBLPTRL
strnext tblrd *+ ;read table entry, post-increment
movf TABLAT,W ;get char in W
bz strdone ;is it 0?
call rs_send ;no, send it
goto strnext ;and get the next one
strdone return ;yes it is 0, end of string
;******************************************
;* Wait for a char from RS232 - return in W
rs_recv btfss PIR1,RCIF ;wait for a byte from RS232
goto rs_recv
movf RCREG,W ;return received byte in W
return
;*************************
;* Send char in W to RS232
rs_send movwf TXREG ;send byte in W
sdloop btfss TXSTA,TRMT ;transmission done?
goto sdloop ;no, then wait some more
return ;yes, done
;***************************
;* Bill BlueRoom's LED macro
LED macro x,y ;MACRO LED <PORTA>, <TRISA>
movlw x
movwf LATA ;LATA = x
movlw y
movwf TRISA ;TRISA = y
call delay
endm ;end macro
;************
;* flash leds
flash LED b'00000001',b'10111110' ;LED <PORTA>, <TRISA>
LED2 LED b'01000000',b'10111110' ;LED <PORTA>, <TRISA>
LED3 LED b'01000000',b'00111111' ;LED <PORTA>, <TRISA>
LED4 LED b'10000000',b'00111111' ;LED <PORTA>, <TRISA>
LED5 LED b'10000000',b'01111110' ;LED <PORTA>, <TRISA>
LED6 LED b'00000001',b'01111110' ;LED <PORTA>, <TRISA>
clrf TRISA ;shut off leds
clrf LATA
return
;*******
;* Delay
delay movlw 0x3c
movwf loop1
outer movlw 0xc8
movwf loop2
inner nop
nop
decfsz loop2,F
goto inner
decfsz loop1,F
goto outer
return
;**************************
;* init RS232 at 9600,8,N,1
rs_init:
movlw 0x0c ;0x0c=9600 baud
movwf SPBRG
bcf TXSTA,BRGH ;clear Baud Rate Select Bit (low speed)
bcf BAUDCTL,BRG16 ;select 8-bit baud rate generator
bcf TXSTA,SYNC ;asynchronous mode
bsf RCSTA,SPEN ;enable serial port
bsf RCSTA,CREN ;enable async receive
bsf TXSTA,TXEN ;transmit enable
return
END