Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic 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.

Register Log in

Multiple timer interrupt for PIC18F4520

Status
Not open for further replies.

tongwe3bw

Newbie level 1
Joined
Jul 1, 2011
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,329
Hi,

I need some help resolving this issue on PIC18F4520. I am trying to run multiple timer, timer0,timer1 and timer3. Each timer will generate short pulse repeatedly, and the pulse is intend to control the position of the servomotor.

if I program in this way:
--------------------------------------------------------------------------------------------------------------------
#include <p18f4520.h>

#define servo1 PORTBbits.RB0
#define servo2 PORTBbits.RB1
#define servo3 PORTBbits.RB2

#pragma config WDT = OFF // turn off watch dog timer

void init (void);
void InterruptAct();
void OnT0(void);
void OnT1(void);
void OnT3(void);

unsigned char servoCon1,servoCon2,servoCon3;
#pragma code Interrupt = 0x08 //interrupt pointer address 0x18 low priority
void Interrupt(void)
{
_asm //assembly code starts
goto InterruptAct //interupt control
_endasm //assembly code ends
}
#pragma code

#pragma interrupt InterruptAct
void InterruptAct()
{
if(INTCONbits.TMR0IF==1)
{
if(servoCon1 == 1)
{
servo1 = 0;
servoCon1 = 0;

TMR0H=0X3C;
TMR0L=0XAF;//20ms
}
else
{
servo1 = 1;
servoCon1 = 1;

TMR0H=0XEE;
TMR0L=0X6B;
}

INTCONbits.TMR0IF=0;
}


}

#pragma code main=0x30
void main (void)
{
init();
OnT0();
OnT1();
OnT3();

while(1)
{

}
}

void init (void)
{
/************************************************/
/* Port setup */
/************************************************/

//TRISBbits.TRISB0=0;
//TRISBbits.TRISB7=0;
TRISB = 0x00;
TRISC = 0x00; //declare as output
TRISD = 0x00; //declare as output

servo1 = 1;


/************************************************/
/* Timer setup */
/************************************************/
//timer0
T0CON=0b00000000;
TMR0H=0X3C;
TMR0L=0XAF;
//timer1
T1CON=0b11010100;
TMR1H=0X3C;
TMR1L=0XAF;
//timer3
T3CON=0b11010100;
TMR3H=0X3C;
TMR3L=0XAF;
//Interrupts
INTCONbits.TMR0IE=1; //Enables the TMR0 overflow interrupt
PIE1bits.TMR1IE=1; //Enables the TMR1 overflow interrupt
PIE2bits.TMR3IE=1; //Enables the TMR3 overflow interrupt
INTCONbits.TMR0IF=0; //TMR0 register clear overflow
PIR1bits.TMR1IF=0; //TMR1 register clear overflow
PIR2bits.TMR3IF=0; //TMR3 register clear overflow

INTCONbits.PEIE=1; //Enables all unmasked peripheral interrupts
INTCONbits.GIE=1; //Enables all unmasked interrupts

}

void OnT0(void)
{
T0CONbits.TMR0ON=1;
}

void OnT1(void)
{
T1CONbits.TMR1ON=1;
}
void OnT3(void)
{
T3CONbits.TMR3ON=1;
}
--------------------------------------------------------------------------------------------------

This code will generate nice pulse at RB0 output.
But when I try to have 3 timers generate interrupt to generate individual pulse width:
-----------------------------------------------------------------------------------------------------
#include <p18f4520.h>

#define servo1 PORTBbits.RB0
#define servo2 PORTBbits.RB1
#define servo3 PORTBbits.RB2

#pragma config WDT = OFF // turn off watch dog timer

void init (void);
void InterruptAct();
void OnT0(void);
void OnT1(void);
void OnT3(void);

unsigned char servoCon1,servoCon2,servoCon3;
#pragma code Interrupt = 0x08 //interrupt pointer address 0x18 low priority
void Interrupt(void)
{
_asm //assembly code starts
goto InterruptAct //interupt control
_endasm //assembly code ends
}
#pragma code

#pragma interrupt InterruptAct
void InterruptAct()
{
if(INTCONbits.TMR0IF==1)
{
if(servoCon1 == 1)
{
servo1 = 0;
servoCon1 = 0;

TMR0H=0X3C;
TMR0L=0XAF;//20ms
}
else
{
servo1 = 1;
servoCon1 = 1;

TMR0H=0XEE;
TMR0L=0X6B;
}

INTCONbits.TMR0IF=0;
}

if(PIR1bits.TMR1IF==1)
{
if(servoCon2 == 1)
{
servo2 = 0;
servoCon2 = 0;

TMR1H=0X3C;
TMR1L=0XAF;//20ms
}
else
{
servo2 = 1;
servoCon2 = 1;

TMR1H=0XEE;
TMR1L=0X6B;
}
PIR1bits.TMR1IF=0;
}

if(PIR2bits.TMR3IF==1)
{
if(servoCon3 == 1)
{
servo3 = 0;
servoCon3 = 0;

TMR3H=0X3C;
TMR3L=0XAF;//20ms
}
else
{
servo3 = 1;
servoCon3 = 1;

TMR3H=0XEE;
TMR3L=0X6B;
}
PIR2bits.TMR3IF=0;
}
}

#pragma code main=0x30
void main (void)
{
init();
OnT0();
OnT1();
OnT3();

while(1)
{

}
}

void init (void)
{
/************************************************/
/* Port setup */
/************************************************/

//TRISBbits.TRISB0=0;
//TRISBbits.TRISB7=0;
TRISB = 0x00;
TRISC = 0x00; //declare as output
TRISD = 0x00; //declare as output

servo1 = 1;


/************************************************/
/* Timer setup */
/************************************************/
//timer0
T0CON=0b00000000;
TMR0H=0X3C;
TMR0L=0XAF;
//timer1
T1CON=0b11010100;
TMR1H=0X3C;
TMR1L=0XAF;
//timer3
T3CON=0b11010100;
TMR3H=0X3C;
TMR3L=0XAF;
//Interrupts
INTCONbits.TMR0IE=1; //Enables the TMR0 overflow interrupt
PIE1bits.TMR1IE=1; //Enables the TMR1 overflow interrupt
PIE2bits.TMR3IE=1; //Enables the TMR3 overflow interrupt
INTCONbits.TMR0IF=0; //TMR0 register clear overflow
PIR1bits.TMR1IF=0; //TMR1 register clear overflow
PIR2bits.TMR3IF=0; //TMR3 register clear overflow

INTCONbits.PEIE=1; //Enables all unmasked peripheral interrupts
INTCONbits.GIE=1; //Enables all unmasked interrupts

}

void OnT0(void)
{
T0CONbits.TMR0ON=1;
}

void OnT1(void)
{
T1CONbits.TMR1ON=1;
}
void OnT3(void)
{
T3CONbits.TMR3ON=1;
}
------------------------------------------------------------------
The interrupts seem to be conflict at one another and only RB2 have pulse output. No pulse at RB0 and RB1.

Appreciate if any help is given.
 

assasinsareus

Junior Member level 3
Joined
Jul 21, 2010
Messages
25
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,283
Activity points
1,428
Hello,

Try using LATB instead of PORTB for your output pin definitions this should help. Flowcode has a built in component to handle servo motors.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top