chipKIT® Development Platform

Inspired by Arduino™

Chipkit Max32 Timer Problem

Created Sat, 27 Sep 2014 18:10:43 +0000 by davidjuliang


davidjuliang

Sat, 27 Sep 2014 18:10:43 +0000

Hello.

I am testing my Chipkit Max 32 Board with the PIC32MX Timer1 API Example (from Microchip site), which is a LED Blinking program. I am using MPLAB X IDE and C32 compiler. The LED is supposed to blink every second. However, the LED blinks like 4 o 5 times per second. Does anyone know why this is happening?

Thanks,

Here is the Code:

#include <plib.h>

#if defined (__32MX360F512L__) || (__32MX460F512L__) || (__32MX795F512L__) || (__32MX430F064L__) || (__32MX450F256L__) || (__32MX470F512L__)
// Configuration Bit settings
// SYSCLK = 80 MHz (8MHz Crystal / FPLLIDIV * FPLLMUL / FPLLODIV)
// PBCLK = 10 MHz (SYSCLK / FPBDIV)
// Primary Osc w/PLL (XT+,HS+,EC+PLL)
// WDT OFF
// Other options are don't care
#pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF
#pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_8
#define SYS_FREQ (80000000L)

#elif defined (__32MX220F032D__) || (__32MX250F128D__)
// Configuration Bit settings
// SYSCLK = 40 MHz (8MHz Crystal / FPLLIDIV * FPLLMUL / FPLLODIV)
// PBCLK = 10 MHz (SYSCLK / FPBDIV)
// Primary Osc w/PLL (XT+,HS+,EC+PLL)
// WDT OFF
// Other options are don't care
#pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_2, FWDTEN = OFF
#pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_4
#define SYS_FREQ (40000000L)
#endif

// Period needed for timer 1 to trigger an interrupt every 1 second
// (10MHz PBCLK / 256 = 39,062KHz Timer 1 clock)
#define PERIOD  39062

int main(void)
{
    // Configure cache, wait states and peripheral bus clock
    // Configure the device for maximum performance but do not change the PBDIV
    // Given the options, this function will change the flash wait states, RAM
    // wait state and enable prefetch cache but will not change the PBDIV.
    // The PBDIV value is already set via the pragma FPBDIV option above...
    SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);

    // Configure Timer 1 using PBCLK as input, 1:256 prescaler
    // Period matches the Timer 1 frequency, so the interrupt handler
    // will trigger every one second...
    OpenTimer3(T3_ON | T3_SOURCE_INT | T3_PS_1_256, PERIOD);

    // Set up the timer interrupt with a priority of 2
    INTEnable(INT_T3, INT_ENABLED);
    INTSetVectorPriority(INT_TIMER_3_VECTOR, INT_PRIORITY_LEVEL_1);
    INTSetVectorSubPriority(INT_TIMER_3_VECTOR, INT_SUB_PRIORITY_LEVEL_0);

    // Enable multi-vector interrupts
    INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
    INTEnableInterrupts();

    // Explorer-16 LEDs are on lower 8-bits of PORTA and to use all LEDs, JTAG port must be disabled.
    mJTAGPortEnable(DEBUG_JTAGPORT_OFF);

    // Clear PORTA bits so there are no unexpected flashes when setting
    // them to output in the next step
    mPORTAClearBits(BIT_3);

    // Set PORTA bits as output
    mPORTASetPinsDigitalOut(BIT_3);

    while (1);
    return 0;
}

// Configure the Timer 1 interrupt handler
void __ISR(_TIMER_3_VECTOR, ipl2) Timer3Handler(void)
{
    // Clear the interrupt flag
    INTClearFlag(INT_T3);

    // Toggle LEDs on the Explorer-16
    mPORTAToggleBits(BIT_3);
}

Regards, David


tom21091

Tue, 19 Jan 2016 18:54:45 +0000

Hi David,

I found your post looking through old unanswered posts. I am sorry nobody responded, but I'll pick this up in case somebody else has a similar problem. I tried replicating your problem, but my board's LED blinks at the correct rate. Did you ever figure out what was causing the Timer interrupt to trigger so fast?

Tommy


davidjuliang

Tue, 19 Jan 2016 20:36:43 +0000

Hello Tom,

I did solved the problem. I was programming the ChipKit through the USB cable (bootloader), without using a Microchip Programmer (Such as Pickit 3). It looks like this method of programming does not change the config bits using #PRAGMA. Thus, the preescalers FPLLMUL, FPLLIDIV and FPLLODIV did not change. I solved this problem by changing the TMRX Prescaler. If a Pickit 3 Programmer is used, none of this happens.

Regards, David


majenko

Tue, 19 Jan 2016 20:54:40 +0000

Yes, when you use the bootloader the config bits are pre-set by the bootloader and cannot be changed. When you switch to using a hardware programmer you replace the bootloader and consequently write your own config bits.


tom21091

Tue, 19 Jan 2016 20:55:40 +0000

Okay, awesome. Thanks for your response! Once again, sorry nobody got back to you much sooner. We will be paying more attention to the chipKIT forums from here on out. We also have a support forum at [url]https://forum.digilentinc.com/[/url] that might have some answers if you need any more help.

Thanks!

Tommy