Myke's Home Page
|
"3RS" ExperimentThe last experiment uses the ability of the transistor RS-232 interface (presented in SimpRS) to "echo" the serial data stream back to the transmitter as well as modify it to indicate that the device is active. To implement this experiment, I used the PICmicro® MCU code (which can be accessed from the CD-ROM by clicking Here):
title "3RS - Simple 3-Wire Communication Protocol"
;
; This Application Implements a simple Communication
; protocol. The Link is assumed to be down unless the
; the character 0x0FF is received. When this character
; is received, then the next character (which is also
; 0x0FF), is received, the PICmicro, Changes bits 4
; through 6 from "1" to "0" and sends it back to the
; "host". The "RSReceive" and "RSTransmit" were taken
; from "SimpRS".
;
;
; Hardware Notes:
; PIC16F84 Running at 4 MHz
; _MCLR is Pulled Up
; PORTB.3 is the Transmit Output
; PORTB.4 is the RS-232 Input
; PORTA.2 is an LED indicating the Link is Up
;
; Myke Predko
; 99.12.30
;
LIST R=DEC
ifdef __16F84
INCLUDE "p16f84.inc"
else
ifdef __16F877
INCLUDE "p16f877.inc"
endif
; Register Usage
CBLOCK 0x020 ; Start Registers at End of the Values
Byte, Count ; Variables for RS-232
Dlay:2 ; Dlay Count
OnCount, OffCount ; Reset On/Off Counts
ENDC
#define TX PORTB, 3
#define RX PORTB, 4
#define LED PORTA, 2
PAGE
ifdef __16F84
__CONFIG _CP_OFF & _WDT_OFF & _XT_OSC & _PWRTE_ON
else
__CONFIG _CP_OFF & _WDT_OFF & _XT_OSC & _PWRTE_ON & _DEBUG_OFF & _LVP_OFF & _BODEN_OFF
endif
; Mainline of SimpRS
org 0
nop
bsf LED ; LED is "Off"
bsf TX ; Start Sending a "1"
bsf STATUS, RP0
bcf LED ; Enable the LED Output
bcf TX ; Enable TX for Output
bcf STATUS, RP0
Loop
clrf Dlay ; Put in a 1/2 Second Delay
clrf Dlay + 1
btfsc RX ; Wait for a Start Bit
goto HaveBit ; If Line High, Have the Start Bit
goto $ + 1
goto $ + 1
decfsz Dlay, f ; Loop 64K Times
goto $ - 5
decfsz Dlay + 1, f
goto $ - 7
bsf LED ; No Start Bit, Start All Over
goto Loop
HaveBit
call HalfBitDlay ; Wait 1/2 a Bit
btfss RX ; Make Sure Bit is Still Low
goto Loop
movlw 8
movwf Count
SynchRXLoop ; Loop Here to Read in the Byte
call BitDlay ; Wait a Full Byte
bcf STATUS, C ; Set Carry Accordingly
btfss RX ; Bit High or Low?
bsf STATUS, C
rrf Byte, f ; Shift in the Byte
goto $ + 1 ; Make 11 Cycles in Loop
goto $ + 1
decfsz Count, f
goto SynchRXLoop
call BitDlay ; Make Sure there is a "Stop" Bit
bsf LED ; Turn Off LED Just in Case
btfsc RX
goto Loop ; Not High, Then No Byte - Start Over
movf Byte, w
xorlw 0x0FF ; Is the Value Received 0x0FF?
btfss STATUS, Z ; No, LED off, Keep Waiting
goto Loop
bcf LED ; Indicate the Link is Up
clrf Dlay ; Put in a 1/2 Second Delay
clrf Dlay + 1
btfsc RX ; Wait for a Start Bit
goto Have2Bit ; If Line High, Have the Start Bit
goto $ + 1
goto $ + 1
decfsz Dlay, f ; Loop 64K Times
goto $ - 5
decfsz Dlay + 1, f
goto $ - 7
bsf LED ; No Start Bit, Start All Over
goto Loop
Have2Bit
call HalfBitDlay ; Wait 1/2 a Bit
bsf LED
btfss RX ; Make Sure Bit is Still Low
goto $ - 1
bcf LED
movlw 9
movwf Count
movlw 5 ; Start at Bit 4 Coming Back
movwf OnCount
movlw 3
movwf OffCount
RXLoop ; Loop Here to Read in the Byte
call BitDlay ; Wait a Full Byte
decfsz OnCount, f
goto $ + 6 ; No - Decrement and Continue
decfsz OffCount, f ; Keep Bit Off?
goto $ + 7
bsf TX ; Put it On
bsf OnCount, 7 ; Yes, Make Sure Never Goes Off Again
goto $ + 7
nop ; Not at Turn Off Yet
goto $ + 1
goto $ + 4
bcf TX ; Output a Low Bit
bsf OnCount, 0 ; Make Sure it Happens Again
nop
decfsz Count, f
goto RXLoop
goto Loop
BitDlay ; Delay 833 - 15 Cycles (including
; Call/Return)
movlw 204
addlw 0x0FF ; Take 1 Away from the Loop
btfss STATUS, Z
goto $ - 2
goto $ + 1
return
HalfBitDlay ; Delay (833 - 15) / 2 Cycles
movlw 100
addlw 0x0FF ; Take 1 Away from the Loop
btfss STATUS, Z
goto $ - 2
return
end
The Visual Basic Interface code can be installed on your PC by clicking Here and consists of the source code: ' 3RS Visual Basic ' ' This Application Polls a Serial Port for an Active ' PICmicro Connected to it. ' ' When "Start" is Clicked, 0x0FF, followed by 0x0FF is ' Sent out of the Serial Port. The two Values are ' checked for a Valid Return (which means the Second ' 0x0FF is Scrambled by the PICmicro). ' ' If nothing is returned, then the "No Connect" ' (Label(0)) is Active, if 0x0FF and no change, then ' (Label(1)) is Active, if 0x0FF is returned and ' Mangled, then (Label(2)) is Active. ' ' 99.12.30 ' Myke Predko Dim oldCombo1 As Integer ' Flag of Old Serial Port The source code for this application can be found in the code\3rs\vb subdirectory of the CD-ROM. If you are going to use this code as the basis for your own application, remember to start up "MSComm1" by adding the Communications interface from the Visual Basic "Components". |