Pic Project-2 RB0/INT

Thread Starter

royalmadhu

Joined Feb 4, 2011
6
hi i'm now making a pulse counter
i'm supplying pulses externally to the pic 16f877a and i want to get the count to display..

the displaying funtion is done..

but i'm not sure how to handle the RB0/int

this is my interrupt function

void interrupt()
{
if(INTCON.INTF==1)
{
count9=count9+1;
if(count9==999)
{
count9=0;
}
intcon.intf=0;
}
}


void main()
{
int a,b,c,d,k,m,n;
TRISB=0b11111111; //portb is configured as an input
INTCON= 0b10110000;//i have enabled the tmr0 interrupt and RB0/int interrupt
OPTION_REG= 0b11000010;//rising edge selected and the prescaleer selected for the tmr0
TRISA=0b11111111 ;

TRISC=0b00000000;
TRISD=0b00000000;
TRISE=0b111;


...
...
...
}
 

AlexR

Joined Jan 16, 2008
732
You might get a better response if you make your code more readable by using the code tags (#) in advanced editing mode (see below).
It also helps to know which compiler you are using.
Rich (BB code):
void interrupt()
{
    if(INTCON.INTF==1)
    {
        count9=count9+1;
        if(count9==999)
        {
            count9=0;
        }
        intcon.intf=0;
    }    
}


void main()
{
    int a,b,c,d,k,m,n;
    TRISB=0b11111111; //portb is configured as an input
    INTCON= 0b10110000;//i have enabled the tmr0 interrupt and RB0/int interrupt
    OPTION_REG= 0b11000010;//rising edge selected and the prescaleer selected for the tmr0
    TRISA=0b11111111 ;

    TRISC=0b00000000;
    TRISD=0b00000000;
    TRISE=0b111;


    ...
    ...
    ...
}
As to your problem, I am unclear as what you are trying to do. Are you trying to count the number of pulses that you receive (in which case what is timer0 doing?) or are you trying to find the time interval between the pulses (in which case where is the code to check timer0?)

Also you have enabled 2 interrupts (tmr0 and RB0/int) but are only checking for (RB0/int) in the ISR (interrupt service routine). This means that if you get a tmr0 interrupt (and you will!) then you will never get out of the ISR.
 

Thread Starter

royalmadhu

Joined Feb 4, 2011
6
You might get a better response if you make your code more readable by using the code tags (#) in advanced editing mode (see below).
It also helps to know which compiler you are using.
Rich (BB code):
void interrupt()
{
    if(INTCON.INTF==1)
    {
        count9=count9+1;
        if(count9==999)
        {
            count9=0;
        }
        intcon.intf=0;
    }    
}


void main()
{
    int a,b,c,d,k,m,n;
    TRISB=0b11111111; //portb is configured as an input
    INTCON= 0b10110000;//i have enabled the tmr0 interrupt and RB0/int interrupt
    OPTION_REG= 0b11000010;//rising edge selected and the prescaleer selected for the tmr0
    TRISA=0b11111111 ;

    TRISC=0b00000000;
    TRISD=0b00000000;
    TRISE=0b111;


    ...
    ...
    ...
}
As to your problem, I am unclear as what you are trying to do. Are you trying to count the number of pulses that you receive (in which case what is timer0 doing?) or are you trying to find the time interval between the pulses (in which case where is the code to check timer0?)

Also you have enabled 2 interrupts (tmr0 and RB0/int) but are only checking for (RB0/int) in the ISR (interrupt service routine). This means that if you get a tmr0 interrupt (and you will!) then you will never get out of the ISR.

Hey thanks for the reply

my task is to count some pulses which are generated from an encoder....
so i have enabled the rising edge bit in the option register...

when the RBO pin receives a pulse it can generate an inturrupt and start the count right?
 

Thread Starter

royalmadhu

Joined Feb 4, 2011
6
and i have used the tmro interrupt and cleared that bit in the ISR in my code..i didn't put that here cos then it will be too long .sorry
 

spinnaker

Joined Oct 29, 2009
7,830
plzz help me
1. You have posted to the wrong forum. You should have posted to the embedded forum.

2. Please read the forum guidelines. People will help you when they have the time. This might take some time. Remember that not everyone is in your time zone.

3. There is no need to repost your code unless there is an update. It only adds to confusion.

4. Use the code block when posting code like you did in your first post.


I don't see where you are attaching your interrupt vector to your interrupt routine.


In C18 you do it like this

Rich (BB code):
#pragma code InterruptVectorHigh = 0x08
void InterruptVectorHigh (void)
{
  _asm
    goto InterruptServiceHigh //jump to interrupt routine
  _endasm
}

#pragma interrupt InterruptServiceHigh  // "interrupt" pragma also for high priority
void InterruptServiceHigh(void)
{
    // Check to see what caused the interrupt
    // (Necessary when more than 1 interrupt at a priority level)

    // Check for INT0 interrupt
    if (INTCONbits.INT0IF)
    {
        // clear (reset) flag
        INTCONbits.INT0IF = 0;

        // change directions
        if (Direction == LEFT2RIGHT)
        {
            Direction = RIGHT2LEFT;     // change direction
        }
        else // (Direction == RIGHT2LEFT)
        {
            Direction = LEFT2RIGHT;     // change direction
        }
        
    }

    // Check for another interrupt, examples:
    // if (PIR1bits.TMR1IF)     // Timer 1
    // if (PIR1bits.ADIF)       // ADC

}  // return from high-priority interrupt
Search for PICkit™ 3 Debug Express PIC18F45K20 – MPLAB® C Lessons on the Microchip website. It explains how to do interrupts.

But if all you want to do is count pulses then you probably want to use your CCP port. But I have not done this so I am not exactly sure how to do it. You could try searching on CCP count pulses.
 

AlexR

Joined Jan 16, 2008
732
Hey thanks for the reply

my task is to count some pulses which are generated from an encoder....
so i have enabled the rising edge bit in the option register...

when the RBO pin receives a pulse it can generate an inturrupt and start the count right?
That's right, your interrupt code looks OK for counting pulsed on RB0.

Also you can disregard what spinnaker said about attaching your interrupt vector. That only applies if you are using the Microchip C18 compiler.

Spinnaker:
There is no standard way that compilers handle interrupts and each compiler does it in its own fashion. Most either have reserved names for the ISR or add a tag to the ISR function definition that tells the compiler to treat the function as an ISR and automatically point the interrupt vector to it. Its only in C18 that you have to go through the rigmarole of manually pointing the processor interrupt vector to the ISR.
 
Top