using three ADC Channels in Atmega32

Thread Starter

skj

Joined May 20, 2010
22
I want to use three ADC channels on Atmega32 (ADC0,ADC1,ADC2) these three channels will receive variable inputs and depending upon the conditions will output a 1 on PB0 lighting a LED(in actual conditions a relay will be there.)
assuming 10 bit accuracy ( 5V =1024 adc counts) follwing are the conditions I am unable to understand how to write code.

1) A>4V(811) & B>4.6V(947) & C=4V(819) then PB0=1
2) A>4V(811) & B<4.6V(947) & C=4V(819) then PB0=1
3) A<4V(811) & B>4.6V(947) & C=4V(819) then PB0=0
4) A>4V(811) & B>4.6V(947) & C=0(0) then PB0=0
5) A>4V(811) & B>4.9V(1005) & C=4V(819) then PB0=0

If none of the above conditions are true then PB0=1

So far I have written the following code, please guide and correct me

Rich (BB code):
#include <avr/interrupt.h>
#include <util/delay.h>
int main(void)
{
	DDRB |= (1 << 0);  //Data direction Register portB PB0 output LED To this pin
			
	ADCSRA |= (1 << ADPS2); // sETTING cLOCK PRESCALER TO 16
	
	ADMUX |= (1 << REFS0); // AREF VOLTAGE IS SET TO VCC
	
							
	ADCSRA |= (1<< ADEN);	// ENABLE ADC
					
	ADCSRA |= (1 << ADIE); //ENABLE ADC'S OWN ADC ISR
	
	sei();					// GLOBAL INTERRUPTS ENABLE
	
	ADCSRA |= (1<<ADSC);	// START A2D CONVERSION
	
	while(1)  				// Loop Forever 
   { 
	}
		
}

ISR(ADC_vect)
{
	//WHAT CODE SHOULD BE WRITTEN HERE CAN i USE SWITCH STATEMENT HERE, IF YES THEN HOW TO WRITE CODE ,SATISFYING MY CONDITIONS

}
 
Last edited by a moderator:

ErnieM

Joined Apr 24, 2011
8,377
Your conditional would look like this:

Rich (BB code):
if ( ( A < 811 & B > 947 & C == 819  ) |
     ( A > 811 & B > 947 & C == 0    ) | 
     ( A > 811 & B > 1005 & C == 819 )   )
 

    { set PB0=0 )

else

    { set PB0=1 )
Since PB0 is either on or off you just need to test for one condition; if it's not so then it must be the other way. Do note I did not check your conditions for consistency, meaning if one sets it hi and another sets it low for the same inputs I could not tell you.

However, an A2D count will exactly equal a specific number so rarely that PB0 will be 1 most always. Do think of another way to do whatever you are trying to do without the equals. (Everything has a tolerance.)
 

donpetru

Joined Nov 14, 2008
185
You want to make the software only with Atmel Studio? If so, I can help but it is very many mistakes in your code above. Software should be rewritten completely. The point is this .... cost.
If you want to show you a code for free, I'd be able to show it in mikroC for AVR, if you want to use this software?!
 

MrChips

Joined Oct 2, 2009
30,824
I will expand on what Ernie has pointed out. Your requirements are impractical and inconsistent.

You cannot test for equality such as C = 819 and C = 0.
Use limits such as C >= 819.
C = 0 should be replaced with a limit such as C <= 23. Note that this test might already be covered by the previous limit C >= 819.

Reduce all of your limits to a boolean value such as

A = (A > 811)
B = (B > 947)
B1 = (B > 1005)
C = (C >819)

You now have a 4-variable logic with one output PB0.

PB0 = F(A, B, B1, C)

Draw the truth table for this function by filling in the values for PB0 for all 16 possibilities of A, B, B1 and C.

You may discover that there are some inconsistencies.

After you have corrected the inconsistencies you will be able to reduce the function for PB0 to one boolean equation.
 

Thread Starter

skj

Joined May 20, 2010
22
Thanks Mr ErnieM for your valuable suggestion I will reconsider my conditions and correct them.

Your conditional would look like this:

Rich (BB code):
if ( ( A < 811 & B > 947 & C == 819  ) |
     ( A > 811 & B > 947 & C == 0    ) | 
     ( A > 811 & B > 1005 & C == 819 )   )
 

    { set PB0=0 )

