News:

;) This forum is the property of Proton software developers

Main Menu

Interrupt priority

Started by Peter Truman, Sep 18, 2024, 07:53 AM

Previous topic - Next topic

Peter Truman

Hi All

PIC 18F25k22

I'm trying to use 2 USARTS and timer 0 simultaneously, all in an ISR, but, I find that my serial ports are corrupted by the timer interrupt.

So I thought I'd try implementing priority interrupts with the USARTs both having a high priority and the timer a low priority

But I'm running into some trouble.

Looking at the attached code, having TX1IE and TX2IE set to 1 nothing runs at all, setting these to 0 at least I have some output from the serial port - but no sign of a timer input.

(This is the first time I've tried to use priority interrupts)

Can anyone offer some guidance? Many thanks in advance (I've included all of my register definitions)

Device = 18F25K22

Config_Start
  FOSC = INTIO67    ;Internal oscillator block
  PLLCFG = On    ;Oscillator used directly
  PRICLKEN = On    ;Primary clock enabled
  FCMEN = OFF    ;Fail-Safe Clock Monitor disbled
  IESO = On   ;Oscillator Switchover mode disabled
  PWRTEN = On    ;Power up timer enabled
  BOREN = SBORDIS    ;Brown-out Reset enabled in hardware only (SBOREN is disabled)
  BORV = 190    ;VBOR set to 1.90 V nominal
  WDTEN = On    ;WDT is always enabled. SWDTEN bit has no effect
  WDTPS = 1024    ;1:32768
  CCP2MX = PORTC1    ;CCP2 input/output is multiplexed with RC1
  PBADEN = OFF    ;PORTB<5:0> pins are configured as digital I/O on Reset
  CCP3MX = PORTB5    ;P3A/CCP3 input/output is multiplexed with RB5
  HFOFST = On    ;HFINTOSC output and ready status are not delayed by the oscillator stable status
  T3CMX = PORTC0    ;T3CKI is on RC0
  P2BMX = PORTB5    ;P2B is on RB5
  MCLRE = EXTMCLR    ;MCLR pin enabled, RE3 input pin disabled
  STVREN = On    ;Stack full/underflow will cause Reset
  LVP = off    ;Single-Supply ICSP disabled
  XINST = OFF    ;Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
  Debug = OFF    ;Disabled
  Cp0 = OFF    ;Block 0 (000800-001FFFh) not code-protected
  CP1 = OFF    ;Block 1 (002000-003FFFh) not code-protected
  CP2 = OFF    ;Block 2 (004000-005FFFh) not code-protected
  CP3 = OFF    ;Block 3 (006000-007FFFh) not code-protected
  CPB = OFF    ;Boot block (000000-0007FFh) not code-protected
  CPD = OFF    ;Data EEPROM not code-protected
  WRT0 = OFF    ;Block 0 (000800-001FFFh) not write-protected
  WRT1 = OFF    ;Block 1 (002000-003FFFh) not write-protected
  WRT2 = OFF    ;Block 2 (004000-005FFFh) not write-protected
  WRT3 = OFF    ;Block 3 (006000-007FFFh) not write-protected
  WRTC = OFF    ;Configuration registers (300000-3000FFh) not write-protected
  WRTB = OFF    ;Boot Block (000000-0007FFh) not write-protected
  WRTD = OFF    ;Data EEPROM not write-protected
  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
  EBTR2 = OFF    ;Block 2 (004000-005FFFh) not protected from table reads executed in other blocks
  EBTR3 = OFF    ;Block 3 (006000-007FFFh) 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

OSCCON.6=1
OSCCON.5=1
OSCCON.4=1
OSCTUNE.6=1                                                                                 'disable the 4x PLLEN

Xtal = 64

PinPullup Enable,PORTB                                                                      'Turn on Port B Pullups


'Define the Pins
Symbol _Signal =   PORTA.5
Symbol _Sw5v =     PORTB.0
Symbol _Option_1 = PORTB.1
Symbol _Option_2 = PORTB.2
Symbol _Switch =   PORTB.3
Symbol _Button =   PORTB.4
Symbol _Led =      PORTB.5


TRISA=%11100000
TRISB=%11011111
TRISC=%10001000

'Declare Create_Coff=True
'setup the I2C comms
Declare SDA_Pin PORTC.4                                                                     '12C declares
Declare SCL_Pin PORTC.3
Declare Slow_Bus On


All_Digital=True                                                                            'start at digital


'Setup USART 1 (Tlit Module)
Declare Hserial1_Baud = 14400
Declare Hserial1_Clear = 1                                                                  ' Enable Error clearing on received characters
Declare HRSOut1_Pin = PORTC.6
Declare HRSIn1_Pin = PORTC.7

'Setup USART 2 (Real World)
Declare Hserial2_Baud = 14400
Declare Hserial2_Clear = 1                                                                  ' Enable Error clearing on received characters
Declare HRSOut2_Pin = PORTB.6
Declare HRSIn2_Pin = PORTB.7
                                                               

'Register Definitions
'INTCON1
Symbol          GIEH    = INTCON.7 '}                                                       'vs GIE
Symbol          GIE     = INTCON.7 '}                                                       'Global Int Enable
Symbol          GIEL    = INTCON.6 '}                                                     
Symbol          PEIE    = INTCON.6 '}                                                       'peripheral int enable
Symbol          TMR0IE  = INTCON.5                                                          'TMR0 Overflow Interrupt Enable
Symbol          INT0IE  = INTCON.4                                                          'External Interrupt Enable bit         
Symbol          RBIE    = INTCON.3                                                          'Port B Interrupt-On-Change (IOCx) Interrupt Enable bit(
Symbol          TMR0IF  = INTCON.2                                                          'TMR0 Overflow Interrupt Flag bit
Symbol          INT0IF  = INTCON.1                                                          'External Interrupt Flag bit                   
Symbol          RBIF    = INTCON.0                                                          'Port B Interrupt-On-Change (IOCx) Interrupt Flag bit

'INTCON2
Symbol          RBPU    = INTCON2.7                                                         'PORTB Pull-up Enable bit
Symbol          INTEDG0 = INTCON2.6                                                         'External Interrupt 0 Edge Select bit
Symbol          INTEDG1 = INTCON2.5                                                         'External Interrupt 1 Edge Select bit
Symbol          INTEDG2 = INTCON2.4                                                         'External Interrupt 2 Edge Select bit
Symbol          TMR0IP  = INTCON2.2                                                         'TMR0 Overflow Interrupt Priority bit
Symbol          RBIP    = INTCON2.0                                                         'RB Port Change Interrupt Priority bit

'INTCON3
Symbol          INT2IP  = INTCON3.7                                                         'INT2 External Interrupt Priority bit         
Symbol          INT1IP  = INTCON3.6                                                         'INT1 External Interrupt Priority bit
Symbol          INT2IE  = INTCON3.4                                                         'INT2 External Interrupt Enable bit
Symbol          INT1IE  = INTCON3.3                                                         'INT1 External Interrupt Enable bit
Symbol          INT2IF  = INTCON3.1                                                         'INT2 External Interrupt Flag bit
Symbol          INT1IF  = INTCON3.0                                                         'INT1 External Interrupt Flag bit

'IPR1
Symbol          ADIP    = IPR1.6                                                            'A/D Converter Interrupt Priority bit
Symbol          RC1IP   = IPR1.5                                                            'EUSART1 Receive Interrupt Priority bit 
Symbol          TX1IP   = IPR1.4
Symbol          SSP1IP  = IPR1.3                                                            'Host Synchronous Serial Port 1 Interrupt Priority bit
Symbol          CCP1IP  = IPR1.2                                                            'CCP1 Interrupt Priority bit
Symbol          TMR2IP  = IPR1.1                                                            'TMR2 to PR2 Match Interrupt Priority bit
Symbol          TMR1IP  = IPR1.0                                                            'TMR1 Overflow Interrupt Priority bit       

'IPR2
Symbol          OSCFIP  = IPR2.7                                                            'Oscillator Fail Interrupt Priority bit
Symbol          C1IP    = IPR2.6                                                            'Comparator C1 Interrupt Priority bit
Symbol          C2IP    = IPR2.5                                                            'Comparator C2 Interrupt Priority bit
Symbol          EEIP    = IPR2.4                                                            'Data EEPROM/Flash Write Operation Interrupt Priority bit
Symbol          BCL1IP  = IPR2.3                                                            'MSSP1 Bus Collision Interrupt Priority bit
Symbol          HLVDIP  = IPR2.2                                                            'Low-Voltage Detect Interrupt Priority bit
Symbol          TMR3IP  = IPR2.1                                                            'TMR3 Overflow Interrupt Priority bit
Symbol          CCP2IP  = IPR2.0                                                            'CCP2 Interrupt Priority bit

'IPR3
Symbol          SSP2IP  = IPR3.7                                                            'Synchronous Serial Port 2 Interrupt Priority bit
Symbol          BCL2IP  = IPR3.6                                                            'Bus Collision 2 Interrupt Priority bit
Symbol          RC2IP   = IPR3.5                                                            'EUSART2 Receive Interrupt Priority bit
Symbol          TX2IP   = IPR3.4                                                            'EUSART2 Transmit Interrupt Priority bit
Symbol          CTMUIP  = IPR3.3                                                            'CTMU Interrupt Priority bit
Symbol          TMR5GIP = IPR3.2                                                            'TMR5 Gate Interrupt Priority bit
Symbol          TMR3GIP = IPR3.1                                                            'TMR3 Gate Interrupt Priority bit
Symbol          TMR1GIP = IPR3.0                                                            'TMR1 Gate Interrupt Priority bit

'IPR4
Symbol          CCP5IP  = IPR4.2                                                            'CCP5 Interrupt Priority bit
Symbol          CCP4IP  = IPR4.1                                                            'CCP4 Interrupt Priority bit
Symbol          CCP3IP  = IPR4.0                                                            'CCP3 Interrupt Priority bit

'IPR5
Symbol          TMR6IP  = IPR5.2                                                            'TMR6 to PR6 Match Interrupt Priority bit
Symbol          TMR5IP  = IPR5.1                                                            'TMR5 Overflow Interrupt Priority bit
Symbol          TMR4IP  = IPR5.0                                                            'TMR4 to PR4 Match Interrupt Priority bit

'PIR1
Symbol          ADIF    = PIR1.6                                                            'A/D Converter Interrupt Flag bit
Symbol          RC1IF   = PIR1.5                                                            'EUSART1 Receive Interrupt Flag bit
Symbol          TX1IF   = PIR1.4                                                            'EUSART1 Transmit Interrupt Flag bit
Symbol          SSP1IF  = PIR1.3                                                            'Host Synchronous Serial Port 1 Interrupt Flag bit
Symbol          CCP1IF  = PIR1.2                                                            'CCP1 Interrupt Flag bit
Symbol          TMR2IF  = PIR1.1                                                            'TMR2 to PR2 Match Interrupt Flag bit
Symbol          TMR1IF  = PIR1.0                                                            'TMR1 Overflow Interrupt Flag bit

'PIR2           
Symbol          OSCFIF  = PIR2.7                                                            'Oscillator Fail Interrupt Flag bit
Symbol          C1IF    = PIR2.6                                                            'Comparator C1 Interrupt Flag bit
Symbol          C2IF    = PIR2.5                                                            'Comparator C2 Interrupt Flag bit
Symbol          EEIF    = PIR2.4                                                            'Data EEPROM/Flash Write Operation Interrupt Flag bit
Symbol          BCL1IF  = PIR2.3                                                            'MSSP1 Bus Collision Interrupt Flag bit
Symbol          HLVDIF  = PIR2.2                                                            'Low-Voltage Detect Interrupt Flag bit
Symbol          TMR3IF  = PIR2.1                                                            'TMR3 Overflow Interrupt Flag bit
Symbol          CCP2IF  = PIR2.0                                                            'CCP2 Interrupt Flag bit

'PIR3
Symbol          SSP2IF  = PIR3.7                                                            'Synchronous Serial Port Interrupt Flag bit
Symbol          BCL2IF  = PIR3.6                                                            'MSSP2 Bus Collision Interrupt Flag bit
Symbol          RC2IF   = PIR3.5                                                            'EUSART2 Receive Interrupt Flag bit
Symbol          TX2IF   = PIR3.4                                                            'EUSART2 Transmit Interrupt Flag bit
Symbol          CTMUIF  = PIR3.3                                                            'CTMU Interrupt Flag bit
Symbol          TMR5GIF = PIR3.2                                                            'TMR5 Gate Interrupt Flag bits
Symbol          TMR3GIF = PIR3.1                                                            'TMR3 Gate Interrupt Flag bits
Symbol          TMR1GIF = PIR3.0                                                            'TMR1 Gate Interrupt Flag bits

'PIR4
Symbol          CCP5IF  = PIR4.2                                                            'CCP5 Interrupt Flag bits
Symbol          CCP4IF  = PIR4.1                                                            'CCP4 Interrupt Flag bits
Symbol          CCP3IF  = PIR4.0                                                            'ECCP3 Interrupt Flag bits
         
'PIR5
Symbol          TMR6IF  = PIR5.2                                                            'TMR6 to PR6 Match Interrupt Flag bit
Symbol          TMR5IF  = PIR5.1                                                            'TMR5 Overflow Interrupt Flag bit
Symbol          TMR4IF  = PIR5.0                                                            'TMR4 to PR4 Match Interrupt Flag bit

'PIE1
Symbol          ADIE    = PIE1.6                                                            'A/D Converter Interrupt Enable bit
Symbol          RC1IE   = PIE1.5                                                            'EUSART1 Receive Interrupt Enable bit
Symbol          TX1IE   = PIE1.4                                                            'EUSART1 Transmit Interrupt Enable bit
Symbol          SSP1IE  = PIE1.3                                                            'Host Synchronous Serial Port 1 Interrupt Enable bit
Symbol          CCP1IE  = PIE1.2                                                            'CCP1 Interrupt Enable bit         
Symbol          TMR2IE  = PIE1.1                                                            'TMR2 to PR2 Match Interrupt Enable bit
Symbol          TMR1IE  = PIE1.0                                                            'TMR1 Overflow Interrupt Enable bit

'PIE2
Symbol          OSCFIE  = PIE2.7                                                            'Oscillator Fail Interrupt Enable bit
Symbol          C1IE    = PIE2.6                                                            'Comparator C1 Interrupt Enable bit
Symbol          C2IE    = PIE2.5                                                            'Comparator C2 Interrupt Enable bit
Symbol          EEIE    = PIE2.4                                                            'Data EEPROM/Flash Write Operation Interrupt Enable bit
Symbol          BCL1IE  = PIE2.3                                                            'MSSP1 Bus Collision Interrupt Enable bit
Symbol          HLVDIE  = PIE2.2                                                            'Low-Voltage Detect Interrupt Enable bit
Symbol          TMR3IE  = PIE2.1                                                            'TMR3 Overflow Interrupt Enable bit
Symbol          CCP2IE  = PIE2.0                                                            'CCP2 Interrupt Enable bit

'PIE3
Symbol          SSP2IE  = PIE3.7                                                            'Host Synchronous Serial Port 2 Interrupt Enable bit
Symbol          BCL2IE  = PIE3.6                                                            'Bus Collision Interrupt Enable bit
Symbol          RC2IE   = PIE3.5                                                            'EUSART2 Receive Interrupt Enable bit
Symbol          TX2IE   = PIE3.4                                                            'EUSART2 Transmit Interrupt Enable bit
Symbol          CTMUIE  = PIE3.3                                                            'CTMU Interrupt Enable bit
Symbol          TMR5GIE = PIE3.2                                                            'TMR5 Gate Interrupt Enable bit
Symbol          TMR3GIE = PIE3.1                                                            'TMR3 Gate Interrupt Enable bit
Symbol          TMR1GIE = PIE3.0                                                            'TMR1 Gate Interrupt Enable bit


'TXSTA
Symbol          CSRC    = TXSTA1.7                                                          'Clock Source Select bit
Symbol          TX9     = TXSTA1.6                                                          '9-bit Transmit Enable bit
Symbol          TXEN    = TXSTA1.5                                                          'Transmit Enable bit
Symbol          SYNC    = TXSTA1.4                                                          'EUSART Mode Select
Symbol          SENDB   = TXSTA1.3                                                          'Send Break Character bit
Symbol          BRGH    = TXSTA1.2                                                          'High Baud Rate Select bit
Symbol          TRMT    = TXSTA1.1                                                          'Transmit Shift Register Status bit
Symbol          TX9D    = TXSTA1.0                                                          'Ninth bit of Transmit Data

'RCSTA1
Symbol          SPEN    = RCSTA1.7                                                          'Serial Port Enable bit
Symbol          RX9     = RCSTA1.6                                                          '9-bit Receive Enable bit
Symbol          SREN    = RCSTA1.5                                                          'Single Receive Enable bit
Symbol          CREN1   = RCSTA1.4                                                          'Continuous Receive Enable bit
Symbol          ADDEN   = RCSTA1.3                                                          'Address Detect Enable bit
Symbol          FERR    = RCSTA1.2                                                          'Framing Error bit
Symbol          OERR    = RCSTA1.1                                                          'Overrun Error bit
Symbol          RX9D    = RCSTA1.0                                                          'Ninth bit of Received Data

'BAUDCONx
Symbol          ABDOVF  = BAUDCON1.7                                                        'Auto-Baud Detect Overflow bit         
Symbol          RCIDL   = BAUDCON1.6                                                        'Receive Idle Flag bit
Symbol          DTRXP   = BAUDCON1.5                                                        'Data/Receive Polarity Select bit
Symbol          CKTXP   = BAUDCON1.4                                                        'Clock/Transmit Polarity Select bit
Symbol          BRG16   = BAUDCON1.3                                                        '16-bit Baud Rate Generator bit
Symbol          WUE     = BAUDCON1.1                                                        'Wake-up Enable bit
Symbol          ABDEN   = BAUDCON1.0                                                        'Auto-Baud Detect Enable bit

'RCON
Symbol          IPEN    = RCON.7                                                            'Interrupt Priority Enable bit
Symbol          SBOREN  = RCON.6                                                            'BOR Software Enable bit
Symbol          RI      = RCON.4                                                            'RESET Instruction Flag bit
Symbol          TOf     = RCON.3                                                            'Watchdog Time-out Flag bit
Symbol          PD      = RCON.2                                                            'Power-down Detection Flag bit
Symbol          POR     = RCON.1                                                            'Power-on Reset Status bit
Symbol          BOR     = RCON.0                                                            'Brown-out Reset Status bit(

'T0CON
Symbol          TMR0ON  = T0CON.7                                                           '(TMR0ON: Timer0 On/Off Control bit)
Symbol          T08BIT  = T0CON.6                                                           '(T08BIT: Timer0 8-bit/16-bit Control bit
Symbol          T0CS    = T0CON.5                                                           '(T0CS: Timer0 Clock Source Select bit
Symbol          T0SE    = T0CON.4                                                           '(T0SE: Timer0 Source Edge Select bit
Symbol          PSA     = T0CON.3                                                           '(PSA: Timer0 Prescaler Assignment bit
Symbol          T0PS2   = T0CON.2                                                           '(T0PS<2:0>: Timer0 Prescaler Select bits




'Setup Variables
Dim A_data_array_1[200] As Byte
Dim A_data_array_2[200] As Byte
Dim B_Rx_Byte_1 As Byte
Dim B_Rx_Byte_2 As Byte
Dim W_writeptr_1 As Word
Dim W_writeptr_2 As Word
Dim b_Gotdata_1 As Bit
Dim b_Gotdata_2 As Bit
Dim S_Prompt As String * 20
Dim B_datachr_1 As Byte
Dim B_datachr_2 As Byte
Dim W_readptr_1 As Word
Dim W_readptr_2 As Word
Dim W_Timeout As Word
Clear
Symbol B_buffsz  200                                                               ;wanted size of input buffer




'HRSOut2 "PROXIMATE",13                                                                      'USART is triggered on HR command

On_Interrupt GoTo Isr                                                                       'We have hardware Interrupts
GoTo Over_Interrupt                                                                         'Jump over the Interrupt Service Routine
'-----------------------------------------------------------------------------------------------------------
'Interrupt Routine
Isr:                                                                                        'Interrupt Service Routine
'T1CON.0=0                                                                                  'stop the timer running
Context Save
'---------------------------------
'Real World - USART 1                                                                       'Radio units only get data from USART2
                                                                                            'Slave units only get data from USART1
'implement a ring buffer on Both UARTS

'PC traffic
If RC1IF = 1 Then                                                                           'usart 1 has received a byte
B_Rx_Byte_1=RC1REG  'load the Rx variable
RC1IF = 0                                'clear the USART interrupt flag     
A_data_array_1[W_writeptr_1] = B_Rx_Byte_1              'plant new byte in buffer
Inc W_writeptr_1                            'writeptr_1 runs from 0 to 999
If W_writeptr_1 = B_buffsz Then W_writeptr_1 = 0  'reset write ptr when beyond end of buffer
b_Gotdata_1 = 1                              'signal new data (may be more than 1 but will be ok)
EndIf

'Proximate traffic
If RC2IF = 1 Then                          'usart 1 has received a byte
B_Rx_Byte_2=RC2REG  'load the Rx variable
RC2IF = 0                                'clear the USART interrupt flag     
A_data_array_2[W_writeptr_2] = B_Rx_Byte_2          'plant new byte in buffer
Inc W_writeptr_2                            'writeptr_1 runs from 0 to 999
If W_writeptr_2 = B_buffsz Then W_writeptr_2 = 0  'reset write ptr when beyond end of buffer
b_Gotdata_2 = 1                              'signal new data (may be more than 1 but will be ok)
EndIf

'Timer 0
If TMR0IF=1 Then                                                                            'Timer 0 interrupt
    Clear TMR0IF                                                                            'clear the flag for next time
    If W_Timeout>0 Then Dec W_Timeout
EndIf   'TMR0IF=1




end_int:
Context Restore
'-----------------------------------------------------------------------------------------------------------

Over_Interrupt: ' setup the ports, initialise variables and display, enable the interrupts.

' Timer0 Registers:' 8-Bit Mode; Prescaler=1:64; TMRH Preset=0; TMRL Preset=6; Freq=1,000.00Hz; Period=1,000,000 ns
TMR0ON = 1 ' Timer0 On/Off Control bit:1=Enables Timer0 / 0=Stops Timer0
T08BIT = 0 ' Timer0 8-bit/16-bit Control bit: 1=8-bit timer/counter / 0=16-bit timer/counter
T0CS   = 0 ' TMR0 Clock Source Select bit: 0=Internal Clock (CLKO) / 1=Transition on T0CKI pin
T0SE   = 0 ' TMR0 Source Edge Select bit: 0=low/high / 1=high/low
PSA    = 0 ' Prescaler Assignment bit: 0=Prescaler is assigned; 1=NOT assigned/bypassed
T0PS2  = 0 ' bits 2-0  PS2:PS0: Prescaler Select bits

IPEN=1
GIEH = 1    'enable high P int
GIEL = 1    'enable low P int
RC1IP = 1   'High P for EUSART 1 Rx
TX1IP = 1   'High P for EUSART1  Tx
RC2IP = 1   'High P for EUSRAT 2 Rx
TX2IP = 1   'High P EUSART 2 Tx
TMR0IP = 0  'Low P for Tmr 0
RC1IE = 1   'enable EUSART 1 Rx int
'tx1ie = 1   'enable EUSART 1 Tx
RC2IE = 1   'enable EUSART 2 Rx int
'TX2IE = 1   'enable EUSART 2 Tx

CREN1 = 1   'continuous rx on EUSART 1


PEIE=1                                                                                      'enable peripheral ints
RC1IF = 0                                                                                   'clear the flag
RC2IF = 0
WUE=0                                                                                       'disable the auto wake up
TMR0IE=1                                                                                    'start the clock
GIE=1                                                                                       'Global interrupt enable

GoTo OVERSUBS
'************************************************************
get_chr_1:
B_datachr_1 = A_data_array_1[W_readptr_1]                ;get first new byte
Inc W_readptr_1                                  ;step take-off pointer
If W_readptr_1 = B_buffsz Then
W_readptr_1 = 0        ;reset pointer if end of buffer
EndIf
If W_readptr_1 = W_writeptr_1 Then
b_Gotdata_1 = 0      ;clear flag if no more data in buffer
EndIf
@Clrwdt 'clear the wdt
Return
'************************************************************
get_chr_2:
B_datachr_2 = A_data_array_2[W_readptr_2]                ;get first new byte
Inc W_readptr_2                                  ;step take-off pointer
If W_readptr_2 = B_buffsz Then
W_readptr_2 = 0        ;reset pointer if end of buffer
EndIf
If W_readptr_2 = W_writeptr_2 Then
b_Gotdata_2 = 0      ;clear flag if no more data in buffer
EndIf
@Clrwdt 'clear the wdt
Return
'************************************************************

OVERSUBS:
DelayMS 100
HRSOut "Startup OK",13
W_Timeout=100
test:
    If W_Timeout=0 Then
        HRSOut "Timeout",13
        W_Timeout=500
        Toggle _Led
    EndIf
    @Clrwdt
GoTo test




Stephen Moss

I think that your On_Interrupt GoTo Isr statement is wrong.
Should it not be...
On_Hardware_Interrupt for High priority hardware interrupts on 18F devices (all hardward interrupts on device that do not have high & low prioriy interrupts) and
On_Low_Interrupt for low priority interrupts.

Also, unless I missed it, it appears that you only have the one ISR in your code and you would need two for it two work, one for the high priority interrupts adn one for the low priority interrupts otherwise all interrupts would be going to the same handler.

Maybe it would be helpful to have an On_High_Interrupt alias for the On_Hardware_Interrupt, to make it clearer in your code.

Peter Truman

High Stephen

Yep - that seems to be it. My test routine now works so I'll start building my code on that - thanks for the help. (I've reposted the code below just in vase someone else runs into the same problem)

Just one question while I'm here - where would one normally keep all these register definitions - it's very tedious typing these out - in the past, I've just added the registers I know I need. I have some vague recollection of there being an include file somewhere that already has these? (Could be wrong - wouldn't be the first time lol)

