Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] dsPIC33FJ12MC202 RESET DURING TIMER 1 INTERRUPT

Status
Not open for further replies.

Dinuwilson

Advanced Member level 4
Joined
Aug 20, 2011
Messages
100
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,298
Activity points
1,985
I am working with 'dsPIC33FJ12MC202' timer 1,using interrupt method.But during timer 1(tmr1) register overflow (TMR1 and PR1 are equql), instead of jumping to interrupt subroutine , controller is resetting.

Please anyone help me to resolve this. (please tell me the definition of _T1Interrupt in which file(MPLAB XC compiler))
 

Hi,

to help you we need your code.
Also additional informations, like clock frequency and so on.
Do you use watchdog? Or is it switched off?

Klaus
 
// Watch Dog Timer is OFF //


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "common.h"  // Common file to define all #defines
#include "Ext_mcal_InOutConfig.h"
#include "Ext_mcal_TmrConfig.h"
#include "Ext_mcal_Irq.h"
 
/* Configuration Bits*/
/****************************************************************************************************/
/* FBS */
#pragma config BWRP     = WRPROTECT_OFF     /* Boot Segment Write Protect (Boot Segment may be written) */
#pragma config BSS      = LARGE_FLASH_STD   /* Boot Segment Program Flash Code Protection (Standard Security, Large-sized Boot Flash) */
/* FGS */
#pragma config GWRP     = OFF               /* General Code Segment Write Protect (User program memory is not write-protected) */
#pragma config GSS      = OFF               /* General Segment Code Protection (User program memory is not code-protected) */
/* FOSCSEL */
#pragma config FNOSC    = PRI               /* Oscillator Mode (Primary Oscillator (XT, HS, EC)) */
#pragma config IESO     = OFF               /* Internal External Switch Over Mode (Start-up device with user-selected oscillator source) */
/* FOSC */
#pragma config POSCMD   = HS                /* Primary Oscillator Source (HS Oscillator Mode) */
#pragma config OSCIOFNC = OFF               /* OSC2 Pin Function (OSC2 pin has clock out function) */
#pragma config IOL1WAY  = ON                /* Peripheral Pin Select Configuration (Allow Only One Re-configuration) */
#pragma config FCKSM    = CSDCMD            /* Clock Switching and Monitor (Both Clock Switching and Fail-Safe Clock Monitor are disabled) */
/* FWDT */
#pragma config WDTPOST  = PS32768           /* Watchdog Timer Postscaler (1:32,768) */
#pragma config WDTPRE   = PR128             /* WDT Prescaler (1:128) */
#pragma config WINDIS   = OFF               /* Watchdog Timer Window (Watchdog Timer in Non-Window mode) */
#pragma config FWDTEN   = OFF               /* Watchdog Timer Enable (Watchdog timer enabled/disabled by user software) */
/* FPOR */
#pragma config FPWRT    = PWR128            /* POR Timer Value (128ms) */
#pragma config ALTI2C   = OFF               /* Alternate I2C  pins (I2C mapped to SDA1/SCL1 pins) */
#pragma config LPOL     = ON                /* Motor Control PWM Low Side Polarity bit (PWM module low side output pins have active-high output polarity) */
#pragma config HPOL     = ON                /* Motor Control PWM High Side Polarity bit (PWM module high side output pins have active-high output polarity) */
#pragma config PWMPIN   = ON                /* Motor Control PWM Module Pin Mode bit (PWM module pins controlled by PORT register at device Reset) */
 
/* FICD */
#pragma config ICS      = PGD2              /* Comm Channel Select (Communicate on PGC2/EMUC2 and PGD2/EMUD2) */
#pragma config JTAGEN   = OFF               /* JTAG Port Enable (JTAG is Disabled) */
 
/****************************************************************************************************/
 
 
void main(void)
{
    /*  The following code example will enable Timer1 interrupts, load the Timer1
        Period register and start Timer1 using an asynchronous external clock and
        a 1:8 prescaler setting.
        When a Timer1 period match interrupt occurs, the interrupt service
        routine must clear the Timer1 interrupt status flag in software.
    */
    //Ext_mcal_AllPortClr();
    Ext_mcal_PORTAConfig(0x0000);
    Ext_mcal_TMR1Config();
    Ext_mcal_IrqTmr1Config();
    TMR1_CLR(0x0000);
    TMR1_STRT();
    IRQ_TMR1_ENABLE();
    while(1)
    {
 
    }
}
/* Example code for Timer1 ISR*/
void __attribute__((__interrupt__, no_auto_psv)) _T1Interrupt(void)
{
    if(IFS0bits.T1IF == 0x01)
    {
        IFS0bits.T1IF = 0; //Reset Timer1 interrupt flag and Return from ISR
        Ext_mcal_PORTASetVal(SET_BIT(0)); //porta  bit 1 on for testing
    }
    
}

 
Last edited by a moderator:

I am working with 'dsPIC33FJ12MC202' ....

Am I correct in assuming you are actually referring to the dsPIC33FJ128MC202, for as far as I know the dsPIC33FJ12MC202 doesn't exist?

If so, I'll change the thread title and your previous posts accordingly.

I'm unable to run either simulations or attempt to recreate the issue on actual hardware, due to the lack of access to the following header and accompanying source files:

Code:
#include "common.h" 
#include "Ext_mcal_InOutConfig.h"
#include "Ext_mcal_TmrConfig.h"
#include "Ext_mcal_Irq.h"

If they are provided by uploading or posting, I will certainly attempt to do so.

One possible issue is the omission of the typically required header file:

Code:
#include <xc.h>

Which is usually placed at the top of the source code preceding all other header files and the configuration bit settings.

Without access to the aforementioned header and source files, it's difficult to determine whether or not proper initialization and configuration of the interrupts and associated modules has occured.


BigDog
 

No, I am using 'dsPIC33FJ12MC202' only(please find attached Screen Shot MPLABX_SS.jpg).

and i have included #include <xc.h> in common.h

Below are my .h files

common.h

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* 
 * File:   common.h
 * 
 *
 * 
 */
 
#ifndef _COMMON_H_
#define _COMMON_H_
 
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
 
#define IF_DIAG_TEST 0
 
#define U8_T    unsigned char
#define S8_T    signed char
#define U16_T   unsigned int
#define S16_T   signed int
#define U32_T   unsigned long int
#define S32_T   signed long int
 
#define LOCAL   static
#define EXTERN 
 
#define SET_BIT(pos) ((U16_T)(0x0001<<pos))
 
#endif  /* COMMON_H */



Ext_mcal_InOutConfig.h

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* 
 * File:   Ext_mcal_InOutConfig.h
 * 
 *
 * 
 */
 
#ifndef _EXT_MCAL_INOUTCONFIG_H_
#define _EXT_MCAL_INOUTCONFIG_H_
 
#include "common.h"
 
 
extern void Ext_mcal_PORTBConfig(U16_T config_val_par);
extern void Ext_mcal_PORTAConfig(U16_T config_val_par);
extern void Ext_mcal_PORTBSetVal(U16_T set_val_par);
extern void Ext_mcal_PORTASetVal(U16_T set_val_par);
 
extern void Ext_mcal_AllPortClr(void);



Ext_mcal_Irq.h

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* 
 * File:   Ext_mcal_Irq.h
 * 
 *
 * 
 */
 
#ifndef _EXT_MCAL_IRQ_H_
#define _EXT_MCAL_IRQ_H_
 
#include "common.h"
 
#define IRQ_TMR1_ENABLE()   IEC0 |= (U16_T)0x0008
#define IRQ_TMR1_DSABLE()   IEC0 &= (U16_T)0xFFF7
 
 
#endif  /* _EXT_MCAL_IRQ_H_ */



Ext_mcal_TmrConfig.h

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/* 
 * File:   Ext_mcal_TmrConfig.h
 * 
 *
 * 
 */
 
#ifndef _EXT_MCAL_TMRCONFIG_H_
#define _EXT_MCAL_TMRCONFIG_H_
 
#include "common.h"
 
#define TMR1_CLR(val)  (TMR1 = (U16_T)(val))
#define TMR1_STRT()    (T1CON |= (U16_T)(0x8000))       /* T1CON <1> SET*/
#define TMR1_STOP()    (T1CON &= (U16_T)(0x7FFF))       /* T1CON <1> CLR*/
#define TMR1_PRSCL     (U16_T)(0x0030)                  /* T1CON <5-4>  */
#define TMR1_Flg_Clr() IFS0 &= 0xFFF7
 
extern void Ext_mcal_TMR1Config(void);
extern void Ext_mcal_TMR2Config(void);
extern void Ext_mcal_TMR3Config(void);
 
extern void Ext_mcal_IrqTmr1Config(void);
 
#endif  /* _EXT_MCAL_TMRCONFIG_H_ */



Ext_mcal_InOutConfig.c

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Ext_mcal_InOutConfig.c
 
#include "Ext_mcal_InOutConfig.h"
 
