Question about interruption with a PIC16F18345

Enhanced mid-range devices. PIC12F1xxx and PIC16F1xxx

Question about interruption with a PIC16F18345

Postby jo7 » Fri Jun 22, 2018 1:34 pm

Hello everyone,
I would like to make an interruption with a push button and an LED. I do not have much control over interruptions. Here is my current code but the problem is that my push button does not work when I make a support. On the other hand, the LED remains on. Thank you in advance ! :)

Code: Select all
// CONFIG1
#pragma config FEXTOSC = OFF        // FEXTOSC External Oscillator mode Selection bits (Oscillator not enabled)
#pragma config RSTOSC = HFINT1      // Power-up default value for COSC bits (HFINTOSC with 2x PLL (32MHz))
#pragma config CLKOUTEN = OFF       // Clock Out Enable bit (CLKOUT function is enabled; FOSC/4 clock appears at OSC2)
#pragma config CSWEN = ON           // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
#pragma config FCMEN = OFF          // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)

// CONFIG2
#pragma config MCLRE = ON           // Master Clear Enable bit (MCLR/VPP pin function is MCLR; Weak pull-up enabled)
#pragma config PWRTE = OFF          // Power-up Timer Enable bit (PWRT disabled)
#pragma config WDTE = OFF           // Watchdog Timer Enable bits (WDT disabled; SWDTEN is ignored)
#pragma config LPBOREN = OFF        // Low-power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = SBOREN       // Brown-out Reset Enable bits (Brown-out Reset enabled according to SBOREN)
#pragma config BORV = LOW           // Brown-out Reset Voltage selection bit (Brown-out voltage (Vbor) set to 2.45V)
#pragma config PPS1WAY = OFF        // PPSLOCK bit One-Way Set Enable bit (The PPSLOCK bit can be set and cleared repeatedly (subject to the unlock sequence))
#pragma config STVREN = OFF         // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will not cause a Reset)
#pragma config DEBUG = OFF          // Debugger enable bit (Background debugger disabled)

// CONFIG3
#pragma config WRT = OFF            // User NVM self-write protection bits (Write protection off)
#pragma config LVP = ON             // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/VPP pin function is MCLR. MCLRE configuration bit is ignored.)

// CONFIG4
#pragma config CP = OFF             // User NVM Program Memory Code Protection bit (User NVM code protection disabled)
#pragma config CPD = OFF            // Data NVM Memory Code Protection bit (Data NVM code protection disabled)

// PORT A
#define ICSPDAT         RA0                     // Port de programmation
#define POWER           RA1                     // Retour d etat charge batterie
#define MCLR            RA3                     // Port de programmation
#define Vactiv          RA5                     // Grille transistor ventilateur

// PORT B
#define ledR            RB7                     // +LED rouge

// PORT C
#define BP              RC5                     // Bouton poussoir

/*#define ON              0                       // Led tirée au +VCC
#define OFF             1                       // Led tiréé au gnd*/


//#define _XTAL_FREQ      8000000                // Oscillateur réglé à 8MHz

#include <xc.h>

int count = 0;

void interrupt Interrupt_bp (void)
{
       
    if(( PIE0bits.IOCIE ) && (PIR0bits.IOCIF))
        PIR0bits.IOCIF = 1;
        PIR0bits.TMR0IF = 0;        // RAZ flag IT
        count++;
        if (count==76){
            count = 0;
            ledR = ~ledR;
        }
    }



void main(void)
{
     
     // Reglages des entrees/sorties
    TRISA = 0b00001011;                         // Choix entrees/sorties
    LATA = 0b00000000;                          // RAZ des ports
    ANSELA = 0b00000000;                        // Choix mode analogique/numerique   
    WPUA = 0b00000010;                          // Resistances de pull-up interne
   
    TRISB = 0b00000000;                         // Choix entrees/sorties
    LATB = 0b10000000;                          // RAZ des ports
    ANSELB = 0b00000000;                        // Choix mode analogique/numerique
    WPUB = 0b00000000;                          // Resistances de pull-up interne
   
    TRISC = 0b00100000;                         // Choix entrees/sorties
    LATC = 0b11000000;;                         // RAZ des ports
    ANSELC = 0b000110000;                        // Choix mode analogique/numerique
    WPUC = 0b00100000;                          // Resistances de pull-up interne
 
    //Reglage des interuptions
    INTCONbits.GIE = 1;                         // Valide les interruptions generales
    INTCONbits.PEIE = 1;                        // Valide les interruptions peripheriques
    INTCONbits.INTEDG = 0;                      // Interruption sur un front descendant                     
   
    //Reglage des peripheriques
    PIE0bits.TMR0IE = 1;                       // Valide l'interruption generee par le debordement du Timer0
    PIE0bits.IOCIE = 1;                        // Valide le changement d'etat
    PIE0bits.INTE = 1;                         // Autoise l'interruption sur un peripherique exterieur
   
    PIR0bits.TMR0IF = 0;                       // RAZ flag  IT
    PIR0bits.IOCIF = 1;                        // Activation du front descendant
    PIR0bits.INTF = 1;                         // Detection d'une interruption
   
    while (1){
       
    }
}
Attachments
Capture_pic.PNG
Capture_pic.PNG (6.18 KiB) Viewed 5534 times
Capture8pic16f18345.PNG
Capture8pic16f18345.PNG (52.12 KiB) Viewed 5534 times
Capture_carte.PNG
Capture_carte.PNG (160.79 KiB) Viewed 5534 times
schema_led.png
schema_led.png (10.08 KiB) Viewed 5534 times
jo7
 
Posts: 4
Joined: Fri Jun 22, 2018 1:27 pm

Re: Question about interruption with a PIC16F18345

Postby ric » Sat Jun 23, 2018 3:35 am

Comes from https://www.microchip.com/forums/m1056450.aspx

I see you have changed your interrupt handling, but it is still incorrect.
Code: Select all
        PIR0bits.IOCIF = 1;
        PIR0bits.TMR0IF = 0;        // RAZ flag IT

should just be
Code: Select all
        PIR0bits.IOCIF = 0;


You have enabled three separate interrupt sources, but only put code in your ISR to handle one of them.

As suggested on the Microchip forum, you should start with simpler code which just reads a pin, and writes the result to an LED.
The key to efficient debugging is only work with one unknown factor at a time. Don't try to debug multiple things all at once.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: Question about interruption with a PIC16F18345

Postby ric » Sat Jun 23, 2018 3:43 am

On a separate topic, you should follow another rule of thumb.
Use PORTx registers to read inputs. (e.g. PORTA, PORTB, PORTC)
Use LATX registers to write to output pins.

So
#define ledR RB7 // +LED rouge
should be
#define ledR LATB7 // +LED rouge

Now, as a first test, do NOT enable interrupts. So
INTCONbits.GIE = 1; // Valide les interruptions generales
becomes
INTCONbits.GIE = 0; // Valide les interruptions generales

then, change
Code: Select all
    while (1){
       
    }

to
Code: Select all
    while (1){
        ledR = BP;    // copy push-button status to LED
    }


and confirm that the LED changes with the push-button.
Once you know the push-button and LED are working correctly, you can move into interrupts.

My next rule of thumb is, it is very rarely a good idea to use interrupts to detect a push-button at all.
Only do it if you are going into a very low current sleep mode where the interrupt must wake up the processor.
In all other situations, poll the push-button state in a timer interrupt. anything from 20 to 100 times a second.
That makes it much easier to work around "button bounce".
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: Question about interruption with a PIC16F18345

Postby AussieSusan » Mon Jun 25, 2018 4:16 am

...and my 'rule of thumb' is never set the DEBUG config item to anything. Leave it out completely. The IDE will set it correctly depending on whether you are doing a 'Release' or 'Debug' build.
Susan
AussieSusan
Verified identity
 
Posts: 173
Joined: Mon Jun 16, 2014 4:45 am
PIC experience: Experienced Hobbyist

Re: Question about interruption with a PIC16F18345

Postby jo7 » Mon Jun 25, 2018 11:03 am

Hello thanks for all this information. I would now like to use this button to activate sleep mode of the μc and return to normal mode on a second press. What would be the right configuration?
jo7
 
Posts: 4
Joined: Fri Jun 22, 2018 1:27 pm

Re: Question about interruption with a PIC16F18345

Postby ric » Mon Jun 25, 2018 9:42 pm

I think trying to get into sleep mode is the LAST thing you should try to implement.
Get past the "flashing an LED", and "reliably detecting a pushbutton press" phase first.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products


Return to 14-Bit Core (enhanced)

Who is online

Users browsing this forum: No registered users and 3 guests

cron