Hi.I'm new in IAR pic18 and trying to drive 18f452 tmr0 .Here is my code
#include <intrinsics.h>
#include <io18f452.h>
//========================================================
#pragma vector=0x0008
__interrupt void ISR (void)
{
if(TMR0IF)
{
TMR0IE=0;
TMR0IF=0;
PORTD=PORTD^255;
TMR0IE=1;
}
}
//========================================================
int main(void)
{
__set_configuration_bits(CONFIG1H,HSPLL_OSC_1);
__set_configuration_bits(CONFIG2L,PWRT_ON_2&BOR_OFF_2);
__set_configuration_bits(CONFIG2H,WDT_OFF_3);
__set_configuration_bits(CONFIG3H,CCP2MX_ON_5);
__set_configuration_bits(CONFIG4L,LVP_OFF_6);
__set_configuration_bits(CONFIG5L,CP_OFF_8);
TRISB=0;PORTB=0;
TRISD=0;PORTD=0;
T0CON=0x87;
TMR0IE=1;
TMR0IP=1;
IPEN=1;
GIEH=1;
GIEL=1;
PEIE=1;
while(1)
{
RB0++;
}
return 0;
}
It works in Proteus but not work in real.The main section (RB0++) works when the above line(#pragma) and the whole interrupt block remove.can you help me.Thanks
arash,
I don't have IAR pic18 but look at your program and what you said i have something to say.
What do you want to do with the intrupt? I see your interupt got nothing to do with your while loop because in the while loop you changing RB0 (PORTB,0) and in the interupt you do something with PORTD.
To check to see if your while loop and pin0 of portB work.
Let create a variable for counter:
int Counter1;
and start with zero:
Counter1 = 0;
Now in your while loop increase Counter1 and check to see if it reach a number than change the state of pin RB0.
Counter1 += 1; // increase by one
if(Counter1 == 50)
{
//toggle RB0; // what is toggle in your IAR?? (if you don't know like me than use next line)
__asm BTG RB0 __endasm // use inline assembly to toggle pin RB0 (hope this inline support by IAR)
Counter1 = 0; // clear Counter1 so we start the counter from 0 to 50, that way RB0 will toggle every 50 count.
}
So your while loop will looks like this:
while(1)
{
Counter1 += 1; // increase by one
if(Counter1 == 50)
{
//toggle RB0; // what is toggle in your IAR?? (if you don't know like me than use next line)
__asm BTG RB0 __endasm // use inline assembly to toggle pin RB0 (hope this inline support by IAR)
Counter1 = 0; // clear Counter1 so we start the counter from 0 to 50, that way RB0 will toggle every 50 count.
}
}
Or you can use delay for some mS (look in your library for delay, I work with MPLAB C18 so I don't know your compiler)
delaymS(20) //delay 20mS;
RB0 = 0;
delaymS(20) //delay 20mS;
RB0 = 1;
After you have your while loop work than debug your interupt routine. Your interupt routine look OK but you are using 1:128 prescale 8 bits, so it may be too long for next interupt to occur in Proteus. I would suggest to set T0CON=0x81; for 8 bits and prescale :4).
Hope this help,
Tom