| Author |
Message |
elrayes
Joined: 03 Jun 2008 Posts: 164 Helped: 8 Location: Egypt
|
09 Jun 2009 14:39 c18 timer 0 |
|
|
|
|
Hi all
I just want to use timer0 in PIC18F4550 and really i'm tired of reading and trying, beside i'm a very bad programmer, so try to help me in this code, I'm using MPLAB C18 compiler, this code is used to light some LED's on PORT D when the timer expires, i used ISR to implement all this, the code building is fine and downloaded on the target but the MCU isn't working:
#include<p18f4550.h>
#include<stdlib.h>
#include<timers.h>
#pragma interrupt high_isr
#pragma interruptlow low_isr
void init_int(void)
{
//configuring interrupts
INTCON = 0xA0;
INTCON2 = 0x04;
}
#pragma code high_vector=0x08
void interrupt_at_high_vector(void)
{
_asm goto high_isr _endasm
}
#pragma code
#pragma code low_vector=0x18
void interrupt_at_low_vector(void)
{
_asm goto low_isr _endasm
}
#pragma code
#pragma interrupt high_isr
void high_isr(void)
{
if(INTCONbits.TMR0IF == 1)
{
INTCONbits.TMR0IF = 0;
PORTD = 0x0f;
}
}
#pragma interruptlow low_isr
void low_isr(void)
{
}
#pragma code
int x = 0x0000;
void main(void)
{
TRISD = 0x00;
LATD = 0x00;
OpenTimer0( TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_8);
WriteTimer0(0x0000);
while(1)
{
high_isr();
}
}
hope someone could solve this.
thanks
|
|
| Back to top |
|
 |
L Varalakshmi
Joined: 28 May 2009 Posts: 15 Helped: 2
|
10 Jun 2009 9:51 pic18f4550 timer |
|
|
|
|
Just an advice....
You want the LEDs to glow based on the Interrupt concept and not the Polling concept. But here you are calling the function "high_isr" in the while loop, which is an ISR. Once the timer expires it will automatically calls the ISR.
Once check with the interrupt and polling concepts to make your program work.
|
|
| Back to top |
|
 |
ravimarcus
Joined: 09 May 2005 Posts: 204 Helped: 19 Location: Bangalore, INDIA
|
14 Jun 2009 11:27 pic18f prescaler timer 2 not working |
|
|
|
|
Have you assigned high priority to the timer0 interrupt?
Cheers
Ravi
|
|
| Back to top |
|
 |
elrayes
Joined: 03 Jun 2008 Posts: 164 Helped: 8 Location: Egypt
|
14 Jun 2009 12:05 latbbits.latb0 |
|
|
|
|
actually i changed the above code to another one i found in C18 examples by microchip and still not working and really i'm in a mess, this is the new code:
#include <p18f4550.h>
#include<timers.h>
//----------------------------------------------------------------------------
void main (void);
void InterruptHandlerHigh (void);
union
{
struct
{
unsigned Timeout:1; //flag to indicate a TMR0 timeout
unsigned None:7;
} Bit;
unsigned char Byte;
} Flags;
//----------------------------------------------------------------------------
// Main routine
void
main ()
{
Flags.Byte = 0;
INTCON = 0x20; //disable global and enable TMR0 interrupt
INTCON2 = 0x84; //TMR0 high priority
RCONbits.IPEN = 1; //enable priority levels
TMR0H = 0; //clear timer
TMR0L = 0; //clear timer
T0CON = 0x87; //set up timer0 - prescaler 1:8//0x82
//T0CON = 0xB8; //no pre-scaler assigned
//T0CON = 0b10011000; //no pre-scaler assigned
INTCONbits.GIEH = 1; //enable interrupts
TRISB = 0;
TRISA = 0;
//LATA = 0x00;
while (1)
{
PORTA = 0x01;
if (Flags.Bit.Timeout == 1)
{ //timeout?
Flags.Bit.Timeout = 0; //clear timeout indicor
LATBbits.LATB7 = LATBbits.LATB0; //copy LED state from RB0 to RB7
}
}
}
//----------------------------------------------------------------------------
// High priority interrupt vector
#pragma code InterruptVectorHigh = 0x08
void
InterruptVectorHigh (void)
{
_asm
goto InterruptHandlerHigh //jump to interrupt routine
_endasm
}
//----------------------------------------------------------------------------
// High priority interrupt routine
#pragma code
#pragma interrupt InterruptHandlerHigh
void
InterruptHandlerHigh ()
{
if (INTCONbits.TMR0IF)
{ //check for TMR0 overflow
INTCONbits.TMR0IF = 0; //clear interrupt flag
Flags.Bit.Timeout = 1; //indicate timeout
LATBbits.LATB0 = !LATBbits.LATB0; //toggle LED on RB0
}
}
high priority interrupts enabled
anyone can help??
thanks in advance
|
|
| Back to top |
|
 |
Google AdSense

|
14 Jun 2009 12:05 Ads |
|
|
|
|
|
|
| Back to top |
|
 |
ravimarcus
Joined: 09 May 2005 Posts: 204 Helped: 19 Location: Bangalore, INDIA
|
14 Jun 2009 12:12 c18 timer interupt |
|
|
|
|
1. I am not that good in C. Have you assigned timer0 interrupt to high priority?
2. Have you simulated in MPLAB SIM? You will get to know the problem.
Cheers
Ravi
|
|
| Back to top |
|
 |
L Varalakshmi
Joined: 28 May 2009 Posts: 15 Helped: 2
|
15 Jun 2009 9:58 pic18f4620 prescaler timer0 |
|
|
|
|
Check with the configuration settings, your board should support the oscillator settings you provide in that.
Ex: incase you gave 4*HSPLL and crystal oscillator is at around 25MHz, it gives around 4*25MHz = 100MHz. But if the controller you are using supports less than this (<100MHz) your code doesn't work with that.
Also, you didn't load values to timer registers(TMR0L)
Initialise every time the registers in the ISR.
|
|
| Back to top |
|
 |