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
}
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++;
}
}
}
......
void HandleShort(void) {
RelayOutput = 1;
if (RetryCounter > 5)
{
RetryCounter = 0;
PermanentShort = 1;
LEDOutput = 1;
} else if (PreviousRetryCounter != Retrycounter) RelayOutput = 0;
}
......
if (Shorted)
{
HandleShort();
} else RelayOutput = 0;
else if (PreviousRetryCounter != Retrycounter) RelayOutput = 0;
No pins should float EVER!Sure floating is not ideal situation but I noticed that on breadboard...
CMCON = 0x07;
Try this configuration.
Code:CMCON = 0x07;
Comparator if enabled affects GP0, GP1 and GP2 pins.
It's less than a zillion, that's the good news.There are zillion registers and every pic is different so how do you know?
Out of curiosity I had a look into the datasheet.Ok, that's no brainer. PIC datasheet says sink current is as well 25mA as source current so that's plenty for most transistor base currents..
... but beware that if you tri-state the pin (TRISx register) it is disconnected from the driver circuit inside the PIC so it will no longer pull the base to ground. If you use that method, you should add a resistor to prevent the base floating.
Brian.
Hi,
Out of curiosity I had a look into the datasheet.
25mA is not an "operational" current. It is the absolute maximum, the edge to get killed.
Klaus
The same applies to all PICs, the pins can be inputs, outputs or tri-stated. If you are using any as an output and drive it high or low, it will have the ability to source or sink current so driving a transistor through a suitable current limiting resistor is fine. However, if you use the TRIS register to tri-state a pin, it effectively becomes disconnected from the output driver circuit so in that scenario, if driving a transistor it leaves the base disconnected and floating. So if you use TRIS to control a pin, it may be wise to add a resistor to ground anyway.Sorry, got bit confused. You mean maybe in some other case eg. PIC16F1847 which has TRISA and TRISB and if those are set "1 = PORTA pin configured as an input (tri-stated)" it needs resistor? In this particular topics case I do declare initially ANSEL digital and TRISIO output and don't touch these after that, so they are either pulled high or down. Correct?
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?