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.
sorry...please completely ignore first version codes.....

I am using channel 0(ol sense) and channel 6(solar sense) for input in second version of code... and i am right justifying the bits for easy calculations....
so adcon0 for channel 0 is 80 and
adcon0 for channel 6 is 98......
and i am using internal clock frquency of 4M Hz....
so clock conversion should be 1.6us.... so i choose 8Tosc...
then adcon1 becomes 0x10;
 

Then Try this...

Code:
[syntax = c]

//Overload, Dusk and Dawn Control

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

__CONFIG(FOSC_INTRCCLK&PWRTE_ON&BOREN_OFF&WDTE_OFF );

#define load PORTCbits.RC3
#define olsense PORTAbits.RA0
#define spulse PORTAbits.RA5
#define solarled PORTCbits.RC4
#define solarsense PORTCbits.RC2

// Prototypes

void msdelay(unsigned int);

// Subroutines

unsigned long int adc(unsigned char source)
{
		
	unsigned long int adcout;

	ADCON0bits.ADON=1;
	ADCON0bits.GO=1;

	switch(source)
	{
		case'o':
			ADCON0=0x80;
			while(ADCON0bits.GO);
			break;

		case's':
			ADCON0=0x98;
			while(ADCON0bits.GO);
			break;
			
	}

	adcout = ADRES;
	return adcout;
}


//#pragma psect code main=0x100
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)
	unsigned long int ssvalue, olvalue;

	TRISAbits. TRISA5=0;			//spulse
	TRISCbits. TRISC4=0;			//solarled
	TRISCbits. TRISC3=0;			//load
	TRISCbits. TRISA0=1;			//solar sense
	TRISAbits. TRISC2=1;			//ol sense
	ANSEL = 0x03;
	// Fosc = 4 Mhz, Tosc = 1/Fosc = 0.25us = 250ns
	// 32 * Tosc = 8us
        // 8 * Tosc = 2us
        // min Tad = 1.6us 
	//ADCS2-ADCS0 = 001
	ADCON1 = 00010000 or 0x10
	ADCON1= 0x10;


while(1)
{
	solarled=1;				//led ON to show uc working

	ssvalue = adc('s');

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

	if(ssvalue <= lon_value)
	{
	msdelay(5);
		if(ssvalue <= lon_value)
		{
			load=1;			//light on
		}
	}	
	else
		load=0;				//light off

	olvalue = adc('o');

	if(olvalue >= olvoltage)
	{
		msdelay(5);
		if(olvalue >= olvoltage)
		{
			load=0;//light off	//light off
		}	
	}



void msdelay(unsigned int time)
{
	unsigned char i,j;
	for(i=0;i<time;i++)
	for(j=0;j<165;j++)
}


}


}

[/syntax]
 
Last edited:

here ADRES is undefined....
we have to take adc results in two bytes i.e ADRESH and ADRESL....
 

Do you use unsigned short for variables to hold adresh and adresl? I think your pic has 10 bit adc? right? Is your code working? Have you modified the adres thing?

---------- Post added at 15:23 ---------- Previous post was at 15:03 ----------

Try this...

Code:
[syntax = c]

//Overload, Dusk and Dawn Control

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

__CONFIG(FOSC_INTRCCLK&PWRTE_ON&BOREN_OFF&WDTE_OFF );

#define load PORTCbits.RC3
#define olsense PORTAbits.RA0
#define spulse PORTAbits.RA5
#define solarled PORTCbits.RC4
#define solarsense PORTCbits.RC2

// Prototypes

void msdelay(unsigned int);

// Subroutines

unsigned long int adc(unsigned char source)
{
		
	unsigned long int _adres;
        unsigned char _adresh, _adresl;

	ADCON0bits.ADON=1;
	ADCON0bits.GO=1;

	switch(source)
	{
		case'o':
			ADCON0=0x80;
			while(ADCON0bits.GO);
			break;

		case's':
			ADCON0=0x98;
			while(ADCON0bits.GO);
			break;
			
	}

        _adresl = ADRESL;                 
        _adresl = _adresl >> 1;	         
        _adresh = ADRESH;	         
        _adresh = _adresh << 7;	 
        _adres = _adresl | _adresh;
	
	return _adres;
}


