home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Acorn User 9
/
AU_CD9.iso
/
Cover_Discs
/
Back_Issues
/
1998_12
/
Regulars
/
RTR
/
SerialIO
< prev
Wrap
Text File
|
1998-09-23
|
5KB
|
141 lines
;Serial I/O User Port
;For an 10 MHz clock
;A0 - Serial out
;A1 - Serial in
;A2 - CTS allows serial input
;A3 - RTS output held until this is low
;A4 - I/O mode 0 = Input mode 1 = Output mode
;Note for 16F84 invert the state of the PWRTE
LIST P=16C84
C EQU 0
PCL EQU 2
Z EQU 2
PORTB EQU 6
PORTA EQU 5
TRISB EQU 86H
TRISA EQU 85H
EECON1 EQU 88H
EEDATA EQU 8
EEADR EQU 9
STATUS EQU 3
RP0 EQU 5
INC EQU 1
cts EQU 2
rts EQU 3
sBuf EQU 0ch ;Serial send buffer
Scount EQU 0dh ;Tempory Counter
Cdown EQU 0eh ;Time delay countdown
rBuf EQU 0fh ;Recieve buffer
;
ORG 0
GOTO Start
;Recieve a serial byte
Rx bcf PORTA,cts ;Allow for incomming data
movlw 8 ;Number of data bits to get
movwf Scount
Sb btfsc PORTA,1 ;Hold until start bit
goto Sb
bsf PORTA,cts ;Stop any more incomming
call hDelay ;Half a baud delay
Rnb bcf STATUS,C ;Clear the carry bit
rrf rBuf,f ;Prepare for next bit
call rDelay ;Full baud delay for recieve
btfsc PORTA,1 ;Test input
bsf rBuf,7 ;Set if needed
decfsz Scount,f ;See if finished
goto Rnb ;Round again if not
call rDelay ;Allow for stop bit to start
return
;Send a serial byte
Send btfsc PORTA,rts ;Only proceed when rts is low
goto Send
movlw 9 ;Number of data bits to send
movwf Scount
bcf PORTA,0 ;Start bit
call cDelay ;Extra delay compensation
Sloop call Delay
btfsc sBuf,0 ;Set the next bit
bsf PORTA,0
btfss sBuf,0
bcf PORTA,0
rrf sBuf,f ;Shift for next time
decfsz Scount,f
goto Sloop ;Round until finished
bsf PORTA,0 ;Stop bit
call cDelay ;Extra delay compensation
call Delay
call Delay
call cDelay ;For good measure
return
cDelay movlw 2 ;Small delay at end of send
movwf Cdown
goto Dloop
hDelay movlw 21 ;half delay for serial recieve 10MHz clock
movwf Cdown ; 19,200 Baud rate
goto Dloop
rDelay movlw 38 ;Delay for serial recieve 10MHz clock
movwf Cdown ; 19,200 Baud rate
goto Dloop
Delay movlw 39 ;Delay for serial send 10MHz clock
movwf Cdown ; 19,200 Baud rate
Dloop decfsz Cdown,f
goto Dloop
return
Start
BSF STATUS,RP0 ;SELECT REGISTER BANK 1
MOVLW 1Ah ;Bits 4,3 & 2 inputs
MOVWF TRISA^80H
BCF STATUS,RP0 ;SELECT REGISTER BANK 0
BTFSC PORTA,4 ;See if in input or output mode
GOTO InLoop
BSF STATUS,RP0 ;SELECT REGISTER BANK 1
MOVLW 0 ;All port B to outputs
MOVWF TRISB^80H
BCF STATUS,RP0 ;SELECT REGISTER BANK 0
BSF PORTA,0 ;Serial at Mark
BSF PORTA,cts ;CTS disabled
outMain
call Rx ;Get byte to output
movf rBuf,w ;Byte to send
movwf PORTB ;Output it
movf PORTB,w ;Get output state
movwf sBuf ;Ready to output it
call Send ;Send it back to computer
goto outMain
InLoop
BSF STATUS,RP0 ;SELECT REGISTER BANK 1
MOVLW 0ffh ;All port B to inputs
MOVWF TRISB^80H
BCF 1,7 ;Enable pull up resistors
BCF STATUS,RP0 ;SELECT REGISTER BANK 0
BSF PORTA,0 ;Serial at Mark
BSF PORTA,cts ;CTS disabled
inMain
call Rx ;Get byte to initiate read
movf PORTB,w ;Get input state
movwf sBuf ;Ready to output it
call Send ;Send it back to computer
goto inMain
end