else

    { set PB0=1 )
Since PB0 is either on or off you just need to test for one condition; if it's not so then it must be the other way. Do note I did not check your conditions for consistency, meaning if one sets it hi and another sets it low for the same inputs I could not tell you.

However, an A2D count will exactly equal a specific number so rarely that PB0 will be 1 most always. Do think of another way to do whatever you are trying to do without the equals. (Everything has a tolerance.)
 

Thread Starter

skj

Joined May 20, 2010
22
Dear MrChips
I want to reopen this thread, I am unable to think how to write the programme as I am new to programming ,as suggested by you I have drawn the truth table for all the 16 conditions and found 12 viable conditions for the variables :-

A(>819),B(>947),B1(1005) and (C<23, C is actually Monitoring Utility Supply A 5 volt regulator through step down transformer(230V Ac =5V),hence this will either Be 0 Or 5V depending upon the availability of Supply )

Simplifying below table using Boolean algebra resulted in the following Boolian equation.

B1(bar)[B(bar) + AB] = Y (bar) means inverted B1 or Inverted B

This equation satisfies all the twelve conditions

A B B1 C Y(Required)

0 0 0 0 1
0 0 0 1 1
0 1 0 0 0
0 1 0 1 0
0 1 1 0 0
0 1 1 1 0
1 0 0 0 1
1 0 0 1 1
1 1 0 0 1
1 1 0 1 1
1 1 1 0 0
1 1 1 1 1
My query is
1) How to put this equation in C programming ?
2) As per logic equation C input is not Used so do we need not to wire up C input to Atmega32 ADC Input ?
 

Thread Starter

skj

Joined May 20, 2010
22
Dear MrChips
I want to reopen this thread, I am unable to think how to write the programme as I am new to programming ,as suggested by you I have drawn the truth table for all the 16 conditions and found 12 viable conditions for the variables :-

A(>819),B(>947),B1(1005) and (C<23, C is actually Monitoring Utility Supply A 5 volt regulator through step down transformer(230V Ac =5V),hence this will either Be 0 Or 5V depending upon the availability of Supply )

Simplifying below table using Boolean algebra resulted in the following Boolian equation.

B1(bar)[B(bar) + AB] = Y Note:(bar) means inverted B1 or Inverted B

This equation satisfies all the twelve conditions

A B B1 C Y(Required)

0 0 0 0 1
0 0 0 1 1
0 1 0 0 0
0 1 0 1 0
0 1 1 0 0
0 1 1 1 0
1 0 0 0 1
1 0 0 1 1
1 1 0 0 1
1 1 0 1 1
1 1 1 0 0
1 1 1 1 1
My query is
1) How to put this equation in C programming ?
2) As per logic equation C input is not Used so do we need not to wire up C input to Atmega32 ADC Input ?
 

MrChips

Joined Oct 2, 2009
30,824
What about the following states?

0010
0011
1010
1011

After you have included the above states, draw the Karnaugh map.
 

Thread Starter

skj

Joined May 20, 2010
22
Thanks for quick response.

1)These four inputs A,B,B1,and C represent different voltage levels of a Solar Panel,A battery and Home Supply 230V AC. different combinations of these variable initiate A relay doing some action.

2)Leftover four conditions do not fit in the requirement hence not considered.
 

MrChips

Joined Oct 2, 2009
30,824
When input conditions do not exist, mark these as "Don't care" conditions using an "X" in your truth table and Karnaugh map.

This could help to simplify the boolean equation.
 

Thread Starter

skj

Joined May 20, 2010
22
Thanks for your help and guidance

I have carefully reviewed my conditions again and found that four leftover conditions instead of don't Care, should be assigned Y = 1 (which is default value in case no conditions are fulfilled).Saying this I have drawn karnaugh Map for all the sixteen conditions.I could simplify equation only to the extent given below.

Y = Bbar + AχBχ B1bar

Now please tell me Should I hard wire C input to the Miceocontroller ADC ?
How do i transfer this equation into the Code in C

I am attaching part circuit in PDF
 

Attachments

donpetru

Joined Nov 14, 2008
185
@skj, you like to get accomplice without any sense. I notice that you overlooked my recommendation, but I don't mind, I wish you good luck!
Remember: the equation that can not be implemented in C, so, anyway. There are many more things that need to be considered, but I let you discover them yourself.
 
Top