News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

help about EXT INT2 18F47k42

Started by okmn, Sep 07, 2021, 03:45 PM

Previous topic - Next topic

okmn

hi guys
i am new for pic18f47k42 and i am studying on it
i wrote some codes to learn EXT INT0 1 AND 2

INT0 INT1 are active and has no problem (when i press the button 1 and 2 pic sending data by uart )

but when i made active(enable code line) all of INT not work (no sendng any data by any INT)
where i am making wrong
could you help me please..this the code
PWM_UART_TMR0_ADC_INT0.zip
1.jpg

'****************************************************************
'*  Name    : UNTITLED.BAS                                      *
'*  Author  : [select VIEW...EDITOR OPTIONS]                    *
'*  Notice  : Copyright (c) 2021 [select VIEW...EDITOR OPTIONS] *
'*          : All Rights Reserved                               *
'*  Date    : 30.08.2021                                        *
'*  Version : 1.0                                               *
'*  Notes   :                                                   *
'*          :                                                   *
'****************************************************************
Device 18F47K42

' Configuration bits: selected in the GUI
Config_Start
' Config1L
  FEXTOSC = HS    ' External Oscillator Selection->HS (crystal oscillator) above 8 MHz; PFM set to high power
  RSTOSC = EXTOSC    ' Reset Oscillator Selection->EXTOSC operating per FEXTOSC bits (Device manufacturing default)

' Config1H
  CLKOUTEN = OFF    ' Clock out Enable bit->CLKOUT function is disabled
  PR1WAY = On    ' PRLOCKED One-Way Set Enable bit->PRLOCK Bit can be cleared And Set only once
  CSWEN = On    ' Clock Switch Enable bit->Writing To NOSC And NDIV is allowed
  FCMEN = On    ' Fail-Safe Clock Monitor Enable bit->Fail-Safe Clock Monitor enabled

' Config2L
  MCLRE = EXTMCLR    ' MCLR Enable bit->If LVP = 0, MCLR Pin is MCLR; If LVP = 1, RE3 pin function is MCLR
  PWRTS = PWRT_OFF    ' Power-up timer selection bits->PWRT is disabled
  MVECEN = OFF    ' Multi-vector Enable bit->Interrupt contoller does not use vector table To prioritze interrupts
  IVT1WAY = On    ' IVTLOCK Bit One-way Set Enable bit->IVTLOCK Bit can be cleared And Set only once
  LPBOREN = OFF    ' Low Power BOR Enable bit->ULPBOR disabled
  BOREN = OFF    ' Brown-out Reset Enable bits->Brown-out Reset disabled

' Config2H
  BORV = VBOR_2P45    ' Brown-out Reset Voltage Selection bits->Brown-out Reset Voltage (VBOR) Set To 2.45V
  ZCD = OFF    ' ZCD Disable bit->ZCD disabled. ZCD can be enabled by setting the ZCDSEN Bit of ZCDCON
  PPS1WAY = OFF    ' PPSLOCK Bit One-Way Set Enable bit->PPSLOCK Bit can be Set And cleared repeatedly (subject To the unlock sequence)
  STVREN = On    ' Stack Full/Underflow Reset Enable bit->Stack full/underflow will cause Reset
  Debug = OFF    ' Debugger Enable bit->Background debugger disabled
  XINST = OFF    ' Extended Instruction Set Enable bit->Extended Instruction Set And Indexed Addressing Mode disabled

' Config3L
  WDTCPS = WDTCPS_31    ' WDT Period selection bits->Divider ratio 1:65536; software control of WDTPS
  WDTE = OFF    ' WDT operating mode->WDT Disabled; SWDTEN is ignored

' Config3H
  WDTCWS = WDTCWS_7    ' WDT Window Select bits->window always open (100%); software control; keyed access not required
  WDTCCS = SC    ' WDT Input clock selector->Software Control

' Config4L
  BBSIZE = BBSIZE_512    ' Boot Block Size selection bits->Boot Block size is 512 words
  BBEN = OFF    ' Boot Block Enable bit->Boot block disabled
  SAFEN = OFF    ' Storage Area Flash Enable bit->SAF disabled
  WRTAPP = OFF    ' Application Block write protection bit->Application Block not write protected

' Config4H
  WRTB = OFF    ' Configuration Register Write Protection bit->Configuration registers (300000-30000Bh) not write-protected
  WRTC = OFF    ' Boot Block Write Protection bit->Boot Block (000000-0007FFh) not write-protected
  WRTD = OFF    ' Data EEPROM Write Protection bit->Data EEPROM not write-protected
  WRTSAF = OFF    ' SAF Write protection bit->SAF not Write Protected
  LVP = On    ' Low Voltage Programming Enable bit->Low voltage programming enabled. MCLR/VPP Pin function is MCLR. MCLRE configuration Bit is ignored

' Config5L
  Cp = OFF    ' PFM And Data EEPROM Code Protection bit->PFM And Data EEPROM Code protection disabled
  Config_End


 Declare Xtal = 20
 Declare LCD_DTPin PORTD.4
 Declare LCD_RSPin PORTC.4
 Declare LCD_ENPin PORTC.5
' Declare HPWM1_Pin PORTB.4 ' Select HPWM port and bit for PWM1 module (ch 1)
' Declare HPWM2_Pin PORTB.5 ' Select HPWM port and bit for PWM 2 module (ch 2)
' Declare HPWM3_Pin PORTB.6 ' Select HPWM port and bit for PWM 3 module (ch 3)
 Dim SAYAC As Word
 Dim SAYAC1 As Word
 Dim TICK As Byte
 Dim UPDATE As Bit
 Dim pDuty As Word
 Dim Temperature[2] As Word
 Dim SICAKLIK As Float
 Dim TOPLAM As Word
 Dim TEMP As Word
 Dim DONGU As Byte
 SAYAC = 0
 SAYAC1 = 0
 TICK = 0
 UPDATE = 1

 System_Init()

On_Hardware_Interrupt GoTo KESME
'System_Init()
GoTo Main
KESME:
Context Save ' Save any variables used in the interrupt
If PIR3Bits_TMR0IF = 1 Then ' Is it a Timer0 overflow interrupt?

    TICK = TICK+1
    If TICK>80 Then
        TICK = 0
        SAYAC = SAYAC+1
        UPDATE = 1
        Toggle PORTB.7 ' Yes. So. Toggle PORTB.7
        If SAYAC > 1023 Then SAYAC = 0
    EndIf

EndIf
'PIR3Bits_TMR0IF = 0 ' Clear the Timer0 Overflow flag
If PIR3Bits_U1RXIF = 1 Then
    HSerIn 100, TIMEOUT, [SAYAC1]

EndIf
  PIR3Bits_U1RXIF = 0

If PIR1Bits_INT0IF = 1 Then

    Toggle PORTC.1
    SAYAC = 0
    SAYAC1 = SAYAC1+1
    HSerOut ["INT-O KESME OLUSTU: ",Dec5 SAYAC1,10,13]

EndIf
'PIR1Bits_INT0IF = 0

If PIR5Bits_INT1IF = 1 Then

    Toggle PORTC.2
    SAYAC1 = 0
    HSerOut ["INT-1 KESME OLUSTU: ",Dec5 SAYAC1,10,13]

EndIf
'PIR5Bits_INT1IF = 0

'If PIR7Bits_INT2IF = 1 Then

'    Toggle PORTC.1
'    Toggle PORTC.2
'    SAYAC1 = 0
'    HSerOut ["INT-2 KESME OLUSTU: ",Dec5 SAYAC1,10,13]

'EndIf
''PIR7Bits_INT2IF = 0

TIMEOUT:
PIR3Bits_TMR0IF = 0 ' Clear the Timer0 Overflow flag
PIR3Bits_U1RXIF = 0

PIR1Bits_INT0IF = 0
PIR5Bits_INT1IF = 0
PIR7Bits_INT2IF = 0

Context Restore

   Main:

    If UPDATE = 1 Then

        TEMP = 0
        TOPLAM = 0
        Print At 1,1,"********************"
        Print At 2,1,"*   PIC18F47K42    *"
        Print At 3,1,"*UART-->",Dec5 SAYAC,"      *"
        Print At 4,1,"********************"
        PWM1_Duty(SAYAC)
        PWM2_Duty(SAYAC)
        PWM3_Duty(SAYAC)
        ADC_Read(0)
        HSerOut ["PIC 18F47K42 LM35 :",Dec1 SICAKLIK,"C-->Channel-",Dec1 SAYAC.0,10,13]
        UPDATE = 0
        Toggle PORTC.1
    EndIf
 GoTo Main
'**********************************************************************
 Proc System_Init()
    Interrupt_Init()
    'PMD_Init()
    EXT_INT_Init()
    PinManager_Init()
    Oscillator_Init()

    FVR_Init()
    ADCC_Init()
    TMR0_Init()
    TMR2_Init()
    TMR4_Init()
    TMR6_Init()
    PWM1_Init()
    PWM2_Init()
    PWM3_Init()
    UART2_Init()
    UART1_Init()

EndProc
'**********************************************************************
 Proc Interrupt_Init()

    INTCON0Bits_GIEH = 1
    INTCON0Bits_GIEL = 1
    INTCON0bits_IPEN = 0

    INTCON0Bits_INT0EDG = 0
    INTCON0Bits_INT1EDG = 0
    INTCON0Bits_INT2EDG = 0

'   PIE3bits_U1TXIE = 1 'PIR3bits.U1TXIF = 1
    PIE3Bits_U1RXIE = 0
    PIR3Bits_TMR0IF = 0

    PIE1Bits_INT0IE = 0
    PIE5Bits_INT1IE = 0
    PIE7Bits_INT2IE = 0

    PIR1Bits_INT0IF = 0
    PIR5Bits_INT1IF = 0
    PIR7Bits_INT2IF = 0

    PIE3Bits_U1IE = 1
    PIE3Bits_U1RXIE = 1 'PIR3bits.U1RXIF = 1
    PIE3Bits_TMR0IE = 1 'PIR3bits.TMR0IF = 1
    PIE3Bits_TMR0IE = 1

    PIE1Bits_INT0IE = 1
    PIE5Bits_INT1IE = 1
    PIE7Bits_INT2IE = 1

    PIE0bits_IOCIE = 0
  EndProc
'**********************************************************************
'  Proc PMD_Init()
'    PMD0 =  $FF
'    PMD1 = $EA
'    PMD2 = $67
'    PMD3 = $F8
'    PMD4 = $E0
'    PMD5 = $07
'    PMD6 = $3F
'    PMD7 = $03
'  EndProc
'**********************************************************************
  Proc EXT_INT_Init()
  PIR1Bits_INT0IF = 0
  PIR5Bits_INT1IF = 0
  PIR7Bits_INT2IF = 0
  EndProc
'**********************************************************************
  Proc PinManager_Init()
      'LATx registers


    LATA = 0
    LATB = 0
    LATC = 0
    LATD = 0
    LATE = 0

    'TRISx registers

    TRISA = 3
    TRISB = $0F
    TRISC = $80
    TRISD = 1
    TRISE = 0
    'ANSELx registers
    ANSELA = 3
    ANSELB = 0
    ANSELC = 0
    ANSELD = 0
    ANSELE = 0

    'WPUx registers

    WPUA = $FC
    WPUB = $7F
    WPUC = $FF
    WPUD = $FF
    WPUE = $0F
'    RxyI2C registers

    RB1I2C = 0
    RB2I2C = 0
    RC3I2C = 0
    RC4I2C = 0
    RD0I2C = 0
    RD1I2C = 0
    'ODx registers


    ODCONA = 0
    ODCONB = 0
    ODCONC = 0
    ODCOND = 0
    ODCONE = 0
    'SLRCONx registers
    SLRCONA = $FF
    SLRCONB = $FF
    SLRCONC = $FF
    SLRCOND = $FF
    SLRCONE = $07
    'INLVLx registers
    INLVLA = 0
    INLVLB = 0
    INLVLC = 0
    INLVLD = 0
    INLVLE = 0

    INT0PPS = $08  'RB0->EXT_INT:INT0
    INT1PPS = $09  'RB1->EXT_INT:INT1
    INT2PPS = $0A  'RB2->EXT_INT:INT2
    RB6PPS = $0B    'RB6->CCP3:CCP3;
    RB5PPS = $0A    'RB5->CCP2:CCP2;
    RB4PPS = $09    'RB4->CCP1:CCP1;
    RD1PPS = $16    'RD1->UART2:TX2;
    U2RXPPS = $18   'RD0->UART2:RX2;
    RC6PPS = $13    'RC6->UART1:TX1;
    U1RXPPS = $17   'RC7->UART1:RX1;
   
    PORTA = $03
    PORTB = $0F
    PORTC = $80
    PORTD = $01
    PORTE = $00
  EndProc
'**********************************************************************
  Proc Oscillator_Init()
    OSCCON1 = $70
    OSCCON3 = $00
    OSCEN = $00
    OSCFRQ = $08
    OSCTUNE = $00
  EndProc
'**********************************************************************
 Proc FVR_Init()
 'CDAFVR off; FVREN enabled; TSRNG Lo_range; ADFVR 1x; TSEN disabled;
    FVRCON = $81
 EndProc
'**********************************************************************
  Proc ADCC_Init()
  'set the ADCC to the options selected in the User Interface
    ' ADLTH 0;
    ADLTHL = 0
    ' ADLTH 0;
    ADLTHH = 0
    ' ADUTH 0;
    ADUTHL = 0
    ' ADUTH 0;
    ADUTHH = 0
    ' ADSTPT 0;
    ADSTPTL = 0
    ' ADSTPT 0;
    ADSTPTH = 0
    ' ADACC 0;
    ADACCU = 0
    ' ADRPT 0;
    ADRPT = 0
    ' ADPCH ANA0;
    ADPCH = 0
    ' ADACQ 0;
    ADACQL = 0
    ' ADACQ 0;
    ADACQH = 0
    ' ADCAP Additional uC disabled;
    ADCAP = 0
    ' ADPRE 0;
    ADPREL = 0
    ' ADPRE 0;
    ADPREH = 0
    ' ADDSEN disabled; ADGPOL digital_low; ADIPEN disabled; ADPPOL Vss;
    ADCON1 = 0
    ' ADCRS 0; ADMD Basic_mode; ADACLR disabled; ADPSIS RES;
    ADCON2 = 0
    'ADCALC Actual result vs filtered value; ADTMD disabled; ADSOI ADGO not cleared;
    ADCON3 = $20
    ' ADMATH registers not updated;
    ADSTAT = 0
    ' ADNREF VSS; ADPREF FVR;
    ADREF = 3
    ' ADACT disabled;
    ADACT = 0
    ' ADCS FOSC/120;
    ADCLK = $3B
    ' ADGO stop; ADFM right; ADON enabled; ADCS FOSC/ADCLK; ADCONT disabled;
    ADCON0 = $84
  EndProc
'**********************************************************************
  Proc TMR0_Init()
    T0CON1 = $48
    TMR0H = $F4
    TMR0L = $00
    T0CON0 = $80
  EndProc
'**********************************************************************
  Proc TMR2_Init()

    T2CLKCON = $01
    T2HLT = $00
    T2RST = $00
    T2PR = $FF
    T2TMR = $00
    PIR4bits_TMR2IF = 0
    T2CONbits_TMR2ON = 1
    T2CON = $A0
  EndProc
'**********************************************************************
  Proc TMR4_Init()
    T4CLKCON = $01
    T4HLT = $00
    T4RST = $00
    T4PR = $FF
    T4TMR = $00
    PIR7bits_TMR4IF = 0
    T4CON = $A0
  EndProc
'**********************************************************************
  Proc TMR6_Init()
     T6CLKCON = $01
     T6HLT = $00
     T6RST = $00
     T6PR = $FF
     PIR9bits_TMR6IF = 0
     T6CON = $A0
  EndProc
'**********************************************************************
  Proc PWM1_Init()
    CCP1CON = $8C
    CCPR1H = $00
    CCPR1L = $00
'Selecting Timer 2
    CCPTMRS0bits_C1TSEL1 = 0
    CCPTMRS0bits_C1TSEL0 = 1
  EndProc
