//#fuses INTRC_IO, NOWDT, NOPROTECT, BROWNOUT, PUT
#define ON 0
#define OFF 1
#define timer_val 0
#define BATTERY_CHARGE PORTC.B5
#define LOAD PORTC.B4
unsigned int Panel_Value, Battery_Value,FLAG_1,FLAG_2=0,FLAG_3,FLAG_4,FLAG_5,FLAG_6,FLAG_7,FLAG_8;
//int OnPulse = 217;
//int OffPulse = 38;
//char TOG = 0;
unsigned int i = 305,j=1, factor = 0,divide_val=0,k=14,l=3;
void interrupt(){
if(INTCON.T0IF){
T0IF_bit = 0;
TMR0 = timer_val;
}
if(FLAG_1 == 1){
LOAD = ON;
//FLAG_2 = 1;
}
}
void main() {
TRISA = 0xff; //port A as input
TRISC = 0x00; //port C as output
PORTC = 0X00; //INITIAL VALUE ON PORTC
ADCON0 = 0b00000000; //Left Justified
ADCON1 = 0b00110000; //Clock derived from internal RC oscilator
ANSEL = 0b00000011; //channel AN0 and AN1 as analog input
INTCON = 0b10100000;
INTCON.T0IF = 0;
//CMCON0 = 0x07; //Disable the comparators
T0CS_bit = 0;
PSA_bit = 0; //Prescaler is assigned to Timer0 module
PS0_bit = 0; //TMR0 rate 1:1
PS1_bit = 0;
PS2_bit = 0;
//TMR0 = timer_val;
while(1){
Panel_Value = ADC_Read(0);
Battery_Value = ADC_Read(1);
if(Panel_value>=122){ //AD Value for 3 volt
BATTERY_CHARGE = ON;
if(Battery_Value>=430){ //AD Value for 12v
BATTERY_CHARGE = OFF;
}
}
if(Battery_Value > 430){
BATTERY_CHARGE = OFF;
}
if(Panel_Value <= 30 ){ //Panel AD value for 1v <=40
BATTERY_CHARGE = OFF;
LOAD = ON;
}
if(Panel_Value > 321){ //Panel Voltage 9 v
LOAD = OFF;
}
if(Battery_Value <=425 && Battery_Value > 310){ /////////////LOOP ------(1)
FLAG_1 = 1;
FLAG_2 = 1;
divide_val = ((Battery_Value/4) + k);
if(divide_val>=200){
divide_val = divide_val + l;
l=l+3;
if(l==42){
l = 3;
}
}
if(TMR0 >= divide_val){
LOAD = OFF;
k++;
//FLAG_1=0;
}
if(k>=134){
k=14;
FLAG_1 = 0;
}
}
if(Battery_Value <= 310 ){ //Battery ADC value for 9v 310,309
while(Battery_Value<=400){
Battery_Value = ADC_Read(1);
LOAD = OFF;
}
LOAD = ON;
}
}
}
Code C - [expand] 1 2 3 l=l+3; if(l==42){ l = 3;
Yes now u got it right, luminosity of LED should be constant at least up to 10.15v from 13v. What to do for that? And how to refer Timer1 as Timer0 is referred as TMR0 how to refer timer 1.
//#fuses INTRC_IO, NOWDT, NOPROTECT, BROWNOUT, PUT
#define ON 0
#define OFF 1
#define timer_val 0
#define BATTERY_CHARGE PORTC.B5
#define LOAD PORTC.B4
unsigned int Panel_Value, Battery_Value,FLAG_1,FLAG_2=0,FLAG_3,FLAG_4,FLAG_5,FLAG_6,FLAG_7,FLAG_8;
unsigned int count_on,count_off;
//int OnPulse = 217;
//int OffPulse = 38;
//char TOG = 0;
unsigned int i = 305,j=1, factor = 0,divide_val=0,k=14,l=3;
void interrupt timer0_ISR()
{
if(INTCON.T0IF)
T0IF_bit = 0;
if(FLAG_1 == 1)
{
LOAD = OFF;
TMR0 = 255-divide_val;
}
else
{
TMR0 = divide_val;
LOAD = ON;
FLAG_1 = 1;
}
}
}
void main() {
TRISA = 0xff; //port A as input
TRISC = 0x00; //port C as output
PORTC = 0X00; //INITIAL VALUE ON PORTC
ADCON0 = 0b00000000; //Left Justified
ADCON1 = 0b00110000; //Clock derived from internal RC oscilator
ANSEL = 0b00000011; //channel AN0 and AN1 as analog input
INTCON = 0b10100000;
INTCON.T0IF = 0;
//CMCON0 = 0x07; //Disable the comparators
T0CS_bit = 0;
PSA_bit = 0; //Prescaler is assigned to Timer0 module
PS0_bit = 0; //TMR0 rate 1:1
PS1_bit = 0;
PS2_bit = 0;
//TMR0 = timer_val;
while(1){
Panel_Value = ADC_Read(0);
Battery_Value = ADC_Read(1);
if(Panel_value>=122){ //AD Value for 3 volt
BATTERY_CHARGE = ON;
if(Battery_Value>=430){ //AD Value for 12v
BATTERY_CHARGE = OFF;
}
}
if(Battery_Value > 430){
BATTERY_CHARGE = OFF;
}
if(Panel_Value <= 30 ){ //Panel AD value for 1v <=40
BATTERY_CHARGE = OFF;
LOAD = ON;
}
if(Panel_Value > 321){ //Panel Voltage 9 v
LOAD = OFF;
}
if(Battery_Value <=425 && Battery_Value > 310)
{ /////////////LOOP ------(1)
divide_val = (Battery_Value-310)*255/425;
if(FLAG_1 == 1)
FLAG_1 = 0;
}
}
if(Battery_Value <= 310 ){ //Battery ADC value for 9v 310,309
while(Battery_Value<=400){
Battery_Value = ADC_Read(1);
LOAD = OFF;
}
LOAD = ON;
}
}
}
//#fuses INTRC_IO, NOWDT, NOPROTECT, BROWNOUT, PUT
#define ON 0
#define OFF 1
#define timer_val 0
#define BATTERY_CHARGE PORTC.B5
#define LOAD PORTC.B4
unsigned int Panel_Value, Battery_Value,FLAG_1,FLAG_2=0,FLAG_3,FLAG_4,FLAG_5,FLAG_6,FLAG_7,FLAG_8;
unsigned int count_on,count_off;
//int OnPulse = 217;
//int OffPulse = 38;
//char TOG = 0;
unsigned int i = 305,j=1, factor = 0,divide_val=0,k=14,l=3;
void interrupt ()
{
if(INTCON.T0IF){
T0IF_bit = 0;
if(FLAG_1 == 1){
LOAD = OFF;
TMR0 = 255-divide_val;
FLAG_1 = 0;
}
else
{
TMR0 = divide_val;
LOAD = ON;
FLAG_1 = 0;
}
}
}
void main() {
TRISA = 0xff; //port A as input
TRISC = 0x00; //port C as output
PORTC = 0X00; //INITIAL VALUE ON PORTC
ADCON0 = 0b00000000; //Left Justified
ADCON1 = 0b00110000; //Clock derived from internal RC oscilator
ANSEL = 0b00000011; //channel AN0 and AN1 as analog input
INTCON = 0b10100000;
INTCON.T0IF = 0;
//CMCON0 = 0x07; //Disable the comparators
T0CS_bit = 0;
PSA_bit = 0; //Prescaler is assigned to Timer0 module
PS0_bit = 0; //TMR0 rate 1:1
PS1_bit = 0;
PS2_bit = 0;
//TMR0 = timer_val;
while(1){
Panel_Value = ADC_Read(0);
Battery_Value = ADC_Read(1);
if(Panel_value>=122){ //AD Value for 3 volt
BATTERY_CHARGE = ON;
if(Battery_Value>=430){ //AD Value for 12v
BATTERY_CHARGE = OFF;
}
}
if(Battery_Value > 430){
BATTERY_CHARGE = OFF;
}
if(Panel_Value <= 30 ){ //Panel AD value for 1v <=40
BATTERY_CHARGE = OFF;
LOAD = ON;
}
if(Panel_Value > 321){ //Panel Voltage 9 v
LOAD = OFF;
}
if(Battery_Value <=425 && Battery_Value > 310){ /////////////LOOP ------(1)
FLAG_1 = 1;
divide_val = (Battery_Value-310)*255/425;
//if(FLAG_1 == 1){
//FLAG_1 = 0;
//}
}
if(Battery_Value <= 310 ){ //Battery ADC value for 9v 310,309
while(Battery_Value<=400){
Battery_Value = ADC_Read(1);
LOAD = OFF;
}
LOAD = ON;
}
}
}
INTCON = 0b10100000;
INTCON.T0IF = 0;
Yes interrupt is working fine for the code.
- - - Updated - - -
Hey mathespbe your code is working much better, thanx a lot. Only one issue remaining is LOAD is not getting OFF in last if(), condition that is below 9 v. Below 9 v it gets very dim, that is ok but then it flickers a lot and LOAD should not get ON until 12.4 v. But its not getin OFF completely and getting ON on even before 9 v. Means voltage at which it gets OFF, it gets ON at same voltage. Why its happening? I tried sevral if conditions, but not working? What must be the reason? I changed FLAG_1 condition, a little, is that correct?
Code://#fuses INTRC_IO, NOWDT, NOPROTECT, BROWNOUT, PUT #define ON 0 #define OFF 1 #define timer_val 0 #define BATTERY_CHARGE PORTC.B5 #define LOAD PORTC.B4 unsigned int Panel_Value, Battery_Value,FLAG_1,FLAG_2=0,FLAG_3,FLAG_4,FLAG_5,FLAG_6,FLAG_7,FLAG_8; unsigned int count_on,count_off; //int OnPulse = 217; //int OffPulse = 38; //char TOG = 0; unsigned int i = 305,j=1, factor = 0,divide_val=0,k=14,l=3; void interrupt () { if(INTCON.T0IF){ T0IF_bit = 0; if(FLAG_1 == 1){ LOAD = OFF; TMR0 = 255-divide_val; FLAG_1 = 0; } else { TMR0 = divide_val; LOAD = ON; FLAG_1 = 0; } } } void main() { TRISA = 0xff; //port A as input TRISC = 0x00; //port C as output PORTC = 0X00; //INITIAL VALUE ON PORTC ADCON0 = 0b00000000; //Left Justified ADCON1 = 0b00110000; //Clock derived from internal RC oscilator ANSEL = 0b00000011; //channel AN0 and AN1 as analog input INTCON = 0b10100000; INTCON.T0IF = 0; //CMCON0 = 0x07; //Disable the comparators T0CS_bit = 0; PSA_bit = 0; //Prescaler is assigned to Timer0 module PS0_bit = 0; //TMR0 rate 1:1 PS1_bit = 0; PS2_bit = 0; //TMR0 = timer_val; while(1){ Panel_Value = ADC_Read(0); Battery_Value = ADC_Read(1); if(Panel_value>=122){ //AD Value for 3 volt BATTERY_CHARGE = ON; if(Battery_Value>=430){ //AD Value for 12v BATTERY_CHARGE = OFF; } } if(Battery_Value > 430){ BATTERY_CHARGE = OFF; } if(Panel_Value <= 30 ){ //Panel AD value for 1v <=40 BATTERY_CHARGE = OFF; LOAD = ON; } if(Panel_Value > 321){ //Panel Voltage 9 v LOAD = OFF; } if(Battery_Value <=425 && Battery_Value > 310){ /////////////LOOP ------(1) FLAG_1 = 1; divide_val = (Battery_Value-310)*255/425; //if(FLAG_1 == 1){ //FLAG_1 = 0; //} } if(Battery_Value <= 310 ){ //Battery ADC value for 9v 310,309 while(Battery_Value<=400){ Battery_Value = ADC_Read(1); LOAD = OFF; } LOAD = ON; } } }
//#fuses INTRC_IO, NOWDT, NOPROTECT, BROWNOUT, PUT
#define ON 0
#define OFF 1
#define timer_val 0
#define BATTERY_CHARGE PORTC.B5
#define LOAD PORTC.B4
unsigned int Panel_Value, Battery_Value,FLAG_1,FLAG_2=0,FLAG_3,FLAG_4,FLAG_5,FLAG_6,FLAG_7,FLAG_8;
unsigned int count_on,count_off;
//int OnPulse = 217;
//int OffPulse = 38;
//char TOG = 0;
unsigned int i = 305,j=1, factor = 0,divide_val=0,k=14,l=3;
void interrupt timer0_ISR()
{
if(INTCON.T0IF)
T0IF_bit = 0;
if(FLAG_1 == 1)
{
LOAD = OFF;
TMR0 = 255-divide_val;
}
else
{
TMR0 = divide_val;
LOAD = ON;
FLAG_1 = 1;
}
}
}
void main() {
TRISA = 0xff; //port A as input
TRISC = 0x00; //port C as output
PORTC = 0X00; //INITIAL VALUE ON PORTC
ADCON0 = 0b00000000; //Left Justified
ADCON1 = 0b00110000; //Clock derived from internal RC oscilator
ANSEL = 0b00000011; //channel AN0 and AN1 as analog input
INTCON = 0b10100000;
INTCON.T0IF = 0;
//CMCON0 = 0x07; //Disable the comparators
T0CS_bit = 0;
PSA_bit = 0; //Prescaler is assigned to Timer0 module
PS0_bit = 0; //TMR0 rate 1:1
PS1_bit = 0;
PS2_bit = 0;
//TMR0 = timer_val;
while(1){
Panel_Value = ADC_Read(0);
Battery_Value = ADC_Read(1);
if(Panel_value>=122)
{
//AD Value for 3 volt
BATTERY_CHARGE = ON;
if(Battery_Value>=430) //AD Value for 12v
BATTERY_CHARGE = OFF;
}
if(Battery_Value > 430)
BATTERY_CHARGE = OFF;
if(Panel_Value <= 30 )
{ //Panel AD value for 1v <=40
BATTERY_CHARGE = OFF;
LOAD = ON;
}
if(Panel_Value > 321) //Panel Voltage 9 v
LOAD = OFF;
if(Battery_Value <=425 && Battery_Value > 310)
{ /////////////LOOP ------(1)
divide_val = (Battery_Value-310)*255/425;
if(FLAG_1 == 1)
FLAG_1 = 0;
}
if(Battery_Value <= 310 )
{ //Battery ADC value for 9v 310,309
while(Battery_Value<=400)
{
Battery_Value = ADC_Read(1);
LOAD = OFF;
}
LOAD = ON;
}
}
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 //#fuses INTRC_IO, NOWDT, NOPROTECT, BROWNOUT, PUT #define ON 0 #define OFF 1 #define timer_val 0 #define BATTERY_CHARGE PORTC.B5 #define LOAD PORTC.B4 unsigned int Panel_Value, Battery_Value,FLAG_1,FLAG_2=0,FLAG_3,FLAG_4,FLAG_5,FLAG_6,FLAG_7,FLAG_8,status; unsigned int count_on,count_off; unsigned int i = 305,j=1, factor = 0,divide_val=0,k=14,l=3; void interrupt () { if(INTCON.T0IF && status){ T0IF_bit = 0; if(FLAG_1 == 1){ LOAD = OFF; TMR0 = 255-divide_val; FLAG_1 = 0; } else { TMR0 = divide_val; LOAD = ON; FLAG_1 = 0; } } } void main() { TRISA = 0xff; //port A as input TRISC = 0x00; //port C as output PORTC = 0X00; //INITIAL VALUE ON PORTC ADCON0 = 0b00000000; //Left Justified ADCON1 = 0b00110000; //Clock derived from internal RC oscilator ANSEL = 0b00000011; //channel AN0 and AN1 as analog input INTCON = 0b10100000; INTCON.T0IF = 0; T0CS_bit = 0; PSA_bit = 0; //Prescaler is assigned to Timer0 module PS0_bit = 0; //TMR0 rate 1:1 PS1_bit = 0; PS2_bit = 0; while(1){ Panel_Value = ADC_Read(0); Battery_Value = ADC_Read(1); if(Panel_value>=122){ //AD Value for 3 volt BATTERY_CHARGE = ON; if(Battery_Value>=430){ //AD Value for 12v BATTERY_CHARGE = OFF; } } if(Battery_Value > 430){ BATTERY_CHARGE = OFF; } if(Panel_Value <= 30 ){ //Panel AD value for 1v <=40 BATTERY_CHARGE = OFF; status = 0; } if(Panel_Value > 321){ //Panel Voltage 9 v status = 1; } if(Battery_Value <=425 && Battery_Value > 310){ /////////////LOOP ------(1) FLAG_1 = 1; divide_val = (Battery_Value-310)*255/425; } if(Battery_Value <= 310 ){ //Battery ADC value for 9v 310,309 while(Battery_Value<=400){ Battery_Value = ADC_Read(1); status = 1; } else status = 0; } } }
Hi mathespbe, this code also making no effect. Conditions are still same. Same ON/ OFF issue on undesired voltages. Below 9 v of battery volt, LOAD should get completely OFF and should not get turned ON until battery voltge of 12.4v. Plz tell me what is not proper in 9V condition? One more thing plz tell me that where should FLAG_1 specifically be 1 and 0, because with conditions in code LOAD is continuously OFF.
Now everything is going absurd n weird in code. In my initial code, LOAD used to get Switch OFF when Solar Panel voltage was going above 9V and switch ON when Solar Panel voltage is below 1V. Now it also has stopped working, guys plz wish me luck.
//#fuses INTRC_IO, NOWDT, NOPROTECT, BROWNOUT, PUT
#define ON 0
#define OFF 1
#define timer_val 0
#define BATTERY_CHARGE PORTC.B5
#define LOAD PORTC.B4
unsigned int Panel_Value, Battery_Value,FLAG_1,FLAG_2=0,FLAG_3,FLAG_4,FLAG_5,FLAG_6,FLAG_7,FLAG_8;
unsigned int count_on,count_off;
//int OnPulse = 217;
//int OffPulse = 38;
//char TOG = 0;
unsigned int i = 305,j=1, factor = 0,divide_val=0,k=14,l=3;
void interrupt timer0_ISR()
{
if(INTCON.T0IF)
T0IF_bit = 0;
if(FLAG_1 == 1)
{
LOAD = OFF;
TMR0 = 255-divide_val;
}
else
{
TMR0 = divide_val;
LOAD = ON;
FLAG_1 = 1;
}
}
}
void main() {
TRISA = 0xff; //port A as input
TRISC = 0x00; //port C as output
PORTC = 0X00; //INITIAL VALUE ON PORTC
ADCON0 = 0b00000000; //Left Justified
ADCON1 = 0b00110000; //Clock derived from internal RC oscilator
ANSEL = 0b00000011; //channel AN0 and AN1 as analog input
INTCON = 0b10100000;
INTCON.T0IF = 0;
//CMCON0 = 0x07; //Disable the comparators
T0CS_bit = 0;
PSA_bit = 0; //Prescaler is assigned to Timer0 module
PS0_bit = 0; //TMR0 rate 1:1
PS1_bit = 0;
PS2_bit = 0;
//TMR0 = timer_val;
while(1){
Panel_Value = ADC_Read(0);
Battery_Value = ADC_Read(1);
if(Panel_value>=122)
{
//AD Value for 3 volt
BATTERY_CHARGE = ON;
if(Battery_Value>=430) //AD Value for 12v
BATTERY_CHARGE = OFF;
}
if(Battery_Value > 430)
BATTERY_CHARGE = OFF;
if(Panel_Value <= 30 )
{ //Panel AD value for 1v <=40
BATTERY_CHARGE = OFF;
}
if(Panel_Value > 321) //Panel Voltage 9 v
{
if(Battery_Value <=425 && Battery_Value > 310)
{ /////////////LOOP ------(1)
divide_val = (Battery_Value-310)*255/425;
if(FLAG_1 == 1)
FLAG_1 = 0;
}
if(Battery_Value <= 310 )
{ //Battery ADC value for 9v 310,309
while(Battery_Value <= 400)
{
Battery_Value = ADC_Read(1);
FLAG_1 = 1;
}
divide_val = (Battery_Value-310)*255/425;
}
}
}
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?