[TUT] [C] Newbie's Guide to the AVR ADC

Go To Last Post
324 posts / 0 new

Pages

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Actually, this code

ADCSRB &= ~((1<<ADTS2) | (1<<ADTS1) |(1<<ADTS0)); 

Did not do what I wanted... instead I had to use:

ADCSRB &= ~(1<<ADTS2) | ~(1<<ADTS1) | ~(1<<ADTS0);

Now it works like it's supposed to, and not by luck! Thanks again for bringing it my attention.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
ADCSRB &= ~(1<<ADTS2) | ~(1<<ADTS1) | ~(1<<ADTS0); 

This can't possibly do what you want. This translates into:

ADCSRB &= 0b11111011 | 0b11111101 | 0b11111110;

which gives you:

ADCSRB &= 0b11111111;

which means that ADCSRB remains unchanged.

Quote:

ADCSRB &= (0<<ADTS2) | (0<<ADTS1) |(0<<ADTS0);

That line does nothing.


Actually, it does do something: it sets the entire register to 0.
Quote:

ADCSRB &= ~((1<<ADTS2) | (1<<ADTS1) |(1<<ADTS0));

Did not do what I wanted...


So what exactly do you want the code to do? If you want to clear the ADTSx bits and only those bits, then that is exactly the code that you need.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Steve,

I want to clear just those three bits. However, the code which did something actually caused my code to quit working. My code which did nothing worked. The reason seems to be that I was attempting to set the free running mode before triggering of the first conversion. Moving the code that you and clawson suggested as being correct to after the trigggering of the first conversion made it start working.

Thanks for all, very educational for me!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

TeonHarasymiv wrote:
Thanks for the tutorial! Much appreciated!

hi! for firs excuse me for my bad english and thank you for very good tutorial!

i have a problem, i have in my system an analog in very bad signal because i work with a plasma cutter system and i have more disturb problem, so..

what do i do for filter this analog in by software too?? i set the prescaler at 1024 but this is insufficient...

thank you for your time!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
what do i do for filter this analog in by software too?? i set the prescaler at 1024 but this is insufficient...

This sounds more like a hardware problem. I don't think that there is much you can do in software except take multiple readings and average them.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Koshchi wrote:
Quote:
what do i do for filter this analog in by software too?? i set the prescaler at 1024 but this is insufficient...

This sounds more like a hardware problem. I don't think that there is much you can do in software except take multiple readings and average them.

hi, thanks for answer.. i have an analog signal in from a plasma cutter system, this is the problem for the noise.. i resolve this problem with this code

code:

ISR(ADC_vect)
{

adc_data = ADCW;

ANALOGIN1= adc_data;
ANALOGIN = (ANALOGIN+ANALOGIN1);
ANALOGIN_IND ++;

if (ANALOGIN_IND==50)
{
ANALOGIN_F=(ANALOGIN/ANALOGIN_IND);
ANALOGIN_IND=0;
ANALOGIN=0;
}

//ANALOGIN = adc_data;
ADCSRA = ADCSRA|0x40; //start the next conversion

}

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, just a question about the prescaler and ADc frequency:
In the datasheet of my atmega644, there is written "If a lower resolution than 10 bits is needed, the
input clock frequency to the ADC can be higher than 200 kHz to get a higher sample rate".
But there is nowwhere written how high it can go. Where is the maximum and what happens if I go higher than the maximum clock rate for ADC?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But I faced great difficulty in showing the actual digital value in LCD. That means if I measure 4.2volt by multimeter, then I who can help me to show that value in LCD?
I only could show the binary value ie 806 for 4.2..but failed to convert it into fractional format...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Convert the 0..1023 reading of the ADC into the value in centi-volts or millivolts then output it to the LCD placing a decimal point 2 or 3 digits from the right.

Say your Vref is 5.00V then 1023 represents 5.00V so use (ADC * 500) / 1023 to get the value in centi-volts. Say the ADC read 619 then the result would be (619 * 500) / 1023 = 303. So that is a reading of 3.03Volts

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

im trying to do this example on a attiny13.

ive understood everything so far, and ive changed what i need to for it to work on what im using...

but i cant find how to set which pin i want to do adc from, the original writer left that part out, since using ADC0 is default.

i need to use ADC0, unless i can use that pin as an input without disabling the reset fuse.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

First you pick the channel you want to use as you are designing your schematic. Within the AVR code you then set the bottom 3 bits of the ADMUX register to the channel you have picked and readings will be made from the selected pin.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

right now this is what my admux looks like.

ADMUX |= (1 << REFS0);

im not sure what bits i have to set :?

im trying to change ADC from ADC0, to ADC1.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i think i figured it out.

this is what i got.

ADMUX |= (0 << MUX1) | (1 << MUX0); // select ADC channel 1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That probably wants to be:

ADMUX &= 0xF8; // clear existing bits
ADMUX |= (0 << MUX1) | (1 << MUX0); // select ADC channel 1

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

but wouldnt &= just make anything that was high, go low, and anything that was low, go high?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Get a good book on Boolean Logic.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

lol.

i said that wrong.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Also be aware that the (0<<MUX1) in your code does nothing.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Koshchi wrote:
Also be aware that the (0<<MUX1) in your code does nothing.

its just there for me :P

i realize that would only do something if it was already high, and i was changing the channel i was on.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

so...who wants to tell me whats wrong with my code. :oops:

#include 
#include 

int main (void)
{
	DDRB = 0b00010011;

   ADCSRA |= (0 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // set prescaler to 8. 1Mhz/8=125Khz
   ADMUX |= (1 << REFS0); // external analog voltage reference


   ADMUX |= (1 << ADLAR); // Left adjust ADC result to allow easy 8 bit reading


   ADMUX &= 0xF8; // clear existing bits
   ADMUX |= (0 << MUX1) | (1 << MUX0); // select ADC channel 1

   ADCSRA |= (1 << ADEN);  // Enable ADC

   ADCSRA |= (1 << ADIE);  // Enable ADC Interrupt
   sei();   // Enable Global Interrupts

   ADCSRA |= (1 << ADSC);  // Start A2D Conversions

   for(;;)  // Loop Forever
   {
   }
}

ISR(ADC_vect)
{
   if(ADCH < 85)
   {
      PORTB = 0b00000001;
   }
      
   else if(ADCH >= 85)
   {
      PORTB = 0b00000010;
   }

   else if(ADCH >= 171)
   {
   	  PORTB = 0b00010000;
   }
} 

i got a lab tomorrow morning, so im going to bed now and dont have time to check my code.

ill be doing so tomorrow, unless someone would be so kind as to tell me what i did wrong... =\

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

whats wrong with my code.

But you haven't told us what you thought it should do and what it actually does. My reading is that you set up and make one reading. It's not clear if that's what you intended or not. If you want it to make repeated readings then add a last line to the ISR:

   ADCSRA |= (1 << ADSC);  // Start A2D Conversions

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ill add that and try it.

i have a green led(PB0), a yellow led(PB1), and a red(PB4) led, hooked up.

anything under 1.6V lights up green, under 3.2V yellow, under 5V red.

i tried putting "X <=ADCH <= X" but that didnt work.

right now only the yellow led lights up.

it turns on as soon as the avr is powered, and never changes.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i believe my problem is here.

if(ADCH < 85)
   {
      PORTB = 0b00000001;
   }
     
   else if(ADCH >= 85)
   {
      PORTB = 0b00000010;
   }

   else if(ADCH >= 171)
   {
        PORTB = 0b00010000;
   } 

i changed it to make repeated adc'.

the yellow is always on, i got it to turn green with a very low voltage, but i cant get it to turn red.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

how do i properly do?

( 25 < ADCH < 100)

the message im getting is "comparisons like x > y > z do not have there mathematical meaning."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

how do i properly do?

( 25 < ADCH < 100)

((25 < ADCH) && (ADCH < 100))

But now you are actually reading the ADCH register twice, and that could be different conversions, so read the value into a temporary variable and compare that:

unsigned char adcvalue = ADCH;
if ((25 < adcvalue) && (adcvalue < 100)) ...

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks :)
ill try that out in a minute.

im still having a major issue somewhere or im doing something really stupid...

i went backwards and simplified my code to see if it would work then, still isn't working how i thought it would.

ADCH starts at 0, and ends at 255, to represent a voltage between 0-5V.

so.. if i divide up 255... 255/5 -- 51 should be 1V, right?

so i set it to light up my green led only anything larger than 51, but when i flashed it and hooked it all up, the led stays on.

#include 

int main (void)
{
	DDRB = 0b00000001;

   ADCSRA |= (0 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // set prescaler to 8. 1Mhz/8=125Khz
   ADMUX |= (1 << REFS0); // external analog voltage reference


   ADMUX |= (1 << ADLAR); // Left adjust ADC result to allow easy 8 bit reading


   ADMUX &= 0xF8; // clear existing bits
   ADMUX |= (0 << MUX1) | (1 << MUX0); // select ADC channel 1

   ADCSRA |= (1 << ADEN);  // Enable ADC

   for(;;)  // Loop Forever
   {

   ADCSRA |= (1 << ADSC);  // Start A2D Conversions


	if(ADCH > 150)
	{
	PORTB = 0b00000001;
	}	

	else
	{
	PORTB = 0b00000000;
	}


   }


}

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So what have you got connected to Aref? (Vcc I presume?)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
So what have you got connected to Aref? (Vcc I presume?)

the micro is connected to 5V via a usb cable i cut up.(pin 8 )

then for the adc tests im using batterys connected to the ground, then the positives on pin 7, which is what i have adc set to.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

the micro is connected to 5V via a usb cable i cut up.(pin 8 )

You have to connect 5 Volts to both VCC and AVCC. I am too lazy to look up what pin number they have on your AVR model and specific package, but it is a fact that you have to hook up two power supply pins on the AVR if you want to use the ADC pins. For every AVR I have played around with this has been clearly stated in the data sheet. Read it.

Also: If you refer to pins by their number in a specific physical package you must also tell us what package that is. There is no guarantee that eg pin 2 has the same function in a DIL package as in a TQFP package.

You can avoid this nuisance if you refer not to the physical package pin number but to the "logical/functional" pin designations, eg PB4, VCC, GND, AREF etc... Then we can discuss your problems without you having to translate from function to physical pin, and then us translating back from physical to function to see what pin you are talking about. Most of us probably don't keep the physical pin numbers in our heads, but are used to talk about the logical/functional pin names.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

there is no avcc on the pinout, and search the data sheet finds nothing.

would i be correct in assuming that AIN0 is what im looking for?

"analog comparator positive input"

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

there is no avcc on the pinout

You are on a attiny13 and you are correct. There is no AVCC pin, and there is no external reference voltage input. My mistake. Apologies are due.

The ATtiny13 ADC takes it's reference voltage from either VCC or an internally generated 1.1 Volt reference. The selection between these is done through the REFS0 bit in the ADMUX register.

In a previous post you have

   ADMUX |= (1 << REFS0); // external analog voltage reference

The data sheet clearly states that this will select the Internal Voltage Reference (1.1V).

The AIN0 pin has nothing to do with the ADC. It is an input for the Analogue Comparator.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

so, since the attiny has no avcc and is using the internally generated voltage, does my vcc still have to be withing .3V?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No reaction to my last post. I'll rephrase:

I've tried to browse over your posts in this thread and see no statement of how high voltages you are trying to measure. I might have missed it though.

Anyhow, the ADC value is presented as fractions of the reference value. With the current selection of reference voltage you can not measure anything higher than 1.1 Volts. Anything over 1.1 Volts will return 1023 decimal (0x3FF) if you are running the ADC with 10 bit resolution, 255 (0xFF) at 8-bit resolution.

In the data sheet

Atmel wrote:
The ADC converts an analog input voltage to a 10-bit digital value through successive approximation. The minimum value represents GND and the maximum value represents the voltage on VCC or an internal 1.1V reference voltage.

HTH!

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

so with the attiny13 there is no avcc, only an internal refrence, which is 1.1V, which means i can only measure between 0-1.1V

i was trying to measure up to 5V.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Go back and read the data sheet on REFS0. Instead of selecting the internal 1.1V reference, reference your measurements to Vcc. You will then be able to read voltages up to your Vcc voltage which is 5V.

Cheers,

Tom

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

tblough wrote:
Go back and read the data sheet on REFS0. Instead of selecting the internal 1.1V reference, reference your measurements to Vcc. You will then be able to read voltages up to your Vcc voltage which is 5V.

:evil: BLARG!?!

when i initially set it up, i did have vcc as reference, but then i copy pasted the original example and modified it, and forgot to change that back.

ill try it out now.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

yay! seems to work now.

although... i have it set to light up if ADCH > 150, and it lights up just from me touching it with my finger.

if i have 5V as my reference an 255 is the highest.
each increment of 51 should be equal to another volt.

which would mean at 150, it getting more than 3V from my just touching it.

add:

i put a 1k Ohm resistor between adc1 and ground, and it seems to work perfectly now. :lol:

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Excellent Tutorial, very useful esp for newbie like me!
One question:-

1) Can the ADC detect only mV change without a transistor? My ultrasonic receiver only send a few mV when a signal is detected. Can I directly connect the Ultrasonic receiver to the ADC? Or is it a MUST to use a transistor?

