Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

RS232 PC Communication

Created Date:

1 Oct 1999

Last Updated / Completion Date:

Status:

Complete
PIC16 Assembly
;
;   Title:   Asynchronous Serial Communication (* = See Datasheet)
;   Author:  Russell Carter
;   Date:    10/99
; 
   List    P=16F84,F=inhx8m
;
;===========Register Files=======================================================
;
indf        equ   00
tmr0        equ   01
pcl         equ   02
status      equ   03
fsr         equ   04
porta       equ   05
portb       equ   06
eedata      equ   08
eeadr       equ   09
pclath      equ   0A
intcon      equ   0B
option_reg  equ   81
trisa       equ   85   ; A "1" sets the relevant port bit as input
trisb       equ   86   ; A "0" sets the relevant port bit as output
eecon1      equ   88
eecon2      equ   89
;
;===========Status Bits==========================================================
;
irp         equ   07   ; Not Used
rp1         equ   06   ; Not Used
rp0         equ   05   ; 0 = Bank0, 1 = Bank1
not_to      equ   04   ; WDT Time-out Bit*
not_pd      equ   03   ; Power Down Bit*
z           equ   02   ; 1 = Result is 0
dc          equ   01   ; Digit Carry/Not-Borrow*
c           equ   00   ; 1 = Carry-out from MSB
;
;===========Intcon Bits==========================================================
;
gie         equ   07   ; 0 = Disables all interrupts
eeie        equ   06   ; 1 = Enables EE Write complete int
t0ie        equ   05   ; 1 = Enables TMR0 int
inte        equ   04   ; 1 = Enables RB0/INT int
rbie        equ   03   ; 1 = Enables RB port change int
t0if        equ   02   ; 1 = TMR0 has overflowed
intf        equ   01   ; 1 = RB0/INT int occured
rbif        equ   00   ; 1 = An RB port pin changed state
;
;===========Option_Reg Bits======================================================
;
not_rbpu    equ   07   ; 0 = PortB Pull-ups enabled
intedg      equ   06   ; 1 = Int. on rising edge
tocs        equ   05   ; TMR0 Clk 1 = External: T0CKI
t0se        equ   04   ; 1 = Inc on falling edge of T0CKI
psa         equ   03   ; 1 = Prescaler on WDT, 0 = Psa on TMR0
ps2         equ   02   ; Prescaler rate select bits*
ps1         equ   01   ; Prescaler rate select bits*
ps0         equ   00   ; Prescaler rate select bits*
;
;===========Eecon1 Bits==========================================================
;
eeif        equ   04   ; 1 = Write op. complete (must be cleared)
wrerr       equ   03   ; 1 = Write op. terminated prematurely
wren        equ   02   ; 1 = Allows write cycles
wr          equ   01   ; 1 = Initiate write (clears automatically)
rd          equ   00   ; 1 = Initiate EEPROM read (clears auto.)
;
;===========Custom Defined Registers (68)========================================
;
;   Usable range: 0Ch to 4Fh
;
t_status    equ   0C   ; Temp storage of Status reg
tx          equ   0D   ; Transmit Reg
rx          equ   0E   ; Recieve Reg
count       equ   0F   ; Bit Counter
flags       equ   10   ; Flags reg
rx_buffer   equ   11   ; Recieve buffers
del         equ   17   ; Delay reg
;
;===========Custom Bits==========================================================
;
rip         equ   01   ; Recieve in progress bit (Flags)
tip         equ   02   ; Transmit in progress bit (Flags)
rxc         equ   03   ; Recieve complete bit (Flags)
txc         equ   04   ; Transmit complete (Flags)
fail        equ   05   ; Recieve failed bit (Flags)
r           equ   00   ; Recieve bit (PortA)
t           equ   01   ; Transmit bit (PortA)
cts         equ   02   ; Clear to send bit

;===========Information==========================================================
;
;   Data format:   Binary   b'MSB...LSB', (Bit7...Bit0)
;   Hex   0x00
;   Decimal   .000
;
;===========Beginning of Program=================================================
;
;   Configuration:   Half Duplex
;   1 Stop bit
;   No Parity
;
   org   0
   goto       init                ; Start Vector
   nop 
   nop 
   nop 
   goto       service_int         ; Interrupt Vector
init    
   clrf       flags
   clrf       rx
   clrf       tx
   clrf       intcon
   clrf       porta
   clrf       portb
   bsf        port    a,t         ; Puts the Transmit Line in the idle state
   bsf        intc    on,t0ie
   bsf        stat    us,rp0      ; Ports are configured as inputs at power-up
   bcf        opti    on_reg,tocs ; TMR0 = Internal clock
   movlw      b'00000001'
   movwf      trisa
   movlw      b'00000000'
   movwf      trisb
   bcf        stat    us,rp0
   clrf       tmr0
main    
   call       rx_enable           ; Main program: wait to recieve byte, output 
check1                            ; to LED's and transmit byte back to PC.
   btfss      flags,rxc
   goto       check1
   bcf        flag    s,rxc
   call       rx_disable
   movf       rx_buffer,0
   movwf      portb
   movwf      tx
   clrf       rx_buffer
   call       init_transmit
check2  
   btfss      flags,txc
   goto       check2
   bcf        intc    on,gie      ; Always disable int after a txc flag!
   bcf        flag    s,txc
   clrf       tx
   goto       main   
service_int                       ; Test for conditions to determine
   movlw      .210                ; program banch
   movwf      tmr0
    btfsc     flags,rip
   goto       recieve
   btfsc      flags,tip
   goto       transmit
   btfss      porta,r
   goto       init_recieve
   bcf        intcon,t0if
   retfie
rx_enable
   bsf        intcon,gie          ; Init rx & set CTS line
   bsf        porta,cts
   movlw      .217
   movwf      tmr0
   return
rx_disable
   bcf        porta,cts           ; Disable rx & clear CTS line
   bcf        intcon,gie
   return
init_recieve
   bsf        flags,rip
   movlw      .144
   movwf      tmr0
   movlw      .9
   movwf      count
   bcf        intcon,t0if
   retfie
init_transmit
   movlw      .10
   movwf      count
   bsf        status,c
   bsf        flags,tip
   bcf        porta,t             ; Beginning of start bit
   movlw      .170
   movwf      tmr0
   bcf        intcon,t0if
   bsf        intcon,gie
   return
recieve
   decf       count
   btfsc      status,z
   goto       end_rx
   btfsc      porta,r
   bsf        rx,0    
   rrf        rx                ; Addlw, Addwf, Sublw, Subwf affect the carry
   movlw      .170
   movwf      tmr0
   bcf        intcon,t0if
   retfie
transmit
   decf       count
   btfsc      status,z
   goto       end_tx
   btfss      tx,0
   bcf        porta,t
   btfsc      tx,0
   bsf        porta,t
   rrf        tx
   movlw      .173
   movwf      tmr0
   bcf        intcon,t0if
   retfie
end_rx
   rrf        rx
   btfss      porta,r
   bsf        flags,fail
   movf       rx,0
   btfss      flags,fail
   movwf      rx_buffer
   bsf        flags,rxc
   bcf        flags,rip
   clrf       rx
   movlw      .223
   movwf      tmr0
   movlw      .9
   movwf      count
   bcf        status,c
   bcf        intcon,t0if
   retfie
end_tx
   bsf        porta,t
   bsf        flags,txc
   bcf        flags,tip
   bcf        status,c
   bcf        intcon,t0if
   retfie
; 
   end
;_______________________________________________________________________________
Expand
Visual Basic 6.0
Private Sub bit_Click(Index As Integer)
    Dim Byte1
    Dim Decml
    Dim Charact
    For N = 7 To 0 Step -1
        Byte1 = Byte1 & bit(N).Value
    Next N
    Decml = Bin2Dec(Byte1)
    Charact = Chr(Decml)
    Com.Output = Charact
End Sub

Private Sub cmdAuto_Click()
    Dim N
    For N = 1 To 255
        txtCount.Text = N
        txt1.Text = txtCount.Text
        txt2.Text = Chr(txt1.Text)
        Com.Output = txt2.Text
        txt3.Text = (Dec2Bin(txt1.Text))
        Duration = Timer + 0.01
        Do Until Timer > Duration
            Wait = DoEvents()
        Loop
        txt5.Text = Com.Input
        If txt5.Text > "" Then txt4.Text = Asc(txt5.Text)
        If txt5.Text > "" Then txt6.Text = (Dec2Bin(txt4.Text))
        Duration = Timer + 0.01
        Do Until Timer > Duration
            Wait = DoEvents()
        Loop
        If txtCount.Text = 0 Then Exit Sub
    Next N
End Sub

Private Sub cmdDown_Click()
    Dim N
    txt1.Text = txtCount.Text
    txt2.Text = Chr(txt1.Text)
    Com.Output = txt2.Text
    txt3.Text = (Dec2Bin(txt1.Text))
    Duration = Timer + 0.05
    Do Until Timer > Duration
        Wait = DoEvents()
    Loop
    txt5.Text = Com.Input
    If txt5.Text > "" Then txt4.Text = Asc(txt5.Text)
    If txt5.Text > "" Then txt6.Text = (Dec2Bin(txt4.Text))
    If txtCount.Text > 0 Then
        N = txtCount.Text
        N = N - 1
        txtCount.Text = N
    Else
        Exit Sub
    End If
End Sub

Private Sub cmdRes_Click()
    txtCount.Text = 0
    txt1.Text = ""
    txt2.Text = ""
    txt3.Text = ""
    txt4.Text = ""
    txt5.Text = ""
    txt6.Text = ""
End Sub


Private Sub cmdUp_Click()
    Dim N
    txt1.Text = txtCount.Text
    txt2.Text = Chr(txt1.Text)
    Com.Output = txt2.Text
    txt3.Text = (Dec2Bin(txt1.Text))
    Duration = Timer + 0.05
    Do Until Timer > Duration
        Wait = DoEvents()
    Loop
    txt5.Text = Com.Input
    If txt5.Text > "" Then txt4.Text = Asc(txt5.Text)
    If txt5.Text > "" Then txt6.Text = (Dec2Bin(txt4.Text))
    If txtCount.Text < 255 Then
        N = txtCount.Text
        N = N + 1
        txtCount.Text = N
    Else
        Exit Sub
    End If
End Sub

Private Sub Command1_Click()
    Form2.Show
    Form1.Hide
End Sub

Private Sub Form_Load()
    Com.PortOpen = True
    statBar.SimpleText = "Com Port: " & Com.CommPort
End Sub

Private Sub Option1_Click()
    Com.PortOpen = False
    Com.CommPort = 1
    Com.PortOpen = True
    statBar.SimpleText = "Com Port: " & Com.CommPort
End Sub

Private Sub Option2_Click()
    Com.PortOpen = False
    Com.CommPort = 2
    Com.PortOpen = True
    statBar.SimpleText = "Com Port: " & Com.CommPort
End Sub
Expand

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *