A simple interrupt RB0 microcontroller problem

Discussion in 'Homework Help' started by kuannygohcheetatt, Jan 27, 2014.

  1. kuannygohcheetatt

    Thread Starter Member

    Oct 31, 2013
    61
    0
    //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
     
  2. blah2222

    Well-Known Member

    May 3, 2010
    554
    33
    Code ( (Unknown Language)):
    1.  
    2. #include <pic.h>
    3. /*Lab1.c - Turn ON PORT B 0 LED*/
    4.  
    5. __CONFIG(0x3f38);
    6.  
    7. delay1s()
    8. {
    9.     for(int i=0;i<30000;i++)
    10.     {
    11.         NOP();
    12.     }
    13. }
    14. void interrupt isr(void)
    15.    {
    16.    if(INTF)
    17.      {
    18.      
    19.      PORTA =0x00;
    20.      INTF = 0; /* Clear interrupt flag */
    21.      }
    22.    }  
    23.  
    24.  
    25. main()
    26. {
    27.     CMCON=0b00000111;
    28.     TRISB=0;
    29.     PORTB=0;
    30.     TRISA=0xFF;
    31.     INTF =1;
    32.     INTE=1;
    33.     GIE =1;
    34.     PEIE=0;
    35.     INTEDG=1;
    36.  
    37.     if(RB1)
    38.  
    39.     {
    40. while(1){
    41.    
    42.         PORTA=0x01;
    43.             delay1s();
    44.             PORTA=0x00;
    45.             delay1s();
    46.                 PORTA=0x02;
    47.             delay1s();
    48.             PORTA=0x00;
    49.             delay1s();
    50.                 PORTA=0x04;
    51.             delay1s();
    52.             PORTA=0x00;
    53.             delay1s();
    54.                 PORTA=0x08;
    55.             delay1s();
    56.             PORTA=0x00;
    57.             delay1s();
    58.  
    59. }
    60.     if(RB2)
    61.  
    62.     {
    63. while(1){
    64.    
    65.         PORTA=0x08;
    66.             delay1s();
    67.             PORTA=0x00;
    68.             delay1s();
    69.                 PORTA=0x04;
    70.             delay1s();
    71.             PORTA=0x00;
    72.             delay1s();
    73.                 PORTA=0x02;
    74.             delay1s();
    75.             PORTA=0x00;
    76.             delay1s();
    77.                 PORTA=0x01;
    78.             delay1s();
    79.             PORTA=0x00;
    80.             delay1s();
    81.  
    82. }
    83. }
    84. }
    85. }
    86.  
    87.  
    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.
     
Loading...