Square wave frequency meter /Interrupt not occur

Thread Starter

Venkateszr

Joined Aug 30, 2018
25
I am New to Program... Interrupt not occur and also always LCD display shows "out of Range" i am using PIC16F723A

C:
#define _XTAL_FREQ 16000000

#define RS RC0
#define EN RC1
#define D4 RC2
#define D5 RC3
#define D6 RC4
#define D7 RC5

#include <xc.h>
#include "lcd.h"

//BEGIN CONFIG
#pragma config FOSC  = HS
#pragma config WDTE  = OFF
#pragma config PWRTE = ON       // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config CP    = OFF         // Flash Program Memory Code Protection bit (Code protection off)
//END CONFIG

long int a=0;

void __interrupt () freq()
{
     if(INTCONbits.RBIF == 1)
     {
                    INTCONbits.RBIE = 0;  // Disable the port change interrupt
                    if(RB4==1)
                    {
                     T1CONbits.TMR1ON = 1;
                    }
                                
                    if(RB4==0)
                    {
                    T1CONbits.TMR1ON = 0;       // Stop timer1
                    a = ((TMR1H<<8)|TMR1L);
                    TMR1H = 0;
                    TMR1L = 0;
                    }

     }
     INTCONbits.RBIE = 1;      // Enable the port change interrupt
     INTCONbits.RBIF = 0;

}

void main()
{
  TRISB =0b00010000;                 //RB4 as Input PIN
  TRISC =0x00;                             //LCD Pins as Output
  //PORTB = 0xFF;
  //ANSELB=0x00;
  INTCONbits.GIE = 1;                //Global Interrupt Enable
  INTCONbits.RBIF = 0;               //Clear PORTB On-Change Interrupt Flag
  INTCONbits.RBIE = 1;               //Enable PORTB On-Change Interrupt
  Lcd_Init();
  Lcd_Clear();
  Lcd_Set_Cursor(1,1);
  Lcd_Write_String("Current");
  Lcd_Set_Cursor(2,1);
  Lcd_Write_String("Measurement");

  __delay_ms(3000);
  Lcd_Clear();

  T1CON = 0x10;                       //Initialize Timer Module

  while(1)
  {
    TMR1H = 0;                        //Sets the Initial Value of Timer
    TMR1L = 0;                        //Sets the Initial Value of Timer
      
    if(a>=1)
    {
      Lcd_Clear();
      Lcd_Set_Cursor(1,1);
      Lcd_Write_String("Frq = ");

      Lcd_Set_Cursor(1,12);
      Lcd_Write_Char(a);

      Lcd_Set_Cursor(1,15);
      Lcd_Write_String("Hz");
    }
    else
    {
      Lcd_Clear();
      Lcd_Set_Cursor(1,1);
      Lcd_Write_String("Out of Range");
    }
    __delay_ms(500);
  }
}
Moderators note : used code tags
 
Last edited by a moderator:

jpanhalt

Joined Jan 18, 2008
10,463
1603363313550.png

I would do that in the reverse order. Enabling the interrupt before clearing the flag will result in an immediate interrupt if that flag is set. With your device, the flag must be cleared in software first.
 
Top