A simple interrupt RB0 microcontroller problem

Thread Starter

kuannygohcheetatt

Joined Oct 31, 2013
61
//Student Name:Ooi Wei Sheng

#include <pic.h>
/*Lab1.c - Turn ON PORT B 0 LED*/

__CONFIG(0x3f38);

delay1s()
{
for(int i=0;i<30000;i++)
{
NOP();
}
}
void interrupt isr(void)
{
if(INTF)
{

PORTA =0x00;
INTF = 0; /* Clear interrupt flag */
}
}


main()
{
CMCON=0b00000111;
TRISB=0;
PORTB=0;
TRISA=0xFF;
INTF =1;
INTE=1;
GIE =1;
PEIE=0;
INTEDG=1;

if(RB1)

{
while(1){

PORTA=0x01;
delay1s();
PORTA=0x00;
delay1s();
PORTA=0x02;
delay1s();
PORTA=0x00;
delay1s();
PORTA=0x04;
delay1s();
PORTA=0x00;
delay1s();
PORTA=0x08;
delay1s();
PORTA=0x00;
delay1s();

}
if(RB2)

{
while(1){

PORTA=0x08;
delay1s();
PORTA=0x00;
delay1s();
PORTA=0x04;
delay1s();
PORTA=0x00;
delay1s();
PORTA=0x02;
delay1s();
PORTA=0x00;
delay1s();
PORTA=0x01;
delay1s();
PORTA=0x00;
delay1s();

}
}
}
}
Homework requirement:
---press RB1 , led will light up from sequence start from porta0 to porta3 and the cycle repeats
press RB0 to stop the sequence
---press RB2 , led will light up from sequence start from porta3 to porta0 and the cycle repeats
press RB0 to stop the sequence

hi all , i have a problem of understanding the interrupt , based on this code, if i press RB0 switch , my portA will become zero , and i will exit the interrupt function , but then what wil the program do? it will start all over again?because from the homework requirment , i am suppose to stop all the lighting of led once the interrupt switch is pressed, but i am afraid the program might continue to carry out its previous function after exiting the interrupt, can someone pls guide and explain to me ?thank you
 

blah2222

Joined May 3, 2010
582
Rich (BB code):
#include <pic.h>
/*Lab1.c - Turn ON PORT B 0 LED*/

__CONFIG(0x3f38);

delay1s()
{
    for(int i=0;i<30000;i++)
    {
        NOP();
    }
}
void interrupt isr(void) 
   {
   if(INTF)
     {
     
     PORTA =0x00;
     INTF = 0; /* Clear interrupt flag */
     }
   }  


main()
{
    CMCON=0b00000111;
    TRISB=0;
    PORTB=0;
    TRISA=0xFF;
    INTF =1;
    INTE=1;
    GIE =1;
    PEIE=0;
    INTEDG=1;

    if(RB1)

    {
while(1){
    
        PORTA=0x01;
            delay1s();
            PORTA=0x00;
            delay1s();
                PORTA=0x02;
            delay1s();
            PORTA=0x00;
            delay1s();
                PORTA=0x04;
            delay1s();
            PORTA=0x00;
            delay1s();
                PORTA=0x08;
            delay1s();
            PORTA=0x00;
            delay1s();

}
    if(RB2)

    {
while(1){
    
        PORTA=0x08;
            delay1s();
            PORTA=0x00;
            delay1s();
                PORTA=0x04;
            delay1s();
            PORTA=0x00;
            delay1s();
                PORTA=0x02;
            delay1s();
            PORTA=0x00;
            delay1s();
                PORTA=0x01;
            delay1s();
            PORTA=0x00;
            delay1s();

}
}
}
}
Next time put your code between the [code ] [/code ] tags to make it easier to read (without the spaces of course).

Based on your code, it will execute the main code without going into either of the while loops if RB1 or RB2 are not set and will finish, which is not what you want as nothing will be running.

I believe depending on how far along the current instruction is, the interrupt will either wait for it to finish or halt it and go perform the ISR function. It will then return to the next instruction in line or perform the instruction that did not get to finish. It won't restart from the beginning.

From what I understand, you obviously want the program to be running continuously, checking the states or RB0/RB1/RB2. To do this you will need to have the state checking and LED flashing code incased in one while loop.
 
Top