• AVR Freaks

Hot!how to use three interrupts on PIC18F4520?

Author
rrodrigofranco
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2021/01/28 16:44:34
  • Location: 0
  • Status: offline
2021/02/28 19:47:54 (permalink)
0

how to use three interrupts on PIC18F4520?

I'm trying to activate the interrupts on ports RB0, RB1 and RB2, but I was only able to activate ports RB0 and RB2, interrupt RB1 is not working, which is the interrupt that activates the "heater", could someone help me? Follow my code below:
 
#define _XTAL_FREQ 8000000
#include "configbits.h"
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include "flexlcd.h"
#include "teclado.h"
//identifiers

#define heater PORTCbits.RC5
#define cooler PORTCbits.RC2

// entradas (linhas), com pull-up externo
#define L1 PORTDbits.RD3
#define L2 PORTDbits.RD2
#define L3 PORTDbits.RD1
#define L4 PORTDbits.RD0

int OP = 0;
char manual[10];
char automatico[10];

void main(void)
{
    //ADCON0 = 0b00000000;
    ADCON1 = 0b00001111; //configura todos os pinos AD como I/O digital
    //ADCON2 = 0b00000000;
    //INTCON = 0b00000000;
    //INTCON2 = 0b00000000;
    // Input or Output
    
    //Interrupções
    INTCON2bits.INTEDG0 = 0; // 0 = Interrupt on falling edge
    INTCON3bits.INT1IP = 0;
    INTCON2bits.RBPU = 0;
    RCONbits.IPEN=1;
    // INT0IF: INT0 External Interrupt Flag bit
    INTCONbits.INT0IF = 0; // The INT0 external interrupt occurred (must be cleared in software)
    // INT0IE: INT0 External Interrupt Enable bit
    INTCONbits.INT0IE = 1; //1 = Enables the INT0 external interrupt
    INTCON3bits.INT2IF = 0; // The INT0 external interrupt occurred (must be cleared in software)
    INTCON2bits.RBIP = 0;
    INTCON3bits.INT2IP = 1;
    INTCON3bits.INT2IE = 1;
    INTCON2bits.INTEDG2 = 1;
    INTCON3bits.INT1IE = 1; //1 = Enables the INT0 external interrupt
    INTCONbits.GIEH = 1;
    
   // TRISA = 0xFF;
 //TRISB = 0b00111111;
    TRISBbits.RB0 = 1;
    TRISBbits.RB1 = 1;
    TRISBbits.RB2 = 1;
 TRISC = 0;
    
    Lcd_Init();
    Lcd_Cmd(LCD_CURSOR_OFF);
    char operacao[20];
    char estado[20];
    char constantes[20];
    ei();
    sprintf(manual, "ON");
    sprintf(automatico, "OFF");
    heater = 0;
    cooler = 0 ;
    while(1)
    {
        CLRWDT();
        sprintf(operacao, "ML: %s Auto: %s", manual, automatico);
        sprintf(estado, "Vent: Temp: ");
        sprintf(constantes, "P: I: D: ");
        
        Lcd_Out(1, 0, &operacao);
        Lcd_Out(2, 0, &estado);
        Lcd_Out(3, 0, &constantes);
        
    }
    return;
}

void interrupt isr(void){
    
    if (INTCONbits.INT0F == 1) //Caso o botão for acionado
    {
        INTCONbits.INT0F = 0; // Clear interrupt flag
        OP = !OP;
        if(OP == 1){
            sprintf(manual, "OFF");
            sprintf(automatico, "ON");
        }else{
            sprintf(manual, "ON");
            sprintf(automatico, "OFF");
        }
    }
     if(INTCON2bits.RBIP == 1){
         INTCON2bits.RBIP = 0;
         heater = !heater;
     }
    
    if(INTCON3bits.INT2IF == 1){
        INTCON3bits.INT2IF = 0;
        cooler = !cooler;
    }
     
    
}

#1

