creating INT0 in PIC18f458

Discussion in 'Embedded Systems and Microcontrollers' started by ect_09, Mar 22, 2013.

  1. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    hello...
    i write this code for external interrupt..
    Code ( (Unknown Language)):
    1. unsigned int flag = 0;
    2.  
    3. void interrupt() {
    4.   if (INTCON.INT0IF==1)
    5.   {
    6.      INTCON.INT0IF = 0;
    7.      flag = 1;
    8.   }
    9. }
    10.  
    11. void main()
    12. {
    13.      TRISA=0x00;
    14.      TRISB.F0=0x01;
    15.      ADCON1 = 0b10000111;
    16.      CMCON = 0x07;
    17.      CVRCON.CVROE = 0;
    18.      INTCON = 0b11010000;
    19.      INTCON2 = 0b11100001;
    20.      INTCON3 = 0b00000000;
    21.  
    22.      while(1){
    23.            if(flag == 1){
    24.                   flag = 0;
    25.                  
    26.  
    27.                PORTA = ~PORTA;
    28.                  
    29.                
    30.                  
    31.           }
    32.      }
    33. }
    its working as when int0 goes high INT comes ,led glows on portA and when again int0 goes high ,it turn off the PORTA..
    but i want that LED's goes high only if int0 is 1,when int0 becomes low led should off.

    simply ,when then push button is pushed LED's should on
    when push button release LED's should off.
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    The INT0 signal is on PORTBbits.RB0. Rather then looping to test for the flag in your main loop, I'd just put the test inside the ISR.

    Set it once and forget it.

    Code ( (Unknown Language)):
    1. void interrupt() {
    2.   if (INTCON.INT0IF==1)
    3.   {
    4.      INTCON.INT0IF = 0;
    5.      if (PORTBbits == 1)  // see if the port is high
    6.         PORTA = 0xFF;   // it's high, PORTA on
    7.      else
    8.         PORTA = 0x00;   // it's low, PORTA off
    9.  
    10.   }
    11. }
    12.  
     
  3. MrChips

    Moderator

    Oct 2, 2009
    12,429
    3,360
    Why bother to use interrupt. Just read the button via an input pin and turn on the LED.
     
  4. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    you are right MrChips,but i'm learning PIC18...
    so trying examples by own..
    i want to get experience with PIC18 .
    so thats why sir i'm trying to work with timers interrupts nd etc in MIKRO C.
    i have worked with MPLAB IDE 8.60, but MIKRO C is totally different...
    so,trying to get good experience with it.
    if you have helping material related MIKRO C .kindly share it...

    and thanks ,ErnieM

    its working....
     
  5. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    can you explain this ,,,,

    Code ( (Unknown Language)):
    1. CMCON = 0x07;
    2.      CVRCON.CVROE = 0;
     
  6. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    Code ( (Unknown Language)):
    1. ADCON1 = 0b10000111;
    2.      CMCON = 0x07;
    3.      CVRCON.CVROE = 0;
    MrChips and ErnieM please expalin it...
     
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    You should learn to look these things up yourself in the data sheet.

    CVRCON turns the comparator functions off

    CVRCON.CVROE disconnects the reference voltage from the I/O pin.

    I believe by default these are already off after a "power on clear", but it makes some people feel better to turn them off again.
     
    ect_09 and absf like this.
Loading...