using three ADC Channels in Atmega32

Discussion in 'Embedded Systems and Microcontrollers' started by skj, Jan 25, 2014.

  1. skj

    Thread Starter Member

    May 20, 2010
    22
    0
    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

    Code ( (Unknown Language)):
    1.  
    2. #include <avr/interrupt.h>
    3. #include <util/delay.h>
    4. int main(void)
    5. {
    6.     DDRB |= (1 << 0);  //Data direction Register portB PB0 output LED To this pin
    7.            
    8.     ADCSRA |= (1 << ADPS2); // sETTING cLOCK PRESCALER TO 16
    9.    
    10.     ADMUX |= (1 << REFS0); // AREF VOLTAGE IS SET TO VCC
    11.    
    12.                            
    13.     ADCSRA |= (1<< ADEN);   // ENABLE ADC
    14.                    
    15.     ADCSRA |= (1 << ADIE); //ENABLE ADC'S OWN ADC ISR
    16.    
    17.     sei();                  // GLOBAL INTERRUPTS ENABLE
    18.    
    19.     ADCSRA |= (1<<ADSC);    // START A2D CONVERSION
    20.    
    21.     while(1)                // Loop Forever
    22.    {
    23.     }
    24.        
    25. }
    26.  
    27. ISR(ADC_vect)
    28. {
    29.     //WHAT CODE SHOULD BE WRITTEN HERE CAN i USE SWITCH STATEMENT HERE, IF YES THEN HOW TO WRITE CODE ,SATISFYING MY CONDITIONS
    30.  
    31. }
    32.  
     
    Last edited by a moderator: Jan 25, 2014
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,606
    Your conditional would look like this:

    Code ( (Unknown Language)):
    1. if ( ( A < 811 & B > 947 & C == 819  ) |
    2.      ( A > 811 & B > 947 & C == 0    ) |
    3.      ( A > 811 & B > 1005 & C == 819 )   )
    4.  
    5.  
    6.     { set PB0=0 )
    7.  
    8. else
    9.  
    10.     { set PB0=1 )
    11.  
    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.)
     
  3. donpetru

    Active Member

    Nov 14, 2008
    186
    25
    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?!
     
  4. MrChips

    Moderator

    Oct 2, 2009
    12,446
    3,362
    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.
     
    skj and ErnieM like this.
  5. skj

    Thread Starter Member

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

     
  6. skj

    Thread Starter Member

    May 20, 2010
    22
    0
    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 ?
     
  7. skj

    Thread Starter Member

    May 20, 2010
    22
    0
    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 ?
     
  8. MrChips

    Moderator

    Oct 2, 2009
    12,446
    3,362
    What about the following states?

    0010
    0011
    1010
    1011

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

    Thread Starter Member

    May 20, 2010
    22
    0
    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.
     
  10. MrChips

    Moderator

    Oct 2, 2009
    12,446
    3,362
    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.
     
  11. skj

    Thread Starter Member

    May 20, 2010
    22
    0
    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
     
  12. donpetru

    Active Member

    Nov 14, 2008
    186
    25
    @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.
     
Loading...