Determine what interrupt triggers

Thread Starter

FroceMaster

Joined Jan 28, 2012
702
Hi
Been struggeling a bit, and can't get my interrupts to work,
PIC16F690
Code:
#include <htc.h>
#include <stdio.h>
#include <stdlib.h>
#include "lcd.h"
#include <string.h>

// CONFIG
#pragma config FOSC = INTRCIO   // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
#pragma config WDTE = OFF        // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF      // MCLR Pin Function Select bit (MCLR pin function is digital input, MCLR internally tied to VDD)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON      // Brown-out Reset Selection bits (BOR enabled)
#pragma config IESO = OFF       // Internal External Switchover bit (Internal External Switchover mode is disabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)

#ifndef _XTAL_FREQ
// Unless specified elsewhere, 8MHz system frequency is assumed
#define _XTAL_FREQ 8000000
#endif
#define KN1 RC5
#define KN2 RC4
#define KN3 RC6
#define KN4 RC7
#define back_light RA2
unsigned int run_to_sec,sekund;

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
static void interrupt isr(void)   // Here is interrupt function - the name is unimportant.
{
  
       if (T0IF)   // timer 0 overflow
       {
       T0IF=0;  
                 run_to_sec++;
        if (run_to_sec==200)
        {run_to_sec=0;
        sekund=1;
       
       }
       }
    }
   


void setup(void)
{
    IRCF0=1; // 8 mhz
    IRCF1=1;
    IRCF2=1;
    SCS=1; // internel clock
    OSTS=0;
   
 
 
    // setup timer 0 to run on Internal FOSC/4
    T0CS=0;
    PSA=0;
    PS0=1;  // Prescale 1:256
    PS1=1;
    PS2=1;
  
   
TRISA0=1;
TRISA1=1;
TRISA2=0;
TRISA3=1;
TRISA4=1;
TRISA5=0;


TRISB4=0; // = out
TRISB5=0;
TRISB6=0;
TRISB7=0;

TRISC0=0; // Out
TRISC1=0;
TRISC2=1; // Other in;
TRISC3=0; // test LED
TRISC4=1;
TRISC5=1;
TRISC6=1;
TRISC7=1; // rc7 in.
ANSEL=0; // all digital.
ANSELH=0;
}


void main(void) {
    setup();
    lcd_init();
    lcd_goto(0);
    lcd_puts("Hello");
    RC3=0;
  
    GIE=1; // Global interrupt
    T0IE=1;
    T0IF=0;

    back_light=0;
    while (1)
Code is running, but Interrupt not triggered.
. will say when i put something in interrupts routine, the code is executed allways,( Dont know if this is the meaning to do ? )
 

jpanhalt

Joined Jan 18, 2008
11,087
I don't do C, but in Assembly, it is quite easy to determine which interrupt(s)* caused the interrupt by checking the flags and registers. You can use either a one-by-one tree (e.g, btfss or btfsc ...) or a table, or a mixture.

*More than one interrupt in a register my be set.
 

spinnaker

Joined Oct 29, 2009
7,830
I don't do C, but in Assembly, it is quite easy to determine which interrupt(s)* caused the interrupt by checking the flags and registers. You can use either a one-by-one tree (e.g, btfss or btfsc ...) or a table, or a mixture.

*More than one interrupt in a register my be set.

His title does not match the question in the post. :eek:
 

Picbuster

Joined Dec 2, 2013
1,047
Hi
Been struggeling a bit, and can't get my interrupts to work,
PIC16F690
Code:
#include <htc.h>
#include <stdio.h>
#include <stdlib.h>
#include "lcd.h"
#include <string.h>

// CONFIG
#pragma config FOSC = INTRCIO   // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
#pragma config WDTE = OFF        // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF      // MCLR Pin Function Select bit (MCLR pin function is digital input, MCLR internally tied to VDD)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON      // Brown-out Reset Selection bits (BOR enabled)
#pragma config IESO = OFF       // Internal External Switchover bit (Internal External Switchover mode is disabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)

#ifndef _XTAL_FREQ
// Unless specified elsewhere, 8MHz system frequency is assumed
#define _XTAL_FREQ 8000000
#endif
#define KN1 RC5
#define KN2 RC4
#define KN3 RC6
#define KN4 RC7
#define back_light RA2
unsigned int run_to_sec,sekund;

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
static void interrupt isr(void)   // Here is interrupt function - the name is unimportant.
{
 
       if (T0IF)   // timer 0 overflow
       {
       T0IF=0; 
                 run_to_sec++;
        if (run_to_sec==200)
        {run_to_sec=0;
        sekund=1;
      
       }
       }
    }
  


void setup(void)
{
    IRCF0=1; // 8 mhz
    IRCF1=1;
    IRCF2=1;
    SCS=1; // internel clock
    OSTS=0;
  


    // setup timer 0 to run on Internal FOSC/4
    T0CS=0;
    PSA=0;
    PS0=1;  // Prescale 1:256
    PS1=1;
    PS2=1;
 
  
TRISA0=1;
TRISA1=1;
TRISA2=0;
TRISA3=1;
TRISA4=1;
TRISA5=0;


TRISB4=0; // = out
TRISB5=0;
TRISB6=0;
TRISB7=0;

TRISC0=0; // Out
TRISC1=0;
TRISC2=1; // Other in;
TRISC3=0; // test LED
TRISC4=1;
TRISC5=1;
TRISC6=1;
TRISC7=1; // rc7 in.
ANSEL=0; // all digital.
ANSELH=0;
}


void main(void) {
    setup();
    lcd_init();
    lcd_goto(0);
    lcd_puts("Hello");
    RC3=0;
 
    GIE=1; // Global interrupt
    T0IE=1;
    T0IF=0;

    back_light=0;
    while (1)
Code is running, but Interrupt not triggered.
. will say when i put something in interrupts routine, the code is executed allways,( Dont know if this is the meaning to do ? )
Looks OK however;
add to
while (1) {
// I had the same problems with the pic 16f6xx series when the while loop is to fast.
// a delay: few nop's will do start with 1 mS to test.
}
 

AlbertHall

Joined Jun 4, 2014
12,346
I have loaded your code and have run it with the XC8 V2 compiler.
As I do not have your LCD code I have commented out the included LCD file and calls to LCD functions.
Your posted code needs a ';' after the 'while(1)' at the end of the ptogram and following that a '}' to close the main function.
Now your code runs and calls the interrupt routine every 65536 cycles.
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
702
reason for missing the closing brackets, is copy paste error. :)
Changed to timer2 and got rid of these troubles,
Function very well now, but the LCD init is givin me som problems,
Will create new post for that.
 
Last edited:
Top