Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   This topic is locked: you cannot edit posts or make replies.
View previous topic Printable version Log in to check your private messages View next topic
Author Message
justinjohney
PostPosted: Jun 11, 2012 - 10:03 AM
Hangaround


Joined: Jun 04, 2012
Posts: 295
Location: Mumbai,India

how can i compare 10bit adc out put with a threashold?
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jun 11, 2012 - 10:10 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62371
Location: (using avr-gcc in) Finchingfield, Essex, England

Code:
if (ADC >= threshold) {

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
justinjohney
PostPosted: Jun 11, 2012 - 12:22 PM
Hangaround


Joined: Jun 04, 2012
Posts: 295
Location: Mumbai,India

thanx Mr.clawson.

Code:
if (ADC>=119){
}

is it work since adc output is 10bit resolution?


if i use 5 adc at a time then how can i compare these out puts with 5 different threashold?
is anyone have any idea?
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jun 11, 2012 - 12:29 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62371
Location: (using avr-gcc in) Finchingfield, Essex, England

Well this depends on your C compiler. Most provide three (sometimes 4) registers to access the ADC result. ADCH and ADCL are 8 bit access to the two halves of the result (8 bits from one, 2 bits from the other) but most C compilers provide a 16 bit access called just ADC (or the 4th optin - sometimes ADCW where W=word=16 bit wide). This reads 16 bits but, of course only 10 bits are actually significant within that.

What C compiler are you using by the way?

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
justinjohney
PostPosted: Jun 11, 2012 - 12:50 PM
Hangaround


Joined: Jun 04, 2012
Posts: 295
Location: Mumbai,India

i'm using avr gcc.
i need to create a protection circuit for overvoltage and under voltage for 3 phases (the last project given to me from my company). ie, i have 5 input signal which seperately compared with different threashold. the first three signals(ie, R,G,B in the 3 phase)compared with three different threashold. the other 2 signals(ground and neutral) are compared sothat they must keep a constant voltage difference between them.so if any threashold condition violate,then an output will produce which trigger the relay. how can i use ADC for this purpose? i want to do this in a tiny avr. tiny167.
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Jun 11, 2012 - 02:25 PM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18599
Location: Lund, Sweden

Quote:

if i use 5 adc at a time then how can i compare these out puts with 5 different threashold?

Iterate over the ADC channels in a round-robin fashion. Have threshold values in an array. Compare the value from a specific channel with the corresponding thrershold value.

Sketchy, not tested, some pseudo-code:

Code:
uint8_t ch = 0;
int threshold[5] = {119, 13, 42, 7, 99};

while (1)
{
   // Select channel ch here
   ...

   // Run a conversion and get the result into a variable adc here
   ...

   // Now test it
   if (adc >= threshold[ch])
   {
      // Handle the condition in some way
   }

   // Next channel
   ch = ch++ % 5;
}


For details on selecting a specific ADC channel, please start by reading the data sheet of your AVR.

For details on how to get the ADC value after selecting channel, this depends in parts on how you select to run the ADC - free running or "one-shot". In any case you will need to wait for the conversion to complete, and this can be done either by inspecting a flag in one of the ADC registers in a busy-wait fashion, or by utilizing interrupts. Again, you should start by browsing the data sheet section on the ADC.

If anything is unclear, then return here for questions.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
bobgardner
PostPosted: Jun 11, 2012 - 03:25 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21274
Location: Orlando Florida

Are the signals AC? What volts and freq? Probably need a diode and cap so the a/d is reading the peak value.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
justinjohney
PostPosted: Jun 12, 2012 - 06:53 AM
Hangaround


Joined: Jun 04, 2012
Posts: 295
Location: Mumbai,India

Quote:
Are the signals AC? What volts and freq? Probably need a diode and cap so the a/d is reading the peak value.

ya ac signal with 50Hz.and a amplitude of 5V.
 
 View user's profile Send private message  
Reply with quote Back to top
Visovian
PostPosted: Jun 12, 2012 - 07:24 AM
Posting Freak


Joined: Aug 07, 2007
Posts: 1478
Location: Czech

Quote:

Code:
if (ADC>=119){
}

is it work since adc output is 10bit resolution?
10bit resolution means the value can vary from 0 to 1023.
Surely each number from interval 0-1023 can be compared with 119.

You can connect Voltage1 to ADC1 input,
Voltage2 to ADC2
Voltage3 to ADC3 ...etc.

Then in main loop:
Code:
//pseudocode
while(1)
{
   Read ADC1 value
   Compare with threashold1.

   Read ADC2 value
   Compare with threashold2...etc.

   Make a decision.
}
 
 View user's profile Send private message  
Reply with quote Back to top
justinjohney
PostPosted: Jun 12, 2012 - 12:13 PM
Hangaround


Joined: Jun 04, 2012
Posts: 295
Location: Mumbai,India

Thanx for your reply Mr.Czech.
can you provide the code for adc reading and comparison with threashold?
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jun 12, 2012 - 12:20 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62371
Location: (using avr-gcc in) Finchingfield, Essex, England

The tutorial forum has articles such as:

Using AVR ADC

That's just one. While in the Tutorial forum simply look through the 5 pages of thread titles and have a look at anything with "ADC" in the name.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
MBedder
PostPosted: Jun 12, 2012 - 01:30 PM
Raving lunatic


Joined: Nov 02, 2009
Posts: 3239
Location: Zelenograd, Russia

justinjohney wrote:
i need to create a protection circuit for overvoltage and under voltage for 3 phases (the last project given to me from my company)
Hopefully it is really the last project given to you from your company Laughing

_________________
Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.
 
 View user's profile Send private message  
Reply with quote Back to top
justinjohney
PostPosted: Jun 16, 2012 - 01:20 PM
Hangaround


Joined: Jun 04, 2012
Posts: 295
Location: Mumbai,India

is anyone can give the full code for atmega168 adc1-5 channel and compare the 5 adc shannels o/p with 5 different threashold and make an led glow.
avr studio4, gcc compiler preffered.
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Jun 16, 2012 - 01:29 PM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18599
Location: Lund, Sweden

Quote:

is anyone can give the full code

You don't get it, do you?

Short answer: No.

Long answer: We are here to help. We like to do that. But it also means that the person we are helping should make a genuine effort on his own.

If your answer to that is "but I am not skilled enough to do it", the you simply have started with something that is too complex. I would suggest you get it working for one voltage, threshold and LED - then expand it.

Another approach is to solve each sub-problem separately. E.g. read the ADC, change channel, test value and perhaps set LED. When all those sub-problems are solved then you can simply insert them into the pseudo-code I gave above, and you should be close to done.

If this is not a school assignment and you simply need finished code w/o having written a row of source yourself then you should go over to the Trading post forum here and offer a sum of money for someone to write it for you.

Sorry for being so blunt, but that's the gist of it..
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
JohanEkdahl
PostPosted: Jun 19, 2012 - 09:25 AM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18599
Location: Lund, Sweden

The OP continues his discussion on "ADC and thresholds" in another thread: http://www.avrfreaks.net/index.php?name ... 424#966424 .

MODERATOR: Suggest lock this thread. [done]
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   This topic is locked: you cannot edit posts or make replies.
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits