Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] problem in reading multiple adc channels

Status
Not open for further replies.
What about batsense, batlow, bathigh, temp sense, templed?
 

batsense (analog channel) to check the battery voltage
wen battery is fully charged bathigh led will glow and
wen battery is low batlow led will glow .....

temp sense is for checking battery temperature.....

---------- Post added at 12:17 ---------- Previous post was at 12:14 ----------

i modified little bit in the code and working very nice check here
Code:
//Overload, Dusk and Dawn Control

#define solarsense PORTC.RC2
#define spulse PORTA.RA5
#define olsense PORTC.RC3
#define load PORTA.RA2
#define solarled PORTC.RC4
#define charging PORTA.RA1
unsigned long int adc(unsigned char source);

// Subroutine

unsigned long int adc(unsigned char source)
{

        unsigned long 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;
                        ADCON0.ADON = 1;
                        Delay_ms(5);
                        break;

                case 's':
                        ADCON0=0x98;
                        ADCON0.ADON = 1;
                        Delay_ms(5);
                        break;

        }

                ADCON0.GO = 1;
                while(ADCON0.GO);
                ADCON0.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
}

//main
void main(void)
{
        unsigned long int ssvalue,olvalue;
        unsigned long int lon_value = 409;              //(for 2V)
        unsigned long int htvalue = 665;                //(for 3.25V)
        unsigned long int ltvalue = 563;                //(for 2.75)
        unsigned long int olvoltage = 819;              //(for 4V)

        TRISC.TRISC2 = 1;          //solar sense
        TRISA.TRISA5 = 0;          //spulse
        TRISC.TRISC3 = 1;          //ol sense
        TRISA.TRISA2 = 0;          //load
        TRISC.TRISC4 = 0;          //solarled
        TRISA.TRISA1 = 0;          //charging
        ANSEL = 0xF0;
        // 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;

        solarled = 1;      //led ON to show uc working

        while(1)
        {
                ssvalue = adc('s');

                if (ssvalue >= ltvalue && ssvalue <= htvalue)
                {
                        spulse = 1;
                        charging = 1;
                }
                else
                        spulse = 0;

                Delay_ms(5);
                olvalue = adc('o');
                if (ssvalue <= lon_value && olvalue < olvoltage)
                {
                        load = 1;
                }
                else if (ssvalue >= lon_value || olvalue >= olvoltage)
                {
                        load = 0;
                }

        }

}
 

It's OK. But to implement battery sense and temp sense you need a circuit like the attached one.
 

Attachments

  • pic16f676v2.rar
    17.7 KB · Views: 98

I encountered problem wen trying to get output from RA1 pin.....

just like solarled=1;
i given charging=1; to pin RA1.... output is not constant.... its blinking....
even i tried by disabling comparator...i.e COMCON=0x07;..... any idea?????

---------- Post added at 14:31 ---------- Previous post was at 14:11 ----------

this is the code
Code:
//Overload, Dusk and Dawn Control

#define solarsense PORTC.RC2
#define spulse PORTA.RA5
#define olsense PORTC.RC3
#define load PORTA.RA2
#define solarled PORTC.RC4
#define charging PORTA.RA1
unsigned long int adc(unsigned char source);

// Subroutine

unsigned long int adc(unsigned char source)
{

        unsigned long 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;
                        ADCON0.ADON = 1;
                        Delay_ms(5);
                        break;

                case 's':
                        ADCON0=0x98;
                        ADCON0.ADON = 1;
                        Delay_ms(5);
                        break;

        }

                ADCON0.GO = 1;
                while(ADCON0.GO);
                ADCON0.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
}

