Author Topic: Half Duplex Software Serial for ATtiny and PIC16F  (Read 2097 times)

0 Members and 1 Guest are viewing this topic.

Offline bequest333Topic starter

  • Contributor
  • Posts: 16
  • Country: jp
Half Duplex Software Serial for ATtiny and PIC16F
« on: June 06, 2014, 01:06:42 am »
Hi,All

I will introduce A small Software Serial Routine for ATtiny and PIC16F.

8bit,NP,1STOP (Output, but will be 2 stop bits, No problems.)


(ATtiny13A) 1+16+10=27words

.cseg
.equ    xtal    =    9600000   ;Hz
.equ    baud    =    9600      ;bps

.def    rs     =R17            ;bit delay counter
.def    cn     =R18            ;bit shift counter
.def    rxd    =R19            ;data buffer

        #define TX      3               ; TX Data output pin (i.e. 2 = PORTB,2)
        #define RX      4               ; RX Data input pin (i.e. 3 = PORTB,3)

        .org   XXXX
        SBI    DDRB,TX                  ; set TX Port

; ********************************************************************
;
;        RS-232C Send 1byte (16words)
;
;        set rxd and call
;
; ********************************************************************

PUTC:
            SBI     PORTB,TX
            LDI     cn,2*(1+8+1)        ; 10-bit Data
            RJMP    PC+5                ; Start bit
            ROR     rxd                 ; Rotate Right through Carry
            BRCC    PC+2
            SBI     PORTB,TX            ; set TX='1' if Carry='1'
            BRCS    PC+2
            CBI     PORTB,TX            ; set TX='0' if Carry='0'
            RCALL   bwait               ; wait 1 bit
            BRNE    PC-6                ; loop
bwait:
            RCALL   bwait2              ; wait 1 bit
bwait2:
            LDI      rs,(xtal/baud-22)/6-1
            SUBI     rs,1
            BRCC     PC-1
            DEC      cn
            RET


; ********************************************************************
;
;        RS-232C Recieve 1byte (10words)
;
;        return in rxd
;
; ********************************************************************

GETC:
            SBIC     PINB,RX
            RJMP     GETC               ; wait Start bit
            RCALL    bwait2             ; wait 1/2 bit
            LDI      cn,2*(1+8)         ; 9-bit Data
            ROR      rxd                ; set Data
            RCALL    bwait              ; wait 1 bit
            SBIS     PINB,RX
            CLC
            BRNE     PC-4               ; loop
            RET



(PIC10F322) 2+18+12=32words

xtal      EQU    8000000  ;Hz
baud      EQU    9600     ;bps

#define  TX 2                ;PIC TX Data output pin (i.e. 2 = RA2)
#define  RX 3                ;PIC RX Data input pin (i.e. 3 = RA3)

             cblock 0x7E
             cn              ;bit shift counter
             rxd             ;data buffer
             endc

             org             XXXX
             clrf            ANSELA              ; digital I/O
             bcf             TRISA,TX            ; set TX Port

; ********************************************************************
;
;                 RS-232C Send 1byte (18words)
;
;                 set W and call
;
; ********************************************************************

PUTC:
             bsf              LATA,TX
             movwf            rxd                 ; rxd=w
             call             bitout+3            ; start bit
             movwf            cn                  ; cn=9
             rrf              rxd,f               ; set Carry
             call             bitout              ; wait 1bit
             decfsz           cn,f                ; send 9bits?
             goto             $-3                 ; loop
bitout:
             btfsc            STATUS,C            ; Bit Output
             bsf              LATA,TX
             btfss            STATUS,C
             bcf              LATA,TX
bwait:
             call             bwait2              ; wait 1 bit
bwait2:
             movlw            .256-((xtal/.4)/baud-.15)/.8
             addlw            0x01
             btfss            STATUS,Z
             goto             $-2
             retlw            0x09

; ********************************************************************
;
;                  RS-232C Recieve 1byte?12words?
;
;                  return in W and rxd
;
; ********************************************************************

GETC:
            btfsc             PORTA,RX
            goto              GETC                ; wait Start bit
            call              bwait2              ; wait 1/2 bit and W=9
            movwf             cn                  ; cn=9
            rrf               rxd,f               ; get bit data
            call              bwait               ; wait 1 bit
            btfss             PORTA,RX
            bcf               STATUS,C
            decfsz            cn,f                ; cn=0?
            goto              $-5                 ; loop
            movf              rxd,w               ; return in w
            return
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf