#include<htc.h>
#include<stdlib.h>
#include<stdio.h>
__CONFIG(FOSC_INTRCCLK&PWRTE_ON&BOREN_OFF&WDTE_OFF);
//Solar Charge Controller
#define batfullled PORTAbits.RA4
#define batlowled PORTAbits.RA0
#define solarsense PORTCbits.RC2
#define spulse PORTAbits.RA5
#define olsense PORTCbits.RC3
#define load PORTAbits.RA2
#define solarled PORTCbits.RC4
#define batsense PORTCbits.RC1
#define tempsense PORTCbits.RC0
// Prototypes
void Delay_ms(unsigned int);
unsigned short int adc(unsigned char source);
// Subroutines
void Delay_ms(unsigned int time)
{
unsigned int i,j;
for(i = 0 ; i < time ; i++);
for(j = 0; j < 165 ; j++);
}
unsigned short int adc(unsigned char source)
{
unsigned short int result = 0;// 16/32 bit variable to hold the 10 bit A/D result
ADRESL = 0x00;
ADRESH = 0x00;
switch(source)
{
case 'o':
ADCON0=0x9C;
break;
case 's':
ADCON0=0x98;
break;
case 'b':
ADCON0=0x94;
break;
case 't':
ADCON0=0xD0;
break;
}
Delay_ms(5);
ADCON0bits.ADON = 1;
Delay_ms(1);
ADCON0bits.GO = 1;
while(ADCON0bits.GO);
ADCON0bits.ADON = 0;
result = ADRESH; // copy high byte to result
result <<= 8; // shift low byte to 2nd byte
result |= ADRESL; // OR result with ADRESL to get both the bytes into one var.
Delay_ms(5);
return result; // return the value in result
}
void main(void)
{
unsigned short int ssvalue,olvalue;
unsigned short int bsvalue,tsvalue;
unsigned short int lon_value = 195; //(for 4V)
unsigned short int htvalue = 819; //(for 17V)
unsigned short int ltvalue = 195; //(for 4V)
unsigned short int olvoltage = 819; //(for 17V)
unsigned short int hbat = 707; //(for 14.5V)
unsigned short int lbat = 532; //(for 11V)
unsigned short int htemp = 200; //196 //(for 49 C)
unsigned short int bivoltage = 584; //(for 12V)
TRISAbits.TRISA0 = 0; //bat low led
TRISAbits.TRISA4 = 0; //bat full led
TRISCbits.TRISC2 = 1; //solar sense
TRISAbits.TRISA5 = 0; //spulse
TRISCbits.TRISC3 = 1; //ol sense
TRISAbits.TRISA2 = 0; //load
TRISCbits.TRISC4 = 0; //solarled
TRISCbits.TRISC0 = 1; //temp sense
TRISCbits.TRISC1 = 1; //bat sense
ANSEL = 0xF0; // analog channel i/p o/p config
// Fosc = 4 Mhz, Tosc = 1/Fosc = 0.25us = 250ns
// Tad = 16 * Tosc = 4us
// min Tad = 1.6us
// ADCS2-ADCS0 = 101
// ADCON1 = 01010000 or 0x50
ADCON1 = 0x50; // select conversion clock
CMCON = 0x07; //comparator off
solarled = 1; //led ON to show uc working
while(1)
{
Delay_ms(1);
ssvalue = adc('s');
Delay_ms(1);
olvalue = adc('o');
Delay_ms(1);
bsvalue = adc('b');
Delay_ms(1);
tsvalue = adc('t');
ADCON0bits.VCFG = 0;
if ((ssvalue >= ltvalue) && (ssvalue <= htvalue) && (bsvalue <= hbat) && (tsvalue <= htemp) && (olvalue <= olvoltage))
{
Delay_ms(1000);
Delay_ms(1000);
if ((ssvalue >= ltvalue) && (ssvalue <= htvalue) && (bsvalue <= hbat) && (tsvalue <= htemp) && (olvalue <= olvoltage))
spulse = 1;
//charging = 1;
//batlowled = 1;
}
else if ((bsvalue >= hbat) || (tsvalue >= htemp) || (olvalue >= olvoltage))
{
Delay_ms(1000);
Delay_ms(1000);
if ((bsvalue >= hbat) || (tsvalue >= htemp) || (olvalue >= olvoltage))
spulse = 0;
}
else
{ spulse = 0;
//charging = 0;
//batlowled = 0;
}
if(bsvalue >= bivoltage)
{
goto loadon;
}
else if((bsvalue <= bivoltage) && (load == 1))
{
goto loadon;
}
else if ((ssvalue >= lon_value) || (olvalue >= olvoltage) || (bsvalue <= lbat) || (tsvalue >= htemp))
{
Delay_ms(1000);
Delay_ms(1000);
if ((ssvalue >= lon_value) || (olvalue >= olvoltage) || (bsvalue <= lbat) || (tsvalue >= htemp))
load = 0;
}
goto jump;
loadon: if ((ssvalue <= lon_value) && (olvalue <= olvoltage) && (bsvalue >= lbat) && (tsvalue <= htemp))
{
Delay_ms(1000);
Delay_ms(1000);
if ((ssvalue <= lon_value) && (olvalue <= olvoltage) && (bsvalue >= lbat) && (tsvalue <= htemp))
load = 1;
}
else if ((ssvalue >= lon_value) || (olvalue >= olvoltage) || (bsvalue <= lbat) || (tsvalue >= htemp))
{
Delay_ms(1000);
Delay_ms(1000);
if ((ssvalue >= lon_value) || (olvalue >= olvoltage) || (bsvalue <= lbat) || (tsvalue >= htemp))
load = 0;
}
jump: if (bsvalue >= hbat)
{
//Delay_ms(1000);
//Delay_ms(1000);
//if (bsvalue >= hbat)
batfullled = 1;
}
else if (bsvalue <= lbat)
{
//Delay_ms(1000);
//Delay_ms(1000);
//if (bsvalue <= lbat)
batlowled = 1;
}
else
{
batfullled = 0;
batlowled = 0;
}
}
}