//main
void main(void)
{
        unsigned long int ssvalue,olvalue;
        unsigned long int lon_value = 409;              //(for 2V)
        unsigned long int htvalue = 665;                //(for 3.25V)
        unsigned long int ltvalue = 563;                //(for 2.75)
        unsigned long int olvoltage = 819;              //(for 4V)

        TRISC.TRISC2 = 1;          //solar sense
        TRISA.TRISA5 = 0;          //spulse
        TRISC.TRISC3 = 1;          //ol sense
        TRISA.TRISA2 = 0;          //load
        TRISC.TRISC4 = 0;          //solarled
        TRISA.TRISA1 = 0;          //charging
        ANSEL = 0xF0;
        // 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;
        CMCON = 0x07;
        solarled = 1;      //led ON to show uc working
        while(1)
        {
                ssvalue = adc('s');

                if (ssvalue >= ltvalue && ssvalue <= htvalue)
                {
                        spulse = 1;
                        charging = 1;
                }
                else
                        spulse = 0;
                        charging = 0;
                        
                Delay_ms(5);
                olvalue = adc('o');
                if (ssvalue <= lon_value && olvalue < olvoltage)
                {
                        load = 1;
                }
                else if (ssvalue >= lon_value || olvalue >= olvoltage)
                {
                        load = 0;
                }

        }

}
 

With RA0 and RA1 the problem is CMCON. Did you check wheather it is COMCON or CMCON? Are you sure 0x07 disables the comparator. I'll try in an hour.

Where is ANSEL configuration?

OK ANSEL is there with the value F0, but it is wrong. When you are not using AN7, ANSEL cannot be 11110000. Tell me which analog channels you are using.
 
Last edited:

check my previous code i am using an7,an6,an5,an4 so ANSEL=11110000
and CMCON is correct according to datasheets of PIC16F676.....
 

In your previous code, where are you reading the other 2 analog channels? OK. ANSEL = 11110000 is right. Send me your isis file.
Why don't you just use PIC16F877/A. Which led is blinking?
 
Last edited:

check this code:
Code:
//Solar Charge Controller
#define batfullled PORTC.RC5
#define batlowled PORTA.RA4
#define solarsense PORTC.RC2
#define spulse PORTA.RA5
#define olsense PORTC.RC3
#define load PORTA.RA2
#define solarled PORTC.RC4
#define charging PORTA.RA1
#define batsense PORTC.RC1
#define tempsense PORTC.RC0
unsigned short int adc(unsigned char source);

// Subroutine

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;
                        ADCON0.ADON = 1;
                        Delay_ms(5);
                        break;

                case 's':
                        ADCON0=0x98;
                        ADCON0.ADON = 1;
                        Delay_ms(5);
                        break;
                        
                case 'b':
                        ADCON0=0x94;
                        ADCON0.ADON = 1;
                        Delay_ms(5);
                        break;

                case 't':
                        ADCON0=0xD0;
                        ADCON0.ADON = 1;
                        Delay_ms(5);
                        break;
        }

                ADCON0.GO = 1;
                while(ADCON0.GO);
                ADCON0.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
}

//main
void main(void)
{
        unsigned short int ssvalue,olvalue;
        unsigned short int bsvalue,tsvalue;
        unsigned short int lon_value = 409;              //(for 2V)
        unsigned short int htvalue = 665;                //(for 3.25V)
        unsigned short int ltvalue = 563;                //(for 2.75)
        unsigned short int olvoltage = 819;              //(for 4V)
        unsigned short int hbat = 778;                   //(for 3.8V)
        unsigned short int lbat = 594;                   //(for 2.9V)
        unsigned short int htemp = 196;                  //(for 49 C)
        TRISA.TRISA4 = 0;          // bat low led
        TRISC.TRISC5 = 0;          // bat full led
        TRISC.TRISC2 = 1;          //solar sense
        TRISA.TRISA5 = 0;          //spulse
        TRISC.TRISC3 = 1;          //ol sense
        TRISA.TRISA2 = 0;          //load
        TRISC.TRISC4 = 0;          //solarled
        TRISA.TRISA4 = 0;          //charging
        TRISC.TRISC0 = 1;          //temp sense
        TRISC.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)
        {
                ssvalue = adc('s');

                if (ssvalue >= ltvalue && ssvalue <= htvalue)
                {
                        spulse = 1;
                        charging = 1;
                }
                else
                        spulse = 0;
                        charging = 0;

                Delay_ms(5);
                olvalue = adc('o');
                if (ssvalue <= lon_value && olvalue < olvoltage)
                {
                        load = 1;
                }
                else if (ssvalue >= lon_value || olvalue >= olvoltage)
                {
                        load = 0;
                }
                
                Delay_ms(5);
                bsvalue = adc('b');
                if (bsvalue >= hbat)
                {
                        spulse = 0;
                        batfullled = 1;
                        //batlowled = 0;
                }
                else if(bsvalue <= lbat)
                {
                        load = 0;
                        batlowled = 1;
                        //batfullled = 0;
                }
                else
                        batfullled = 0;
                        batlowled = 0;
                
                Delay_ms(5);
                tsvalue = adc('t');
                if(tsvalue >= htemp)
                {
                        load = 0;
                        spulse = 0;
                }

        }

}
 

Upload the isis file. I will compile and check. Is it working with you?

You've removed the subroutine Delayms, and I am getting errors.

Check the attached file.
 

Attachments

  • ss4.JPG
    ss4.JPG
    256.1 KB · Views: 102
Last edited:

here i used 4 channels
1)solar sense (wen it is in the range 2.75-3.25 ->spulse=1 wen below 2V->load=1)
2)ol sense(wen it is above 4V->load=0)
3)bat sense(wen it is above 3.8v spulse=0 and batfull led=1)
(wen it is below 2.9V load=0 and batlowled=1)
4)tempsense(wen the temperature is above 49 C ->load=0 and spulse=0)
 

i attach isis file and cfile.... check once
 

Attachments

  • newversio.rar
    17.5 KB · Views: 88

I am getting so many errors. I don't know what you have done. It is not recognizing ANSEL, ADCON0, and ADCON1. I need some time to check.

Fix the errors and send the compiling file. Then I will check.

I tried till now. It is not compiling.
 

Attachments

  • ss5.JPG
    ss5.JPG
    325.3 KB · Views: 94
Last edited:

I finally did it. Check it. It's working now.

Code:
[syntax]


#include<htc.h>
#include<stdlib.h>
#include<stdio.h>

__CONFIG(FOSC_INTRCCLK&PWRTE_ON&BOREN_OFF&WDTE_OFF);

//Solar Charge Controller
#define batfullled PORTCbits.RC5
#define batlowled PORTAbits.RA4
#define solarsense PORTCbits.RC2
#define spulse PORTAbits.RA5
#define olsense PORTCbits.RC3
#define load PORTAbits.RA2
#define solarled PORTCbits.RC4
#define charging PORTAbits.RA1
#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;
        }
				
				ADCON0bits.ADON = 1;
                Delay_ms(5);
                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
}

//#pragma psect code main=0x100
void main(void)
{
	unsigned short int ssvalue,olvalue;
    unsigned short int bsvalue,tsvalue;
    unsigned short int lon_value = 409;              //(for 2V)
    unsigned short int htvalue = 665;                //(for 3.25V)
    unsigned short int ltvalue = 563;                //(for 2.75)
    unsigned short int olvoltage = 819;              //(for 4V)
    unsigned short int hbat = 778;                   //(for 3.8V)
    unsigned short int lbat = 594;                   //(for 2.9V)
    unsigned short int htemp = 196;                  //(for 49 C)

	TRISAbits.TRISA4 = 0;          // bat low led
    TRISCbits.TRISC5 = 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
    TRISAbits.TRISA4 = 0;          //charging
    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)
        {
                ssvalue = adc('s');

                if (ssvalue >= ltvalue && ssvalue <= htvalue)
                {
                        spulse = 1;
                        charging = 1;
                }
                else
                        spulse = 0;
                        charging = 0;

                Delay_ms(5);
                olvalue = adc('o');
                if (ssvalue <= lon_value && olvalue < olvoltage)
                {
                        load = 1;
                }
                else if (ssvalue >= lon_value || olvalue >= olvoltage)
                {
                        load = 0;
                }
                
                Delay_ms(5);
                bsvalue = adc('b');
                if (bsvalue >= hbat)
                {
                        spulse = 0;
                        batfullled = 1;
                        //batlowled = 0;
                }
                else if(bsvalue <= lbat)
                {
                        load = 0;
                        batlowled = 1;
                        //batfullled = 0;
                }
                else
                        batfullled = 0;
                        batlowled = 0;
                
                Delay_ms(5);
                tsvalue = adc('t');
                if(tsvalue >= htemp)
                {
                        load = 0;
                        spulse = 0;
                }

        }

}

[/syntax]

There were lots of programming errors. Don't change the code anymore.
 

u got errors because u compiled it in hitech c compiler,
i am using mikro c(as u suggested) for compiling...
its completely different from hi tech compiler, and i am using inbuilt delay function(Delay_ms) so no need of extra subroutine.....
compile in mikro c, u will get error free....
 

OK. I didnt check that. I changed the code to Hitech C code. It is working fine. I will check with mikroC and reply.
OK. It compiled sucessfully in mikroC. Now, what is the problem you are facing with the leds. And why have you used 2.5V ref for ADC. Do you want me to check the circuit with refrence voltage, or without reference voltage?. If yes have you configured ADCON0.VCFG? OK. I got it. You are using ref for only temp measurement. right?

Your RC1 is S Sense and RC2 is b sense right? It is interchanged in isis. So your s pulse is switching on only when b sense voltage = 1.25 V and s ense V = 2.75 - 3.25. It might be a problem with isis. did you check the code with real hardware?

check this mikroC code

Code:
[syntax = mikroC]

//Solar Charge Controller
#define batfullled PORTC.RC5
#define batlowled PORTA.RA4
#define solarsense PORTC.RC2
#define spulse PORTA.RA5
#define olsense PORTC.RC3
#define load PORTA.RA2
#define solarled PORTC.RC4
#define charging PORTA.RA1
#define batsense PORTC.RC1
#define tempsense PORTC.RC0

// Prototype

unsigned short int adc(unsigned char source);

// Subroutine

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 'b':
                        ADCON0=0x98;
                        break;
                        
                case 's':
                        ADCON0=0x94;
                        break;

                case 't':
                        ADCON0=0xD0;
                        break;
        }

                ADCON0.ADON = 1;
                Delay_ms(2);
                ADCON0.GO = 1;
                while(ADCON0.GO);
                ADCON0.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.
                return result;          // return the value in result
}

//main
void main()
{
        unsigned short int ssvalue,olvalue;
        unsigned short int bsvalue,tsvalue;
        unsigned short int lon_value = 409;              //(for 2V)
        unsigned short int htvalue = 665;                //(for 3.25V)
        unsigned short int ltvalue = 563;                //(for 2.75)
        unsigned short int olvoltage = 819;              //(for 4V)
        unsigned short int hbat = 778;                   //(for 3.8V)
        unsigned short int lbat = 594;                   //(for 2.9V)
        unsigned short int htemp = 196;                  //(for 49 C)
        TRISA.TRISA4 = 0;          // bat low led
        TRISC.TRISC5 = 0;          // bat full led
        TRISC.TRISC2 = 1;          //solar sense
        TRISA.TRISA5 = 0;          //spulse
        TRISC.TRISC3 = 1;          //ol sense
        TRISA.TRISA2 = 0;          //load
        TRISC.TRISC4 = 0;          //solarled
        TRISA.TRISA4 = 0;          //charging
        TRISC.TRISC0 = 1;          //temp sense
        TRISC.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 = 0x10;     // select conversion clock
        CMCON = 0x07;      //comparator off
        solarled = 1;      //led ON to show uc working
        while(1)
        {
                ssvalue = adc('s');
                Delay_ms(2);
                olvalue = adc('o');
                Delay_ms(2);
                bsvalue = adc('b');
                Delay_ms(2);
                tsvalue = adc('t');

                if ((ssvalue >= ltvalue) && (ssvalue <= htvalue) && (bsvalue < hbat))
                {
                        spulse = 1;
                        charging = 1;
                }
                else if (tsvalue >= htemp)
                {       spulse = 0;
                }
                else
                {       spulse = 0;
                        charging = 0;
                }
                        

                
                if ((ssvalue <= lon_value) && (olvalue < olvoltage) && (bsvalue > lbat) && (tsvalue < htemp))
                {
                        load = 1;
                }
                else if ((ssvalue >= lon_value) || (olvalue >= olvoltage) || (bsvalue <= lbat) || (tsvalue >= htemp))
                {
                        load = 0;
                }
                
                
                if (bsvalue >= hbat)
                {
                        //spulse = 0;
                        batfullled = 1;
                        //batlowled = 0;
                }
                else if(bsvalue <= lbat)
                {
                        //load = 0;
                        batlowled = 1;
                        //batfullled = 0;
                }
                else
                        batfullled = 0;
                        batlowled = 0;
                
                
                if(tsvalue >= htemp)
                {
                        //load = 0;
                        //spulse = 0;
                }

        }

}

