home *** CD-ROM | disk | FTP | other *** search
/ Acorn User 9 / AU_CD9.iso / Cover_Discs / Back_Issues / 1998_12 / Regulars / RTR / SerialIO < prev   
Text File  |  1998-09-23  |  5KB  |  141 lines

  1.  ;Serial I/O User Port
  2.  ;For an 10 MHz clock
  3.  ;A0 - Serial out
  4.  ;A1 - Serial in
  5.  ;A2 - CTS allows serial input
  6.  ;A3 - RTS output held until this is low
  7.  ;A4 - I/O mode 0 = Input mode   1 = Output mode
  8.  
  9.  ;Note for 16F84 invert the state of the PWRTE
  10.       LIST P=16C84
  11.        
  12.  
  13.  C       EQU     0
  14.  PCL     EQU     2
  15.  Z       EQU     2
  16.  PORTB   EQU     6
  17.  PORTA   EQU     5
  18.  TRISB   EQU     86H
  19.  TRISA   EQU     85H
  20.  EECON1  EQU     88H
  21.  EEDATA  EQU     8
  22.  EEADR   EQU     9
  23.  STATUS  EQU     3
  24.  RP0     EQU     5
  25.  INC     EQU     1
  26.  cts     EQU     2
  27.  rts     EQU     3
  28.  sBuf    EQU    0ch     ;Serial send buffer
  29.  Scount  EQU    0dh     ;Tempory Counter
  30.  Cdown   EQU    0eh     ;Time delay countdown
  31.  rBuf    EQU    0fh     ;Recieve buffer
  32.  
  33. ;
  34.    ORG 0
  35.         GOTO    Start
  36.         
  37. ;Recieve a serial byte
  38. Rx      bcf     PORTA,cts       ;Allow for incomming data
  39.         movlw   8               ;Number of data bits to get
  40.         movwf   Scount
  41. Sb      btfsc   PORTA,1         ;Hold until start bit
  42.         goto    Sb
  43.         bsf     PORTA,cts       ;Stop any more incomming
  44.         call    hDelay          ;Half a baud delay
  45. Rnb     bcf     STATUS,C        ;Clear the carry bit
  46.         rrf     rBuf,f          ;Prepare for next bit  
  47.         call    rDelay          ;Full baud delay for recieve
  48.         btfsc   PORTA,1         ;Test input
  49.         bsf     rBuf,7          ;Set if needed
  50.         decfsz  Scount,f        ;See if finished
  51.         goto    Rnb             ;Round again if not
  52.         call    rDelay          ;Allow for stop bit to start
  53.         return
  54.         
  55.  
  56. ;Send a serial byte
  57. Send    btfsc   PORTA,rts       ;Only proceed when rts is low
  58.         goto    Send
  59.         movlw   9               ;Number of data bits to send
  60.         movwf   Scount
  61.         bcf     PORTA,0         ;Start bit
  62.         call    cDelay          ;Extra delay compensation
  63. Sloop   call    Delay
  64.         btfsc   sBuf,0          ;Set the next bit
  65.         bsf     PORTA,0
  66.         btfss   sBuf,0
  67.         bcf     PORTA,0
  68.         rrf     sBuf,f          ;Shift for next time
  69.         decfsz  Scount,f
  70.         goto    Sloop           ;Round until finished
  71.         bsf     PORTA,0         ;Stop bit
  72.         call    cDelay          ;Extra delay compensation
  73.         call    Delay
  74.         call    Delay
  75.         call    cDelay          ;For good measure
  76.         return
  77.         
  78. cDelay  movlw   2               ;Small delay at end of send
  79.         movwf   Cdown           
  80.         goto    Dloop
  81.         
  82. hDelay  movlw   21              ;half delay for serial recieve 10MHz clock
  83.         movwf   Cdown           ; 19,200 Baud rate
  84.         goto    Dloop
  85.         
  86. rDelay  movlw   38              ;Delay for serial recieve 10MHz clock
  87.         movwf   Cdown           ; 19,200 Baud rate
  88.         goto    Dloop
  89.         
  90. Delay   movlw   39              ;Delay for serial send 10MHz clock
  91.         movwf   Cdown           ; 19,200 Baud rate
  92. Dloop   decfsz  Cdown,f
  93.         goto    Dloop
  94.         return
  95.         
  96.  
  97. Start
  98.         BSF     STATUS,RP0      ;SELECT REGISTER BANK 1
  99.         MOVLW   1Ah             ;Bits 4,3 & 2 inputs
  100.         MOVWF   TRISA^80H
  101.         BCF     STATUS,RP0      ;SELECT REGISTER BANK 0
  102.         BTFSC   PORTA,4         ;See if in input or output mode
  103.         GOTO    InLoop
  104.         
  105.         BSF     STATUS,RP0      ;SELECT REGISTER BANK 1
  106.         MOVLW   0               ;All port B to outputs
  107.         MOVWF   TRISB^80H
  108.         BCF     STATUS,RP0      ;SELECT REGISTER BANK 0
  109.         
  110.         BSF     PORTA,0         ;Serial at Mark
  111.         BSF     PORTA,cts       ;CTS disabled
  112.           
  113.  
  114. outMain
  115.       call      Rx              ;Get byte to output
  116.       movf      rBuf,w          ;Byte to send
  117.       movwf     PORTB           ;Output it
  118.       movf      PORTB,w         ;Get output state
  119.       movwf     sBuf            ;Ready to output it
  120.       call      Send            ;Send it back to computer
  121.       goto      outMain
  122.  
  123. InLoop               
  124.        BSF     STATUS,RP0      ;SELECT REGISTER BANK 1
  125.        MOVLW   0ffh            ;All port B to inputs
  126.        MOVWF   TRISB^80H
  127.        BCF     1,7             ;Enable pull up resistors
  128.        BCF     STATUS,RP0      ;SELECT REGISTER BANK 0
  129.         
  130.        BSF     PORTA,0         ;Serial at Mark
  131.        BSF     PORTA,cts       ;CTS disabled
  132.           
  133.  
  134. inMain
  135.       call      Rx              ;Get byte to initiate read
  136.       movf      PORTB,w         ;Get input state
  137.       movwf     sBuf            ;Ready to output it
  138.       call      Send            ;Send it back to computer
  139.       goto      inMain
  140.         end                
  141.