Please post your latest code.
Also, I would suggest initialising the ADC once, and leave it turned on for now. You're saving insignificant current by turning it off after every conversion.
/*
* File: GarageDoorLights.c
* Author: Ken
*
* Created on June 28, 2014, 3:59 PM
*/
// PIC12F1501 Configuration Bit Settings
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#ifndef __PICCPRO__
#define __PICCPRO__
#endif
// CONFIG1
#pragma config FOSC = INTOSC // Oscillator Selection Bits (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable (PWRT enabled)
#pragma config MCLRE = OFF // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
#pragma config STVREN = OFF // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will not cause a Reset)
#pragma config BORV = HI // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), high trip point selected.)
#pragma config LPBOR = ON // Low-Power Brown Out Reset (Low-Power BOR is enabled)
#pragma config LVP = ON // Low-Voltage Programming Enable (Low-voltage programming enabled)
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#define _XTAL_FREQ 16000000
#define FALSE (0)
#define TRUE (!0)
#define FRONT_DOOR_OPEN (PORTAbits.RA0 == 1)
#define FRONT_DOOR_CLOSE (PORTAbits.RA0 == 0)
#define BACK_DOOR_OPEN (PORTAbits.RA1 == 1)
#define BACK_DOOR_CLOSE (PORTAbits.RA1 == 0)
#define KITCHEN_DOOR_OPEN (PORTAbits.RA2 == 1)
#define KITCHEN_DOOR_CLOSE (PORTAbits.RA2 == 0)
// #define LED_On (LATAbits.LATA5 = 1)
// #define LED_Off (LATAbits.LATA5 = 0)
// #define Green_LED_On (LATAbits.LATA4 = 1)
// #define Green_LED_Off (LATAbits.LATA4 = 0)
char LightOut = 0; // Sample of light from outside, used to decide if light out
char LightTrip = 0x38; // Trip point 30. If the Light out is less than X value.
// This value is based on the output of the solar panel at
// a poit that additional light in the garage would be prefered.
char Counter = 0; // Used to adjust the PWM output duration.
// Long delay * Counter = Time that PWM is at max
char FrontJustOpened;
char BackJustOpened;
char KitchenJustOpened;
char FrontWasClosed = TRUE;
char BackWasClosed = TRUE;
char KitchenWasClosed = TRUE;
void WaitForDoorOpening();
void Dev_Init();
void Check_If_It_Is_Day_Or_Night();
void Cycle_The_Lights_Once();
void UpDelay();
void ShortDelay();
void LongDelay();
void main() // The start of the entire functional program.
{
Dev_Init(); // Configures the device inputs and outputs.
while(1) // Keep running in this loop.
{
WaitForDoorOpening(); // leave this function only when you want the lights to come on.
Check_If_It_Is_Day_Or_Night();
}
}
//************************************************************************
// This will only exit when a door transitions from closed to
// opened, any door combination. Example - If all are closed and
// one door opens, the lights will turn on for one cycle. If the door is
// still open after the light cycle completes, the lights will turn
// off. Now if the door is still open and another one opens the lights
// will then cycle one more time.
// ***********************************************************************
void WaitForDoorOpening()
{
int LightOnBySensor = FALSE;
FrontJustOpened = FALSE;
BackJustOpened = FALSE;
KitchenJustOpened = FALSE;
// the next 3 if statements simply will note that a door
// has transitioned from closed to opened
while(!FrontJustOpened && // Checks if the doors just opened
!BackJustOpened && // this instant.
!KitchenJustOpened &&
!LightOnBySensor
) // Stay in loop till one opens.
{
;
ShortDelay(); // waste some time
if (FrontWasClosed)
{
if (FRONT_DOOR_OPEN) // was closed so lets see if it just opened.
{
FrontJustOpened = TRUE; // since just opened we will then go and turn on lights
FrontWasClosed = FALSE; // no longer closed.
}
}
else if (FRONT_DOOR_CLOSE) // Front door was opened but now closed
{
FrontWasClosed = TRUE;
ShortDelay(); // Debounce needed on close only since open has a 1.5 minute debounce
}
else // Front door is open so see if the sensor wants to turn on the lights
{
// Room to add an optional sensor.
}
if (BackWasClosed)
{
if (BACK_DOOR_OPEN)
{
BackJustOpened = TRUE;
BackWasClosed = FALSE;
}
}
else if (BACK_DOOR_CLOSE) //
{
BackWasClosed = TRUE;
ShortDelay(); // Debounce needed on close only since open has a 2 minute debounce
}
if (KitchenWasClosed)
{
if (KITCHEN_DOOR_OPEN)
{
KitchenJustOpened = TRUE;
KitchenWasClosed = FALSE;
}
}
else if (KITCHEN_DOOR_CLOSE) // door was opened but now closed
{
KitchenWasClosed = TRUE;
ShortDelay(); // Debounce needed on close only since open has a 2 minute debounce
}
} // end of the while loop waiting for a door to open.
}
void Dev_Init() // setup the OPTION_REG , Load with 0xD4
{
OPTION_REG = 0x08; //
INTCON = 0; // Interups Off till needed.
OSCCON = 0x7A; // Int RC Osc at 16 MHz, Int Osc is system CLK
PORTA = 0x00; // Clear PORTA
LATA = 0x00; // Clear LATA
TRISA0 = 1; // Front Door Switch input active High.
TRISA1 = 1; // Back Door Switch input active High.
TRISA2 = 1; // Kitchen Door Switch input active High.
TRISA3 = 1; // Dig Input, not used, MCLR.
TRISA4 = 1; // Light Input. From Solar Panel
TRISA5 = 0; // PWM output.
ANSELA = 0x10; // AN3 Only is On. This is RA4 on Pin 3.
}
void Check_If_It_Is_Day_Or_Night()
{
ADRESH = 0; // clear the last result
ADCON1 = 0x60; // ADRESH is Result, FOSC/64, Vref = VDD
TRISAbits.TRISA4 = 1; // RA4 is set for input
ANSELAbits.ANSA4 = 1; // RA4/AN3 set for Analog input
ADCON0 = 0x0D; // Chan AN3 selected, Not in Progress, ADC Enabled
__delay_ms(5); // Cap Charge time, may be a bit much
ADCON0bits.GO = 1; // Start the conversion
while(ADCON0bits.GO == 1) // Wait for the conversion to finish
{
; // *** Loop till done
}
LightOut = ADRESH; // move value to LightOut
ADCON0 = 0x00; // ADC turned off,
// if(LightOut > LightTrip) // is it dark out
// { // Not yet, wait for switch to close
// WaitForDoorOpening();
// }
if(LightOut <= LightTrip) // is it dark?
{ // Yes it is dark, OK for PWM to run
Cycle_The_Lights_Once(); //
}
}
void Cycle_The_Lights_Once()
{
T2CON=0x07; // Timer2 setup configuration
while(TMR2IF==0); // test Timer interupt overflow bit, PWM timing
TMR2IF=0; // Clear the Interupt flag
PR2=0x7C; // Set the Period of the PWM output,
PWM4CON=0xE0; // PWM control settings
PWM4DCH = 0x00; // High Bits of the Duty cycle
PWM4DCL = 0x00; // Low Bits of the Duty cycle. Not Needed. Register not being used.
while(PWM4DCH < 0x7C) // Max value is 0x7C ~100% Duty Cycle.
// bring lights up in incriments
{
PWM4DCH = PWM4DCH++; // Increase the duty cycle +1
UpDelay(); // Step interval to bring lights to turn up brightness
}
PWM4DCH = 0x7C; // Increase the duty cycle +1
for(Counter = 0;Counter < 90;Counter++)
{ // Duration of the Max PWM Duty Cycle next.
LongDelay(); // Time of 500 ms, to be 120 seconds in the end.
}
while (PWM4DCH > 0x00)
{
PWM4DCH = PWM4DCH--; // Decrease the duty cycle by 1
ShortDelay(); // Step interval to bring lights down in brightness
}
T2CON = 0x00; // TMR2 Off till needed next time
}
void UpDelay()
{
__delay_ms(5);
return;
}
void ShortDelay()
{
__delay_ms(40);
return;
}
void LongDelay() // one second delay
{
__delay_ms(1000);
return;
}
Users browsing this forum: No registered users and 3 guests