jeswanth123
Junior Member level 2

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.
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.