//#pragma psect code main=0x100
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)
	unsigned long int ssvalue, olvalue;

	TRISAbits. TRISA5=0;			//spulse
	TRISCbits. TRISC4=0;			//solarled
	TRISCbits. TRISC3=0;			//load
	TRISCbits. TRISA0=1;			//solar sense
	TRISAbits. TRISC2=1;			//ol sense
	ANSEL = 0x03;
	// Fosc = 4 Mhz, Tosc = 1/Fosc = 0.25us = 250ns
	// 32 * Tosc = 8us
        // 8 * Tosc = 2us
        // min Tad = 1.6us 
	//ADCS2-ADCS0 = 001
	ADCON1 = 00010000 or 0x10
	ADCON1= 0x10;


while(1)
{
	solarled=1;				//led ON to show uc working

	ssvalue = adc('s');

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

	if(ssvalue <= lon_value)
	{
	msdelay(5);
		if(ssvalue <= lon_value)
		{
			load=1;			//light on
		}
	}	
	else
		load=0;				//light off

	olvalue = adc('o');

	if(olvalue >= olvoltage)
	{
		msdelay(5);
		if(olvalue >= olvoltage)
		{
			load=0;//light off	//light off
		}	
	}



void msdelay(unsigned int time)
{
	unsigned char i,j;
	for(i=0;i<time;i++)
	for(j=0;j<165;j++)
}


}


}

[/syntax]
 

tried by modifying codes.... but its still not working....
i connected solarled(made 1 in the code) to led.... only this pin is working....
here i implementing two channels ch0 (olsense) and ch6 (solarsense)....

but when i used only one channel i.e channel6(solar sense), it perfectly worked....
i.e spulse (connected to led) will become high between the voltage range specified (2.75-3.25)....

i dont know what is the magic behind this....
 

I think you need to modify this -

hbytes=ADRESH;
lbytes=ADRESL;
adcouts=lbytes|(hbytes<<8);

to some thing like this-
value =ADRESL;
value +=(ADRESH<<8);
dec_value =value;

Good Luck
 

hbyte=ADRESH;
lbyte=ADRESL;
adcout = lbyte|(hbyte<<8);
return adcout;
same thing i modified like above...still..... not working...:(

any other way???... i tried in all ways...
wat about interrupt or comparator(by using special trigger event).... will it work????
if so how to give interrupt service routine for multiple channels.... and we have only adif flag bit to use for adc....
 

Try this...

Code:
[syntax = c]

//Overload, Dusk and Dawn Control

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

__CONFIG(FOSC_INTRCCLK&PWRTE_ON&BOREN_OFF&WDTE_OFF );

#define load PORTCbits.RC3
//#define olsense PORTAbits.RA0
#define solarsense PORTAbits.RA0
#define spulse PORTAbits.RA5
#define solarled PORTCbits.RC4
#define olsense PORTCbits.RC2

// Prototypes

void msdelay(unsigned int);

// Subroutines

unsigned long int adc(unsigned char source)
{
		
	unsigned long int _adres;
        unsigned char _adresh, _adresl;

	ADCON0bits.ADON=1;
	ADCON0bits.GO=1;

	switch(source)
	{
		case'o':
			ADCON0=0x80;
			while(ADCON0bits.GO);
			break;

		case's':
			ADCON0=0x98;
			while(ADCON0bits.GO);
			break;
			
	}

        _adresl = ADRESL;                 
        _adres = ADRESH;               // copy high byte to __adres	         
        _adres = _adres << 8;	       // shift low byte to 2nd byte  
        _adresl = _adresl;	               // copy low byte to _adresl
        _adres = _adres | _adresl;   // OR _adres with _adresl to get both the bytes into one var.
	
	return _adres;
}


//#pragma psect code main=0x100
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)
	unsigned long int ssvalue, olvalue;

	TRISAbits. TRISA5=0;			//spulse
	TRISCbits. TRISC4=0;			//solarled
	TRISCbits. TRISC3=0;			//load
	TRISCbits. TRISA0=1;			//solar sense
	TRISAbits. TRISC2=1;			//ol sense
	ANSEL = 0x03;
	// Fosc = 4 Mhz, Tosc = 1/Fosc = 0.25us = 250ns
	// 32 * Tosc = 8us
        // 8 * Tosc = 2us
        // min Tad = 1.6us 
	// ADCS2-ADCS0 = 001
	// ADCON1 = 00010000 or 0x10
	ADCON1= 0x10;


while(1)
{
	solarled=1;				//led ON to show uc working

	ssvalue = adc('s');

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

	if(ssvalue <= lon_value)
	{
	msdelay(5);
		if(ssvalue <= lon_value)
		{
			load=1;			//light on
		}
	}	
	else
		load=0;				//light off

	olvalue = adc('o');

	if(olvalue >= olvoltage)
	{
		msdelay(5);
		if(olvalue >= olvoltage)
		{
			load=0;//light off	//light off
		}	
	}



void msdelay(unsigned int time)
{
	unsigned char i,j;
	for(i=0;i<time;i++)
	for(j=0;j<165;j++)
}


}


}

[/syntax]


---------- Post added at 16:48 ---------- Previous post was at 16:24 ----------

Why don't you use mikroC. You can do the adc thing in just one line of code. The demo version of mikroC will compile code size of 2KB.

---------- Post added at 17:05 ---------- Previous post was at 16:48 ----------

Is it compiling?

---------- Post added at 17:16 ---------- Previous post was at 17:05 ----------

How do you do conversions from char to int in htc?
 
Last edited:

Try this finally...

Code:
[syntax = c]

//Overload, Dusk and Dawn Control

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

__CONFIG(FOSC_INTRCCLK&PWRTE_ON&BOREN_OFF&WDTE_OFF );

#define load PORTCbits.RC3
//#define olsense PORTAbits.RA0
#define solarsense PORTAbits.RA0
#define spulse PORTAbits.RA5
#define solarled PORTCbits.RC4
#define olsense PORTCbits.RC2

// Prototypes

void msdelay(unsigned int);

// Subroutines

unsigned long int adc(unsigned char source)
{
		
	unsigned long int _adres;
    unsigned char _adresl;
	//char c_adres[2];
	//unsigned long int i_adres;

	ADCON0bits.ADON=1;
	ADCON0bits.GO=1;

	switch(source)
	{
		case'o':
			ADCON0=0x80;
			while(ADCON0bits.GO);
			break;

		case's':
			ADCON0=0x98;
			while(ADCON0bits.GO);
			break;
			
	}

        _adresl = ADRESL;                 
        _adres = ADRESH;                	// copy high byte to __adres	         
        _adres = _adres << 8;	        	// shift low byte to 2nd byte  
        _adres = _adres | _adresl;   		// OR _adres with _adresl to get both the bytes into one var.
		
	    return _adres;
}


//#pragma psect code main=0x100
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)
	unsigned int time;

	
	TRISAbits. TRISA5=0;				//spulse
	TRISCbits. TRISC4=0;				//solarled
	TRISCbits. TRISC3=0;				//load
	//TRISCbits. TRISA0=1;				//solar sense
	//TRISAbits. TRISC2=1;				//ol sense
	TRISAbits. TRISA0=1;				//solar sense
	TRISCbits. TRISC2=1;				//ol sense
	ANSEL = 0x03;
	// Fosc = 4 Mhz, Tosc = 1/Fosc = 0.25us = 250ns
	// 32 * Tosc = 8us
    // 8 * Tosc = 2us
    // min Tad = 1.6us 
	//ADCS2-ADCS0 = 001
	//ADCON1 = 00010000 or 0x10
	ADCON1= 0x10;


	while(1)
	{
		solarled=1;							//led ON to show uc working

		ssvalue = adc('s');

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

		if(ssvalue <= lon_value)
		{
		msdelay(5);
			if(ssvalue <= lon_value)
			{
				load=1;						//light on
			}
		}	
		else
			load=0;							//light off

		olvalue = adc('o');

		if(olvalue >= olvoltage)
		{
			msdelay(5);
			if(olvalue >= olvoltage)
			{
				load=0;						//light off
			}	
		}



		void msdelay(unsigned int time)
		{
			unsigned char i,j;
	
			for(i=0;i<time;i++);
			for(j=0;j<165;j++);
		}


	}


}

[/syntax]


---------- Post added at 18:11 ---------- Previous post was at 17:50 ----------

I did it. Check this code.

Code:
[syntax = c]

//Overload, Dusk and Dawn Control

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

__CONFIG(FOSC_INTRCCLK&PWRTE_ON&BOREN_OFF&WDTE_OFF );

#define load PORTCbits.RC3
//#define olsense PORTAbits.RA0
#define solarsense PORTAbits.RA0
#define spulse PORTAbits.RA5
#define solarled PORTCbits.RC4
#define olsense PORTCbits.RC2

// Prototypes

void msdelay(unsigned int);

// Subroutines

unsigned long int adc(unsigned char source)
{
		
	unsigned long int _adres;
    unsigned char _adresl;
	//char c_adres[2];
	//unsigned long int i_adres;

	ADCON0bits.ADON=1;
	ADCON0bits.GO=1;

	switch(source)
	{
		case'o':
			ADCON0=0x80;
			while(ADCON0bits.GO);
			break;

		case's':
			ADCON0=0x98;
			while(ADCON0bits.GO);
			break;
			
	}

        _adresl = ADRESL;                 
        _adres = ADRESH;                	// copy high byte to __adres	         
        _adres = _adres << 8;	        	// shift low byte to 2nd byte  
        _adres = _adres | _adresl;   		// OR _adres with _adresl to get both the bytes into one var.
		
	    return _adres;
}

void msdelay(unsigned int time)
		{
			unsigned int i,j;
	
			for(i=0;i<time;i++);
			for(j=0;j<165;j++);
		}

//#pragma psect code main=0x100
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)
	unsigned int time;

	
	TRISAbits. TRISA5=0;				//spulse
	TRISCbits. TRISC4=0;				//solarled
	TRISCbits. TRISC3=0;				//load
	//TRISCbits. TRISA0=1;				//solar sense
	//TRISAbits. TRISC2=1;				//ol sense
	TRISAbits. TRISA0=1;				//solar sense
	TRISCbits. TRISC2=1;				//ol sense
	ANSEL = 0x03;
	// Fosc = 4 Mhz, Tosc = 1/Fosc = 0.25us = 250ns
	// 32 * Tosc = 8us
    // 8 * Tosc = 2us
    // min Tad = 1.6us 
	//ADCS2-ADCS0 = 001
	//ADCON1 = 00010000 or 0x10
	ADCON1= 0x10;


	while(1)
	{
		solarled=1;							//led ON to show uc working

		ssvalue = adc('s');

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

		if(ssvalue <= lon_value)
		{
		msdelay(5);
			if(ssvalue <= lon_value)
			{
				load=1;						//light on
			}
		}	
		else
			load=0;							//light off

		olvalue = adc('o');

		if(olvalue >= olvoltage)
		{
			msdelay(5);
			if(olvalue >= olvoltage)
			{
				load=0;						//light off
			}	
		}


		


	}


}

[/syntax]


---------- Post added at 18:12 ---------- Previous post was at 18:11 ----------

I did it. Check this code.

Code:
[syntax = c]

//Overload, Dusk and Dawn Control

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

__CONFIG(FOSC_INTRCCLK&PWRTE_ON&BOREN_OFF&WDTE_OFF );

#define load PORTCbits.RC3
//#define olsense PORTAbits.RA0
#define solarsense PORTAbits.RA0
#define spulse PORTAbits.RA5
#define solarled PORTCbits.RC4
#define olsense PORTCbits.RC2

// Prototypes

void msdelay(unsigned int);

// Subroutines

unsigned long int adc(unsigned char source)
{
		
	unsigned long int _adres;
    unsigned char _adresl;
	//char c_adres[2];
	//unsigned long int i_adres;

	ADCON0bits.ADON=1;
	ADCON0bits.GO=1;

	switch(source)
	{
		case'o':
			ADCON0=0x80;
			while(ADCON0bits.GO);
			break;

		case's':
			ADCON0=0x98;
			while(ADCON0bits.GO);
			break;
			
	}

        _adresl = ADRESL;                 
        _adres = ADRESH;                	// copy high byte to __adres	         
        _adres = _adres << 8;	        	// shift low byte to 2nd byte  
        _adres = _adres | _adresl;   		// OR _adres with _adresl to get both the bytes into one var.
		
	    return _adres;
}

void msdelay(unsigned int time)
		{
			unsigned int i,j;
	
			for(i=0;i<time;i++);
			for(j=0;j<165;j++);
		}

//#pragma psect code main=0x100
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)
	unsigned int time;

	
	TRISAbits. TRISA5=0;				//spulse
	TRISCbits. TRISC4=0;				//solarled
	TRISCbits. TRISC3=0;				//load
	//TRISCbits. TRISA0=1;				//solar sense
	//TRISAbits. TRISC2=1;				//ol sense
	TRISAbits. TRISA0=1;				//solar sense
	TRISCbits. TRISC2=1;				//ol sense
	ANSEL = 0x03;
	// Fosc = 4 Mhz, Tosc = 1/Fosc = 0.25us = 250ns
	// 32 * Tosc = 8us
    // 8 * Tosc = 2us
    // min Tad = 1.6us 
	//ADCS2-ADCS0 = 001
	//ADCON1 = 00010000 or 0x10
	ADCON1= 0x10;


	while(1)
	{
		solarled=1;							//led ON to show uc working

		ssvalue = adc('s');

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

		if(ssvalue <= lon_value)
		{
		msdelay(5);
			if(ssvalue <= lon_value)
			{
				load=1;						//light on
			}
		}	
		else
			load=0;							//light off

		olvalue = adc('o');

		if(olvalue >= olvoltage)
		{
			msdelay(5);
			if(olvalue >= olvoltage)
			{
				load=0;						//light off
			}	
		}


		


	}


}

[/syntax]


---------- Post added at 18:13 ---------- Previous post was at 18:12 ----------

I did it. Check this code. It surely will work.

Code:
[syntax]

//Overload, Dusk and Dawn Control

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

__CONFIG(FOSC_INTRCCLK&PWRTE_ON&BOREN_OFF&WDTE_OFF );

#define load PORTCbits.RC3
//#define olsense PORTAbits.RA0
#define solarsense PORTAbits.RA0
#define spulse PORTAbits.RA5
#define solarled PORTCbits.RC4
#define olsense PORTCbits.RC2

// Prototypes

void msdelay(unsigned int);

// Subroutines

unsigned long int adc(unsigned char source)
{
		
	unsigned long int _adres;
    unsigned char _adresl;
	//char c_adres[2];
	//unsigned long int i_adres;

	ADCON0bits.ADON=1;
	ADCON0bits.GO=1;

	switch(source)
	{
		case'o':
			ADCON0=0x80;
			while(ADCON0bits.GO);
			break;

		case's':
			ADCON0=0x98;
			while(ADCON0bits.GO);
			break;
			
	}

        _adresl = ADRESL;                 
        _adres = ADRESH;                	// copy high byte to __adres	         
        _adres = _adres << 8;	        	// shift low byte to 2nd byte  
        _adres = _adres | _adresl;   		// OR _adres with _adresl to get both the bytes into one var.
		
	    return _adres;
}

void msdelay(unsigned int time)
		{
			unsigned int i,j;
	
			for(i=0;i<time;i++);
			for(j=0;j<165;j++);
		}

//#pragma psect code main=0x100
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)
	unsigned int time;

	
	TRISAbits. TRISA5=0;				//spulse
	TRISCbits. TRISC4=0;				//solarled
	TRISCbits. TRISC3=0;				//load
	//TRISCbits. TRISA0=1;				//solar sense
	//TRISAbits. TRISC2=1;				//ol sense
	TRISAbits. TRISA0=1;				//solar sense
	TRISCbits. TRISC2=1;				//ol sense
	ANSEL = 0x03;
	// Fosc = 4 Mhz, Tosc = 1/Fosc = 0.25us = 250ns
	// 32 * Tosc = 8us
    // 8 * Tosc = 2us
    // min Tad = 1.6us 
	//ADCS2-ADCS0 = 001
	//ADCON1 = 00010000 or 0x10
	ADCON1= 0x10;


	while(1)
	{
		solarled=1;							//led ON to show uc working

		ssvalue = adc('s');

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

		if(ssvalue <= lon_value)
		{
		msdelay(5);
			if(ssvalue <= lon_value)
			{
				load=1;						//light on
			}
		}	
		else
			load=0;							//light off

		olvalue = adc('o');

		if(olvalue >= olvoltage)
		{
			msdelay(5);
			if(olvalue >= olvoltage)
			{
				load=0;						//light off
			}	
		}


		


	}


}

[/syntax]
 

no need to convert 'char' into 'int' it does automatically.....

its successfully compiling and even i debug in mplab sim using break points and add register injection (to ADRESL) using stimulus
and it perfectly executes according to code.....

dont know wat could be the problem(may be compiler error)

i used PIC Simulator IDE to cross check the code....
but it throwing some error like this..."input program file in intel hex format contains errors"

cluelesss:!:

successfully compiled.... but unsuccessful on board.....:?:
 

Give me the circuit diagram. I'll try to test it using isis.

I think the problem comes if you are using windows 7. You have to run pic simulator on windows xp.

Which programmer are you using? Have you selected the right programmer in mplab?
 
Last edited:

Check with this hex file.
 

Attachments

  • pic16f676.rar
    768 bytes · Views: 91

actually i am using presto asix up programmer, and running under windows xp....
 

