Continue to Site

Welcome to EDAboard.com

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

problem using the timer0 with PIC18F4550, Please help. I am new to these things

Status
Not open for further replies.

jeswanth123

Junior Member level 2
Joined
Mar 6, 2011
Messages
20
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,471
Hello all,

I have a devl kit with pic18f4550. I am using programming the chip using the USB bootloader. The code given in the cd that came iwth the kit worked fine but its not working if i try to change the timing in the code. Bellow is the code that is working. A led is attached to the d1 and blinks.

I am looking for a code which makes the d1 pin or any pin go on for 1ms (milli second). or if possible 1us - 1 micro second. I am using mplab c18 lite. if you cannot read this post till end, leave it.

#include<p18f4550.h>

/** V A R I A B L E S ********************************************************/
#pragma udata

/** P R I V A T E P R O T O T Y P E S ***************************************/
extern void _startup (void); // See c018i.c in your C18 compiler dir
void Timer0_Init(void);
void High_ISR(void);
void Low_ISR(void);
/** V E C T O R R E M A P P I N G *******************************************/


#pragma code _RESET_INTERRUPT_VECTOR = 0x000800
void _reset (void)
{
_asm goto _startup _endasm
}
#pragma code

#pragma code _HIGH_INTERRUPT_VECTOR = 0x000808
void _high_ISR (void)
{
_asm goto High_ISR _endasm

}

#pragma code _LOW_INTERRUPT_VECTOR = 0x000818
void _low_ISR (void)
{
_asm goto Low_ISR _endasm
}
#pragma code


/** D E C L A R A T I O N S **************************************************/
#pragma code

//#pragma config WDT=OFF

void Timer0_Init()
{
T0CON=0b00000111; //Timer is OFF, Timer is 16 BIT,Int Clk,Prescaler is assigned
//Prescaler FOSC/256 Fosc=48Mhz
TMR0H=0xA4;
TMR0L=0x73; //500mSec Refer Datasheet for Timer calculations
INTCONbits.TMR0IF=0; //Clear Timer OverFlow flag
INTCONbits.TMR0IE=1; //Enable Timer Overflow interrupt
INTCONbits.PEIE=1; //Enable Peripheral Interrupt
T0CONbits.TMR0ON=1; //Start Timer
}


void main()
{
Timer0_Init(); //Initialixe Timer
INTCONbits.GIE=1; //Enable Global Interrupts
TRISDbits.TRISD1=0; //PORTD1 is O/p
LATDbits.LATD1=0; //clear PORTD1 pin
while(1)
{
//loop forever
}
}

#pragma interrupt High_ISR
void High_ISR()
{
if (INTCONbits.TMR0IF)
{
if(LATDbits.LATD1){LATDbits.LATD1=0;} //Toggle Led status
else {LATDbits.LATD1=1;}
INTCONbits.TMR0IF=0; // Clear timer ovflow flag
TMR0H=0xA4;
TMR0L=0x73; //500mSec
}
}

#pragma interruptlow Low_ISR
void Low_ISR(void)
{

}

Using this code the led blinks. It is given in the code that it blinks for 500ms.

TMR0H=0xA4;
TMR0L=0x73; //500mSec

If I change the above code to

TMR0H=0xff;
TMR0L=0xd0; //500mSec

The led is not blinking at all.

between 0xA4 and 0x73 there is a difference of 49.

and

between 0xff and 0xd0 there is a difference of 47.

I am confused with the calculation why does the above setting make a 500ms. A prescalar of 256 is also used in the code.

So, the led must blink, cause I have only changed the TMR0H and TMR0l values only in the code and nothing else. Everything except this is the same code. I, Have also tried setting timer to 8-bit.

If anyone out there can make a code which blinks the led for 1ms - 10ms, please do post it. I believe that led blinking for 1ms cannot be seen with naked eye. But I need this code to control another circuit, which runs for 1ms intervals.

Please do reply me. I know there are many professionals and gigs in this forum. Please help me.
 

Timer high and timer low combine to create hex 0xa473
This subtracted from 0xffff = 0x5b8c

Timer high and timer low combine to create hex 0xffd0
This subtracted from 0xffff = 0x002f


A clock of 48MHz sounds a bit too high for me, are you sure thats correct?

Anyway, 48/4 = 12000000 cycle time

timer value = 0.001 / ((1/12000000) * 256) = ~47.

So your value is correct.

Try using mplab sim, use the stopwatch and put a breakpoint in the interrupt routine.
Set the correct osc. frequency in the debugger settings. The stopwatch will tell you the interrupt frequency.
If it works in mplab sim, it usually works ok when you run it.
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top