It is currently Wed Aug 20, 2014 5:31 pm



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Timer Interrupt
PostPosted: Fri Mar 23, 2012 7:44 am 
Offline

Joined: Thu Mar 22, 2012 10:46 pm
Posts: 3
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,


Top
 Profile  
 
 Post subject: Re: Timer Interrupt
PostPosted: Sat Mar 24, 2012 6:54 am 
Offline
User avatar

Joined: Sat May 28, 2011 6:34 am
Posts: 645
Location: Southern California
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:
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.


Top
 Profile  
 
 Post subject: Re: Timer Interrupt
PostPosted: Sat Mar 24, 2012 6:16 pm 
Offline

Joined: Thu Mar 22, 2012 10:46 pm
Posts: 3
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:


Top
 Profile  
 
 Post subject: Re: Timer Interrupt
PostPosted: Sat Mar 24, 2012 6:24 pm 
Offline

Joined: Thu Mar 22, 2012 10:46 pm
Posts: 3
#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 ;)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Theme designed by stylerbb.net © 2008
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All times are UTC [ DST ]