OK. I Check this file. I am simulating in isis, and it is working. I changed RA1 input to RC1. The problem was because oc CMCON register. CMCON has to be set properly if RA0 and RA1 is used as analog. It was confusing, so to check the working I changed the input from RA0 to RC1. There is a problem with delay. ISIS gives warnings "ADC conversion started before 'wait' time has expired following previous conversion or channel change". Anyway I am sending you the hex file and isis dsn file. download proteus 7.7 sp2 and install. open the dsn file in isis. right click mcu and choose "edit properties" and in the dialog box "program file" set the program file to pic16f676.hex and simulate. Everything is working fine except the warning messages.

Code:
[syntax = c]


//Overload, Dusk and Dawn Control

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

__CONFIG(FOSC_INTRCCLK&PWRTE_ON&BOREN_OFF&WDTE_OFF);

#define solarsense PORTAbits.RC1
#define spulse PORTAbits.RA5
#define olsense PORTCbits.RC2
#define load PORTCbits.RC3
#define solarled PORTCbits.RC4


// Prototypes

void msdelay(unsigned int);
unsigned long int adc(unsigned char source);

// Subroutines

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=0x94;
			break;

		case 's':
			ADCON0=0x98;
			break;
			
	}
		
        ADCON0bits.ADON = 1;				
		ADCON0bits.GO = 1;
		while(ADCON0bits.GO);
		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
}

void msdelay(unsigned int time)
		{
			unsigned int i,j;
	
			for(i = 0;i < time;i++);
			for(j = 0;j < 165;j++);
		}

//#pragma psect code main=0x100
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)
	
	TRISCbits.TRISC1 = 1;					//solar sense	
	TRISAbits.TRISA5 = 0;					//spulse
	TRISCbits.TRISC2 = 1;					//ol sense
	TRISCbits.TRISC3 = 0;					//load
	TRISCbits.TRISC4 = 0;					//solarled
	ANSEL = 0x41;
	// Fosc = 4 Mhz, Tosc = 1/Fosc = 0.25us = 250ns
	// Tad = 32 * Tosc = 8us
    // Tad = 8 * Tosc = 2us
    // min Tad = 1.6us 
	// ADCS2-ADCS0 = 001
	// ADCON1 = 00010000 or 0x10
	// ADCON1= 0x10;
	ADCON1= 0x20;

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

	while(1)
	{
		

		ssvalue = adc('s');

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

		if (ssvalue <= lon_value)
		{
			load = 1;						//light on
		}	
		else
			load = 0;						//light off

		olvalue = adc('o');

		if (olvalue >= olvoltage)
		{
			load = 0;						//light off
		}

	}

}

[/syntax]
 

Attachments

  • PIC16F676.rar
    13.9 KB · Views: 85
  • pic16f676.JPG
    pic16f676.JPG
    288.6 KB · Views: 115
Last edited:

Where do you set the device clock frequency in hi tech c compiler or mplab?
 

Scan0001.JPG

just the check the above image.....

i am just trying to include two channels and wanted to know how it works(actually i have to use 4 channels)

input channel 1) solar sense (pin no 8 RC2/AN6) wen this pin gets the voltage between 2.75 and 3.25 pin no 2 spulse should be high
and wen it takes below 2V pin no.7 load(RC3) should be high..... **** using ldr as input
input channel 2)ol sense (pin no 13 RA0/AN0) wen this pen gets the voltage above 4V, pin no 7(load) should bo low.....**** using variable dc supply



i think now u got brief idea....
 

View attachment 74783

just the check the above image.....

i am just trying to include two channels and wanted to know how it works(actually i have to use 4 channels)

input channel 1) solar sense (pin no 8 RC2/AN6) wen this pin gets the voltage between 2.75 and 3.25 pin no 2 spulse should be high
and wen it takes below 2V pin no.7 load(RC3) should be high..... **** using ldr as input
input channel 2)ol sense (pin no 13 RA0/AN0) wen this pen gets the voltage above 4V, pin no 7(load) should bo low.....**** using variable dc supply



i think now u got brief idea....

---------- Post added at 10:26 ---------- Previous post was at 10:19 ----------

i find proteus 7 is not a freeware version, can u please give me a link where i can download ***** version....
 

I don't have that settings option. Is there a way to set clock freq to 4 MHz in the code? OK. If you are going to use RA) and RA! then you have to set CMCON properly, otherwise the analog channels will not work properly.

Hope this link works https://www.2shared.com/file/AzI6L8bj/Nouveau_dossier.html

Try to use leds with RA) and RA1 and use RC0-RC3 for analog sensing.
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top