[/syntax]
 
Last edited:

Check this. I think it is working properly. I think mikroC is creating wrong hex files.


Except the batlowled, which should turn on when bsense is < 2.9 V, everything is working fine. Modify this code for mikroC and select the right OSC in Project > Edit Properties, and compile and check. If batlowled works then the problem is with hitech c or isis.

Code:
[syntax = Hi Tech C]

#include<htc.h>
#include<stdlib.h>
#include<stdio.h>

__CONFIG(FOSC_INTRCCLK&PWRTE_ON&BOREN_OFF&WDTE_OFF);

//Solar Charge Controller
#define batfullled PORTCbits.RC5
#define batlowled PORTAbits.RA4
#define solarsense PORTCbits.RC2
#define spulse PORTAbits.RA5
#define olsense PORTCbits.RC3
#define load PORTAbits.RA2
#define solarled PORTCbits.RC4
#define charging PORTAbits.RA1
#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(5);
                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
}

//#pragma psect code main=0x100
void main(void)
{
	unsigned short int ssvalue,olvalue;
    unsigned short int bsvalue,tsvalue;
    unsigned short int lon_value = 409;              //(for 2V)
    unsigned short int htvalue = 665;                //(for 3.25V)
    unsigned short int ltvalue = 563;                //(for 2.75)
    unsigned short int olvoltage = 819;              //(for 4V)
    unsigned short int hbat = 778;                   //(for 3.8V)
    unsigned short int lbat = 594;                   //(for 2.9V)
    unsigned short int htemp = 200; //196;                  //(for 49 C)

    TRISAbits.TRISA1 = 1;          // Vref 2.5V
    TRISAbits.TRISA4 = 0;          // bat low led
    TRISCbits.TRISC5 = 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
    TRISAbits.TRISA4 = 0;          //charging
    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)
        {
                ssvalue = adc('s');
				Delay_ms(5);
                olvalue = adc('o');
				Delay_ms(5);
                bsvalue = adc('b');
				Delay_ms(5);
                tsvalue = adc('t');
				
				if ((ssvalue >= ltvalue) && (ssvalue <= htvalue) && (bsvalue < hbat) && (tsvalue < htemp))
                {
                        spulse = 1;
                        charging = 1;
                }
                else if (tsvalue >= htemp || bsvalue >= hbat)
                {       
                        spulse = 0;
                }
                else
                {       spulse = 0;
                        charging = 0;
                }
                        

                
                if ((ssvalue <= lon_value) && (olvalue < olvoltage) && (bsvalue > lbat) && (tsvalue < htemp))
                {
                        load = 1;
                }
                else if ((ssvalue >= lon_value) || (olvalue >= olvoltage) || (bsvalue <= lbat) || (tsvalue >= htemp))
                {
                        load = 0;
                }
                
                
                if (bsvalue >= hbat)
                {
                    batfullled = 1;
                                        }
                else if(bsvalue <= lbat)
                {
                    batlowled = 1;
                }
                else
                     batfullled = 0;
                     batlowled = 0;
						

        }

}

[/syntax]

I think you are confused with charging and batlowled. I think both are same.

Code:
#define batlowled PORTAbits.RA4
#define charging PORTAbits.RA1

And I think

Code:
charging = 1;
charging = 0;

should be

Code:
batlowled = 1;
batlowled = 0;

because your charging, RA1 is connected to 2.5V refrence and not led.