/*************************************************************/
EXTERN void Ext_mcal_PORTBConfig(U16_T config_val_par)
{
    TRISB |= config_val_par;
    //LATB   = (U16_T)0x0000;
}
/*************************************************************/
EXTERN void Ext_mcal_PORTAConfig(U16_T config_val_par)
{
    TRISA  = config_val_par;
    LATA   = (U16_T)0x0000;
}
/*************************************************************/
EXTERN void Ext_mcal_PORTBSetVal(U16_T set_val_par)
{
    LATB  |= set_val_par;
}
/*************************************************************/
EXTERN void Ext_mcal_PORTASetVal(U16_T set_val_par)
{
    LATA  |= set_val_par;
}
/*************************************************************/
EXTERN void Ext_mcal_AllPortClr(void)
{
    TRISA = (U16_T)0x0000;
    TRISB = (U16_T)0x0000;
    LATA  = (U16_T)0x0000;
    LATB  = (U16_T)0x0000;
}



Ext_mcal_Irq.c

Code C - [expand]
1
2
3
// Ext_mcal_Irq.c
 
#include "Ext_mcal_Irq.h"



Ext_mcal_TmrConfig.c

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Ext_mcal_TmrConfig.c
 
#include "Ext_mcal_TmrConfig.h"
 
EXTERN void Ext_mcal_TMR1Config(void) 
{
    T1CON  = (U16_T)((0x0000)|(TMR1_PRSCL));
    TMR1_CLR(0x0000);
    PR1    = (U16_T)(0xFFFF);
    TMR1_Flg_Clr();
}
 
EXTERN void Ext_mcal_TMR2Config(void) 
{
    /* Yet to add */
}
 
EXTERN void Ext_mcal_TMR3Config(void) 
{
    /* Yet to add */
}
 
EXTERN void Ext_mcal_IrqTmr1Config(void)
{
    SR      |= (U16_T)0x0060; /* CPU Prio 3        */
    IEC0    &= (U16_T)0xFFF7; /* Disable Interrupt */
    INTCON1 = (U16_T)0x8000;
    INTCON2 = (U16_T)0x0000;
    IFS0    = (U16_T)0x0000;
    IPC0    = (U16_T)0x4000;  /* TMR1 Prio 4       */
    INTTREG = (U16_T)0x0000;  
}

 
Last edited by a moderator:

No, I am using 'dsPIC33FJ12MC202' only

I don't know why I had forgotten about the dsPIC33FJ12 series, I actually have a PIM module with a dsPIC33FJ12MC202, other than I rarely use these low pin count devices.


I've posted the corrected code below, it now compiles and simulates without any issues.

I've made changes only within the main.c, rather than modify your other source files.

main.c

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "common.h"  // Common file to define all #defines
#include "Ext_mcal_InOutConfig.h"
#include "Ext_mcal_TmrConfig.h"
#include "Ext_mcal_Irq.h"
 
/* Configuration Bits*/
/****************************************************************************************************/
 
/* FBS */
#pragma config BWRP = WRPROTECT_OFF     // Boot Segment Write Protect (Boot Segment may be written)
#pragma config BSS = NO_FLASH           // Boot Segment Program Flash Code Protection (No Boot program Flash segment)
 
/* FGS */
#pragma config GWRP     = OFF               /* General Code Segment Write Protect (User program memory is not write-protected) */
#pragma config GSS      = OFF               /* General Segment Code Protection (User program memory is not code-protected) */
 
/* FOSCSEL */
#pragma config FNOSC    = PRI               /* Oscillator Mode (Primary Oscillator (XT, HS, EC)) */
#pragma config IESO     = OFF               /* Internal External Switch Over Mode (Start-up device with user-selected oscillator source) */
 
/* FOSC */
#pragma config POSCMD   = HS                /* Primary Oscillator Source (HS Oscillator Mode) */
#pragma config OSCIOFNC = OFF               /* OSC2 Pin Function (OSC2 pin has clock out function) */
#pragma config IOL1WAY  = ON                /* Peripheral Pin Select Configuration (Allow Only One Re-configuration) */
#pragma config FCKSM    = CSDCMD            /* Clock Switching and Monitor (Both Clock Switching and Fail-Safe Clock Monitor are disabled) */
 
/* FWDT */
#pragma config WDTPOST  = PS32768           /* Watchdog Timer Postscaler (1:32,768) */
#pragma config WDTPRE   = PR128             /* WDT Prescaler (1:128) */
#pragma config WINDIS   = OFF               /* Watchdog Timer Window (Watchdog Timer in Non-Window mode) */
#pragma config FWDTEN   = OFF               /* Watchdog Timer Enable (Watchdog timer enabled/disabled by user software) */
 