6 Replies Related Threads

    ric
    Super Member
    • Total Posts : 30223
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: how to use three interrupts on PIC18F4520? 2021/02/28 20:06:46 (permalink)
    +2 (2)
    You are using the wrong bits for INT1
    Change
         if(INTCON2bits.RBIP == 1){
             INTCON2bits.RBIP = 0;
             heater = !heater;
         }

    to
         if(INTCON3bits.INT1IF == 1){
             INTCON3bits.INT1IF = 0;
             heater = !heater;
         }

    Also, change
    #define heater PORTCbits.RC5
    #define cooler PORTCbits.RC2

    to
    #define heater LATCbits.LATC5
    #define cooler LATCbits.LATC2

    NEVER write individual bits via a PORTx register, always use LATx.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    rrodrigofranco
    New Member
    • Total Posts : 17
    • Reward points : 0
    • Joined: 2021/01/28 16:44:34
    • Location: 0
    • Status: offline
    Re: how to use three interrupts on PIC18F4520? 2021/02/28 20:25:44 (permalink)
    0
    I made the changes you asked for, but unfortunately it didn't work
     

    /*
    * File: mainDeb.c
    * Author: Alan Kardek
    *
    * Created on 12 de Novembro de 2020, 12:13
    */
    #define _XTAL_FREQ 8000000
    #include "configbits.h"
    #include <xc.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include "flexlcd.h"
    #include "teclado.h"
    //identifiers
    #define heater LATCbits.LATC5
    #define cooler LATCbits.LATC2
    int OP = 0;
    char manual[10];
    char automatico[10];
    void main(void)
    {
    //ADCON0 = 0b00000000;
    ADCON1 = 0b00001111; //configura todos os pinos AD como I/O digital
    //ADCON2 = 0b00000000;
    //INTCON = 0b00000000;
    //INTCON2 = 0b00000000;
    // Input or Output

    //Interrupções
    INTCON2bits.INTEDG0 = 0; // 0 = Interrupt on falling edge
    INTCON3bits.INT1IP = 0;
    INTCON2bits.RBPU = 0;
    RCONbits.IPEN=1;
    // INT0IF: INT0 External Interrupt Flag bit
    INTCONbits.INT0IF = 0; // The INT0 external interrupt occurred (must be cleared in software)
    // INT0IE: INT0 External Interrupt Enable bit
    INTCONbits.INT0IE = 1; //1 = Enables the INT0 external interrupt
    INTCON3bits.INT2IF = 0; // The INT0 external interrupt occurred (must be cleared in software)
    INTCON2bits.RBIP = 0;
    INTCON3bits.INT2IP = 1;
    INTCON3bits.INT2IE = 1;
    INTCON2bits.INTEDG2 = 1;
    INTCON3bits.INT1IE = 1; //1 = Enables the INT0 external interrupt
    INTCONbits.GIEH = 1;

    // TRISA = 0xFF;
    //TRISB = 0b00111111;
    TRISBbits.RB0 = 1;
    TRISBbits.RB1 = 1;
    TRISBbits.RB2 = 1;
    TRISC = 0;

    Lcd_Init();
    Lcd_Cmd(LCD_CURSOR_OFF);
    char operacao[20];
    char estado[20];
    char constantes[20];
    ei();
    sprintf(manual, "ON");
    sprintf(automatico, "OFF");
    heater = 0;
    cooler = 0 ;
    while(1)
    {
    CLRWDT();
    sprintf(operacao, "ML: %s Auto: %s", manual, automatico);
    sprintf(estado, "Vent: Temp: ");
    sprintf(constantes, "P: I: D: ");

    Lcd_Out(1, 0, &operacao);
    Lcd_Out(2, 0, &estado);
    Lcd_Out(3, 0, &constantes);

    }
    return;
    }
    void interrupt isr(void){

    if (INTCONbits.INT0F == 1) //Caso o botão for acionado
    {
    INTCONbits.INT0F = 0; // Clear interrupt flag
    OP = !OP;
    if(OP == 1){
    sprintf(manual, "OFF");
    sprintf(automatico, "ON");
    }else{
    sprintf(manual, "ON");
    sprintf(automatico, "OFF");
    }
    }
    if(INTCON3bits.INT1IF == 1){
    INTCON3bits.INT1IF = 0;
    heater = !heater;
    }

    if(INTCON3bits.INT2IF == 1){
    INTCON3bits.INT2IF = 0;
    cooler = !cooler;
    }
    }

    #3
    rrodrigofranco
    New Member
    • Total Posts : 17
    • Reward points : 0
    • Joined: 2021/01/28 16:44:34
    • Location: 0
    • Status: offline
    Re: how to use three interrupts on PIC18F4520? 2021/02/28 20:32:31 (permalink)
    0
    Now it's working. I just took off the RCONbits.IPEN=1. Thanks for help me!
     
    #4
    ric
    Super Member
    • Total Posts : 30223
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: how to use three interrupts on PIC18F4520? 2021/02/28 20:40:33 (permalink)
    +2 (2)
    Next, you have enabled interrupt priorities, but you only have a single interrupt service routine.
    Change
    RCONbits.IPEN=1;
    to
    RCONbits.IPEN=0;    // disable interrupt priorities
     
    Right now you are assigning INT1 to priority 0 (low), INT2 to priority 1 (high)
    (INT0 is always high priority)
    but you are not setting GIEL, so the low priority interrupts are not being serviced.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #5
    davea
    Super Member
    • Total Posts : 642
    • Reward points : 0
    • Joined: 2016/01/28 13:12:13
    • Location: Tampa Bay FL USA
    • Status: offline
    Re: how to use three interrupts on PIC18F4520? 2021/02/28 20:55:16 (permalink)
    0
    is there anything that prevents heater and cooler on at same time ??
    ie; heater = 1;
         cooler = 0;
    #6
    ric
    Super Member
    • Total Posts : 30223
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: how to use three interrupts on PIC18F4520? 2021/02/28 22:28:37 (permalink)
    +1 (1)
    davea
    is there anything that prevents heater and cooler on at same time ??
    ie; heater = 1;
         cooler = 0;

    I don't understand what exactly you are asking.
    Discussion of this code seems to have moved to https://www.microchip.com/forums/m1167630.aspx

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #7
    Jump to:
    © 2021 APG vNext Commercial Version 4.5