KSControl.com TITLE 'Middle ware UART PIC16C711' LIST P=16C711 INCLUDE "P16C711.INC" [OSC=HS 4MHz OSC,WDT=ON] [ KAESUNG CONTROL_Jong-Kyoon Lee / www.kscontrol.com ] =============================================== [ Working Register Define ] TMP_W EQU 20H TMP_PSW EQU 21H TrxStsR EQU 22H TxDatR EQU 23H RxDatR EQU 24H BitCnt EQU 25H TstCnt EQU 26H TMRtmr EQU 27H TMR01S EQU 28H TMR1S EQU 29H BitDefine! TrxStsR TstrngF EQU 0 TxDoF EQU 1 TstrBF EQU 2 TstpBF EQU 3 RxDoF EQU 4 RxDoneF EQU 5 [ Port_A Bit-Define ] [ Port_B Bit-Define ] RxDin EQU 0 TxDout EQU 1 [Constant Define ] #define _C STATUS,C #define _Z STATUS,Z #define _ROMpge0 bcf PCLATH,3 #define _ROMpge1 bsf PCLATH,3
#define _RAMpge0 bcf STATUS,RP0 #define _RAMpge1 bsf STATUS,RP0 MSB equ 7 LSB equ 0 STX EQU 02h CR EQU 0Dh LF EQU 0Ah 2400bps TrxBd10 EQU.204 TrxBd15 EQU.152 ////////////////////////////////// ORG 0000H goto INIT ORG 0004 INTPROCmovwf TMP_W swapf STATUS,W bcf STATUS,RP0 movwf TMP_PSW btfsc INTCON,T0IF goto TxDpro btfsc INTCON,INTF goto RxDpro ENDINT swapf TMP_PSW,W movwf STATUS swapf TMP_W swapf TMP_W,W retfie TxDpro btfss TrxStsR,TxDoF btfsc TrxStsR,RxDoF goto RcvByt In Rxing btfss TrxStsR,TstrBF goto SndStrB btfsc TrxStsR,TstpBF goto SndStpB rrf TxDatR btfsc _C bsf PORTB,TxDout bcf PORTB,TxDout decfsz BitCnt bsf TrxStsR,TstpBF SndStrB bcf PORTB,TxDout Send Start-bit movlw.8 movwf BitCnt bsf TrxStsR,TstrBF SndStpB bsf PORTB,TxDout Send Stop-bit bcf TrxStsR,TstrBF bcf TrxStsR,TstpBF bcf TrxStsR,TxDoF
TmrUp incf TMRtmr movlw.240 0.1Sec @ 2400bps subwf TMRtmr,W goto ClT0IF clrf TMRtmr incf TMR01S 0.1Sec User's Timer movlw.10 subwf TMR01S,W goto ClT0IF clrf TMR01S incf TMR1S 1Sec User's Timer ClT0IF movlw TrxBd10 movwf TMR0 bcf INTCON,T0IF goto ENDINT RcvByt bcf _C rrf RxDatR btfsc PORTB,RxDin bsf RxDatR,7 decfsz BitCnt bcf TrxStsR,RxDoF bsf TrxStsR,RxDoneF RxDpro bcf INTCON,INTE bcf INTCON,INTF btfsc TrxStsR,TxDoF goto ENDINT InTxing! RcvStrB movlw.8 movwf BitCnt bsf TrxStsR,RxDoF movlw TrxBd15 1.5Bit Period movwf TMR0 bcf INTCON,T0IF goto ENDINT INIT movlw B'00011000' movwf STATUS clrwdt clrf PORTA movlw B'00000001' movwf PORTB clrf INTCON bsf STATUS,5 movlw B'10010011' RTCC x 16 movwf 81H OPTION_REG clrf TRISA movlw B'00000001' movwf TRISB bcf STATUS,5 clrf TrxStsR clrf PORTA movlw B'00000011' movwf PORTB movlw B'10110000'
movwf INTCON ----------------------------------- <<<< MAIN ROUTINES >>>> /// Test Loop MAIN clrwdt call RxPro call TxStrng call UserPGM 사용자루틴 goto MAIN // UserPGM에서 보낼데이터있음버퍼에보낼스트링옮긴후 bsf TrxStsR,TstrngF로전송할문자있음을표시 수신이필요할경우아래두줄 bcf INTCON,INTF bsf INTCON,INTE Permit RxRcv! 을실행후 RxPro에서 1바이트혹은스트링수신구현 //////SUB/////// TxStrng btfss TrxStsR,TstrngF Present String to Send? No! bcf INTCON,INTE Rx Masking btfsc TrxStsR,TxDoF in Txmting a byte! movlw 0 ROM_Addr Pointer_Change if U need! movwf PCLATH incf TstCnt movf TstCnt,W addwf PCL goto TxChar1 goto TxChar2 goto TxChar3 goto TxChar4 goto TxChar# StrLength to Send(if U need!) clrf TstCnt bcf TrxStsR,TstrngF End of Transmit TxData to Send= "HELLO" TxChar1 movlw 'H' TxChar2 movlw 'E' TxChar3 movlw 'L' TxChar4 movlw 'L'
TxChar5 movlw 'O' TxChar#movf UsrReg,W RxPro btfss TrxStsR,RxDoneF bcf INTCON,INTE Inhibit RxRcv! movf RxDatR,W movwf UsrReg // User RcvProc bcf TrxStsR,RxDoneF Reset Rx_Done Flag! UserPGMnop User Working Program END Coded by J.K Lee ///// 하드웨어 UART와소프트웨어 UART의중간의장단점을갖는방법을생각하다보니 Middle-Ware UART 라는어중간? 한이름을붙이게되네요. 하드웨어 UART 를사용하는것이제일낫기는하지만, 칩의선택문제라든지, 통신처럼중계하는용도 로는한개의하드웨어 UART로는모자랄경우소프트웨어 UART 를구현하게되는데, 소프트웨어방 식의가장큰문제점은타이밍을흐트리지않기위해서는송수신중루프내에서머물러야하는제약이 다른일을못하게만드는단점이있습니다. 특히, 시간적으로수신하는동안수신루틴에서만머무를수없는 FND다이내믹표시라든가하드웨어 적으로꼭처리해야하는일이있을경우소프트웨어방식은쓸수없습니다. 이런문제점을해결하는방법으로, TMER를이용한준하드웨어방식의 UART구현의방법론을알려드 리니응용해보실만할겁니다. [ 개요 ] 1. 사용하는크리스탈이나보오래이트에맞겠금적절히 TMER(ex:TMR0) 의프리스케일러값을세트합니다. 이렇게할경우인터럽트주기는머신싸이클/ 프리스케일러/256의보레이트를갖는인터럽트주기가될것입니다. 2. 여기에서, 매인터럽트를끝내고나오는시점에원하는보레이트가되는값을 TMR0에릴로드시 키고 T0IF 를클리어시키고빠져나오는구조를갖춥니다. ( 예: 4Mh크리스탈일경우프리스케일러 를 2 분주로했을때, d204를릴로드시키면 9600bps, 152를릴로드시키면 4800bps 의인터럽트주기를갖습니다) [ 주] 본코드는디버깅을거치지않은프로그램으로세세한부분에손볼필요도있을겁니다.