Device = 18F25K22

Config_Start
  FOSC = INTIO67    ;Internal oscillator block
  PLLCFG = On    ;Oscillator used directly
  PRICLKEN = On    ;Primary clock enabled
  FCMEN = OFF    ;Fail-Safe Clock Monitor disbled
  IESO = On   ;Oscillator Switchover mode disabled
  PWRTEN = On    ;Power up timer enabled
  BOREN = SBORDIS    ;Brown-out Reset enabled in hardware only (SBOREN is disabled)
  BORV = 190    ;VBOR set to 1.90 V nominal
  WDTEN = On    ;WDT is always enabled. SWDTEN bit has no effect
  WDTPS = 1024    ;1:32768
  CCP2MX = PORTC1    ;CCP2 input/output is multiplexed with RC1
  PBADEN = OFF    ;PORTB<5:0> pins are configured as digital I/O on Reset
  CCP3MX = PORTB5    ;P3A/CCP3 input/output is multiplexed with RB5
  HFOFST = On    ;HFINTOSC output and ready status are not delayed by the oscillator stable status
  T3CMX = PORTC0    ;T3CKI is on RC0
  P2BMX = PORTB5    ;P2B is on RB5
  MCLRE = EXTMCLR    ;MCLR pin enabled, RE3 input pin disabled
  STVREN = On    ;Stack full/underflow will cause Reset
  LVP = off    ;Single-Supply ICSP disabled
  XINST = OFF    ;Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
  Debug = OFF    ;Disabled
  Cp0 = OFF    ;Block 0 (000800-001FFFh) not code-protected
  CP1 = OFF    ;Block 1 (002000-003FFFh) not code-protected
  CP2 = OFF    ;Block 2 (004000-005FFFh) not code-protected
  CP3 = OFF    ;Block 3 (006000-007FFFh) not code-protected
  CPB = OFF    ;Boot block (000000-0007FFh) not code-protected
  CPD = OFF    ;Data EEPROM not code-protected
  WRT0 = OFF    ;Block 0 (000800-001FFFh) not write-protected
  WRT1 = OFF    ;Block 1 (002000-003FFFh) not write-protected
  WRT2 = OFF    ;Block 2 (004000-005FFFh) not write-protected
  WRT3 = OFF    ;Block 3 (006000-007FFFh) not write-protected
  WRTC = OFF    ;Configuration registers (300000-3000FFh) not write-protected
  WRTB = OFF    ;Boot Block (000000-0007FFh) not write-protected
  WRTD = OFF    ;Data EEPROM not write-protected
  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
  EBTR2 = OFF    ;Block 2 (004000-005FFFh) not protected from table reads executed in other blocks
  EBTR3 = OFF    ;Block 3 (006000-007FFFh) 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

OSCCON.6=1
OSCCON.5=1
OSCCON.4=1
OSCTUNE.6=1                                                                                 'disable the 4x PLLEN

Xtal = 64

PinPullup Enable,PORTB                                                                      'Turn on Port B Pullups


'Define the Pins
Symbol _Signal =   PORTA.5
Symbol _Sw5v =     PORTB.0
Symbol _Option_1 = PORTB.1
Symbol _Option_2 = PORTB.2
Symbol _Switch =   PORTB.3
Symbol _Button =   PORTB.4
Symbol _Led =      PORTB.5


TRISA=%11100000
TRISB=%11011111
TRISC=%10001000

'Declare Create_Coff=True
'setup the I2C comms
Declare SDA_Pin PORTC.4                                                                     '12C declares
Declare SCL_Pin PORTC.3
Declare Slow_Bus On


All_Digital=True                                                                            'start at digital


'Setup USART 1 (Tlit Module)
Declare Hserial1_Baud = 14400
Declare Hserial1_Clear = 1                                                                  ' Enable Error clearing on received characters
Declare HRSOut1_Pin = PORTC.6
Declare HRSIn1_Pin = PORTC.7

'Setup USART 2 (Real World)
Declare Hserial2_Baud = 14400
Declare Hserial2_Clear = 1                                                                  ' Enable Error clearing on received characters
Declare HRSOut2_Pin = PORTB.6
Declare HRSIn2_Pin = PORTB.7
                                                               

'Register Definitions
'INTCON1
Symbol          GIEH    = INTCON.7 '}                                                       'vs GIE
Symbol          GIE     = INTCON.7 '}                                                       'Global Int Enable
Symbol          GIEL    = INTCON.6 '}                                                     
Symbol          PEIE    = INTCON.6 '}                                                       'peripheral int enable
Symbol          TMR0IE  = INTCON.5                                                          'TMR0 Overflow Interrupt Enable
Symbol          INT0IE  = INTCON.4                                                          'External Interrupt Enable bit         
Symbol          RBIE    = INTCON.3                                                          'Port B Interrupt-On-Change (IOCx) Interrupt Enable bit(
Symbol          TMR0IF  = INTCON.2                                                          'TMR0 Overflow Interrupt Flag bit
Symbol          INT0IF  = INTCON.1                                                          'External Interrupt Flag bit                   
Symbol          RBIF    = INTCON.0                                                          'Port B Interrupt-On-Change (IOCx) Interrupt Flag bit

'INTCON2
Symbol          RBPU    = INTCON2.7                                                         'PORTB Pull-up Enable bit
Symbol          INTEDG0 = INTCON2.6                                                         'External Interrupt 0 Edge Select bit
Symbol          INTEDG1 = INTCON2.5                                                         'External Interrupt 1 Edge Select bit
Symbol          INTEDG2 = INTCON2.4                                                         'External Interrupt 2 Edge Select bit
Symbol          TMR0IP  = INTCON2.2                                                         'TMR0 Overflow Interrupt Priority bit
Symbol          RBIP    = INTCON2.0                                                         'RB Port Change Interrupt Priority bit

'INTCON3
Symbol          INT2IP  = INTCON3.7                                                         'INT2 External Interrupt Priority bit         
Symbol          INT1IP  = INTCON3.6                                                         'INT1 External Interrupt Priority bit
Symbol          INT2IE  = INTCON3.4                                                         'INT2 External Interrupt Enable bit
Symbol          INT1IE  = INTCON3.3                                                         'INT1 External Interrupt Enable bit
Symbol          INT2IF  = INTCON3.1                                                         'INT2 External Interrupt Flag bit
Symbol          INT1IF  = INTCON3.0                                                         'INT1 External Interrupt Flag bit

'IPR1
Symbol          ADIP    = IPR1.6                                                            'A/D Converter Interrupt Priority bit
Symbol          RC1IP   = IPR1.5                                                            'EUSART1 Receive Interrupt Priority bit 
Symbol          TX1IP   = IPR1.4
Symbol          SSP1IP  = IPR1.3                                                            'Host Synchronous Serial Port 1 Interrupt Priority bit
Symbol          CCP1IP  = IPR1.2                                                            'CCP1 Interrupt Priority bit
Symbol          TMR2IP  = IPR1.1                                                            'TMR2 to PR2 Match Interrupt Priority bit
Symbol          TMR1IP  = IPR1.0                                                            'TMR1 Overflow Interrupt Priority bit       

'IPR2
Symbol          OSCFIP  = IPR2.7                                                            'Oscillator Fail Interrupt Priority bit
Symbol          C1IP    = IPR2.6                                                            'Comparator C1 Interrupt Priority bit
Symbol          C2IP    = IPR2.5                                                            'Comparator C2 Interrupt Priority bit
Symbol          EEIP    = IPR2.4                                                            'Data EEPROM/Flash Write Operation Interrupt Priority bit
Symbol          BCL1IP  = IPR2.3                                                            'MSSP1 Bus Collision Interrupt Priority bit
Symbol          HLVDIP  = IPR2.2                                                            'Low-Voltage Detect Interrupt Priority bit
Symbol          TMR3IP  = IPR2.1                                                            'TMR3 Overflow Interrupt Priority bit
Symbol          CCP2IP  = IPR2.0                                                            'CCP2 Interrupt Priority bit

'IPR3
Symbol          SSP2IP  = IPR3.7                                                            'Synchronous Serial Port 2 Interrupt Priority bit
Symbol          BCL2IP  = IPR3.6                                                            'Bus Collision 2 Interrupt Priority bit
Symbol          RC2IP   = IPR3.5                                                            'EUSART2 Receive Interrupt Priority bit
Symbol          TX2IP   = IPR3.4                                                            'EUSART2 Transmit Interrupt Priority bit
Symbol          CTMUIP  = IPR3.3                                                            'CTMU Interrupt Priority bit
Symbol          TMR5GIP = IPR3.2                                                            'TMR5 Gate Interrupt Priority bit
Symbol          TMR3GIP = IPR3.1                                                            'TMR3 Gate Interrupt Priority bit
Symbol          TMR1GIP = IPR3.0                                                            'TMR1 Gate Interrupt Priority bit

'IPR4
Symbol          CCP5IP  = IPR4.2                                                            'CCP5 Interrupt Priority bit
Symbol          CCP4IP  = IPR4.1                                                            'CCP4 Interrupt Priority bit
Symbol          CCP3IP  = IPR4.0                                                            'CCP3 Interrupt Priority bit

'IPR5
Symbol          TMR6IP  = IPR5.2                                                            'TMR6 to PR6 Match Interrupt Priority bit
Symbol          TMR5IP  = IPR5.1                                                            'TMR5 Overflow Interrupt Priority bit
Symbol          TMR4IP  = IPR5.0                                                            'TMR4 to PR4 Match Interrupt Priority bit

'PIR1
Symbol          ADIF    = PIR1.6                                                            'A/D Converter Interrupt Flag bit
Symbol          RC1IF   = PIR1.5                                                            'EUSART1 Receive Interrupt Flag bit
Symbol          TX1IF   = PIR1.4                                                            'EUSART1 Transmit Interrupt Flag bit
Symbol          SSP1IF  = PIR1.3                                                            'Host Synchronous Serial Port 1 Interrupt Flag bit
Symbol          CCP1IF  = PIR1.2                                                            'CCP1 Interrupt Flag bit
Symbol          TMR2IF  = PIR1.1                                                            'TMR2 to PR2 Match Interrupt Flag bit
Symbol          TMR1IF  = PIR1.0                                                            'TMR1 Overflow Interrupt Flag bit

'PIR2           
Symbol          OSCFIF  = PIR2.7                                                            'Oscillator Fail Interrupt Flag bit
Symbol          C1IF    = PIR2.6                                                            'Comparator C1 Interrupt Flag bit
Symbol          C2IF    = PIR2.5                                                            'Comparator C2 Interrupt Flag bit
Symbol          EEIF    = PIR2.4                                                            'Data EEPROM/Flash Write Operation Interrupt Flag bit
Symbol          BCL1IF  = PIR2.3                                                            'MSSP1 Bus Collision Interrupt Flag bit
Symbol          HLVDIF  = PIR2.2                                                            'Low-Voltage Detect Interrupt Flag bit
Symbol          TMR3IF  = PIR2.1                                                            'TMR3 Overflow Interrupt Flag bit
Symbol          CCP2IF  = PIR2.0                                                            'CCP2 Interrupt Flag bit

'PIR3
Symbol          SSP2IF  = PIR3.7                                                            'Synchronous Serial Port Interrupt Flag bit
Symbol          BCL2IF  = PIR3.6                                                            'MSSP2 Bus Collision Interrupt Flag bit
Symbol          RC2IF   = PIR3.5                                                            'EUSART2 Receive Interrupt Flag bit
Symbol          TX2IF   = PIR3.4                                                            'EUSART2 Transmit Interrupt Flag bit
Symbol          CTMUIF  = PIR3.3                                                            'CTMU Interrupt Flag bit
Symbol          TMR5GIF = PIR3.2                                                            'TMR5 Gate Interrupt Flag bits
Symbol          TMR3GIF = PIR3.1                                                            'TMR3 Gate Interrupt Flag bits
Symbol          TMR1GIF = PIR3.0                                                            'TMR1 Gate Interrupt Flag bits

'PIR4
Symbol          CCP5IF  = PIR4.2                                                            'CCP5 Interrupt Flag bits
Symbol          CCP4IF  = PIR4.1                                                            'CCP4 Interrupt Flag bits
Symbol          CCP3IF  = PIR4.0                                                            'ECCP3 Interrupt Flag bits
         
'PIR5
Symbol          TMR6IF  = PIR5.2                                                            'TMR6 to PR6 Match Interrupt Flag bit
Symbol          TMR5IF  = PIR5.1                                                            'TMR5 Overflow Interrupt Flag bit
Symbol          TMR4IF  = PIR5.0                                                            'TMR4 to PR4 Match Interrupt Flag bit

'PIE1
Symbol          ADIE    = PIE1.6                                                            'A/D Converter Interrupt Enable bit
Symbol          RC1IE   = PIE1.5                                                            'EUSART1 Receive Interrupt Enable bit
Symbol          TX1IE   = PIE1.4                                                            'EUSART1 Transmit Interrupt Enable bit
Symbol          SSP1IE  = PIE1.3                                                            'Host Synchronous Serial Port 1 Interrupt Enable bit
Symbol          CCP1IE  = PIE1.2                                                            'CCP1 Interrupt Enable bit         
Symbol          TMR2IE  = PIE1.1                                                            'TMR2 to PR2 Match Interrupt Enable bit
Symbol          TMR1IE  = PIE1.0                                                            'TMR1 Overflow Interrupt Enable bit

'PIE2
Symbol          OSCFIE  = PIE2.7                                                            'Oscillator Fail Interrupt Enable bit
Symbol          C1IE    = PIE2.6                                                            'Comparator C1 Interrupt Enable bit
Symbol          C2IE    = PIE2.5                                                            'Comparator C2 Interrupt Enable bit
Symbol          EEIE    = PIE2.4                                                            'Data EEPROM/Flash Write Operation Interrupt Enable bit
Symbol          BCL1IE  = PIE2.3                                                            'MSSP1 Bus Collision Interrupt Enable bit
Symbol          HLVDIE  = PIE2.2                                                            'Low-Voltage Detect Interrupt Enable bit
Symbol          TMR3IE  = PIE2.1                                                            'TMR3 Overflow Interrupt Enable bit
Symbol          CCP2IE  = PIE2.0                                                            'CCP2 Interrupt Enable bit

'PIE3
Symbol          SSP2IE  = PIE3.7                                                            'Host Synchronous Serial Port 2 Interrupt Enable bit
Symbol          BCL2IE  = PIE3.6                                                            'Bus Collision Interrupt Enable bit
Symbol          RC2IE   = PIE3.5                                                            'EUSART2 Receive Interrupt Enable bit
Symbol          TX2IE   = PIE3.4                                                            'EUSART2 Transmit Interrupt Enable bit
Symbol          CTMUIE  = PIE3.3                                                            'CTMU Interrupt Enable bit
Symbol          TMR5GIE = PIE3.2                                                            'TMR5 Gate Interrupt Enable bit
Symbol          TMR3GIE = PIE3.1                                                            'TMR3 Gate Interrupt Enable bit
Symbol          TMR1GIE = PIE3.0                                                            'TMR1 Gate Interrupt Enable bit


'TXSTA
Symbol          CSRC    = TXSTA1.7                                                          'Clock Source Select bit
Symbol          TX9     = TXSTA1.6                                                          '9-bit Transmit Enable bit
Symbol          TXEN    = TXSTA1.5                                                          'Transmit Enable bit
Symbol          SYNC    = TXSTA1.4                                                          'EUSART Mode Select
Symbol          SENDB   = TXSTA1.3                                                          'Send Break Character bit
Symbol          BRGH    = TXSTA1.2                                                          'High Baud Rate Select bit
Symbol          TRMT    = TXSTA1.1                                                          'Transmit Shift Register Status bit
Symbol          TX9D    = TXSTA1.0                                                          'Ninth bit of Transmit Data

'RCSTA1
Symbol          SPEN    = RCSTA1.7                                                          'Serial Port Enable bit
Symbol          RX9     = RCSTA1.6                                                          '9-bit Receive Enable bit
Symbol          SREN    = RCSTA1.5                                                          'Single Receive Enable bit
Symbol          CREN1   = RCSTA1.4                                                          'Continuous Receive Enable bit
Symbol          ADDEN   = RCSTA1.3                                                          'Address Detect Enable bit
Symbol          FERR    = RCSTA1.2                                                          'Framing Error bit
Symbol          OERR    = RCSTA1.1                                                          'Overrun Error bit
Symbol          RX9D    = RCSTA1.0                                                          'Ninth bit of Received Data

'BAUDCONx
Symbol          ABDOVF  = BAUDCON1.7                                                        'Auto-Baud Detect Overflow bit         
Symbol          RCIDL   = BAUDCON1.6                                                        'Receive Idle Flag bit
Symbol          DTRXP   = BAUDCON1.5                                                        'Data/Receive Polarity Select bit
Symbol          CKTXP   = BAUDCON1.4                                                        'Clock/Transmit Polarity Select bit
Symbol          BRG16   = BAUDCON1.3                                                        '16-bit Baud Rate Generator bit
Symbol          WUE     = BAUDCON1.1                                                        'Wake-up Enable bit
Symbol          ABDEN   = BAUDCON1.0                                                        'Auto-Baud Detect Enable bit

'RCON
Symbol          IPEN    = RCON.7                                                            'Interrupt Priority Enable bit
Symbol          SBOREN  = RCON.6                                                            'BOR Software Enable bit
Symbol          RI      = RCON.4                                                            'RESET Instruction Flag bit
Symbol          TOf     = RCON.3                                                            'Watchdog Time-out Flag bit
Symbol          PD      = RCON.2                                                            'Power-down Detection Flag bit
Symbol          POR     = RCON.1                                                            'Power-on Reset Status bit
Symbol          BOR     = RCON.0                                                            'Brown-out Reset Status bit(

'T0CON
Symbol          TMR0ON  = T0CON.7                                                           '(TMR0ON: Timer0 On/Off Control bit)
Symbol          T08BIT  = T0CON.6                                                           '(T08BIT: Timer0 8-bit/16-bit Control bit
Symbol          T0CS    = T0CON.5                                                           '(T0CS: Timer0 Clock Source Select bit
Symbol          T0SE    = T0CON.4                                                           '(T0SE: Timer0 Source Edge Select bit
Symbol          PSA     = T0CON.3                                                           '(PSA: Timer0 Prescaler Assignment bit
Symbol          T0PS2   = T0CON.2                                                           '(T0PS<2:0>: Timer0 Prescaler Select bits




'Setup Variables
Dim A_data_array_1[200] As Byte
Dim A_data_array_2[200] As Byte
Dim B_Rx_Byte_1 As Byte
Dim B_Rx_Byte_2 As Byte
Dim W_writeptr_1 As Word
Dim W_writeptr_2 As Word
Dim b_Gotdata_1 As Bit
Dim b_Gotdata_2 As Bit
Dim S_Prompt As String * 20
Dim B_datachr_1 As Byte
Dim B_datachr_2 As Byte
Dim W_readptr_1 As Word
Dim W_readptr_2 As Word
Dim W_Timeout As Word
Clear
Symbol B_buffsz  200                                                               ;wanted size of input buffer




'HRSOut2 "PROXIMATE",13                                                                      'USART is triggered on HR command

'On_Interrupt GoTo Isr                                                                       'We have hardware Interrupts
On_Hardware_Interrupt GoTo HISR
On_Low_Interrupt GoTo LISR


GoTo Over_Interrupt                                                                         'Jump over the Interrupt Service Routine


HISR:
'-----------------------------------------------------------------------------------------------------------
'Interrupt Routine
Isr:                                                                                        'Interrupt Service Routine
'T1CON.0=0                                                                                  'stop the timer running
Context Save
'---------------------------------
'Real World - USART 1                                                                       'Radio units only get data from USART2
                                                                                            'Slave units only get data from USART1
'implement a ring buffer on Both UARTS

'PC traffic
If RC1IF = 1 Then                                                                           'usart 1 has received a byte
B_Rx_Byte_1=RC1REG  'load the Rx variable
RC1IF = 0                                'clear the USART interrupt flag     
A_data_array_1[W_writeptr_1] = B_Rx_Byte_1              'plant new byte in buffer
Inc W_writeptr_1                            'writeptr_1 runs from 0 to 999
If W_writeptr_1 = B_buffsz Then W_writeptr_1 = 0  'reset write ptr when beyond end of buffer
b_Gotdata_1 = 1                              'signal new data (may be more than 1 but will be ok)
EndIf

'Proximate traffic
If RC2IF = 1 Then                          'usart 1 has received a byte
B_Rx_Byte_2=RC2REG  'load the Rx variable
RC2IF = 0                                'clear the USART interrupt flag     
A_data_array_2[W_writeptr_2] = B_Rx_Byte_2          'plant new byte in buffer
Inc W_writeptr_2                            'writeptr_1 runs from 0 to 999
If W_writeptr_2 = B_buffsz Then W_writeptr_2 = 0  'reset write ptr when beyond end of buffer
b_Gotdata_2 = 1                              'signal new data (may be more than 1 but will be ok)
EndIf
'end_int:
Context Restore
'-----------------------------------------------------------------------------------------------------------
LISR:
Context Save
'Timer 0
If TMR0IF=1 Then                                                                            'Timer 0 interrupt
    Clear TMR0IF                                                                            'clear the flag for next time
    If W_Timeout>0 Then Dec W_Timeout
EndIf   'TMR0IF=1
'end_int:
Context Restore
'-----------------------------------------------------------------------------------------------------------


Over_Interrupt: ' setup the ports, initialise variables and display, enable the interrupts.

' Timer0 Registers:' 8-Bit Mode; Prescaler=1:64; TMRH Preset=0; TMRL Preset=6; Freq=1,000.00Hz; Period=1,000,000 ns
TMR0ON = 1 ' Timer0 On/Off Control bit:1=Enables Timer0 / 0=Stops Timer0
T08BIT = 0 ' Timer0 8-bit/16-bit Control bit: 1=8-bit timer/counter / 0=16-bit timer/counter
T0CS   = 0 ' TMR0 Clock Source Select bit: 0=Internal Clock (CLKO) / 1=Transition on T0CKI pin
T0SE   = 0 ' TMR0 Source Edge Select bit: 0=low/high / 1=high/low
PSA    = 0 ' Prescaler Assignment bit: 0=Prescaler is assigned; 1=NOT assigned/bypassed
T0PS2  = 0 ' bits 2-0  PS2:PS0: Prescaler Select bits

IPEN=1
GIEH = 1    'enable high P int
GIEL = 1    'enable low P int
RC1IP = 1   'High P for EUSART 1 Rx
TX1IP = 1   'High P for EUSART1  Tx
RC2IP = 1   'High P for EUSRAT 2 Rx
TX2IP = 1   'High P EUSART 2 Tx
TMR0IP = 0  'Low P for Tmr 0
RC1IE = 1   'enable EUSART 1 Rx int
'tx1ie = 1   'enable EUSART 1 Tx
RC2IE = 1   'enable EUSART 2 Rx int
'TX2IE = 1   'enable EUSART 2 Tx

CREN1 = 1   'continuous rx on EUSART 1


PEIE=1                                                                                      'enable peripheral ints
RC1IF = 0                                                                                   'clear the flag
RC2IF = 0
WUE=0                                                                                       'disable the auto wake up
TMR0IE=1                                                                                    'start the clock
GIE=1                                                                                       'Global interrupt enable

GoTo OVERSUBS
'************************************************************
get_chr_1:
B_datachr_1 = A_data_array_1[W_readptr_1]                ;get first new byte
Inc W_readptr_1                                  ;step take-off pointer
If W_readptr_1 = B_buffsz Then
W_readptr_1 = 0        ;reset pointer if end of buffer
EndIf
If W_readptr_1 = W_writeptr_1 Then
b_Gotdata_1 = 0      ;clear flag if no more data in buffer
EndIf
@Clrwdt 'clear the wdt
Return
'************************************************************
get_chr_2:
B_datachr_2 = A_data_array_2[W_readptr_2]                ;get first new byte
Inc W_readptr_2                                  ;step take-off pointer
If W_readptr_2 = B_buffsz Then
W_readptr_2 = 0        ;reset pointer if end of buffer
EndIf
If W_readptr_2 = W_writeptr_2 Then
b_Gotdata_2 = 0      ;clear flag if no more data in buffer
EndIf
@Clrwdt 'clear the wdt
Return
'************************************************************

OVERSUBS:
DelayMS 100
HRSOut "Startup OK",13
W_Timeout=100
test:
    If W_Timeout=0 Then
        HRSOut "Timeout",13
        W_Timeout=100
        Toggle _Led
    '    delayms 750
    EndIf
    @Clrwdt
    'Toggle _Led
    'DelayMS 750   
GoTo test

Peter Truman

Interestingly - enabling TX1IE (PIE1.4) stops the code from running (as soon as I try to write to USART 1) and enabling TX2IE (PIE3.4) does the same when I try to write to USART 2. Clearly I don't understand the function of these (the data sheet a bit opaque on this)

 

Stephen Moss

#4
Quote from: Peter Truman on Sep 18, 2024, 11:11 PMJust one question while I'm here - where would one normally keep all these register definitions - it's very tedious typing these out

I assume you are referring to...
Symbol          GIEH    = INTCON.7 '}                                                       'vs GIE
Symbol          GIE     = INTCON.7 '}                                                       'Global Int Enable
Symbol          GIEL    = INTCON.6 '}                                                     
Symbol          PEIE    = INTCON.6 '}                                                       'peripheral int enable
Symbol          TMR0IE  = INTCON.5                                                          'TMR0 Overflow Interrupt Enable
Symbol          INT0IE  = INTCON.4                                                          'External Interrupt Enable bit         
Symbol          RBIE    = INTCON.3                                                          'Port B Interrupt-On-Change (IOCx) Interrupt Enable bit(
Symbol          TMR0IF  = INTCON.2                                                          'TMR0 Overflow Interrupt Flag bit
Symbol          INT0IF  = INTCON.1                                                          'External Interrupt Flag bit                   
Symbol          RBIF    = INTCON.0                                                          'Port B Interrupt-On-Change (IOCx) Interrupt Flag bit

In the original IDE if you use the Code explorer, select the device name to expand to the Registers and then Left click on the applicable register name, that will then display a table of the bit names. If the register has bit names associated with it (see the devices Bit Names file) there is an option it the bottom of the table to generate the code and paste in at the current cursor position.
 
If you are using Positron Studio you have to double click the register name to display the table but have the same past option. I am not sure about FineLine but it probably has something similar.
If there is not a BFN file for the device you are using I crated a load of them, a link to them can be found in my post https://protoncompiler.com/index.php?msg=8005 although I noticed that in John's post he states that Positron Studio gets that information from the devices .def file rather then the .bnf files.
Note: In case I did not mention it in the linked post very few of the bitnames in the files I created will have associated descriptions as the Microchip source files I was using rarely contained the relevant data, so for devices you use often you may want to spend a few minutes adding the description yourself to save you time later.

top204

#5
Within each device's .def file, there are the individual meta macros for the bits associated with an SFR, that the program listing can use.

So instead of creating all the SFR bit names as aliases in a program listing, just use the meta-macros in the .def file.

For example on a PIC18F25K22 device, the GIE bit would be used as:

INTCONbits_GIE = 1

And if you look in the 18F25K22.def file, you will see on line 1806, the meta macro: $define INTCONbits_GIE INTCON.7

And for the TMR0IF bit, use:

If INTCONbits_TMR0IF = 1 Then

Etc...

The .def files are located here: "C:\Program Files (x86)\ProtonIDE\PDS\Includes\Defs\"

The IDE will even change its colour when it sees one of the preprocessor's meta macros in a listing.

Also, the directives: On_Interrupt and On_Hardware_Interrupt are the same. The directive's name was changed to On_Hardware_Interrupt later in the compiler's life, but kept the original On_Interrupt directive for backward compatability.