/* FPOR */
#pragma config FPWRT    = PWR128            /* POR Timer Value (128ms) */
#pragma config ALTI2C   = OFF               /* Alternate I2C  pins (I2C mapped to SDA1/SCL1 pins) */
#pragma config LPOL     = ON                /* Motor Control PWM Low Side Polarity bit (PWM module low side output pins have active-high output polarity) */
#pragma config HPOL     = ON                /* Motor Control PWM High Side Polarity bit (PWM module high side output pins have active-high output polarity) */
#pragma config PWMPIN   = ON                /* Motor Control PWM Module Pin Mode bit (PWM module pins controlled by PORT register at device Reset) */
 
/* FICD */
#pragma config ICS      = PGD2              /* Comm Channel Select (Communicate on PGC2/EMUC2 and PGD2/EMUD2) */
#pragma config JTAGEN   = OFF               /* JTAG Port Enable (JTAG is Disabled) */
 
 
/****************************************************************************************************/
 
 
int main(void)
{
    /*  The following code example will enable Timer1 interrupts, load the Timer1
        Period register and start Timer1 using an asynchronous external clock and
        a 1:8 prescaler setting.
        When a Timer1 period match interrupt occurs, the interrupt service
        routine must clear the Timer1 interrupt status flag in software.
    */
    
    volatile int temp;
    //Ext_mcal_AllPortClr();
    AD1PCFGL = 0xFF;  // Sets All Pins to Digital, Rather than Analog Inputs
    Ext_mcal_PORTAConfig(0x0000);
    Ext_mcal_TMR1Config();
    Ext_mcal_IrqTmr1Config();
    TMR1_CLR(0x0000);
    TMR1_STRT();
    IRQ_TMR1_ENABLE();
    
    while(1)
    {
        temp++;
    }
}
 
 
 
/* Example code for Timer1 ISR*/
void __attribute__((__interrupt__, __auto_psv__)) _T1Interrupt(void)
{
    if(IFS0bits.T1IF == 0x01)
    {
        IFS0bits.T1IF = 0; //Reset Timer1 interrupt flag and Return from ISR
        LATAbits.LATA0 = ~PORTAbits.RA0; // Flips the State of RA0 
        //Ext_mcal_PORTASetVal(SET_BIT(0)); //porta  bit 1 on for testing
    }
    
}



The first issue is the following configuration bit setting.

Replace:
Code:
#pragma config BSS = LARGE_FLASH_STD   /* Boot Segment Program Flash Code Protection (Standard Security, Large-sized Boot Flash) */

With
Code:
#pragma config BSS = NO_FLASH           // Boot Segment Program Flash Code Protection (No Boot program Flash segment)



Although no strictly necessary in this case, I would recommend using auto PSV on the interrupt routine and don't forget the double underscores.

Replace:
Code:
void __attribute__((__interrupt__, no_auto_psv)) _T1Interrupt(void)

With:
Code:
void __attribute__((__interrupt__, __auto_psv__)) _T1Interrupt(void)


While the above changes will allow the ISR to be called correctly and avoid the reset condition, many of the PORTA pins are set as ADC inputs by default, therefore to change the state of RA0, you must first set them to digital inputs with the following statement:

Code:
AD1PCFGL = 0xFF;  // Sets All Pins to Digital, Rather than Analog Inputs

Which now allows the RA0 pin to be configured as a digital output and set or cleared within the ISR.

Also, you should be aware that some compilers, particularly with high optimization levels set, can essentially optimize an empty while loop out of existence, therefore it's prudent to either insert an inline assembler NOP instruction or increment a volatile variable:

Code:
   volatile int temp;

    while(1)
    {
        temp++;
    }

And for more of an effect, I substituted a statement which flips the state of the RA0 pin on each ISR call:

Code:
     LATAbits.LATA0 = ~PORTAbits.RA0; // Flips the State of RA0

I've not had a chance yet to dig up my dsPIC33FJ12MC202 PIM, attach it to an Explorer board and test the code on hardware, however I do not foresee any issues.

So try it out on your hardware and post back to this thread if you have any other issues.


BigDog
 
it's start working.....Thanks for your support....
 

Glad to hear it is now functioning properly.


While I'm a bit of cautious user of predefined macros, one situation I do frequently make use of them is with the interrupt compiler directives:

Code:
#define _ISR_PSV __attribute__((__interrupt__, __auto_psv__))

void _ISR_PSV _T1Interrupt()
{
    LATAbits.LATA0 = ~LATAbits.LATA0;    //toggle/flip RA0 pin
    IFS0bits.T1IF = 0; //clear T1 interrupt flag
}

Which not only reduces the required typing, but reduces the chances of small errors creeping into your code.


BigDog
 

Yes I have tried this too. Working fine.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top