; DGPSビーコン受信機用スピードコンバータ、アンテナ切替コントローラー ; DGPS 200bps/4800bps Converter, Antenna Switch Controller ; Copy Right(1997): Hiroyuki Yamamoto e-mail: jr1avlこの倍角文字を取る@attglobal.net ; 商用目的での使用はあらかじめメールでご連絡ください。非商用目的であっても ; 第三者へコピーを渡す場合はメールでご連絡ください。山本 ; PIC16f84 10MHz Assembler: MPASM LIST P=16F84, R=DEC INCLUDE P16F84.INC Prescaler EQU B'00000001' ;Pre-scaler value for 4800bps Interrupt EQU B'10100000' ;Timer0 initial value for 4800bps Timer0_init EQU 131 ;TMR0 initial value ;Timer0_init EQU 240 RxSampling EQU 24 ;No of Rx sampling ;-------------------------------------------------------------- ; Variables ;-------------------------------------------------------------- RxChipCounter EQU 0x20 RxBitCounter EQU RxChipCounter+1 RxPrevBit EQU RxChipCounter+2 RxCurrentBit EQU RxChipCounter+3 RxDataBuffer EQU RxChipCounter+4 RxDataBuffer2 EQU RxChipCounter+5 RxByteFull EQU RxChipCounter+6 RxSample EQU RxChipCounter+7 Work EQU RxChipCounter+8 TxDataBuffer EQU RxChipCounter+9 TxDataBuffer2 EQU TxDataBuffer+1 W_TEMP EQU TxDataBuffer+2 STATUS_TEMP EQU TxDataBuffer+3 TxBitCounter EQU TxDataBuffer+4 ShortBitFlag EQU TxDataBuffer+5 LongBitFlag EQU TxDataBuffer+6 ErrTimer1 EQU TxDataBuffer+7 ErrTimer2 EQU TxDataBuffer+8 ErrTimer3 EQU TxDataBuffer+9 ErrTimer4 EQU ErrTimer3+1 BitChangeFlag EQU ErrTimer3+2 ShortBitCounter EQU ErrTimer3+3 SyncFlag EQU ErrTimer3+4 StartFlag EQU ErrTimer3+5 SyncCount EQU ErrTimer3+6 AntennaFlag EQU ErrTimer3+7 OutOfSync EQU ErrTimer3+8 ORG H'00' GOTO Start ORG 4 ;Timer Intterrupt BCF INTCON,2 ;TOIF GOTO Timer ;-------------------------------------------------------------- ; Main Program ;-------------------------------------------------------------- ORG H'50' Start ;Initialize Registers ;Clear all file registers 0x20 - 0x4f MOVLW 0x20 MOVWF FSR NEXT CLRF INDF INCF FSR,F MOVLW 0x50 XORWF FSR,0 BTFSS STATUS,Z GOTO NEXT ;Initialize Prescaler and Intterrupt Control registers BSF STATUS,RP0 ;Bank 1 MOVLW Prescaler ;Set prescaler value MOVWF OPTION_REG MOVLW Interrupt ;Enable TMR0 overflow interrupt MOVWF INTCON ;Initialize Port settings MOVLW B'00010001' ;Set PortA 1-3 5-7 be output ports, 0 4 be input port MOVWF TRISA MOVLW B'00000001' ;Set PortB 1-7 be output ports, 0 be input port MOVWF TRISB BCF STATUS,RP0 ;Bank 0 MOVLW Timer0_init ;Preset TMR0 MOVWF TMR0 ;---------------MAIN LOOP-------------------------------------- MainLoop NOP NOP GOTO MainLoop ;-------------------------------------------------------------- ;--------------Interrrupt Routines----------------------------- Timer ;Initiated by the TMR0 interrupt PUSH MOVWF W_TEMP SWAPF STATUS,W MOVWF STATUS_TEMP MOVLW Timer0_init ;Preset TMR0 MOVWF TMR0 ISR ;INTERRUPT SEVICES BTFSC RxByteFull,0 CALL Send_one_Bit CALL Check_Rx_Port CALL Error_Check CALL Antenna_Switch POP SWAPF STATUS_TEMP,W MOVWF STATUS SWAPF W_TEMP,F SWAPF W_TEMP,W RETFIE ;----------------------------------------------------------- Send_one_Bit MOVF TxBitCounter,1 ;Go to SendStartBit if the count is 0 BTFSC STATUS,Z GOTO SendStartBit MOVLW 9 ;Go to SendStopBit if the count is 9 XORWF TxBitCounter,0 BTFSC STATUS,Z GOTO SendStopBit SendOneBit BTFSC TxDataBuffer,0 ;bit pointer in the buffer BSF PORTA,1 BTFSS TxDataBuffer,0 BCF PORTA,1 RRF TxDataBuffer,1 ;shift out one bit to right INCF TxBitCounter,1 RETURN SendStartBit BCF PORTA,1 ;Send one start bit to PortA0 INCF TxBitCounter,1 MOVF TxDataBuffer2,0 MOVWF TxDataBuffer RETURN SendStopBit BSF PORTA,1 ;Send one stop bit to PortA0 CLRF TxBitCounter CLRF RxByteFull RETURN ;----------------------------------------------------------- Check_Rx_Port BTFSS PORTA,4 ;Skip next if Port A4 is 1 BCF RxCurrentBit,0 BTFSC PORTA,4 ;Skip next if Port A4 is 0 BSF RxCurrentBit,0 MOVF RxChipCounter,0 ;Sample input when the chip counter is 12 XORLW 12 BTFSS STATUS,Z GOTO BitComp MOVF RxCurrentBit,0 MOVWF RxSample BitComp MOVF RxPrevBit,0 ;Check if input changes XORWF RxCurrentBit,0 MOVWF Work BTFSC Work,0 GOTO RxBitChange RxBitCont INCF RxChipCounter,1 ;Increment Chip Counter MOVF RxChipCounter,0 ;If Chip Counter is less than SUBLW 23 ;24 then continue to count up BTFSC STATUS,C ;otherwise add one bit to RX RETURN AddOneBit INCF RxBitCounter,1 ; RRF RxDataBuffer,1 ;Right-shift by one BTFSS RxSample,0 ;Insert one bit BCF RxDataBuffer,5 ;Insert 0 BTFSC RxSample,0 BSF RxDataBuffer,5 ;Insert 1 MOVF RxCurrentBit,0 ;Store RxCurrentBit to RxPrevBit MOVFW RxPrevBit CLRF RxChipCounter ;Initialize RxChipCounter MOVF RxBitCounter,0 ;If RxBitCounter is >= 6 then go to SUBLW 5 ;BufferFull BTFSC STATUS,C RETURN BufferFull CLRF RxBitCounter ;Initialize RxBitCounter MOVF RxDataBuffer,0 ANDLW B'00111111' ;Add a header IORLW B'01000000' MOVWF TxDataBuffer2 BSF RxByteFull,0 ;Set Flag for byte full RETURN RxBitChange MOVF RxCurrentBit,0 ;Update RxPrevBit flag MOVWF RxPrevBit BSF BitChangeFlag,0 ;Set BitChangeFlag for Error_Check MOVF RxChipCounter,0 ;Check if the bit change period is too short SUBLW 14 ;stable data receptions BTFSC STATUS,C BCF ShortBitFlag,0 ;Set ShortBitFlag for Error_Check SUBLW 10 ;Exclude "chip low" BTFSC STATUS,C BSF ShortBitFlag,0 MOVF RxChipCounter,0 ;Check if the change occured after sampling SUBLW 13 ;otherwise go to RxBitChange2 to clear the BTFSC STATUS,C ;chip counter GOTO RxBitChange2 MOVLW 23 ;Set the chip counter to 23 to restert the MOVWF RxChipCounter ;chip count GOTO RxBitCont RxBitChange2 CLRF RxChipCounter ;Clear the chip counter and return RETURN ;-------------------------------------------------------------- Error_Check CALL Error_Check_Long_Short CALL Error_Check_Sync CALL Err_Timer RETURN Error_Check_Sync Error_Check_Sync_Long BCF SyncFlag,0 MOVF SyncFlag,0 ANDLW B'00000110' XORLW B'00000110' BTFSC STATUS,Z BSF SyncFlag,0 MOVF ErrTimer2,1 BTFSS STATUS,Z ;If Timer3 is 0 not then go to Sync Short GOTO Error_Check_Sync_Short BCF OutOfSync,0 ;For Antenna Switch BTFSS SyncFlag,0 BSF OutOfSync,0 BTFSS SyncFlag,0 GOTO Long_OOS Long_Sync BSF PORTB,3 BCF PORTB,1 BSF SyncFlag,1 GOTO Error_Check_Sync_Short Long_OOS BCF PORTB,3 BSF SyncFlag,1 Error_Check_Sync_Short MOVF ErrTimer2,1 BTFSS STATUS,Z ;If Timer2 is not 0 then RETURN GOTO End_of_Long_Short BTFSS SyncFlag,0 GOTO Short_OOS Short_Sync BSF PORTB,3 BCF PORTB,1 BSF SyncFlag,2 GOTO End_of_Long_Short Short_OOS BCF PORTB,3 BSF SyncFlag,2 End_of_Long_Short BSF SyncFlag,1 BSF SyncFlag,2 RETURN Error_Check_Long_Short Error_Check_Long BTFSC BitChangeFlag,0 BCF LongBitFlag,0 BCF BitChangeFlag,0 MOVF ErrTimer2,1 BTFSS STATUS,Z GOTO Error_Check_Short BTFSS LongBitFlag,0 GOTO End_of_Chk_Lng BSF PORTB,1 ;Set ERR LED BCF SyncFlag,1 ;Clear SyncFlag 1 Long End_of_Chk_Lng BSF LongBitFlag,0 Error_Check_Short BTFSC ShortBitFlag,0 INCF ShortBitCounter BCF ShortBitFlag,0 MOVF ErrTimer2,1 BTFSS STATUS,Z RETURN MOVF ShortBitCounter,0 SUBLW 6 BTFSC STATUS,C GOTO End_of_Chk_Shrt BSF PORTB,1 ;Set ERR LED BCF SyncFlag,2 ;Clear SyncFlag 2 Short End_of_Chk_Shrt CLRF ShortBitCounter RETURN ;Timers ; Err_Timer MOVF ErrTimer2,1 BTFSC STATUS,Z INCF ErrTimer2 MOVF ErrTimer3,1 BTFSC STATUS,Z INCF ErrTimer3 MOVF ErrTimer4,1 BTFSC STATUS,Z INCF ErrTimer4 Err_Timer1 ;100/4800 seconds INCF ErrTimer1 MOVF ErrTimer1,0 SUBLW 100 BTFSS STATUS,C GOTO Err_Timer2 RETURN Err_Timer2 ;1/2 seconds CLRF ErrTimer1 INCF ErrTimer2 MOVF ErrTimer2,0 SUBLW 24 BTFSS STATUS,C GOTO Err_Timer3 RETURN Err_Timer3 ;5 seconds CLRF ErrTimer2 INCF ErrTimer3 MOVF ErrTimer3,0 SUBLW 10 BTFSS STATUS,C GOTO Err_Timer4 RETURN Err_Timer4 ;10 seconds CLRF ErrTimer3 INCF ErrTimer4 MOVF ErrTimer4,0 SUBLW 2 BTFSS STATUS,C CLRF ErrTimer4 RETURN ;-------------------------------------------------------------- ; Anttena Switch Antenna_Switch MOVF ErrTimer2,1 BTFSS STATUS,Z RETURN Sync_Check BTFSC OutOfSync,0 INCF SyncCount MOVF ErrTimer4,1 BTFSS STATUS,Z RETURN MOVF SyncCount,0 SUBLW 8 BTFSC STATUS,C GOTO End_of_Antenna_Switch BTFSC AntennaFlag,0 GOTO CLR_A_Flag Set_A_Flag BSF AntennaFlag,0 BSF PORTB,2 GOTO End_of_Antenna_Switch CLR_A_Flag BCF AntennaFlag,0 BCF PORTB,2 End_of_Antenna_Switch CLRF SyncCount RETURN end