'**********************************************************************
  Proc PWM2_Init()
    CCP2CON = $8c
    CCPR2H = $00
    CCPR2L = 0$00
    CCPTMRS0bits_C2TSEL1 = 1
    CCPTMRS0bits_C2TSEL0 = 0
  EndProc
'**********************************************************************
  Proc PWM3_Init()
    CCP3CON = $8C
    CCPR3H = $00
    CCPR3L = $00
    CCPTMRS0bits_C3TSEL1 = 1
    CCPTMRS0bits_C3TSEL0 = 1
  EndProc
'**********************************************************************
  Proc UART1_Init()

    U1P1L = $00
    U1P1H = $00
    U1P2L = $00
    U1P2H = $00
    U1P3L = $00
    U1P3H = $00
    U1CON0 = $B0
    U1CON1 = $80
    U1CON2 = $C4
'//////////////////////////   
    U1BRGL = $2A
    U1BRGH = $00     'BAUDRATE 115200
'//////////////////////////   
'    U1BRGL = $03
'    U1BRGH = $01   'BAUDRATE 19200
'//////////////////////////
    U1FIFO = $00
    U1UIR = $00
    U1ERRIR = $00
    U1ERRIE = $00

  EndProc
'**********************************************************************
  Proc UART2_Init()
    U2P1L = $00
    U2P2L = $00
    U2P3L = $00
    U2CON0 = $B0
    U2CON1 = $80
    U2CON2 = $C4
'//////////////////////////     
    U2BRGL = $08  'BAUDRATE 9600
    U2BRGH = $02
'//////////////////////////     
    U2FIFO = $00
    U2UIR = $00
    U2ERRIR = $00
    U2ERRIE = $00
  EndProc
'**********************************************************************

 Proc PWM1_Duty(pDuty As Word)
    pDuty = pDuty & $03FF   'MAX. 10BIT
    'Load duty cycle value
        CCPR1H = pDuty.HighByte
        CCPR1L = pDuty.LowByte
 EndProc
'**********************************************************************
 Proc PWM2_Duty(pDuty As Word)

    pDuty = pDuty & $03FF
    'Load duty cycle value
        CCPR2H = pDuty.HighByte
        CCPR2L = pDuty.LowByte

 EndProc
'**********************************************************************
 Proc PWM3_Duty(pDuty As Word)
    pDuty = pDuty & $03FF
        CCPR3H = pDuty.HighByte
        CCPR3L = pDuty.LowByte
 EndProc
'**********************************************************************
 Proc ADC_Read(pAdres As Bit)
      ADPCH = pAdres
      'Turn on the ADC module
          ADCON0bits_ADON = 1
          DelayUS 50
          'Start the conversion
          ADCON0bits_ADGO = 1
          While ADCON0bits_ADGO = 1 : Wend         ' Poll the GO_DONE flag for completion of conversion
          ADCON0bits_ADON = 0                         ' Disable the ADC, to save power
          TOPLAM.LowByte = ADRESL
          TOPLAM.HighByte = ADRESH

     'Set the ADC channel to AVss.
     ADPCH = $3B
     SICAKLIK = (102.4*(TOPLAM))/4095
 EndProc


top204

#1
You are resetting the INTx flags on every interrupt, so they will be reset regardless if it was an INTx that triggered it. So they may never fire because the flags are always reset by any interrupt occurance.

Only reset a peripheral's interrupt flag if it was actually that peripheral that fired the interrupt, and within the comparison of the peripheral's interrupt handler. For example:

If INT0_Flag() = 1 Then
' Code here for an INT0 interrupt
INT0_Flag() = 0
EndIf

If INT1_Flag() = 1 Then
' Code here for an INT1 interrupt
INT1_Flag() = 0
EndIf

If INT2_Flag() = 1 Then
' Code here for an INT2 interrupt
INT2_Flag() = 0
EndIf


The 18FxxK42 devices are really nice and have lots of peripherals and flash and RAM. The compiler also optimises them so the new Movffl mnemonic is not used if it is not required, so the flash memory usage does not increase dramatically as it does with other compilers when compiling for 18F devices with more than 4096 bytes of RAM.