Discussion in "PIC Microcontroller Discussion" started by    toinhatminh    Dec 23, 2013.
Mon Dec 23 2013, 04:19 pm
#1
Hi Pro,
I have a Exercise like this : Detect interrupt on RB0 by Press Button.
When button is pressed , the variable is increased , and output port E RE1, RE2, respectively.
The MCU doesn't work as requirement, please help me see the code and advice,

Thanks a lot,
Minh
#include <htc.h>


// Interrupt on RB0,:


__CONFIG(INTIO & WDTDIS & PWRTEN & MCLREN & UNPROTECT & DUNPROTECT & BORDIS & IESODIS & FCMDIS & LVPDIS);

unsigned int press_count ;

void main()
{
    ANSEL = 0;
    ANSELH = 0; // Annalog unEnable.
   
    TRISB = 0xf1; //RB0 is Input.

    TRISE=0x00; // Set Port E is out Put
  
    RBPU = 0;//PORTB pull up enable.
    WPUB = 0x01;// RB0 is Weak Pull up.
    INTEDG = 0;// Edge down
    INTF = 0;// Clear Interrupt Flag
    INTE = 1;// Enable external interrupt
    GIE = 1;// Enable Global Interrupt
   
while(1){

   if(INTE && INTF){

                   void interrupt_isr();// When interrupt is detected on RBO, call fuction interrupt_isr()
                   }

        }
}
void interrupt_isr()
{
        press_count ++;// Increase the variable Press_count
        switch(press_count){
            case 1:
                RE1 = 1;// depent on value of Press_count that MCU will out put on RE1 and RB2.
                RE2 = 0;
                break;
            case 2:
                RE1 = 0;
                RE2 = 1;
                break;
            case 3:
                RE1 = 1;
                RE2 = 1;
                break;
            default:
                RE1 = 0;
                RE2 = 0;
                press_count = 0;
                break;
                              }
}
Wed Dec 25 2013, 01:12 am
#2
interrupts does not work this way

while(1){
    if(INTE && INTF){
        void interrupt_isr();// When interrupt is detected on RBO, call fuction interrupt_isr()
    }
}

Your ISR will be called automatically when interrupt occurs. I think you need to first read about how interrupt works. Incase of PIC microcontroller, when interrupt occurs, CPU jumps from normal execution to ISR vector location and then executes from there. Here is a PPT that might help you get started:



Which compiler are you using for PIC?

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

AntoniaRoons
Fri Apr 19 2024, 09:59 pm
carpinteyrowrl
Fri Apr 19 2024, 02:51 pm
DonaldJAX
Fri Apr 19 2024, 01:08 pm
Lewisuhakeply
Thu Apr 18 2024, 06:00 pm
Darrellciz
Thu Apr 18 2024, 11:07 am
Charlessber
Thu Apr 18 2024, 09:29 am
BartonSem
Thu Apr 18 2024, 04:56 am
DonaldKnown
Thu Apr 18 2024, 12:24 am