PIC18F24Q10 interrupt serial routine with terminator


+ Reply to Thread
Results 1 to 6 of 6
  1. #1
    Junior Member JonW's Avatar
    Join Date
    Dec 2019
    Posts
    11
    Thumbs Up
    Received: 1
    Given: 0
    Total Downloaded
    154.49 MB

    0 Not allowed!

    Default PIC18F24Q10 interrupt serial routine with terminator

    Here is a fast interrupt driven serial routine with defined buffer. Its bit bashed (persay) so does not use compiler commands for serial as these are not supported.

    The routine vectors to ISR on a RX byte and triggers the timer on receive of a byte and refreshed on another RX. if a "#" in my case is not received before the timer elapsed the pointer is reset and avoids overflow.

    Shows the simplicity and power of this compiler.


    Code:
    '****************************************************************
    '*  Name    : FAST SERIAL.BAS                                      *
    '*  Author  : JON WALKER                                        *
    '*  Notice  : Copyright (c) 2019 ELECIUM LTD                    *
    '*          : All Rights Reserved                               *
    '*  Date    : 27/12/2019                                        *
    '*  Version : 1.0                                               *
    '*  Notes   :                                                   *
    '*          :                                                   *
    '****************************************************************
    ;-------------------------------------------------------------------------------
    
    
    Device = 18F24Q10
    
    
    Config_Start
      FEXTOSC = OFF	                  ; Oscillator not enabled
      RSTOSC = HFINTOSC_64MHZ	      ; HFINTOSC with HFFRQ = 64 MHz and CDIV = 1:1
      CLKOUTEN = OFF	              ; CLKOUT function is disabled
      CSWEN = On	                  ; Writing to NOSC and NDIV is allowed
      FCMEN = On	                  ; Fail-Safe Clock Monitor enabled
      MCLRE = INTMCLR	              ; If LVP = 0, MCLR pin (RE3) is an input; If LVP =1, MCLR pin (RE3) is MCLR
      PWRTE = OFF	                  ; Power up timer disabled
      LPBOREN = On	                  ; Low power BOR is enabled
      BOREN = SBORDIS	              ; Brown-out Reset enabled , SBOREN bit is ignored
    '  BORV = VBOR_2P45	              ; Brown-out Reset Voltage (VBOR) set to 2.45V
      ZCD = OFF	                      ; ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCON
      PPS1WAY = On	                  ; PPSLOCK bit can be cleared and set only once; PPS registers remain locked after one clear/set cycle
      STVREN = On	                  ; Stack full/underflow will cause Reset
     ' Debug = OFF	                  ; Background debugger disabled
     ' XINST = OFF	                  ; Extended Instruction Set and Indexed Addressing Mode disabled
      WDTCPS = WDTCPS_31	          ; Divider ratio 1:65536; software control of WDTPS
      WDTE = On  	                  ; WDT ON
      WDTCWS = WDTCWS_7	              ; window always open (100%); software control; keyed access not required
      WDTCCS = SC	                  ; Software Control
      WRT0 = OFF	                  ; Block 0 (000800-001FFFh) not write-protected
      WRT1 = OFF	                  ; Block 1 (002000-003FFFh) not write-protected
      WRTC = OFF	                  ; Configuration registers (300000-30000Bh) not write-protected
      WRTB = OFF	                  ; Boot Block (000000-0007FFh) not write-protected
      WRTD = OFF	                  ; Data EEPROM not write-protected
      SCANE = On	                  ; Scanner module is available for use, SCANMD bit can control the module
      LVP = On	                      ; Low voltage programming enabled. MCLR/VPP pin function is MCLR. MCLRE configuration bit is ignored
      Cp = OFF	                      ; UserNVM code protection disabled
      CPD = OFF	                      ; DataNVM code protection disabled
      EBTR0 = OFF	                  ; Block 0 (000800-001FFFh) not protected from table reads executed in other blocks
      EBTR1 = OFF	                  ; Block 1 (002000-003FFFh) not protected from table reads executed in other blocks
      EBTRB = OFF	                  ; Boot Block (000000-0007FFh) not protected from table reads executed in other blocks
      Config_End
    
    
    ;**** End of Fuse Configurator Settings ****
    ;-------------------------------------------------------------------------------
    
    
    Xtal = 64                               ; PLL at full speed (62.5nS/instruction)
    Declare Watchdog = On
    On_Hardware_Interrupt GoTo GET_SERIAL
    ;----------------------   PORT ASSIGNMENT    -------------------------------------
    Dim TX_USB    As PORTC.6                ; Digital O/P 
    Dim RX_USB    As PORTC.7                ; Digital O/P 
    
    
    ;----------------------   VARIABLE DECLARATIONS    -------------------------
    Dim Temp As Byte                        ; Totally tempory byte
    Dim RXBUFF[100] As Byte                 ; Create Array
    Dim RX_Pntr As Byte
    Dim MSGRXD As Bit                       ; FLAG A MESSAGE IN BUFFER
    ;---------------------------- ALIAS -----------------------------------------
    Dim RXINT_EN As PIR3.5
    Dim GIE_EN  As INTCON.7
    Dim TMR0_INTF As PIR0.5 = 0              ; TIMER0 INTERRUPT FLAG
    Dim TMR0_INT_EN As PIE0.5 = 1            ; TIMER0 INTERRUPT ENABLE
    
    
    ;-------------------------------------------------------------------------------------       
    ;                          SET UP PIC  
    ;-------------------------------------------------------------------------------------
    
    
            INTCON =    %11000000      ; enable interrupts
            PIE3 =      %00100000      ; Enable Receive Interrupt
            
    ; WATCHDOG SETUP
            WDTCON0 =   %00010011      ; WDT 512ms, Watchdog on
            'WDTCON0 =   %00010000     ; WDT 512ms, Watchdog FF        
            WDTCON1 =   %00000111      ; LF OSC (31kHz) window open 100%
                    
    ;OSCILLATOR SETUP
            OSCCON1 =   %01100000      ; Internal Osc 64MHz
            OSCFRQ =    %00001000      ; 64MHz 
            CLKRCON =   %00000000      ; disable Reference clock
            
    ; PORT CONFIGURATION
            ANSELA =    %01100011      ; RA0, RA1, RA5, RA6 as Analog
            ANSELB =    %00000000
            ANSELC =    %00000000
            WPUA   =    %00000000     ; Pullups disabled   
            WPUB   =    %00000000     ; Pullups disabled
            WPUC   =    %00000000     ; Pullups disabled
            ODCONA =    %00000000     ; Open Drain Disabled
            ODCONB =    %00000000     ; Open Drain Disabled
            ODCONC =    %00000000     ; Open Drain Disabled  
            SLRCONA =   %11111111     ; Slew Limited (reduce EMI) 
            SLRCONB =   %11111111     ; Slew Limited (reduce EMI) 
            SLRCONC =   %11111111     ; Slew Limited (reduce EMI) 
            INLVLA =    %11111111     ; ST INPUTS
            INLVLB =    %11111111     ; ST INPUTS
            INLVLC =    %11111111     ; ST INPUTS                                        
            TRISA =     %01100011     ; PORT DIRECTION REGISTERS
            TRISB =     %00000000     ;
            TRISC =     %10000000     ;
            
    ; ADC CONFIG
            ADCON0 =    %00000100     ; ADC OFF, ADC cleared, CLK FOSC, Right Justified
    ; DAC CONFIG
            DAC1CON0 =  %00000000     ; DAC OFF  
    ; Fixed Voltage Reference Config
            FVRCON =    %10110010     ; REF on, TEMP IND ON, Low Range, Comp Buffer off, ADC Buffer on 2.048V 
    ; Pheripheral Module Disable
            PMD0 =      %00010000     ; Disable CRC Module
            PMD1 =      %11100000     ; Timer 0,1,2,3,4 enabled, rest disabled
            PMD2 =      %01000111     ; DAC, Comparator and ZCD Disabled, ADC on
            PMD3 =      %00001100     ; PWM3,4 Disabled. CCP1 & 2 on
            PMD4 =      %00010001     ; MSSP & CWG Disabled, UART on
            PMD5 =      %00000001     ; Data signal modulator Disabled
    ; TIMER SETUP
            T0CON0 =    %10000000     ; TIMER0 on, 8-Bit and 1:1 Post 
            T0CON1 =    %01011011     ; CLOCK SOURCE = fOSC/4, SYNC, 1:2048 PRESCALAR (32mS per rollover)
            T2CON  =    %10000000     ; Timer on prescaler and postscaler 0
            T4CON  =    %10000000     ; Timer on prescaler and postscaler 0 
            T2HLT =     %10100000     ; Free Run Timer, prescaler and CLK Syncro
            T4HLT =     %10100000     ; Free Run Timer, prescaler And CLK Syncro        
            T2CLKCON =  %00000001     ; Fosc/4
            T4CLKCON =  %00000001     ; Fosc/4   
            T2PR     =  %11111111     ; Set Timer period PWM to 62.5kHz   
            T4PR     =  %11111111     ; Set Timer period PWM to 62.5kHz                   
    ; Serial Port
            SP1BRGH =   %00000000
            SP1BRGL =   34
            TX1STA  =   %00100100
            RC1STA =    %10010000
            BAUD1CON =  %00000000
    ;pps registers        
            RC1PPS = $06 
            RC2PPS = $05            
            RX1PPS = $17
            RC7PPS = $17
            RC6PPS = $09         
                      
            GoTo START                   ; JUMP TO SEXY BIT
    
    
    
    
    '*************************************************************************************
    '                              Interrupt 
    '*************************************************************************************             
    GET_SERIAL:
            Context Save ' Save any variables or SFRs before the interrupt starts
            GIE_EN = 0                            ; DISABLE INT
            
    ;RECEIVE INTERUPT (MUST BE FIRST)                    
            If PIR3.5 = 1 Then                    ; UART INTERRUPT?
                RXBUFF[RX_Pntr] = RC1REG          ; YUP SO PROCESS..
                TMR0L = 0                         ; CLEAR TIMER REGISTER ON EVERY RECEIVE
                TMR0_INT_EN = 1                   ; ENABLE TIMER INTERRUPT
                If RXBUFF[RX_Pntr] = "#" Then     ; SEARCH FOR TERMINATOR "#"           
                    MSGRXD = 1                    ; YES SIR MESSAGE OK, PROCEED FOR DECODE...
                    TMR0_INT_EN = 0               ; DISABLE TIMER 0 INTERRUPT AS TERMINATOR FOUND                
                EndIf
                Inc RX_Pntr                       ; NO TERMINATOR SO CLOCK IN ANOTHER BYTE
                GoTo EXIT_INT
            EndIf      
    ;TIMER INTERUPT AFTER UART INTERUPT           ; THIS RUNS WHILE RX BIT RECIVED BAND "#" NOT RECIVED  
             If TMR0_INTF = 1 Then                ; TEST INTERRUPT FLAG, DO IF TIMER TIMEOUT
                 RX_Pntr = 0                      ; BASICALLY IF TIMER TIMES OUT WE RESET POINTER TO TRASH THE MESSAGE AND AVOID OVERFLOW
                 TMR0_INT_EN = 0                  ; DISABLE TIMER 0 INTERRUPT...  MESSAGE IN THE BIN           
             EndIf                                ; CATCHES ANY ODD CHARACTERS WITHOUT "#" TERMINATOR      
                       
    EXIT_INT: 
            TMR0_INTF = 0                         ; CLEAR THE INTERRUPT FLAG       
            GIE_EN = 1
            Context Restore                       ; Restore any variables or SFRs and exit the interrupt      
        
    ;-------------------------------------------- SUBS ------------------------------------------------ 
    '*************************************************************************************
    ' 
    ' Name:                 "CTX"
    ' Passes:    
    ' Returns:              NOTHING
    ' Description            WAIT FOR TX BYTE TO GO..
    '*************************************************************************************  
    CTX:
            While PIR3.4 = 0:Wend
            Return
    '*************************************************************************************
    ' 
    ' Name:                 "SEND_BUFFER"
    ' Passes:    
    ' Returns:              NOTHING
    ' Description           STREAM RX BUFFER OUT
    '*************************************************************************************         
    SEND_BUFFER:  
            For Temp = 0 To (RX_Pntr - 1)
                TX1REG = RXBUFF[Temp]
                Call CTX                      ; CAN INTERRUPT IF MORE SPEED NEEDED
            Next
            RX_Pntr = 0
            MSGRXD = 0
            Return
            
            
    ;----------------------------------------------------------------------------------------------------        
    ;------------------------------------------ CODE ENTRY ---------------------------------------------- 
    ;----------------------------------------------------------------------------------------------------               
    START:  
            MSGRXD = 0
            
    MLOOP:
            If MSGRXD = 1 Then
                  'Call SEND_BUFFER                         ; Send out Received for debug (without Hrsout)
                  GoTo PROCESS                              ; Process the received message
            EndIf
            Clrwdt
            GoTo MLOOP     
                                               
    PROCESS:
                Select RXBUFF[0]
                
                        Case "A"
                            If RXBUFF [1] = "1" Then         ' Endless case select, call Subs to keep tight and easy to read
                                                             'DO SOMETHING
                            EndIf
                            
                        Case "B"
                                                             'DO SOMETHING            
                
                End Select
                GoTo START    
                                                                    
                End
    Enjoy

    Jon
    Last edited by JonW; 28th December 2019 at 01:23.

Attention

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

The advertisements we display are relevant to this web site and your browsing history

Please consider supporting us by disabling your ad blocker.


Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues


Thank you for your attention.

  • #2
    Fanatical Contributor Tim's Avatar
    Join Date
    Jan 2003
    Posts
    7,682
    Thumbs Up
    Received: 47
    Given: 95
    Total Downloaded
    1.32 GB

    0 Not allowed!

    Default Re: PIC18F24Q10 interrupt serial routine with terminator

    Hi Looks really useful. Thanks for posting. Can I just recommend you add in a catch for buffer overrun. Its just a couple of lines of code but is 100% worth it.

    Usart_Int:
    If OERR = 1 Then GoTo USART_ERR ; Check for usart overrun




    USART_ERR: ; Handle a usart overrun error
    WREG = RCREG
    CREN = 0
    CREN = 1




    Tim

  • Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #3
    Prolific Poster John Drew's Avatar
    Join Date
    Feb 2002
    Posts
    2,829
    Thumbs Up
    Received: 89
    Given: 34
    Total Downloaded
    4.60 GB

    0 Not allowed!

    Default Re: PIC18F24Q10 interrupt serial routine with terminator

    Greetings Jon,
    Would you put this on the WIKI?
    Good info can be lost in the forum whereas in the WIKI it can be easily found. From memory there's a section on serial code.
    Cheers
    John

  • Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #4
    Senior Member tumbleweed's Avatar
    Join Date
    May 2011
    Posts
    495
    Thumbs Up
    Received: 59
    Given: 0
    Total Downloaded
    408.14 MB

    0 Not allowed!

    Default Re: PIC18F24Q10 interrupt serial routine with terminator

    In addition to Tim's comment I have a few...

    - Move the enable interrupts to the end of the setup
    - In the ISR, it would be a good idea to check that 'Inc RX_Pntr' never exceeds the size of buffer, otherwise you run the risk of trashing other variables
    - Remove the GIE_EN statements from the ISR. Interrupts are automatically disabled when the ISR runs, and re-enabled at the end when the RETFIE instruction is executed (which is part of 'ContextRestore'). It's a bad idea to manipulate that bit yourself.

  • Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #5
    Junior Member JonW's Avatar
    Join Date
    Dec 2019
    Posts
    11
    Thumbs Up
    Received: 1
    Given: 0
    Total Downloaded
    154.49 MB

    0 Not allowed!

    Default Re: PIC18F24Q10 interrupt serial routine with terminator

    Good point Tim!


    Cheers
    Jon

  • Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #6
    Junior Member JonW's Avatar
    Join Date
    Dec 2019
    Posts
    11
    Thumbs Up
    Received: 1
    Given: 0
    Total Downloaded
    154.49 MB

    1 Not allowed!

    Default Re: PIC18F24Q10 interrupt serial routine with terminator

    Thanks for the other comment Guys!. Will update and move to the Wiki

    Cheers

    Jon

    Code:
    '****************************************************************
    '*  Name    : FAST SERIAL.BAS                                      *
    '*  Author  : JON WALKER                                        *
    '*  Notice  : Copyright (c) 2019 ELECIUM LTD                    *
    '*          : All Rights Reserved                               *
    '*  Date    : 27/12/2019                                        *
    '*  Version : 1.0                                               *
    '*  Notes   :                                                   *
    '*          :                                                   *
    '****************************************************************
    ;-------------------------------------------------------------------------------
    
    
    Device = 18F24Q10
    
    
    Config_Start
      FEXTOSC = OFF                      ; Oscillator not enabled
      RSTOSC = HFINTOSC_64MHZ          ; HFINTOSC with HFFRQ = 64 MHz and CDIV = 1:1
      CLKOUTEN = OFF                  ; CLKOUT function is disabled
      CSWEN = On                      ; Writing to NOSC and NDIV is allowed
      FCMEN = On                      ; Fail-Safe Clock Monitor enabled
      MCLRE = INTMCLR                  ; If LVP = 0, MCLR pin (RE3) is an input; If LVP =1, MCLR pin (RE3) is MCLR
      PWRTE = OFF                      ; Power up timer disabled
      LPBOREN = On                      ; Low power BOR is enabled
      BOREN = SBORDIS                  ; Brown-out Reset enabled , SBOREN bit is ignored
    '  BORV = VBOR_2P45                  ; Brown-out Reset Voltage (VBOR) set to 2.45V
      ZCD = OFF                          ; ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCON
      PPS1WAY = On                      ; PPSLOCK bit can be cleared and set only once; PPS registers remain locked after one clear/set cycle
      STVREN = On                      ; Stack full/underflow will cause Reset
     ' Debug = OFF                      ; Background debugger disabled
     ' XINST = OFF                      ; Extended Instruction Set and Indexed Addressing Mode disabled
      WDTCPS = WDTCPS_31              ; Divider ratio 1:65536; software control of WDTPS
      WDTE = On                        ; WDT ON
      WDTCWS = WDTCWS_7                  ; window always open (100%); software control; keyed access not required
      WDTCCS = SC                      ; Software Control
      WRT0 = OFF                      ; Block 0 (000800-001FFFh) not write-protected
      WRT1 = OFF                      ; Block 1 (002000-003FFFh) not write-protected
      WRTC = OFF                      ; Configuration registers (300000-30000Bh) not write-protected
      WRTB = OFF                      ; Boot Block (000000-0007FFh) not write-protected
      WRTD = OFF                      ; Data EEPROM not write-protected
      SCANE = On                      ; Scanner module is available for use, SCANMD bit can control the module
      LVP = On                          ; Low voltage programming enabled. MCLR/VPP pin function is MCLR. MCLRE configuration bit is ignored
      Cp = OFF                          ; UserNVM code protection disabled
      CPD = OFF                          ; DataNVM code protection disabled
      EBTR0 = OFF                      ; Block 0 (000800-001FFFh) not protected from table reads executed in other blocks
      EBTR1 = OFF                      ; Block 1 (002000-003FFFh) not protected from table reads executed in other blocks
      EBTRB = OFF                      ; Boot Block (000000-0007FFh) not protected from table reads executed in other blocks
      Config_End
    
    
    ;**** End of Fuse Configurator Settings ****
    ;-------------------------------------------------------------------------------
    
    
    Xtal = 64                               ; PLL at full speed (62.5nS/instruction)
    Declare Watchdog = On
    On_Hardware_Interrupt GoTo GET_SERIAL
    ;----------------------   PORT ASSIGNMENT    -------------------------------------
    Dim TX_USB    As PORTC.6                ; Digital O/P 
    Dim RX_USB    As PORTC.7                ; Digital O/P 
    
    
    ;----------------------   VARIABLE DECLARATIONS    -------------------------
    Dim Temp As Byte                        ; Totally tempory byte
    Dim RXBUFF[100] As Byte                 ; Create Array
    Dim RX_Pntr As Byte
    Dim MSGRXD As Bit                       ; FLAG A MESSAGE IN BUFFER
    ;---------------------------- ALIAS -----------------------------------------
    Dim RXINT_EN As PIR3.5
    Dim GIE_EN  As INTCON.7
    Dim TMR0_INTF As PIR0.5 = 0              ; TIMER0 INTERRUPT FLAG
    Dim TMR0_INT_EN As PIE0.5 = 1            ; TIMER0 INTERRUPT ENABLE
    
    
    ;-------------------------------------------------------------------------------------       
    ;                          SET UP PIC  
    ;-------------------------------------------------------------------------------------
    
    
            INTCON =    %01000000      ; Disable interrupts
            PIE3 =      %00100000      ; Enable Receive Interrupt
            
    ; WATCHDOG SETUP
            WDTCON0 =   %00010011      ; WDT 512ms, Watchdog on
            'WDTCON0 =   %00010000     ; WDT 512ms, Watchdog FF        
            WDTCON1 =   %00000111      ; LF OSC (31kHz) window open 100%
                    
    ;OSCILLATOR SETUP
            OSCCON1 =   %01100000      ; Internal Osc 64MHz
            OSCFRQ =    %00001000      ; 64MHz 
            CLKRCON =   %00000000      ; disable Reference clock
            
    ; PORT CONFIGURATION
            ANSELA =    %01100011      ; RA0, RA1, RA5, RA6 as Analog
            ANSELB =    %00000000
            ANSELC =    %00000000
            WPUA   =    %00000000     ; Pullups disabled   
            WPUB   =    %00000000     ; Pullups disabled
            WPUC   =    %00000000     ; Pullups disabled
            ODCONA =    %00000000     ; Open Drain Disabled
            ODCONB =    %00000000     ; Open Drain Disabled
            ODCONC =    %00000000     ; Open Drain Disabled  
            SLRCONA =   %11111111     ; Slew Limited (reduce EMI) 
            SLRCONB =   %11111111     ; Slew Limited (reduce EMI) 
            SLRCONC =   %11111111     ; Slew Limited (reduce EMI) 
            INLVLA =    %11111111     ; ST INPUTS
            INLVLB =    %11111111     ; ST INPUTS
            INLVLC =    %11111111     ; ST INPUTS                                        
            TRISA =     %01100011     ; PORT DIRECTION REGISTERS
            TRISB =     %00000000     ;
            TRISC =     %10000000     ;
            
    ; ADC CONFIG
            ADCON0 =    %00000100     ; ADC OFF, ADC cleared, CLK FOSC, Right Justified
    ; DAC CONFIG
            DAC1CON0 =  %00000000     ; DAC OFF  
    ; Fixed Voltage Reference Config
            FVRCON =    %10110010     ; REF on, TEMP IND ON, Low Range, Comp Buffer off, ADC Buffer on 2.048V 
    ; Pheripheral Module Disable
            PMD0 =      %00010000     ; Disable CRC Module
            PMD1 =      %11100000     ; Timer 0,1,2,3,4 enabled, rest disabled
            PMD2 =      %01000111     ; DAC, Comparator and ZCD Disabled, ADC on
            PMD3 =      %00001100     ; PWM3,4 Disabled. CCP1 & 2 on
            PMD4 =      %00010001     ; MSSP & CWG Disabled, UART on
            PMD5 =      %00000001     ; Data signal modulator Disabled
    ; TIMER SETUP
            T0CON0 =    %10000000     ; TIMER0 on, 8-Bit and 1:1 Post 
            T0CON1 =    %01011011     ; CLOCK SOURCE = fOSC/4, SYNC, 1:2048 PRESCALAR (32mS per rollover)
            T2CON  =    %10000000     ; Timer on prescaler and postscaler 0
            T4CON  =    %10000000     ; Timer on prescaler and postscaler 0 
            T2HLT =     %10100000     ; Free Run Timer, prescaler and CLK Syncro
            T4HLT =     %10100000     ; Free Run Timer, prescaler And CLK Syncro        
            T2CLKCON =  %00000001     ; Fosc/4
            T4CLKCON =  %00000001     ; Fosc/4   
            T2PR     =  %11111111     ; Set Timer period PWM to 62.5kHz   
            T4PR     =  %11111111     ; Set Timer period PWM to 62.5kHz                   
    ; Serial Port
            SP1BRGH =   %00000000
            SP1BRGL =   34
            TX1STA  =   %00100100
            RC1STA =    %10010000
            BAUD1CON =  %00000000
    ;pps registers        
            RC1PPS = $06 
            RC2PPS = $05            
            RX1PPS = $17
            RC7PPS = $17
            RC6PPS = $09         
            INTCON =    %11000000      ; Enable interrupts                  
            GoTo START                   ; JUMP TO SEXY BIT
    
    
    
    
    '*************************************************************************************
    '                              Interrupt 
    '*************************************************************************************             
    GET_SERIAL:
            Context Save ' Save any variables or SFRs before the interrupt starts
            
    ;RECEIVE INTERUPT (MUST BE FIRST)             
            If RX_Pntr = 99 Then                  ; STOP BUFFER OVERLOWING
               GoTo EXIT_WITH_RESET
            EndIf    
            
            If RC1STA.1 = 1 Then                   ; IF BUFFER OVERUN THEN CANCEL MESSAGE AND RESET UART                       
               RC1STA.7 = 0                        ; RESET UART TRASH DATA 
               RC1STA.7 = 1  
               GoTo EXIT_WITH_RESET   
            EndIf      
            
            If PIR3.5 = 1 Then                    ; UART INTERRUPT?
                RXBUFF[RX_Pntr] = RC1REG          ; YUP SO PROCESS..
                TMR0L = 0                         ; CLEAR TIMER REGISTER ON EVERY RECEIVE
                TMR0_INT_EN = 1                   ; ENABLE TIMER INTERRUPT
                If RXBUFF[RX_Pntr] = "#" Then     ; SEARCH FOR TERMINATOR "#"           
                    MSGRXD = 1                    ; YES SIR MESSAGE OK, PROCEED FOR DECODE...
                    TMR0_INT_EN = 0               ; DISABLE TIMER 0 INTERRUPT AS TERMINATOR FOUND                
                EndIf
                Inc RX_Pntr                       ; NO TERMINATOR SO CLOCK IN ANOTHER BYTE
                GoTo EXIT_INT
            EndIf      
    ;TIMER INTERUPT AFTER UART INTERUPT           ; THIS RUNS WHILE RX BIT RECIVED BAND "#" NOT RECIVED  
             If TMR0_INTF = 1 Then                ; TEST INTERRUPT FLAG, DO IF TIMER TIMEOUT          
    EXIT_WITH_RESET:
                 TMR0_INT_EN = 0                  ; DISABLE TIMER 0 INTERRUPT...  MESSAGE IN THE BIN 
                 MSGRXD = 0                       ; CANCEL MESSAGE
                 RX_Pntr = 0                      ; RESET BUFFER      
            EndIf                 
    EXIT_INT: 
            TMR0_INTF = 0                         ; CLEAR THE INTERRUPT FLAG       
            Context Restore                       ; Restore any variables or SFRs and exit the interrupt 
    ;-------------------------------------------- SUBS ------------------------------------------------ 
    '*************************************************************************************
    ' 
    ' Name:                 "CTX"
    ' Passes:    
    ' Returns:              NOTHING
    ' Description            WAIT FOR TX BYTE TO GO..
    '*************************************************************************************  
    CTX:
            While PIR3.4 = 0:Wend
            Return
    '*************************************************************************************
    ' 
    ' Name:                 "SEND_BUFFER"
    ' Passes:    
    ' Returns:              NOTHING
    ' Description           STREAM RX BUFFER OUT
    '*************************************************************************************         
    SEND_BUFFER:  
            For Temp = 0 To (RX_Pntr - 1)
                TX1REG = RXBUFF[Temp]
                Call CTX                      ; CAN INTERRUPT IF MORE SPEED NEEDED
            Next
            RX_Pntr = 0
            MSGRXD = 0
            Return
            
            
    ;----------------------------------------------------------------------------------------------------        
    ;------------------------------------------ CODE ENTRY ---------------------------------------------- 
    ;----------------------------------------------------------------------------------------------------               
    START:  
            MSGRXD = 0
            
    MLOOP:
            If MSGRXD = 1 Then
                  'Call SEND_BUFFER                         ; Send out Received for debug (without Hrsout)
                  GoTo PROCESS                              ; Process the received message
            EndIf
            Clrwdt
            GoTo MLOOP     
                                               
    PROCESS:
                Select RXBUFF[0]
                
                        Case "A"
                            If RXBUFF [1] = "1" Then         ' Endless case select, call Subs to keep tight and easy to read
                                                             'DO SOMETHING
                            EndIf
                            
                        Case "B"
                                                             'DO SOMETHING            
                
                End Select
                GoTo START    
                                                                    
                End
    Last edited by JonW; 29th December 2019 at 16:24.

  • Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

    Thread Information

    Users Browsing this Thread

    There are currently 1 users browsing this thread. (0 members and 1 guests)

       

    Similar Threads

    1. Interrupt during delay routine
      By VaGyver in forum Proton Plus Compiler v3
      Replies: 12
      Last Post: 31st January 2014, 10:05
    2. Gosub in interrupt routine
      By basparky in forum Proton Plus Compiler v3
      Replies: 5
      Last Post: 27th December 2012, 17:32
    3. No delay in interrupt routine
      By Derk vd Wal in forum Interrupt Sources
      Replies: 4
      Last Post: 21st March 2011, 16:59
    4. Adin in interrupt routine
      By basparky in forum Proton Plus Compiler v3
      Replies: 8
      Last Post: 10th November 2010, 05:52
    5. [SOLVED !] Clearing Interrupt flag in interrupt service routine - is it necessary ?
      By Kapitan in forum Proton Plus Compiler v3
      Replies: 12
      Last Post: 18th January 2009, 22:59

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts