Myke's Home Page

Book CD-ROM Home

File Copying/Harddrive Setup

Development Tools

Experiments

Projects

Useful Code Snippets and Macros

Introduction to Electronics

Introduction to Programming

Datasheets

PCBs

Links

McGraw-Hill Professional Publishing

"3RS" Experiment

The 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

Private Sub Combo1_Click() ' Combo1 Box is Clicked on. Can Only Change if "Start" is in ' Command2 If (Command1.Caption = "Start") Then oldCombo1 = Combo1.ListIndex + 1 End If End Sub
Private Sub Command1_Click() ' "Quit" Button If (Command2.Caption = "Stop") Then MSComm1.PortOpen = False End If End End Sub
Private Sub Command2_Click() ' "Start"/"Stop" Button is Active If (Command2.Caption = "Start") Then MSComm1.CommPort = Combo1.ListIndex + 1 On Error GoTo invalidCommPort MSComm1.PortOpen = True Timer1.Enabled = True Command2.Caption = "Stop" Else ' Stop the Action MSComm1.PortOpen = False Timer1.Enabled = False Command2.Caption = "Start" End If Exit Sub invalidCommPort: ' Notify of Invalid Selection MsgBox "Selected CommPort Could NOT be Opened. Try Another", _ vbExclamation, "Invalid CommPort" Exit Sub End Sub
Private Sub Timer1_Timer() ' 1/2 Second "Ping" Timer MSComm1.Output = Chr$(&HFF) If (MSComm1.InBufferCount <> 0) Then temp$ = MSComm1.Input If (temp$ = Chr$(&HFF)) Then If (Label1(2).BackColor <> &HFFFF&) Then Label1(1).BackColor = &HFFFF& Label1(0).BackColor = &HFFFFFF Label1(2).BackColor = &HFFFFFF End If Else ' Data Scrambled Label1(2).BackColor = &HFFFF& Label1(0).BackColor = &HFFFFFF Label1(1).BackColor = &HFFFFFF End If Else ' Nothing Received Label1(0).BackColor = &HFFFF& Label1(1).BackColor = &HFFFFFF Label1(2).BackColor = &HFFFFFF End If End Sub
Private Sub Form_Load() Combo1.AddItem "COM1", 0 Combo1.AddItem "COM2" Combo1.AddItem "COM3" Combo1.ListIndex = 0 Label1(0).BackColor = &HFFFF& Label1(1).BackColor = &HFFFFFF Label1(2).BackColor = &HFFFFFF End Sub

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".

Click Here to look at the last experiment - Debug