If your charging is batlowled, then try the below code. Except RA4 i.e., batlowled, everything is working fine. The problem might be with isis.

Modify this code to mikroC code and then check. If batlowled lights up, then it is the problem is with hitech c. If the same problem comes with mikroC, then it is the problem of isis. Check with real hardware, If baltlowled works with this code, then the problem is with isis.

Code:
[syntax = Hi Tech C]

#include<htc.h>
#include<stdlib.h>
#include<stdio.h>

__CONFIG(FOSC_INTRCCLK&PWRTE_ON&BOREN_OFF&WDTE_OFF);

//Solar Charge Controller
#define batfullled PORTCbits.RC5
//#define batlowled PORTAbits.RA4
#define batlowled PORTAbits.RA4
#define solarsense PORTCbits.RC2
#define spulse PORTAbits.RA5
#define olsense PORTCbits.RC3
#define load PORTAbits.RA2
#define solarled PORTCbits.RC4
//#define charging PORTAbits.RA1
#define charging PORTAbits.RA1
#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(5);
                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
}

//#pragma psect code main=0x100
void main(void)
{
	unsigned short int ssvalue,olvalue;
    unsigned short int bsvalue,tsvalue;
    unsigned short int lon_value = 409;              //(for 2V)
    unsigned short int htvalue = 665;                //(for 3.25V)
    unsigned short int ltvalue = 563;                //(for 2.75)
    unsigned short int olvoltage = 819;              //(for 4V)
    unsigned short int hbat = 778;                   //(for 3.8V)
    unsigned short int lbat = 594;                   //(for 2.9V)
    unsigned short int htemp = 200; //196;                  //(for 49 C)

    TRISAbits.TRISA1 = 1;
    TRISAbits.TRISA4 = 0;          // bat low led
    TRISCbits.TRISC5 = 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
    //TRISAbits.TRISA4 = 0;          //charging
    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)
        {
                ssvalue = adc('s');
				Delay_ms(5);
                olvalue = adc('o');
				Delay_ms(5);
                bsvalue = adc('b');
				Delay_ms(5);
                tsvalue = adc('t');
				
				if ((ssvalue >= ltvalue) && (ssvalue <= htvalue) && (bsvalue < hbat) && (tsvalue < htemp))
                {
                        spulse = 1;
                        //charging = 1;
						//batlowled = 1;
                }
                else if (tsvalue >= htemp || bsvalue >= hbat)
                {       
                        spulse = 0;
                }
                else
                {       spulse = 0;
                        //charging = 0;
						//batlowled = 0;
                }
                        

                
                if ((ssvalue <= lon_value) && (olvalue < olvoltage) && (bsvalue > lbat) && (tsvalue < htemp))
                {
                        load = 1;
                }
                else if ((ssvalue >= lon_value) || (olvalue >= olvoltage) || (bsvalue <= lbat) || (tsvalue >= htemp))
                {
                        load = 0;
                }
                
                
                if (bsvalue >= hbat)
                {
                    batfullled = 1;
                }
                else if (bsvalue <= lbat)
                {
                    batlowled = 1;
                }
                else
                     batfullled = 0;
                     batlowled = 0;
						

        }

}

[/syntax]

This mikroC code is ok, but I think mikroC is creating wrong hex file. Compile this code in mikroC and check.

Code:
[syntax = mikroC]

//Solar Charge Controller
#define batfullled PORTC.F5
#define batlowled PORTA.F4
#define solarsense PORTC.F2
#define spulse PORTA.F5
#define olsense PORTC.F3
#define load PORTA.F2
#define solarled PORTC.F4
#define charging PORTA.F1
#define batsense PORTC.F1
#define tempsense PORTC.F0

