Jules_Theone
Member
I'm trying to make a li ion battery charger using a PIC because the one I recently bought from Ebay looks like a simple SMPS with no protection or anything.
Anyway the problem I am having is I'm trying to get the PIC to read the voltage from an ADC and turn on a LED (eventually a transistor) to supply current to the cell and switch it off when the voltage reaches 4.0V but I can't see why it's not working - probably something small (syntax, register not setup properly, etc.) All that happens is D1 is on all the time and I can't tell if the ADC part is working or not.
It's been a couple of months since I was last tinkering with PICs and I was struggling with the basics then.
FYI, I'm using a PICKIT1 and MPLAB 8.53 if that makes a difference.
Jules
Anyway the problem I am having is I'm trying to get the PIC to read the voltage from an ADC and turn on a LED (eventually a transistor) to supply current to the cell and switch it off when the voltage reaches 4.0V but I can't see why it's not working - probably something small (syntax, register not setup properly, etc.) All that happens is D1 is on all the time and I can't tell if the ADC part is working or not.
Code:
/*
Main.Single.Cell.Charger.c
Created by: Jules Lewarne
Date: 17/09/2010
*/
#include <htc.h>
//CONFIGURATION BASED ON 16F684.h and pic.h and CONFIG reg pg 94
__CONFIG(FCMDIS & IESODIS & BORDIS & UNPROTECT & MCLRDIS & PWRTDIS & WDTDIS & INTIO);
// FCMDIS: Disabled Fail-safe clock monitor because we don't use an ext osc
// IESODIS: Internal/External Switch over is disabled (we aren't using an ext osc)
// BORDIS: Brown Out detection disabled
// UNPROTECT: Disable Program Code Protection
// MCLRDIS: Disables pin 4 on chip from being able to clear memory externally
// PWRTDIS: Allows for a min time before PIC works
// WDTDIS: Disable Watchdog timer
// INTIO: RA4 and RA5 are I/O instead of CLKOUT
//Define Variables
int i,j;
int dlay = 100;
int CellVoltage = 0;
int Current = 0;
int ADCState = 0;
void init(void)
{
CMCON0 = 0x07; //Comparators off. Digital I/O
PORTA = 0x00; //Clear PORTA
//PORTC = 0x00; //Clear PORTC
TRISA = 0b00001111; //Set PORTA all as outputs
//TRISC = 0x00; //Set RC0 and RC1 as outputs, rest to Output
//ADC Initialize
ANSEL = 0x01; //Set AN0 as analogue input
ADCON0 = 0x01; //Turn on ADC channel 0
//Bit 7 - Left Justified
//Bit 6 - Use Vdd as ref.
//Bit 4:2 - Channel 0
//Bit 1 - Do Not Start
//Bit 0 - Turn on ADC
ADCON1 = 0x10; //Select the clock Fosc/8
}
void cellvolt(void)
{
switch (ADCState) //ADC State Machine
{
case 0: //Finished, Start next sample
GODONE = 1;
ADCState++;
break;
case 1: //Wait for ADC to complete
if (!GODONE)
ADCState++;
break;
case 2: //Save sample value in "CellVoltage"
CellVoltage = ADRESH;
ADCState = 0;
break;
}
}
void main(void)
{
INTCON = 0;
init();
while(1)
{
PORTA = 0b00100000;
for (i = 0; i < dlay; i++)
{
for (j = 0; j < 160; j++) {};
}
cellvolt(); //Read CellVoltage
if (CellVoltage < 128) //check ADC count is less than 128
PORTA = 0x00; //switch on D0
else
PORTA = 0x10; //switch off D0 when threshold reached
for (i = 0; i < dlay; i++)
{
for (j = 0; j < 160; j++) {};
}
}
}
It's been a couple of months since I was last tinkering with PICs and I was struggling with the basics then.
FYI, I'm using a PICKIT1 and MPLAB 8.53 if that makes a difference.
Jules