kasamiko
Full Member level 3
- Joined
- May 23, 2004
- Messages
- 154
- Helped
- 16
- Reputation
- 32
- Reaction score
- 18
- Trophy points
- 1,298
- Location
- Philippines
- Activity points
- 1,121
#include <pic.h>
#include <math.h>
#include "lcd.h"
#define charge_Batt_1_Solar 0x15
#define charge_Batt_2_Solar 0x0E
#define Batt_Low_Solar 0x07
#define charge_over 0x08
#define charge_Batt_1_AC 0x11
#define charge_Batt_2_AC 0x0A
#define Batt_Low_AC 0x03
void delay(unsigned int time) // Delay
{
unsigned int i, j;
for(i=0;i<time;i++)
for(j=0;j<time;j++);
}
void ADC_Init() // Intialise ADC
{
ADCON0 = 0x01;
ADCON1 = 0xC4; //Right justified,AN0,AN1,AN3 are analog input.
ADRESH = 0;
ADRESL = 0;
}
unsigned int ADC_Read(unsigned char channel)
{
ADCON0 &= 0xC7; //Clearing the Channel Selection Bits
ADCON0 |= channel<<3; //Setting the required Bits
delay(10); //Acquisition time to charge hold capacitor
ADGO = 1; //Initializes A/D Conversion
while(ADGO == 1); //Wait for A/D Conversion to complete
return (256 * ADRESH + ADRESL); //Returns Result
}
void main(void)
{ unsigned int Solar,Batt_1,Batt_2,temp,unit,a,b,c,d,x,y,z;
TRISA = 0x0F; //
TRISB = 0x00; // port B as output
TRISC = 0x00; // port C as output
TRISD = 0x00; // port D as output
lcdint(); // intialised LCD
cmm(0x01); // clear display
cmm(0x80); // 1'st line of LCD
data_str(" SMART CHARGER ");
cmm(0xC0); // 2'nd line of LCD
data_str(" Version 1.0 ");
delay(300);
cmm(0x01); // clear display
ADC_Init(); // Initialise ADC
while(1)
{
Solar = ADC_Read(0);
unit = Solar; // Temperature is conveted to ASCII
if(unit > 900)
{ Batt_1 = ADC_Read(1);
Batt_2 = ADC_Read(3);
if((Batt_1 < 650)&&(Batt_2 > 850))
{
cmm(0x80); // 2nd line of LCD
data_str("BATT-1 CHARGING");
cmm(0xC0); // 2nd line of LCD
data_str("BATT-2 FULL SOL.");
PORTC = charge_Batt_1_Solar;
delay(100);
}
if((Batt_2 < 650)&&(Batt_1 > 850))
{
cmm(0x80); // 2nd line of LCD
data_str("BATT-2 CHARGING");
cmm(0xC0); // 2nd line of LCD
data_str("BATT-1 FULL SOL.");
PORTC = charge_Batt_2_Solar;
delay(100);
}
if((Batt_1 > 850)&&(Batt_2 > 850))
{
cmm(0x80); // 2nd line of LCD
data_str("BATT-2 FULL");
cmm(0xC0); // 2nd line of LCD
data_str("BATT-1 FULL");
PORTC = charge_over;
delay(100);
}
if((Batt_1 < 650)&&(Batt_2 < 650))
{
cmm(0x80); // 1st line of LCD
data_str("BATT-1 BATT-2");
cmm(0xC0); // 2nd line of LCD
data_str("CHARGING SOL.");
PORTC = Batt_Low_Solar;
delay(100);
}
}
else
{ Batt_1 = ADC_Read(1);
Batt_2 = ADC_Read(3);
if((Batt_1 < 650)&&(Batt_2 > 850))
{
cmm(0x80); // 2nd line of LCD
data_str("BATT-1 CHARGING");
cmm(0xC0); // 2nd line of LCD
data_str("BATT-2 FULL AC");
PORTC = charge_Batt_1_AC;
delay(100);
}
if((Batt_2 < 650)&&(Batt_1 > 850))
{
cmm(0x80); // 2nd line of LCD
data_str("BATT-2 CHARGING");
cmm(0xC0); // 2nd line of LCD
data_str("BATT-1 FULL AC");
PORTC = charge_Batt_2_AC;
delay(100);
}
if((Batt_1 > 650)&&(Batt_2 > 850))
{
cmm(0x80); // 2nd line of LCD
data_str("BATT-2 FULL");
cmm(0xC0); // 2nd line of LCD
data_str("BATT-1 FULL");
PORTC = charge_over;
delay(100);
}
if((Batt_1 < 650)&&(Batt_2 < 650))
{
cmm(0x80); // 1st line of LCD
data_str("BATT-1 BATT-2");
cmm(0xC0); // 2nd line of LCD
data_str("CHARGING AC");
PORTC = Batt_Low_AC;
delay(100);
}
}
}
}
if the battery or solar voltage is low
if v1 < some value then
if v2 < some value then
output that both V1 and V2 are low
else if v2 > some value then
output that v1 is low but v2 is high
else
output that v1 is low but v2 is in between
else
if v2 < some value then
output that V1 is above some value and v2 is below
...
Does the power source to the PIC16F877A go low at any time? Which power source does the unit draw from? Battery? Solar? An entirely different source? Forgive the question if you've considered this already.
Looking at your code, are you using the 'solar.c" or the "solar_RBT.c"?
Also it is bad coding practice to put executable code into an ".h" file (the lcd.h file in your case) - the ".h" file should declare the functions and global variables that are defined in the corresponding ".c" file but which are referenced from each file that has the ".h" file included.
Looking at the document in your .rar file, it says (last paragraph, page 89) that the microcontroller is always powered by "Batt1.1" which feeds through the 7805 to the Vcc line.
By the sound of it, that means that "Batt1.1" must remain above (something like) 7v to 8V (to account for the minimum input voltage to the regulator). Are you saying that the battery voltage is dropping low so that the regulator cannot deliver the 5V to the PIC?
Looking at the way you can constructed the main loop with all of those "if" statements that contain 'and'ed booleans, I suspect that there are cases that you have not considered - for example, assuming that the ADC channel #0 reads above 900, if the ADC channels #1 and #2 both give a value of 700 then none of the subsequent 'if" statements will be true which means that nothing will be updated on the LCD.
It would be better to rationalise that sort of coding a bit with something like (pseudocode):
That way you will not miss various combinations.Code:if v1 < some value then if v2 < some value then output that both V1 and V2 are low else if v2 > some value then output that v1 is low but v2 is high else output that v1 is low but v2 is in between else if v2 < some value then output that V1 is above some value and v2 is below ...
Are you initialising the LCD correctly? Many LCD displays I have used require mSec pauses after power up and also after some of the reset commands are issued. It wold seem that everything is being driven by your 'cmm' function that only has 1mSec pauses. Also, once the LCD is initialised, they generally only require uSec pauses between signal transitions.
Susan
According to your code that should not happen as there is no LCD Clear Code in while(1) loop. If this is happening then are you also experiencing flickering or a little blinking of display in normal operation ?
If yes, then maybe PIC is resetting and when voltage is less than 3V and does not match with any if() conditions the LCD gets cleared and remains blank.
By the look of the codes you are sending to it in the 'lcdint' function, you are using an LCD that contains one of the standard HD44780 controller chips. The data sheet for that chip (at least the one I have) says that after sending the 'Function Set' command (the 0x38 in your case) you should wait at least 4mSec before continuing. However you seem to only be waiting for 1mSec. (Also, you need to wait at least 40mSec after the power to the LCD gets above 2.7V - this may or may not be happening in your case.)
void lcdint()
{
cmm(0x38);
cmm(0x0C);
cmm(0x01);
cmm(0x06);
cmm(0x80);
According to your code that should not happen as there is no LCD Clear Code in while(1) loop. If this is happening then are you also experiencing flickering or a little blinking of display in normal operation ?
If yes, then maybe PIC is resetting and when voltage is less than 3V and does not match with any if() conditions the LCD gets cleared and remains blank.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?