Sign In

Problem With Using Interrupts In PIC18F87J50

M.VENKATA KRISHNA makkevenkatakrishna@gmail.com India

Hardware: PIC18F87J50 MICROCONTROLLER,CLICKER 2 BOARD Software: C LANGUAGE,mikroC PRO For PIC

when I make my interrupt pin(RB2) LOW then only it is detecting the interrupt and showing the result,but for my project i need it to detect the interrupt when RB2 pin is high,
just see the interrupt initialization and main() function in my code u can understand
Hope u will help me

Images

Problem with using interrupts in PIC18F87J50

Program

/*******************************************************************************
MCU USED:-PIC18F87J50
CLICKER 2 BOARD
MIKRO BORAD IS THUNDER CLICKER

*******************************************************************************/
#define _INDOR 0x12
#define _OUTDOOR 0x0E

sbit Thunder_CS           at LATC2_bit;
sbit Thunder_CS_Direction at TRISC2_bit;



long int value;
short int  value1;
char Out_Text[11];
int Total_Lightings_Detected = 0;
char interrupt_detected = 0;
char interrupt_source = 0;


/*******************************************************************************
* Function Init()
* ------------------------------------------------------------------------------
* Overview: Initializes MCU and sets Interrupt priority level for External int
* Input: Nothing
* Output: Nothing
*******************************************************************************/
void Init() {
  ANCON0 = 1;            // Configure AN pins as digital I/O
  UART1_Init(9600);
  Delay_ms(100);
 INT2IF_bit = 0;           //interrupt flag bit set to zero
  INT2IE_bit=1;          //enable external interrupts
  INT2IP_bit=1;          //priority
  GIEH_bit=1;             //gloabal interrupt enable
  INTEDG2_bit=1;           //rising edge
  RBPU_bit=1;               //disable weak pull up resistor
                  //peripheral interrupt enable
                 //IPEN bit to zero
 TRISB2_bit=1; //set as digital input
}
/*******************************************************************************
*  Interrupt External INT0
*  ICS: AUTO,  Interrupt level 4
*******************************************************************************/
void interrupt1() iv 0x0008
{
if(INT2IF_bit==1)
{
INT2IF_bit=0;    //clear int2 flag
interrupt_detected=1;

}

}

/*******************************************************************************
* Function DrawFrame()
* ------------------------------------------------------------------------------
* Overview: Draws main screen of the exampel
* Input: Nothing
* Output: Nothung
*******************************************************************************/
void DrawFrame(){
  UART1_Write_Text("Welcome to Thunder clicker ");
}

/*******************************************************************************
* Function Thunder_Write(unsigned short address, unsigned short data1)
* ------------------------------------------------------------------------------
* Overview: Function writes desired byte into specified register address
* Input: register address, byte
* Output: Nothung
*******************************************************************************/
void Thunder_Write(unsigned short address, unsigned short data1)  {

  address.B7 = 0;
  address.B6 = 0;
  Thunder_CS = 0;
  SPI1_Write(address);
  Thunder_CS = 1;
}

/*******************************************************************************
* Function Thunder_Read(unsigned short address)
* ------------------------------------------------------------------------------
* Overview: Function reads byte from specified address
* Input: register address
* Output: desired byte
*******************************************************************************/
unsigned short Thunder_Read(unsigned short address) {
  unsigned short tmp = 0;

  address.B7 = 0;
  address.B6 = 1;
  Thunder_CS = 0;
  SPI1_Write(address);
  tmp = SPI1_Read(0);
  Thunder_CS = 1;

  return tmp;
}

/*******************************************************************************
* Function Thunder_Init()
* ------------------------------------------------------------------------------
* Overview: Function Initializes Thunder chip
* Input: register address, data
* Output: Nothing
*******************************************************************************/
void Thunder_Init(void) {
unsigned short temp;
  Thunder_CS_Direction = 0;            // Set CS directions as output
  Thunder_CS = 1;                      // Set CS to idle

  Thunder_Write(0x3C, 0x96);           // set all registers in default mode
  Thunder_Write(0x3D, 0x96);           // calibrate internal oscillator

  temp = Thunder_Read(0x00) & 0xC1;
  Thunder_Write(0x00, ((_INDOR << 1) | temp)); // set to indoor

  temp = Thunder_Read(0x01) & 0x80;
  Thunder_Write(0x01, 0x44 | temp);    // set NFL and WDTreshold

  temp = Thunder_Read(0x02) & 0x80;    // clear statistics, min number of ligtning, spike rejection
  Thunder_Write(0x02, 0x40 | temp);

  temp = Thunder_Read(0x03) & 0x1F;    // Frequency division ratio(antenna),mask disturber, interrupt
  Thunder_Write(0x03, 0x00 | temp);

  Thunder_Write(0x08, 0x00);           // LCO, SRCO, TRCO on IRQ, capacitors tuning
}

