; -*- tab-width: 4 -*- ; 19.2Kbps Serial Bitbang Receive ; For use on HOODMICRO PIC12F6XX at 4MHz ; ; Copyright (c) 2005, K9spud LLC. ; http://www.k9spud.com/hoodmicro/ ; ; This program is free software; you can redistribute it and/or ; modify it under the terms of the GNU General Public License ; as published by the Free Software Foundation; either version 2 ; of the License, or (at your option) any later version. ; ; This program is distributed in the hope that it will be useful, ; but WITHOUT ANY WARRANTY; without even the implied warranty of ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ; GNU General Public License for more details. ; ; You should have received a copy of the GNU General Public License ; along with this program; if not, write to the Free Software ; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. processor 12F629 RX_PIN equ 0 LED_PIN equ 4 STATUS equ 3 Z equ 2 C EQU 0 GPIO equ 5 INTCON equ 0x0B GIE equ 7 global _getch signat _getch,73 psect text0,local,class=CODE,delta=2 psect text0 _getch$c set ?a_getch+0 _getch$bitno set ?a_getch+1 _getch: ;; First we have to wait for the start bit. The signal line ;; will remain low until the start bit is sent. StartBitWaitLoop: btfss GPIO, RX_PIN ; 1 goto StartBitWaitLoop ; 1 2 call Wait23 ; 23 25 btfss GPIO, RX_PIN ; 1 26 are we still receiving the start bit goto StartBitWaitLoop ; 1 27 no, must've just been noise... movlw -8 ; 1 28 movwf _getch$bitno ; 1 29 call Wait23 ; 23 52 ;; at this point, sender has finished sending the start bit and will ;; now start sending the first bit ;; Number of clock cycles (at 1us per clock) for 19.2Kbps serial: ;; 56us too slow, garbage ;; 54us works ;; 52us works - recommended setting ;; 50us works ;; 48us too fast, garbage ;; 52 / 2 = 26 CaptureBit: call Wait24 ; 24 24 bcf STATUS, C ; 1 25 btfss GPIO, RX_PIN ; 1 26 sample the data in the middle of frame bsf STATUS, C ; 1 27 rrf _getch$c, f ; 1 28 call Wait21 ; 21 49 incfsz _getch$bitno, f ; 1 50 goto CaptureBit ; 2 52 movf _getch$c, w return FNSIZE _getch,2,0 global ?a_getch ;-------------------------------------------------------------------------- ; WaitXX - Burns cpu cycles for timing purposes. XX = number of cycles ;-------------------------------------------------------------------------- Wait21: nop goto Wait18 Wait23: nop goto Wait20 Wait24: goto $+1 ; 24 Wait22: goto $+1 ; 22 Wait20: goto $+1 ; 20 Wait18: goto $+1 ; 18 Wait16: goto $+1 ; 16 Wait14: goto $+1 ; 14 Wait12: goto $+1 ; 12 Wait10: goto $+1 ; 10 Wait8: goto $+1 ; 8 Wait6: goto $+1 ; 6 return ; 4 (initial call used 2) psect text1,local,class=CODE,delta=2 psect text1 end