; --------------------------------------------------------- ; Soft Serial testing code. ; serial data output ; --------------------------------------------------------- ; programed by rerofumi ; --------------------------------------------------------- list p=12f683 include P12F683.INC ; ----- configuration __CONFIG _INTOSCIO & _CP_OFF & _CPD_OFF & _MCLRE_OFF & _WDT_OFF & _BOD_OFF & _PWRTE_OFF ; ----- define SERIALPORT EQU GPIO SERIALOBIT EQU 5 ; 25 = 16MHz, 9600bps, 0.1042 ms ; 12 = 8MHz, 9600bps STIMING EQU d'12' SENDSIZE EQU d'9' ; --- Serial send work SENDBYTE EQU 028h SENDBIT EQU 029h SENDDATA EQU 02ah SENDWAIT EQU 02bh ; --- ADC work SENDTOP EQU 030h ASCIIX EQU 030h XL EQU 031h XH EQU 032h ASCIIY EQU 033h YL EQU 034h YH EQU 035h ASCIIZ EQU 036h ZL EQU 037h ZH EQU 038h ADWAIT EQU 040h TESTWAIT EQU 050h ; --------------------------------------------------------- ; vector address ; --------------------------------------------------------- ; ------------------------------ ; cold start ; ------------------------------ org 000h goto Main ; ------------------------------ ; interuppt ; ------------------------------ org 004h goto IntStart ; --------------------------------------------------------- ; interrupt ; --------------------------------------------------------- IntStart ; retfie ; --------------------------------------------------------- ; ; Timer0 wait ; ; --------------------------------------------------------- ; -------------------------- ; --- timer wait ; w = wait count (0.1ms) Timer0Wait bcf INTCON, T0IF sublw 0ffh movwf TMR0 tm0wait_loop1 btfss INTCON, T0IF goto tm0wait_loop1 bcf INTCON, T0IF return ; --------------------------------------------------------- ; ; ADC read subroutine ; ; --------------------------------------------------------- ; --- sample hold AdcWait movlw d'45' movwf ADWAIT AdcWaitLoop decfsz ADWAIT,f goto AdcWaitLoop return AdcOn bcf STATUS, RP0 movlw b'10000001' movwf ADCON0 return AdcOff bcf STATUS, RP0 bcf ADCON0, ADON return AdcRead call AdcOn ; bcf STATUS, RP0 ; --- AD0 bcf ADCON0, CHS0 bcf ADCON0, CHS1 ; ACQUISITION TIME call AdcWait ; ADC start bsf ADCON0, GO AdcAd0Wait btfsc ADCON0, GO goto AdcAd0Wait ; get data bsf STATUS, RP0 movf ADRESL, w bcf STATUS, RP0 addwf XL, f btfsc STATUS, C incf XH movf ADRESH, w addwf XH, f ; --- AD1 bsf ADCON0, CHS0 bcf ADCON0, CHS1 ; ACQUISITION TIME call AdcWait ; ADC start bsf ADCON0, GO AdcAd1Wait btfsc ADCON0, GO goto AdcAd1Wait ; get data bsf STATUS, RP0 movf ADRESL, w bcf STATUS, RP0 addwf YL, f btfsc STATUS, C incf YH movf ADRESH, w addwf YH, f ; --- AD2 bcf ADCON0, CHS0 bsf ADCON0, CHS1 ; ACQUISITION TIME call AdcWait ; ADC start bsf ADCON0, GO AdcAd2Wait btfsc ADCON0, GO goto AdcAd2Wait ; get data bsf STATUS, RP0 movf ADRESL, w bcf STATUS, RP0 addwf ZL, f btfsc STATUS, C incf ZH movf ADRESH, w addwf ZH, f ; call AdcOff return ; --------------------------------------------------------- ; ; Soft Serial send ; ; --------------------------------------------------------- SerialSend movlw SENDTOP movwf FSR movlw SENDSIZE movwf SENDBYTE SerialSend_loop movf INDF, w movwf SENDDATA call SerialSendByte incf FSR decfsz SENDBYTE goto SerialSend_loop ; return ; --- send 1byte to serial SerialSendByte movlw 008h movwf SENDBIT ; - start bit bcf SERIALPORT, SERIALOBIT call SerialBitWait ; adjust nop nop nop nop nop nop ; - send 8 bit SerialSendBit_loop btfss SENDDATA, 0 goto SerialSendBit_setbit1 bsf SERIALPORT, SERIALOBIT goto SerialSendBit_setbit2 SerialSendBit_setbit1 bcf SERIALPORT, SERIALOBIT SerialSendBit_setbit2 rrf SENDDATA, f ; call SerialBitWait ; next bit decfsz SENDBIT, f goto SerialSendBit_loop ; - stop bit bsf SERIALPORT, SERIALOBIT call SerialBitWait ; return ; --- wait 16clock * STIMING SerialBitWait movlw STIMING movwf SENDWAIT SerialBitWait_loop nop nop nop nop nop nop nop nop nop nop nop nop nop decfsz SENDWAIT, f goto SerialBitWait_loop return ; --------------------------------------------------------- ; ; main routine ; ; --------------------------------------------------------- Main ; ------------------------------ ; initalize ; ------------------------------ ; -- RAM bank 1 bsf STATUS, RP0 ; clock setting movlw b'01110101' movwf OSCCON ; IO setting ; AN0-4 enable movlw b'01010111' movwf ANSEL movlw b'00000111' movwf TRISIO ; Timer0 bcf OPTION_REG, T0CS bcf OPTION_REG, PSA bsf OPTION_REG, PS0 bsf OPTION_REG, PS1 bsf OPTION_REG, PS2 ; -- RAM bank 0 bcf STATUS, RP0 ; port clear clrf GPIO ; ADC port movlw b'00000000' movwf ADCON0 ; bcf STATUS, RP0 ; --- bsf SERIALPORT, SERIALOBIT movlw a'X' movwf ASCIIX movlw a'Y' movwf ASCIIY movlw a'Z' movwf ASCIIZ call AdcOn ; ------------------------------ ; loop work ; ------------------------------ MainWorkLoop clrf XH clrf XL clrf YH clrf YL clrf ZH clrf ZL ; - Read ADC call AdcRead call AdcRead call AdcRead call AdcRead call AdcRead call AdcRead call AdcRead call AdcRead ; - shift 3bit rlf XL rlf XH rlf XL rlf XH rlf XL rlf XH rlf YL rlf YH rlf YL rlf YH rlf YL rlf YH rlf ZL rlf ZH rlf ZL rlf ZH rlf ZL rlf ZH ; - serial send call SerialSend ; - wait for next movlw d'2' movwf TESTWAIT MainWait movlw 0ffh call Timer0Wait decfsz TESTWAIT, f goto MainWait goto MainWorkLoop end