Hi, I am new to PIC programming, only started a couple of days ago. I am currently using the following comparator code:
In some pseudo-code:
If CIN+ < CIN- is true for a minimum of 2 seconds
Turn LED ON
Else, do nothing
If CIN+ > CIN- is true for a minimum of 2 seconds
Turn OFF LED
Else, do nothing
So the LED will only turn on if CIN+ < CIN- is true for more than 2 seconds. Once the LED is ON, if CIN+ > CIN- for a minimum of 2 seconds, it will turn the LED OFF.
Thanks for the help, I'm a newbie at this.
What I want do is to add a 2 second delay before the LED is ON and before the LED is OFF/************************************************************************
* *
* Filename: MC_L6-Comp_LED-HTC.c *
* Date: 17/8/09 *
* File Version: 1.0 *
* *
* Author: David Meiklejohn *
* Company: Gooligum Electronics *
* *
*************************************************************************
* *
* Architecture: Midrange PIC *
* Processor: 12F629 *
* Compiler: HI-TECH C PRO v9.65PL1 Lite *
* *
*************************************************************************
* *
* Files required: none *
* *
*************************************************************************
* *
* Description: Lesson 6, example 1a *
* *
* Demonstrates basic comparator operation *
* *
* Turns on LED when voltage on CIN+ > voltage on CIN- *
* *
*************************************************************************
* *
* Pin assignments: *
* CIN+ - voltage to be measured (e.g. pot output or LDR) *
* CIN- - threshold voltage (set by voltage divider resistors) *
* GP5 - indicator LED *
* *
************************************************************************/
#include <htc.h>
/***** CONFIGURATION *****/
// ext reset, no code or data protect, no brownout detect,
// no watchdog, power-up timer enabled, 4MHz int clock
__CONFIG(MCLREN & UNPROTECT & BORDIS & WDTDIS & PWRTEN & INTIO);
// Pin assignments
#define LED GPIO5 // indicator LED on GP5
#define nLED 5 // (port bit 5)
/***** MAIN PROGRAM *****/
void main()
{
// Initialisation
TRISIO = ~(1<<nLED); // configure LED pin (only) as an output
// configure comparator
CMCON = 0b010; // select mode 2 (CM = 010):
// +ref is CIN+, -ref is CIN-,
// comparator on, no external output
CINV = 1; // output inverted
// -> COUT = 1 if CIN+ < CIN-
// Main loop
for (;
{
LED = COUT; // continually display comparator output
}
}
In some pseudo-code:
If CIN+ < CIN- is true for a minimum of 2 seconds
Turn LED ON
Else, do nothing
If CIN+ > CIN- is true for a minimum of 2 seconds
Turn OFF LED
Else, do nothing
So the LED will only turn on if CIN+ < CIN- is true for more than 2 seconds. Once the LED is ON, if CIN+ > CIN- for a minimum of 2 seconds, it will turn the LED OFF.
Thanks for the help, I'm a newbie at this.