creating INT0 in PIC18f458

Thread Starter

ect_09

Joined May 6, 2012
180
hello...
i write this code for external interrupt..
Rich (BB code):
unsigned int flag = 0;

void interrupt() {
  if (INTCON.INT0IF==1)
  {
     INTCON.INT0IF = 0;
     flag = 1;
  }
}

void main()
{
     TRISA=0x00;
     TRISB.F0=0x01;
     ADCON1 = 0b10000111;
     CMCON = 0x07;
     CVRCON.CVROE = 0;
     INTCON = 0b11010000;
     INTCON2 = 0b11100001;
     INTCON3 = 0b00000000;

     while(1){
           if(flag == 1){
                  flag = 0;
                 

               PORTA = ~PORTA;
                  
               
                  
          }
     }
}
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.
 

ErnieM

Joined Apr 24, 2011
8,377
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.

Rich (BB code):
void interrupt() {
  if (INTCON.INT0IF==1)
  {
     INTCON.INT0IF = 0;
     if (PORTBbits == 1)  // see if the port is high
        PORTA = 0xFF;   // it's high, PORTA on
     else
        PORTA = 0x00;   // it's low, PORTA off

  }
}
 

Thread Starter

ect_09

Joined May 6, 2012
180
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....
 

Thread Starter

ect_09

Joined May 6, 2012
180
Rich (BB code):
ADCON1 = 0b10000111;
     CMCON = 0x07;
     CVRCON.CVROE = 0;
MrChips and ErnieM please expalin it...
 

ErnieM

Joined Apr 24, 2011
8,377
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.
 
Top