Veketti
Full Member level 3
Dear All,
I'm once again turning to you with one issue that I'm not able to solve by my self. I'm using PIC12F683 which reads through ADC voltage and blinks led in various frequencies. That part works like it should in the code. Then there is a part which should control relay NC contacts (through transistor, RelayOutput at GP1_bit) when input GP3 is high. Program should turn GP1 high when short is sensed and retry 6 times turning GP1 low in 10 seconds intervals. 6th time it will exit the loop and switch GP1 permanently on.
This is is sort of working. But GP3 when turned on inside the loop is switched on and off by 29khz frequency. See below screenshot.
I've tried numerous things and then tested to comment the if shorted -clause and tested inside the loop by RelayOutput = ~RelayOutput; to see what would the frequency then be and it was arund twice faster.
I really don't get it why is that GP3 behaving like this. Please help.
Here's the complete mikroC code:
I'm once again turning to you with one issue that I'm not able to solve by my self. I'm using PIC12F683 which reads through ADC voltage and blinks led in various frequencies. That part works like it should in the code. Then there is a part which should control relay NC contacts (through transistor, RelayOutput at GP1_bit) when input GP3 is high. Program should turn GP1 high when short is sensed and retry 6 times turning GP1 low in 10 seconds intervals. 6th time it will exit the loop and switch GP1 permanently on.
This is is sort of working. But GP3 when turned on inside the loop is switched on and off by 29khz frequency. See below screenshot.
I've tried numerous things and then tested to comment the if shorted -clause and tested inside the loop by RelayOutput = ~RelayOutput; to see what would the frequency then be and it was arund twice faster.
I really don't get it why is that GP3 behaving like this. Please help.
Here's the complete mikroC code:
Code:
bit Timer, PermanentShort;
char RetryTimerCounter=0, RetryCounter = 0, PreviousRetryCounter = 0;
double PressureReading = 0.0;
sbit LEDOutput at GP2_bit;
sbit RelayOutput at GP1_bit;
sbit Shorted at GP3_bit;
// Initializing registers
void InitMain(void) {
ANSEL = 0b00010001; // Set input to digital
TRISIO = 0b00111001; // input Pin 7 ADC, 4 Short circuit ind. Output Pin 5 LED, 6 Relay
OPTION_REG = 0b10000000; // GPPU disabled
INTCON = 0b11000000; // GIE and PEIE enabled
PIE1 = 0b00000001; // Enable Timer1 interrupt
T1CON = 0b00111101; //Timer1 Registers Prescaler= 8 - TMR1 Preset = 0 - Freq = 1.91 Hz - Period = 0.524288 seconds
TMR1H = 0; // preset for timer1 MSB register
TMR1L = 0; // preset for timer1 LSB register
ADCON0 = 0b10000001;
}
// Interrupt handling
void interrupt(void)
{
if (TMR1IF_bit) // If timer1 is interrupting
{
Timer = 1;
TMR1IF_bit = 0; // Reset timer1 Interrupt
if ((RetryTimerCounter > 20) && Shorted ) // ~10 seconds and short is present
{
RetryTimerCounter = 0;
Retrycounter++;
} else if (Shorted) //
{
PreviousRetryCounter = Retrycounter;
RetryTimerCounter++;
}
}
}
// Read the pressure
double ReadPressure (void) {
double analogvalue = 0.0;
ADCON0.f1 = 1; // Enable conversion
while (ADCON0.f1); // Wait for conversion to end
analogvalue = (ADRESH << 8 ) + ADRESL; // Store results
analogvalue = (analogvalue/1024.00)*5.0; //calculate actual voltage across the sensor
return analogvalue;
}
void HandleShort(void) {
RelayOutput = 1;
if (RetryCounter > 5)
{
RetryCounter = 0;
PermanentShort = 1;
LEDOutput = 1;
} else if (PreviousRetryCounter != Retrycounter) RelayOutput = 0;
}
unsigned int HandleLedBlinking(double BlinkCase) {
unsigned int BlinkInterval1;
if (BlinkCase > 1.8) Blinkinterval1 = 3740; // 5 hz
else if (BlinkCase > 0.89) Blinkinterval1 = 6233; // 3 hz
else if (BlinkCase > 0.544) Blinkinterval1 = 9350; // 2 hz
else if (BlinkCase > 0.2) Blinkinterval1 = 18700; // 1 hz
else if (BlinkCase < 0.2) BlinkInterval1 = 0; // Led off
return BlinkInterval1;
}
void main() {
unsigned int BlinkInterval = 0, i = 0;
InitMain();
Timer = 0;
PermanentShort = 0;
LEDOutput = 0;
RelayOutput = 0;
while(1)
{
if (Timer)
{
Timer = 0;
PressureReading = ReadPressure();
BlinkInterval = HandleLedBlinking(PressureReading);
}
// Check whether there is short circuit
if (Shorted)
{
HandleShort();
} else RelayOutput = 0;
// RelayOutput = ~RelayOutput; // Testing the frequency
// Short circuit is persistent. Exit loop.
if (PermanentShort) break;
if (BlinkInterval) { // Blink led through various frequency
if (i > BlinkInterval) {
LEDOutput = ~LEDOutput;
i = 0;
}
i++;
} else LEDOutput = 0;
}
RelayOutput = 1; // Turn relay on as there is permanent short
}
Last edited: