Timer Interrupt

Melancolia
Posts: 3
Joined: Thu Mar 22, 2012 10:46 pm

Timer Interrupt

Postby Melancolia » Fri Mar 23, 2012 7:44 am

Hello everyone,

I try to do a simple test with the timer of my chipkit max 32.
I want to turn on a LED with an interruption of the Timer1.
I try this code, but it doesn't function.

Can you help me please ?




// INITIALISATION (rapport cyclique, frequence pwm)
void setup()
{
OC1RS = 0x00001000; // Définition du rapport cyclique
OC1CON = 0x0006;// Configuration en mode PWM
T2CONSET = 0x0008;// Timer en mode 32 bits
PR2 = 0x00002000;// Période de la pwm ici 10kHz
T2CONSET = 0x8000;// Enable Timer2
OC1CONSET = 0x8020;// Enable OC1 in 32-bit mode.
OC1CONSET = 0x8000;// Enable OC1
attachInterrupt (0, change_pwm, RISING); // Déclaration de l'interruption (fonction : change_pwm, pin d'interruption : 3)
interrupts(); // activation des interruptions
}



// MAIN
void loop()
{
while (1)
{
// Ne rien faire
}
}




// INTERRUPTION
void change_pwm()
{
// Une fois sur deux : rapport cyclique de 25%
if (altern==1)
{
altern=0;
OC1RS = 0x00000800;
}
// Une fois sur deux : rapport cyclique de 50%
else
{
altern=1;
OC1RS = 0x0001000;
}
}





I apologize for my English, but i'm french...

Thank you !

Melancolia,

User avatar
Jacob Christ
Posts: 682
Joined: Sat May 28, 2011 6:34 am
Location: Southern California
Contact:

Re: Timer Interrupt

Postby Jacob Christ » Sat Mar 24, 2012 6:54 am

Melancolia wrote:Hello everyone,

// MAIN
void loop()
{
while (1)
{
// Ne rien faire
}
}



The while loop inside loop() is not needed...

Melancolia wrote:Hello everyone,

// INTERRUPTION
void change_pwm()
{
// Une fois sur deux : rapport cyclique de 25%
if (altern==1)
{
altern=0;
OC1RS = 0x00000800;
}
// Une fois sur deux : rapport cyclique de 50%
else
{
altern=1;
OC1RS = 0x0001000;
}
}





Where does change_pwm() come from? Is this an Arduino abstraction for an interrupt?

My interrupts tend to look like this:

Code: Select all

extern "C"
{
void __ISR(_TIMER_3_VECTOR,ipl3) pwmOn(void)
{
  mT3ClearIntFlag();  // Clear interrupt flag
}
}


Jacob
PONTECH Quick240 an industrial form factor platform for chipKIT and Arduino users.

Melancolia
Posts: 3
Joined: Thu Mar 22, 2012 10:46 pm

Re: Timer Interrupt

Postby Melancolia » Sat Mar 24, 2012 6:16 pm

I apologize, i do a failure and don't give my good code...


#include <plib.h>

void setup()
{
T1CON = 0x0; // Stop the Timer and Reset Control register
// Set prescaler at 1:1, internal clock source
TMR1 = 0x0; // Clear timer register
PR1 = 0xFFFF; // Load period register
IPC1SET = 0x000C; // Set priority level=3
IPC1SET = 0x0001; // Set subpriority level=1
IFS0CLR = 0x0010; // Clear Timer interrupt status flag
IEC0SET = 0x0010; // Enable Timer interrupts
T1CONSET = 0x8000; // Start Time
digitalWrite(13, LOW); => led mise à zéro
}

void loop()
{
while(1)
{}
}


void __ISR(_TIMER_1_VECTOR, ipl3) Timer1Handler(void)
{
digitalWrite(13, HIGH); // changement de la led => on l'allume
IFS0CLR = 0x0010; //Clear the Timer 1 interrupt status
}




That's better :roll:

Melancolia
Posts: 3
Joined: Thu Mar 22, 2012 10:46 pm

Re: Timer Interrupt

Postby Melancolia » Sat Mar 24, 2012 6:24 pm

#include <plib.h>

void setup()
{
T1CON = 0x0; // Stop the Timer and Reset Control register
// Set prescaler at 1:1, internal clock source
TMR1 = 0x0; // Clear timer register
PR1 = 0xFFFF; // Load period register
IPC1SET = 0x000C; // Set priority level=3
IPC1SET = 0x0001; // Set subpriority level=1
IFS0CLR = 0x0010; // Clear Timer interrupt status flag
IEC0SET = 0x0010; // Enable Timer interrupts
T1CONSET = 0x8000; // Start Time
digitalWrite(13, LOW); //=> led mise à zéro
}




extern "C"
{
void __ISR(_TIMER_1_VECTOR,ipl3) pwmOn(void)
{
digitalWrite(13, LOW);
mT3ClearIntFlag(); // Clear interrupt flag
}
}



This program functions :D
I really thank you very much Jacob Christ ! You help me a lot ;)


Return to “chipKIT Boards”

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 1 guest