;-- -------------------------------------------------------------------------- ;-- ;-- configuration fuses ;-- ;-- -------------------------------------------------------------------------- ;-- no code protection 11_11 ;-- don't care bit x ;-- no data protection 1 ;-- LVP disabled 0 ;-- brownout reset enabled 1 ;-- internal MCLR 0 ;-- power-up delay enabled 0 ;-- watchdog disabled 0 ;-- HS osc 0 10 __CONFIG 0x3fff & _LVP_OFF & _MCLRE_ON & _BODEN_OFF & _PWRTE_OFF & _WDT_OFF & _HS_OSC ; PROCESSOR 16F628 ; #include "p16f628.inc" ; ;---------------------------------------------------------------------------- ENDINT macro goto EndInt endm RETINT macro retfie endm ;Constantes SPBRG_VAL EQU .10 ;set baud rate 115200 for 4Mhz clock V35_BIT_RX EQU .3 ;PIN Input DATA V35 (PORTB) V35_BIT_TX EQU .0 ;PIN Output DATA V35 (PORTA) ;---------------------------------------------------------------------------- ;Variables CBLOCK 0x20 ContCTS ContWait Cont V35FlagT V35FlagR V35Bit TmpDeb V35RXIndice V35Out V35BuffIn V35BuffOut V35_TEMP ;Temporal para Byte recibido por V35 RS232Buffer ;transmit data buffer (RS232) V35Buffer ;Transmit data buffer (V35) ENDC ;---------------------------------------------------------------------------- ;Macros to select the register bank ; Many bank changes can be optimized when only one STATUS bit changes Bank0 MACRO ;macro to select data RAM bank 0 bcf STATUS,RP0 ENDM ;----------------------------------------------------------------------------- org 0x00 ;IMPORTANTE: Si el programa pasa de 1 Kb hay que verificar el uso de PCLATH clrf PCLATH ;select program memory page 0 goto Main ;go to beginning of program ; org 0x04 ;NOTA: el programa trabaja solo en el BANK0 InterruptCode: V35Int: bcf INTCON,INTF ;Output movf V35Out,W movwf PORTA ;Input movf PORTB,W movwf V35Bit ;-> incf ContWait,F btfss ContWait,4 goto V35TX bcf V35Out,2 clrf ContCTS V35TX: bcf STATUS,C rrf RS232Buffer,F bcf V35Out,V35_BIT_TX btfsc STATUS,C bsf V35Out,V35_BIT_TX ;Analiza Bit recibido V35Input: btfss V35FlagR,0 goto V35RXStartBit V35RX: rrf V35_TEMP,F bcf V35_TEMP,7 btfsc V35Bit,V35_BIT_RX bsf V35_TEMP,7 decfsz V35RXIndice,F ENDINT V35Fin: clrf V35FlagR movwf V35Buffer bsf V35FlagT,0 EndInt: btfss PIR1,RCIF ;test RCIF receive interrupt goto RS232TX movf RCREG,W ;get received data movwf RS232Buffer bsf V35Out,V35_BIT_TX incf ContCTS,F btfsc ContCTS,2 bsf V35Out,2 clrf ContWait RS232TX: btfss PIR1,TXIF ;test TXIF Transmit interrupt RETINT btfss V35FlagT,0 RETINT clrf V35FlagT movf V35Buffer,W movwf TXREG RETINT V35RXStartBit: btfss V35Bit,V35_BIT_RX ENDINT ;Lecturó un 0, ignorar bsf V35FlagR,0 movlw 0x08 movwf V35RXIndice clrf V35_TEMP ENDINT ;============================================================================ ; MAIN LOOP ;============================================================================ Main: clrf PORTA clrf PORTB MOVLW 0X07 MOVWF CMCON Bank1 clrf TRISA movlw 0xFF movwf TRISB bsf OPTION_REG,INTEDG bsf INTCON,INTE Bank0 call SetupSerial ;set up serial port and buffers Bank0 bcf INTCON,INTF clrf PIR1 bsf INTCON,GIE MainLoop: goto MainLoop ;---------------------------------------------------------------------------- ;Set up serial port and buffers. SetupSerial: Bank1 ;select bank 1 BSF TRISB,1 BSF TRISB,2 movlw SPBRG_VAL ;set baud rate movwf SPBRG movlw 0x24 ;enable transmission and high baud rate movwf TXSTA Bank0 ;select bank0 movlw 0x90 ;enable serial port and reception movwf RCSTA call InitV35Buffer ;initialize V35 buffer call InitRS232Buffer ;initialize RS232 buffer return ;---------------------------------------------------------------------------- ;Circular buffer routines. ;---------------------------------------------------------------------------- ;Initialize V35 buffer InitV35Buffer: movlw V35Buffer ;take address of transmit buffer movwf V35BuffIn ;and place in transmit start pointer movwf V35BuffOut ;and place in transmit end pointer clrf V35RXIndice clrf V35FlagR clrf V35FlagT clrf V35Out clrf ContCTS clrf ContWait clrf Cont return ;Initialize RS232 buffer InitRS232Buffer: clrf RS232Buffer return END