When its inaccurate it never gives an interrupt signal until I reset the circuit
How is the interrupt derived from the sensor?No, signal from the sensor is correctly read by the controller as I am using LCD as well which shows the value of current. But again the problem is with the interrupt signal it sometimes give and sometimes it does not....
Exactly, a threshold value has been defined in the code, as the sensor's value reach the threshold value a particular time delay is calculated after which interrupt signal should be generated and the LED must glow at that time.How is the interrupt derived from the sensor?
I take it from your descriptor that you are continuously monitoring the sensor output with the MCU (and so displaying the value the LCD). Is there a separate signal from the sensor to the MCU that is acting as the interrupt?
Or are you comparing the value with some threshold value in your code and triggering your 'interrupt' (I would not call it that in this case) that way?
Perhaps a schematic diagram would help.
Susan
It is better to use Timer1 interrupt for the delay generation as it gives accurate delays.
What is your required delay range based on your Amps range?
Amps = 0.007;
float Ip = 0.005 * 1 ;
if(Amps>Ip)
{
float PSM;
float top ;
float TMS = 1.0 ;
float tactual =0.0 ;
PSM = Amps/Ip ; // 1.4
top = TMS * (0.14 / (pow(PSM,0.02)-1)) ; // 20.73
//tactual = top * TMS ;
delayy(top);
Explain these so that I can understand the code fully and then debug the code.
What is Ip, PSM, TMS, top, tactual?
Code:Amps = 0.007; float Ip = 0.005 * 1 ; if(Amps>Ip) { float PSM; float top ; float TMS = 1.0 ; float tactual =0.0 ; PSM = Amps/Ip ; // 1.4 top = TMS * (0.14 / (pow(PSM,0.02)-1)) ; // 20.73 //tactual = top * TMS ; delayy(top);
Are you sure XC8 compiler's __delay_ms() function accepts float values are arguments?
#include <xc.h>
#include "LCD.h"
#include <math.h>
#define _XTAL_FREQ 20000000
// CONFIG
// CONFIG1H
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
int count;
int k;
//**ADC FUnctions***//
void ADC_Initialize()
{
ADCON0 = 0b01000001; //ADC ON and Fosc/16 is selected
ADCON1 = 0b11000000; // Internal reference voltage is selecte
}
unsigned int ADC_Read(unsigned char channel)
{
ADCON0 &= 0x11000101; //Clearing the Channel Selection Bits
ADCON0 |= channel<<3; //Setting the required Bits
__delay_ms(2); //Acquisition time to charge hold capacitor
GO_nDONE = 1; //Initializes A/D Conversion
while(GO_nDONE); //Wait for A/D Conversion to complete
return ((ADRESH<<8)+ADRESL); //Returns Result
}
//***End of ADC Functions***//
void main(void)
{
int adc=0; //Variable to read ADC value
float Voltage1; //Variable to store voltage
float Amps; //Variable to store Amps value
int Am1,Am2,Am3; //Variable to split Amps value into char
float top;
float PSM;
TRISA4 =1; //AN4 declared as input
TRISB = 0x00;
TRISD0 = 0;
TRISD1 = 0;
char bool = 1;
TRISC0 =1;
TRISC1 =1;
TRISC2 =1;
ADC_Initialize();
InitLCD();
RD0=0;
RD1=0;
while(1)
{
/***Current Calculation*****/
for (int i=0; i<20;i++) //Read value for 20 Times
{
adc=0;// Initialize the LCD
adc=ADC_Read(4); //Read ADC
Voltage1 = (adc*4.8828); //Calculate the Voltage
if( Voltage1>=2500)
{
//If the current is positive
Amps += ((Voltage1-2500)/18.5);
}
else if (Voltage1<=2500) //If the current is negative
{
Amps += ((2500-Voltage1)/18.5);
}
}
Amps/=20; //Average the value that was read for 20 times
if (bool) //during the normal operating condition
{
RD0 = 1; // LED ON
RD1 = 0; // LED ON
}
GotoYXPositionOnLCD(LCD_LINE1, 0);
Am1 = fmod((Amps/100) , 10) ;
Am2 = fmod((Amps/10 ) ,10) ;
Am3 = fmod(Amps,10);
//Am4 = fmod(Amps *10,10);
WriteStringToLCD("CURRENT ="); // displaying the value of current sensed by the ACS 712
WriteDataToLCD(Am1+'0');
WriteDataToLCD(Am2+'0');
WriteDataToLCD('.');
WriteDataToLCD(Am3+'0');
//WriteDataToLCD(Am4+'0');
WriteDataToLCD('A');
float Ip = 30.0; // giving a threshold value for tripping
float Is = 80.0; // giving a threshold value for instantaneous tripping
if(Amps > Is )
{
RD0 = 0; // LED ON
RD1 = 1;
while (1){
GotoYXPositionOnLCD(LCD_LINE1, 0);
WriteStringToLCD(" SHORT CIRCUIT");
GotoYXPositionOnLCD(LCD_LINE2, 0);
WriteStringToLCD(" ALERT");
}
}
else if( Amps> Ip)
{
// timer 1 config starting
TMR1=0;
TMR1CS=0;
T1CKPS0=0;
T1CKPS1=0;
TMR1ON=1;
TMR1IE=1;
TMR1IF=0;
GIE=1;
PEIE=1;
// timer 1 config ending
PSM = Amps/Ip ;
top = (13.5 / (PSM-1)) ; // time delay calculation after which the interrupt signal should be generated
k = (top * 20000000)/(4*65536); // overflow counter calculation
bool= 0 ;
}
}
}
__interrupt() void timer_isr()
{
if (TMR1IF)
{
count++;
if (count == k)
{
RD0 = 0; // LED ON
RD1 = 1;
count=0;
}
TMR1IF=0;
}//interrupt service routine
}
kindly help me.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?