//main
void main()
{
        unsigned short int ssvalue,olvalue;
        unsigned short int bsvalue,tsvalue;
        unsigned short int lon_value = 409;              //(for 2V)
        unsigned short int htvalue = 665;                //(for 3.25V)
        unsigned short int ltvalue = 563;                //(for 2.75)
        unsigned short int olvoltage = 819;              //(for 4V)
        unsigned short int hbat = 778;                   //(for 3.8V)
        unsigned short int lbat = 594;                   //(for 2.9V)
        unsigned short int htemp = 200;                  //(for 49 C)
        
        TRISA = 0b001011;
        TRISC = 0b001111;
        ANSEL = 0xF0;                                    // analog channel i/p o/p config
        ADCON1 = 0x20;                                   // select conversion clock
        CMCON = 0x07;                                    //comparator off
        solarled = 1;                                    //led ON to show uc working
        
        while(1)
        {
                ssvalue = ADC_Read(6);
                Delay_ms(2);
                olvalue = ADC_Read(7);
                Delay_ms(2);
                bsvalue = ADC_Read(5);
                Delay_ms(2);
                tsvalue = ADC_Read(4);
                                               
                if ((ssvalue >= ltvalue) && (ssvalue <= htvalue) && (bsvalue < hbat) && (tsvalue < htemp))
                {
                        spulse = 1;
                }
                else if (tsvalue >= htemp || bsvalue >= hbat)
                {       
                        spulse = 0;
                }
                else
                {       spulse = 0;
                        
                }
                 
                if ((ssvalue <= lon_value) && (olvalue < olvoltage) && (bsvalue > lbat) && (tsvalue < htemp))
                {
                        load = 1;
                }
                else if ((ssvalue >= lon_value) || (olvalue >= olvoltage) || (bsvalue <= lbat) || (tsvalue >= htemp))
                {
                        load = 0;
                }
                
                
                if (bsvalue >= hbat)
                {
                    batfullled = 1;
                }
                else if(bsvalue <= lbat)
                {
                    batlowled = 1;
                }
                else
                     batfullled = 0;
                     batlowled = 0; 
        }

}

[/syntax]


ok, I did it in mikroC Check the file pic16f676_mikroC. It's working, and here is the code.

Code:
[syntax = mikroC]

//Solar Charge Controller
#define batfullled PORTC.F5
#define batlowled PORTA.F4
#define solarsense PORTC.F2
#define spulse PORTA.F5
#define olsense PORTC.F3
#define load PORTA.F2
#define solarled PORTC.F4
#define charging PORTA.F1
#define batsense PORTC.F1
#define tempsense PORTC.F0

//main
void main()
{
        unsigned long int ssvalue,olvalue;
        unsigned long int bsvalue,tsvalue;
        unsigned long int lon_value = 409;              //(for 2V)
        unsigned long int htvalue = 665;                //(for 3.25V)
        unsigned long int ltvalue = 563;                //(for 2.75)
        unsigned long int olvoltage = 819;              //(for 4V)
        unsigned long int hbat = 778;                   //(for 3.8V)
        unsigned long int lbat = 594;                   //(for 2.9V)
        unsigned long int htemp = 196;                  //(for 49 C)
        
        TRISA = 0b001011; // 0x0B;
        TRISC = 0b001111; // 0x0F;
        ANSEL = 0xF0;                                    // analog channel i/p o/p config
        ADCON1 = 0x20;                                   // select conversion clock
        CMCON = 0x07;                                    //comparator off
        solarled = 1;                                    //led ON to show uc working
        
        while(1)
        {
                ssvalue = ADC_Read(6);
                olvalue = ADC_Read(7);
                bsvalue = ADC_Read(5);
                ADCON0.VCFG = 1;
                tsvalue = ADC_Read(4);
                ADCON0.VCFG = 0;
                                               
                if ((ssvalue >= ltvalue) && (ssvalue <= htvalue) && (bsvalue < hbat) && (tsvalue < htemp))
                {
                        spulse = 1;
                }
                else if (tsvalue >= htemp || bsvalue >= hbat)
                {       
                        spulse = 0;
                }
                else
                {       spulse = 0;
                        
                }
                 
                if ((ssvalue <= lon_value) && (olvalue < olvoltage) && (bsvalue > lbat) && (tsvalue < htemp))
                {
                        load = 1;
                }
                else if ((ssvalue >= lon_value) || (olvalue >= olvoltage) || (bsvalue <= lbat) || (tsvalue >= htemp))
                {
                        load = 0;
                }
                
                
                if (bsvalue >= hbat)
                {
                    batfullled = 1;
                }
                else if(bsvalue <= lbat)
                {
                    batlowled = 1;
                }
                else
                {
                     batfullled = 0;
                     batlowled = 0;
                } 
        }

}

[/syntax]

ok. I did that also in hi tech c. check this hex file pic16f676_hitechC.rar. Here is the h tech c code.

Code:
[syntax = hi tech c]

#include<htc.h>
#include<stdlib.h>
#include<stdio.h>

__CONFIG(FOSC_INTRCCLK&PWRTE_ON&BOREN_OFF&WDTE_OFF);

//Solar Charge Controller
#define batfullled PORTCbits.RC5
#define batlowled PORTAbits.RA4
#define solarsense PORTCbits.RC2
#define spulse PORTAbits.RA5
#define olsense PORTCbits.RC3
#define load PORTAbits.RA2
#define solarled PORTCbits.RC4
//#define charging PORTAbits.RA1
//#define charging PORTAbits.RA1
#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(5);
                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(1);
                return result;          // return the value in result
}

//#pragma psect code main=0x100
void main(void)
{
	unsigned short int ssvalue,olvalue;
    unsigned short int bsvalue,tsvalue;
    unsigned short int lon_value = 409;              //(for 2V)
    unsigned short int htvalue = 665;                //(for 3.25V)
    unsigned short int ltvalue = 563;                //(for 2.75)
    unsigned short int olvoltage = 819;              //(for 4V)
    unsigned short int hbat = 778;                   //(for 3.8V)
    unsigned short int lbat = 594;                   //(for 2.9V)
    unsigned short int htemp = 200; //196;                  //(for 49 C)

	//TRISAbits.TRISA0 = 0;
	TRISAbits.TRISA1 = 1;
	TRISAbits.TRISA4 = 0;          // bat low led
    TRISCbits.TRISC5 = 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
    //TRISAbits.TRISA4 = 0;          //charging
    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))
                {
                        spulse = 1;
                        //charging = 1;
						//batlowled = 1;
                }
                else if ((bsvalue >= hbat) || (tsvalue >= htemp))
                {       
                        spulse = 0;
                }
                else
                {       spulse = 0;
                        //charging = 0;
						//batlowled = 0;
                }
                        

                
                if ((ssvalue <= lon_value) && (olvalue < olvoltage) && (bsvalue > lbat) && (tsvalue < htemp))
                {
                        load = 1;
                }
                else if ((ssvalue >= lon_value) || (olvalue >= olvoltage) || (bsvalue <= lbat) || (tsvalue >= htemp))
                {
                        load = 0;
                }
                
                
                if (bsvalue >= hbat)
                {
                    batfullled = 1;
                }
                else if (bsvalue <= lbat)
                {
                    batlowled = 1;
                }
                else
				{
                     batfullled = 0;
                     batlowled = 0;
				}
						

        }

}


[/syntax]
 

Attachments

  • pic16f676_rev5.rar
    17.9 KB · Views: 85
  • pic16f676_hex.rar
    769 bytes · Views: 83
  • pic16f676_mikroC.rar
    17.2 KB · Views: 85
  • pic16f676_hitechC.rar
    786 bytes · Views: 87
Last edited:
  • Like
Reactions: bkar

    bkar

    Points: 2
    Helpful Answer Positive Rating
sorry for late reply....
charging is different with batlow led.....
in my last code, i used RA1 for charging(not thought of Vref), wen i implement temperature sensor i used(RA1) it.... that was the mistake....
i included this extra thing.... when spulse is high the charging pin should blink.... that indicates charging of battery.....

so now i left with only one pin i.e, RA0 and have to implement that for charging......
 

Just make RA0 as charging, and add charging = 1; and charging = 0; at the proper place.
 

if i used 'short' instead of 'long' to assign data type for variables, its not working(but compiling)....
and using long takes more spaces of rom and ram....
even short itself holds 16 bits of data and it is more than enough....

but here unless putting "long" its not working..... wat is the reason behind this....

---------- Post added at 13:01 ---------- Previous post was at 12:39 ----------

in mikro C....
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top