/*******************************************************************************
* Function Thunder_Read_Energy()
* ------------------------------------------------------------------------------
* Overview: Function reads energy of detected thunder
* Input: Nothing
* Output: Measured result
*******************************************************************************/
long int Thunder_Read_Energy() {
  unsigned short low_byte, mid_byte;
  long int Out_thunder_energy;

  Out_thunder_energy = Thunder_Read(0x06) & 0x1F;
  mid_byte = Thunder_Read(0x05);
  low_byte = Thunder_Read(0x04);

  Out_thunder_energy = (Out_thunder_energy << 8);
  Out_thunder_energy = (Out_thunder_energy | mid_byte);
  Out_thunder_energy = (Out_thunder_energy << 8);
  Out_thunder_energy = (Out_thunder_energy | low_byte);

  return Out_thunder_energy;
}

/*******************************************************************************
* Function Thunder_Read_distance()
* ------------------------------------------------------------------------------
* Overview: Function reads distance from detected thunder
* Input: Nothing
* Output: Measured result
*******************************************************************************/
unsigned int Thunder_Read_distance() {
  int Out_thunder_distance;

  Out_thunder_distance = Thunder_Read(0x07) & 0x3F;

  return Out_thunder_distance;
}

/*******************************************************************************
* Function Display_Value_energy(long int disp_val )
* ------------------------------------------------------------------------------
* Overview: Function displays approximate value of thunder energy on display
* Input: energy value
* Output: Nothing
*******************************************************************************/
void Display_Value_energy(long int disp_val) {

  LongWordToStr(disp_val, Out_Text);

  UART1_Write_Text("Energy value");
  Delay_ms(100);
  UART1_Write_Text(Out_Text);
}

/*******************************************************************************
* Function Display_Value_energy(int disp_val)
* ------------------------------------------------------------------------------
* Overview: Function displays approximate value of distance from thiderstorm
* Input: energy value
* Output: Nothing
*******************************************************************************/
void Display_Value_distance(int disp_val) {

  IntToStr(disp_val, Out_Text);

  UART1_Write_Text("Distance");
  Delay_ms(100);
  UART1_Write_Text(Out_Text);
}

/*******************************************************************************
* Function Display_Value_energy(int disp_val)
*******************************************************************************/
void main() {

  Init();
  DrawFrame();


  // Initiliaze SPI3 module for Lightning Sensor chip
  SPI1_Init();
  Delay_ms(500);



  // Initialize Thunder module
  Thunder_Init();
  Delay_ms(1000);







  while(1){
    // event was detected
    if (interrupt_detected > 0){
      interrupt_detected = 0;

      // read the source of the interrupt
      interrupt_source = Thunder_Read(0x03) & 0x0F;
      // check which event occurred
      switch (interrupt_source){
        case 8 : { // in case of thunder
                   UART1_Write_Text("Thunder Storm was detected");
                   Delay_ms(100);

                  value = Thunder_Read_Energy();
                  Display_Value_energy(value);
                  // read and display distance
                  value1 = Thunder_Read_distance();
                  Display_Value_distance(value1);   // Display distance value read  (km)



                  Total_Lightings_Detected ++;
                  IntToStr(Total_Lightings_Detected, Out_Text);
                  UART1_Write_Text("Total Lightnings Detected");
                  Delay_ms(100);
                  UART1_Write(Out_Text);


                 }; break;
        case 4 : { // in case of disturbance


                  UART1_Write_Text("Disturber detected");
                  Delay_ms(1000);


                 }; break;
        case 1 : { // in case of noise
                  UART1_Write_Text("Noise Level too high");
                  Delay_ms(100);


                 }; break;
       default:{                     //the default case
                                     UART1_Write_Text("Wow it is working");
                                     };break;

      }
    }
  }
}

Replies

R Avinash
2016-05-23 14:06:13
In the given program, the edge of the external interrupt is HIGH only. That means the interrupt will trigger on rising edge only. But you are telling that it is firing on the falling edge. That is not possible.
R M.VENKATA KRISHNA
2016-05-24 05:02:34
hello sir,
when i give the connection from RB2 pin to GND ,it is showing me the msg ,but i have studied my thunder clicker board's IRQ pin will become high when an interrupt occurs means it will make my interrupt pin high then i will get the required msg,instead of that i made my RB2 pin high manually but it is not working,
Is my approach correct?
If not please tell me what is happening?
hope u may help me in this regard.
R Avinash Gupta
2016-05-24 05:16:20
See what is the idle state of the RB2 using a multimeter. Is it pulled up to high?

Upload a picture of your measurement.

Touch red pin of DMM to RB2 of the MCU and other probe to GND
R M.VENKATA KRISHNA
2016-05-24 05:24:21
Thank u Avinash
Now i got it ,My IRQ pin will be always high, when an event occurs AS3935 will pull down the IRQ high and display the interrupt,
So it is displaying the msg when I connect it to the gnd.

Post a reply to M.VENKATA KRISHNA

Think you can help M.VENKATA KRISHNA ? Then post your thoughts that might help M.VENKATA KRISHNA. You will earn a lot of reputation in the technical community.

If your program is in multiple files, then ZIP the entire folder and attach below

Images and Screenshots

These helps other better understand your suggestion.