2) Why a PIC can directly measure a ultrasonic receiver without a transistor? Does that mean the PIC ADC is more sensitive compared to AVR microcontroller?
http://www.micro-examples.com/public/microex-navig/doc/090-ultrasonic-ranger.html This is the website where they directly connect the ultrasonic receiver to the PIC ADC pin.

Thanks in advance...

cs

I'm happy ytd, today, and tmr :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

With an AREF=5.0V then if you manage to achieve 10 bit accuracy each step will be 5.000V/1024 ~= 5mV

So what is the exact spec of your ultrasonic receiver?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

my ultrasonic receiver has no name! no spec! It only labelled as T and another one as R (the two look exactly the same), so I guess the one labelled as T is transmitter and R is a receiver.

By the look of my ultrasonic transmitter and receiver, it looks like this one:-

So I dont know how many mV it will generate, as a matter of fact, there is no datasheet for this pair of ultrasonic transmitter/receiver.

cs

I'm happy ytd, today, and tmr :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Silly question then - how do you propose to use a piece of electronics that you don't understand the operation of?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How would i add a second ir sensor so if i have one in front of a robot and one in back (i got 1 working so far)?

Hobbles and Company â„¢

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Silly question then - how do you propose to use a piece of electronics that you don't understand the operation of?

Well, I am really out of desperation, I can't find any ultrasonic Tx/Rx, so when I see one, even without any documentation and label, I just bought it! I hope I can play around with it and understand it by experimentation!

BTW,

clawson wrote:
With an AREF=5.0V then if you manage to achieve 10 bit accuracy each step will be 5.000V/1024 ~= 5mV

Can we use AREF as 3.0V? then 3.000V/1024 ~= 2.92 mV more sensitive? Is there any limit? Can I use 0.3V as AREF? then the AVR is so sensitive 0.292 mV? My commonsense told me no...So, what is the lowest AREF I can use?

I read the documentation of my AVR 169, it says "The ADC has a separate analog supply voltage pin, AVCC. AVCC must not differ more than ± 0.3V from VCC" Pg 193.

So, if my VCC is 3.3V, then my AVCC can be 3.0V. Is this the lowest?

cs

I'm happy ytd, today, and tmr :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hobbles wrote:
How would i add a second ir sensor so if i have one in front of a robot and one in back (i got 1 working so far)?

Well, if you can make the first ir work, then it should not a problem to make a 2nd ir work. You should tell us more about your problem, what is not working? Do you use modulation for your ir? Perhaps a TSOP17xx series chip for detection?

Can you post your schematics and code here? Just coincidence, I am developing a robot with ir sensors and now the ultrasonic sensors too.

cs

I'm happy ytd, today, and tmr :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oh a ultrasonic sensor would be awesome radio shack sells them for too much $32 for the parallax one. Well the tut kinda skipped the part on how to use like adc0 and adc1 and the others at the same time im using a 168 with this ir circuit http://www.reconnsworld.com/ir_ultrasonic_basicirdetectemit.html and the output is connected to adc0. So im not sure how to read whats coming off the second sensor from adc1.

Hobbles and Company â„¢

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

can anyone write this program in assembly for at90pwm3b uC?
thank you very much

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,

I've been trying to use the sample Battery Charger provided by Atmel. It runs on the BC100 board with a ATTINY861 processor. The app note is AVR458.

The I'm having trouble with the unipolar differential conversion across two pins (pin 11 and 12). Using a multimeter, I see that the voltage difference between the two pins is .6 millivolts. However, when I try to read the ADC result from a Unipolar Differential Conversion, the result is 0.

Below is a snippet of my code. Can anyone tell me why I'm running into this error? I'm pretty sure the problem lies with my code.

			ADMUX = (1<<REFS0)| 0x17;							// Use External Voltage Reference of 2.5 volts			
			ADCSRA = (1<<ADEN) | (1<<ADSC) | ADC_PRESCALER;		// Start conversion, no interrupt (disable ADC-ISR).

			do
			{ // Wait for conversion to finish.
			} while (!(ADCSRA & (1<<ADIF)));

			ADCSRA |= (1<<ADIF);  								// Clear ADC interrupt flag manually.


			sensorArray[11] = ADC;								// Save the sampled offset.

ADD~ Sorry, just realized that I'm not supposed to post questions to this forum! I re-posted this question in the correct forum.

Attachment(s): 

Last Edited: Mon. Jul 20, 2009 - 05:00 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

Thanks for the tutorial Ken; it was just what I was looking for.

I am trying to get this working with an attiny45. I read through the entire post hoping someone else was running into the same problem I am. I found a post where the user was having my exact issue, but unfortunately a resolution was never posted.

https://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&p=425057&highlight=#425057

My code is basically the same as pepper_bg’s. Here is how my hardware is connected:

Pin 1 to output of the 10k pot voltage divider.
Pins 2 and 3 have the LED’s.
Pin 4 to ground.
Pin 8 to VCC

I also have my AVRISP mkII connected as well.

MKII to attiny45
Pin 1 to Pin 6
Pin 2 to VCC
Pin 3 to Pin 7
Pin 4 to Pin 5
Pin 5 to Pin 1
Pin 6 to Ground

With the MKII connected, it works as described in pepper_bg’s post. LED1 turns on at about 2.1V, LED2 at about 2.5 at which point LED1 turns off. LED2 then stays on up to max. With the MKII disconnected LED1 turns on at max only, about 4.85 volts.

Any help would be greatly appreciated.

I am totally new to this stuff and have tried some other easier tutorials with success. But was wondering why when the MKII is disconnected the programs run differently or sometimes not at all. Once a chip is programmed shouldn't it function as intended with the MKII disconnected?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I used the tutorial in a Mega16A, off course i had to do a little modification, and i have the ADC working, but... I don´t understand why!

here is the code:


#define F_CPU 1000000UL  //1MHz

#include 

//Ports

#define LED1 PA0
#define LED2 PA1
#define POT ADC2

int main(void){
   
   DDRA|=_BV(LED1)|_BV(LED2);  //LEds input,

//ADC CONF

   
   //1MHz/8=125kHz   limit Prescaler [50 200] kHz

   ADCSRA|=_BV(ADPS1)|_BV(ADPS0);   //Define prescaler  8=(0b011)
   ADMUX|=_BV(MUX1);                //input ADC2
   ADMUX|=_BV(REFS0);               //Ref AVCC

   
   //Free-running mode 000
   ADCSRA|=_BV(ADATE); //set Auto Triguer

   ADMUX|=_BV(ADLAR); //use only ADCH

   ADCSRA|=_BV(ADEN); //ADC on

   ADCSRA|=_BV(ADSC);  //start ADC

//end ACD CONF
   
   
   while(1){
      if(ADCH<128){  // Led1 on
         PORTA|=_BV(LED2);
         PORTA&=~_BV(LED1);
      }
      else{           //led2 on
         PORTA|=_BV(LED1);
         PORTA&=~_BV(LED2);
      }

      ADCSRA|=_BV(ADIF); // if i remove this line it stops working
   }
   return (0);
}

in the datasheet they say that the Interrupt flag is set when the conversion ends, so why do i have to set it manually?

the code is correct?

Thanks for your help

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Awesome tutorial thanks.

Pages

Topic locked