PIC12F1501, ADC not converting
Posted: Fri Jul 04, 2014 6:41 pm
Hello,
I'm having difficulty with an ADC segment working on a PIC12F1501. Using a PIC12F1501, programmed with a PICkit3, using MPLAB X ver 2.0 and XC8.
After reviewing the data sheet for ADC configuration for the past two days, and changing things around, I have not been able to get past this road bump.
The program is meant to monitor door switches being NC, when one opens, the PIC would check the light level outside, if it is bright, no need to turn the lights on. If it is dark, the lights would be turned on via the PWM code segment. I do not seem to be resolving the light change value in the ADC sequence.
I have a variable resistor(simulates a solar cell) connected between the VSS and VDD, wiper going to a 1K resistor into Pin 7 of the PIC. this is meant to simulate a difference in light level being encountered.
Have I not turned one of the other peripherals off or have I missed a step in this ADC code segment?
I Did read about alternate pin functions in the data sheet but do not see a correlation to this directly?
Thanks,
Ken
I'm having difficulty with an ADC segment working on a PIC12F1501. Using a PIC12F1501, programmed with a PICkit3, using MPLAB X ver 2.0 and XC8.
After reviewing the data sheet for ADC configuration for the past two days, and changing things around, I have not been able to get past this road bump.
The program is meant to monitor door switches being NC, when one opens, the PIC would check the light level outside, if it is bright, no need to turn the lights on. If it is dark, the lights would be turned on via the PWM code segment. I do not seem to be resolving the light change value in the ADC sequence.
I have a variable resistor(simulates a solar cell) connected between the VSS and VDD, wiper going to a 1K resistor into Pin 7 of the PIC. this is meant to simulate a difference in light level being encountered.
Have I not turned one of the other peripherals off or have I missed a step in this ADC code segment?
I Did read about alternate pin functions in the data sheet but do not see a correlation to this directly?
Thanks,
Ken
- Code: Select all
/*
* 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.
// 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
char LightOut = 0; // Sample of light from outside, used to decide if light out
char LightTrip = 0x7F; // Trip point 50. If the Light out is less than X value. Trip
char Counter = 0; // used to adjust the PWM output.
void Dev_Init();
void MonitorSwitch();
void TestLight();
void RunLights();
void HoldTillClosed();
void LightRampUp(); // may not be needed ?
void LightRampDown(); // ISR is turned on
void HasDoorClosed();
void main() // The start of the entire functional program.
{
Dev_Init();
OSCCON=0x7A; // 16 MHz
T2CON=0x07; // was 04,
while(TMR2IF==0);
TMR2IF=0;
PR2=0x7C; // 0x50=50us ,c7 = 60 us, D7 = 84 us,F7 = 92us,
PWM1CON=0xE0; // was F0
PWM1DCH = 0x00; // Start with cycle at 0 %
PWM1DCL = 0x00; // Start with cycle at 0 %
while(1)
{
MonitorSwitch();
TestLight();
RunLights();
HoldTillClosed();
}
}
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 SETUP
PORTA = 0x00; // Clear PORTA
LATA = 0x00;
ANSELA = 0x00; // Analog OFF
TRISA = 0b101001;
TRISA0 = 1; // Photo Cell input
TRISA1 = 0; // Not Used, was TS output led
TRISA2 = 0; // PWM output.
TRISA3 = 1; // Not Used, MCLR
TRISA4 = 0; // Not used
TRISA5 = 1; // Switch input active High
LATAbits.LATA1 = 0; // turn off ts LED
}
void MonitorSwitch()
{
while(PORTAbits.RA5==0) // Test the door switches.
{
// NormISRSetSleep(); // Doors are closed, start Power save
}
}
void TestLight()
{
ADCON1 = 0x60; // ADRESH is Result, FOSC/64, Vref = VDD
TRISAbits.TRISA0 = 1;
ANSELAbits.ANSA0 = 1;
ADCON0 = 0x01; // Chan AN0 selected, Not in Progress, ADC Enabled
__delay_ms(50); // Cap Charge time, may be a bit much
ADCON0bits.GO = 1; // Start the conversion
while(ADCON0bits.GO == 1) // Wait for the conversion to finish
{
LATAbits.LATA1 = 1; // turn on trouble shoot LED ****
; // *** it gets to hear.
}
LightOut = ADRESH; //
ADCON0 = 0x00; // ADC turned off, save power
if(LightOut > LightTrip) // is it dark out
{
MonitorSwitch(); // still light out, monitor the switch state.
}
// If Light out is greater, continue on to RunLights
}
void RunLights()
{
while(PWM1DCH < 0x7C) // Max value is 0x7C 100% Duty Cycle.
{
PWM1DCH = PWM1DCH++; // Increase the duty cycle +1
PWM1DCL=0x00; //
__delay_ms(10); // Incriment Step, Time interval
}
// Duration of the Max PWM Duty Cycle next.
__delay_ms(1000); // Time of 5 seconds, to be 120 seconds in production.
while (PWM1DCH > 0x00)
{
PWM1DCH = PWM1DCH--; // Decrease the Duty cycle
PWM1DCL = 0x00;
__delay_ms(10); // Decriment Step time interval
}
}
void HoldTillClosed() // may need to be refined.
{
while(PORTAbits.RA5==1) // Door is open, Start PWM Output low to
{ // high duty cycle. Once at high rate,
// Stay and Wait // hold for a set time, then ramp down.
__delay_ms(2000);
LATAbits.LATA1 = 0; // turn off trouble shoot LED
}
}