;GID−μPC 2 include "pic16f84.inc" LIST P= 16F84 ; __config _LP_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF #define rx PORTB,0 #define tx PORTB,6 #define adc_sclk PORTB,3 #define adc_din PORTB,7 #define adc_dout PORTA,4 ;'0' = output '1' = input trisa_m equ b'11110000' ;I/O set mask trisb_m equ b'00000001' ;I/O set mask ; org 10h ;Start of available PORT RAM rx_buf_0 equ 0x10 rx_buf_1 equ 0x11 rx_buf_2 equ 0x12 rx_buf_3 equ 0x13 rx_buf_4 equ 0x14 rx_buf_5 equ 0x15 rx_buf_6 equ 0x16 rx_buf_7 equ 0x17 rx_buf_8 equ 0x18 rx_buf_9 equ 0x19 rx_buf_a equ 0x1a rx_buf_b equ 0x1b rx_buf_c equ 0x1c rx_buf_d equ 0x1d rx_buf_e equ 0x1e rx_buf_f equ 0x1f tx_buf equ 0x20 rx_buf equ 0x21 tmp equ 0x22 w_stack equ 0x24 s_stack equ 0x25 adcbuf_0 equ 0x26 adcbuf_1 equ 0x27 adcbuf_2 equ 0x28 loop75_buf equ 0x30 loop255_0_buf equ 0x31 loop255_1_buf equ 0x32 ;************************************************* ;PICホットスタートアドレス ;************************************************* org 0h ;Start of code space (ROM) goto init ;************************************************* org 04h rx__ ;(4) interrupt start up time incfsz FSR,f ;(1)incfszはフラグに影響しない nop ;(1) nop ;(1) nop ;(1) nop ;(1) nop ;(1) rx1200 btfss rx ;(1) bsf rx_buf,0 ;(1) movwf w_stack ;(1) push W swapf w_stack,f ;(1) swapf STATUS,w ;(1) push status movwf s_stack ;(1) btfss rx ;(1) bsf rx_buf,1 ;(1) movlw b'00001111' ;(1) FSR mask andwf FSR,f ;(1) FSR = buffer address bsf FSR,4 ;(1) set 00010000b nop nop btfss rx ;(1) bsf rx_buf,2 ;(1) movf rx_buf,w ;(1) (buffer address) = rx_buf movwf INDF ;(1) nop nop clrf rx_buf ;(1)RX_buf clear btfss rx ;(1) bsf INDF,3 ;(1) call wait5 ;(5) btfss rx ;(1) bsf INDF,4 ;(1) call wait4 ;(4) btfss rx ;(1) bsf INDF,5 ;(1) call wait5 ;(5) btfss rx ;(1) bsf INDF,6 ;(1) call wait5 ;(5) btfss rx ;(1) bsf INDF,7 ;(1) rx_end swapf s_stack,w ;(1) pop W movwf STATUS ;(1) pop status swapf w_stack,w ;(1) bcf INTF ;(1) INTF enable retfie ;(2) RETurn From Interrupt ; and GIE enable ;************************************************* ;PICの初期化ルーチン ;************************************************* init bsf RP0 ;ram file page 0 movlw trisa_m movwf TRISA ;Set port to output. movlw trisb_m movwf TRISB ;"0" = Output "1" = Input movlw b'01000100' ;RBPU,TMR0=CLKOUT,PSR=TMR0,1:32 movwf OPTION_REG ;内部プルアップの設定 bcf RP0 ;ram file page 0 bsf INTE ;INTE(int PORTB0) enable clrf PORTA clrf PORTB ;************************************************* ;メインルーチン ;************************************************* main movlw 0fh ;rx buffer movwf FSR clrf INDF ;rx buffer clear bsf GIE ;割り込み許可 clrf rx_buf ;************************************************* ;コマンド入力待ちルーチン ;************************************************* loop255_1_s clrf loop255_1_buf loop255_1 loop255_0_s clrf loop255_0_buf loop255_0 call z_test ;75 decfsz loop255_0_buf,f goto loop255_0 decfsz loop255_1_buf,f goto loop255_1 call getcnv_c goto loop255_1_s z_test movlw d'6' movwf loop75_buf z_test_2 movf rx_buf_0,w ; sublw 'z' ; btfsc Z ; call getcnv_c movlw 0fh ; movwf FSR decfsz loop75_buf,f goto z_test_2 movf rx_buf_0,w ; sublw 'z' ; btfsc Z ; call getcnv_c movlw 0fh ; movwf FSR nop movf rx_buf_0,w ; sublw 'z' ; btfsc Z ; call getcnv_c movlw 0fh ; movwf FSR return ;************************************************* ;command getcnv ;************************************************* getcnv_c bcf GIE btfsc GIE goto getcnv_c call getcnv movlw 0fh ;rx buffer movwf FSR clrf rx_buf_0 ;rx buffer clear bsf GIE ;割り込み許可 return ;************************************************* ;WAITルーチン各種 ;************************************************* wait5 nop wait4 return wait64 call wait32 wait32 call wait5 wait27 nop wait26 nop wait25 nop wait24 nop wait23 nop wait22 nop wait21 nop wait20 call wait4 ;(4) wait17 call wait4 ;(4) wait14 call wait4 ;(4) wait11 call wait4 ;(4) return ;************************************************* ;GID-ADC AD CONVERT ;************************************************* getcnv movlw 0x0d movwf tx_buf call tx__ movlw a'K' movwf tx_buf call tx__ movlw 0x0d movwf tx_buf call tx__ movlw a'0' movwf tx_buf call tx__ movlw a':' movwf tx_buf call tx__ movlw b'10001101' movwf tmp call gid_dout movlw a'1' movwf tx_buf call tx__ movlw a':' movwf tx_buf call tx__ movlw b'11001101' movwf tmp call gid_dout movlw a'2' movwf tx_buf call tx__ movlw a':' movwf tx_buf call tx__ movlw b'10011101' movwf tmp call gid_dout movlw a'3' movwf tx_buf call tx__ movlw a':' movwf tx_buf call tx__ movlw b'11011101' movwf tmp call gid_dout movlw a'4' movwf tx_buf call tx__ movlw a':' movwf tx_buf call tx__ movlw b'10101101' movwf tmp call gid_dout movlw a'5' movwf tx_buf call tx__ movlw a':' movwf tx_buf call tx__ movlw b'11101101' movwf tmp call gid_dout movlw a'6' movwf tx_buf call tx__ movlw a':' movwf tx_buf call tx__ movlw b'10111101' movwf tmp call gid_dout movlw a'7' movwf tx_buf call tx__ movlw a':' movwf tx_buf call tx__ movlw b'11111101' movwf tmp call gid_dout movlw 0x0d movwf tx_buf call tx__ return ;************************************************* ;GID_DOut ;************************************************* sclk_du bcf adc_sclk bsf adc_sclk return ;************************************************* gid_dout bsf adc_din movlw 0x30 movwf adcbuf_0 movwf adcbuf_1 movwf adcbuf_2 out_1 bcf adc_din ;command start call sclk_du ;1 out_2 bsf adc_din btfsc tmp,6 bcf adc_din ;SEL2 call sclk_du ;2 out_3 bsf adc_din btfsc tmp,5 ;SEL1 bcf adc_din call sclk_du ;3 bsf adc_din btfsc tmp,4 ;SEL0 bcf adc_din call sclk_du ;4 bsf adc_din btfsc tmp,3 ;UNI/~BIP bcf adc_din call sclk_du ;5 bsf adc_din btfsc tmp,2 ;SGL/~DIF bcf adc_din call sclk_du ;6 bsf adc_din ;PD1 call sclk_du ;7 bcf adc_din ;PD0 call sclk_du ;8 bsf adc_din call sclk_du ;9 btfsc adc_dout ;data 11 MSB bsf adcbuf_2,3 call sclk_du ;10 btfsc adc_dout ;data 10 bsf adcbuf_2,2 call sclk_du ;11 btfsc adc_dout ;data 9 bsf adcbuf_2,1 call sclk_du ;12 btfsc adc_dout ;data 8 bsf adcbuf_2,0 call sclk_du ;13 btfsc adc_dout ;data 7 bsf adcbuf_1,3 call sclk_du ;14 btfsc adc_dout ;data 6 bsf adcbuf_1,2 call sclk_du ;15 btfsc adc_dout ;data 5 bsf adcbuf_1,1 call sclk_du ;16 btfsc adc_dout ;data 4 bsf adcbuf_1,0 call sclk_du ;17 btfsc adc_dout ;data 3 bsf adcbuf_0,3 call sclk_du ;18 btfsc adc_dout ;data 2 bsf adcbuf_0,2 call sclk_du ;19 btfsc adc_dout ;data 1 bsf adcbuf_0,1 call sclk_du ;20 btfsc adc_dout ;data 0 bsf adcbuf_0,0 char_2 movlw -a':' addwf adcbuf_2,w btfss C ;rx_buf_2 >= '7' goto char_1 movlw a'A'-a':' addwf adcbuf_2,f char_1 movlw -a':' addwf adcbuf_1,w btfss C ;rx_buf_1 >= '7' goto char_0 movlw a'A'-a':' addwf adcbuf_1,f char_0 movlw -a':' addwf adcbuf_0,w btfss C ;rx_buf_0 >= '7' goto adc_tx movlw a'A'-a':' addwf adcbuf_0,f adc_tx movf adcbuf_2,w movwf tx_buf call tx__ movf adcbuf_1,w movwf tx_buf call tx__ movf adcbuf_0,w movwf tx_buf call tx__ movlw 0x20 movwf tx_buf call tx__ return ;************************************************* ;tx__ ;1文字送信ルーチン。 ;tx_bufにデータを書き込んでからコールする。 ;************************************************* tx__ bsf tx ;(1) start bit movwf tx_buf goto tx12_0 tx12_0 btfsc tx_buf,0 ;(1) 0 goto tx12_c_0 ;(2) nop ;(1) bsf tx ;(1) goto tx12_p0 ;(2) tx12_p0 nop tx12_1 btfsc tx_buf,1 ;(1) 1 goto tx12_c_1 ;(2) nop ;(1) bsf tx ;(1) goto tx12_p1 ;(2) tx12_p1 tx12_2 btfsc tx_buf,2 ;(1) 2 goto tx12_c_2 ;(2) nop ;(1) bsf tx ;(1) goto tx12_p2 ;(2) tx12_p2 nop tx12_3 btfsc tx_buf,3 ;(1) 3 goto tx12_c_3 ;(2) nop ;(1) bsf tx ;(1) goto tx12_p3 ;(2) tx12_p3 nop tx12_4 btfsc tx_buf,4 ;(1) 4 goto tx12_c_4 ;(2) nop ;(1) bsf tx ;(1) goto tx12_p4 ;(2) tx12_p4 nop tx12_5 btfsc tx_buf,5 ;(1) 5 goto tx12_c_5 ;(2) nop ;(1) bsf tx ;(1) goto tx12_p5 ;(2) tx12_p5 nop tx12_6 btfsc tx_buf,6 ;(1) 6 goto tx12_c_6 ;(2) nop ;(1) bsf tx ;(1) goto tx12_p6 ;(2) tx12_p6 nop tx12_7 btfsc tx_buf,7 ;(1) 7 goto tx12_c_7 ;(2) nop ;(1) bsf tx ;(1) goto tx12_p7 ;(2) tx12_p7 goto tx12_stop_b ;(2) tx12_stop_b nop bcf tx nop ;ストップビットの幅は7必要. return ;Return+Call+movlw+1だと6なので,入れておく. tx12_c_0 bcf tx goto tx12_p0 tx12_c_1 bcf tx goto tx12_p1 tx12_c_2 bcf tx goto tx12_p2 tx12_c_3 bcf tx goto tx12_p3 tx12_c_4 bcf tx goto tx12_p4 tx12_c_5 bcf tx goto tx12_p5 tx12_c_6 bcf tx goto tx12_p6 tx12_c_7 bcf tx goto tx12_p7 end