NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » Multiple ADC ports
July 30, 2010 by norby31 |
I'm going to have 4 different voltage sources in a circuit that change at non-specified times and I want to use ADC ports to read them. Can I change between ports quickly to "poll" among all four inputs or can I assign 4 different pins to be ADC inputs at all times? If I could do either, what may be the better choice? |
---|---|
July 30, 2010 by Rick_S |
I made a joystick control board for one of the CNC machines at the machine shop I work at. In the program for that board, I polled 3 different analog inputs to determine the x/y/z positions of the stick to (depending on mode) operate as a mouse on a pc or move the machine. The code is fairly simple and quick enough for smooth mouse movement. Not sure what your application is or how fast it needs to be but as long as you aren't talking microsecond response times, you could probably make polling work fine. Here's some sections of code I used for the analog input... First I defined the ports I used for analog input (Note, this was on a different atmel not one of the Nerdkit Chips so you would have to change the ports to the correct ones for the chip you use)
This code was in the main portion of my program
The remaining functions do the work
Hope that helps out with your project some. Rick PS... Wow, they've changed the code button even more... ME LIKES :) |
July 30, 2010 by hevans (NerdKits Staff) |
Hi Norby, You only have 1 ADC on board your chip, so you have no choice but to multiplex and use polling if you want to read several analogue sources. There is a built in way of doing that with the chip though, you just have to connect the different sources to different pins and set the ADMUX register to tell the ADC where to read from. There is a great post by Mike on the subject of reading from multiple sensors using the ADC(which coincidentally contains quite a bit of our now very pretty formatted code). You might want to double check the datasheet but I think the ADC runs at clk/128, and it takes 13 cycles to complete one conversion. So assuming you are reading the values as soon as they are available you're ADC will be running at about 8.8Khz. That is not terribly fast, but it is hopefully fast enough for your application. Ralph, I'm glad you are enjoying the new code formatting. Hopefully it will enable better learning, now that we can easily see what people are coding, easily copy and paste into our own code, and refer to specific lines with line numbers. Humberto |
July 30, 2010 by Rick_S |
Umm.. Humberto, I'm Rick :) |
July 30, 2010 by hevans (NerdKits Staff) |
My apologies Rick. You are both such helpful members of our community, and I failed to double check what I typed. It was early in the morning here =/. Humberto |
July 30, 2010 by norby31 |
He got Rick-Rolled. |
July 30, 2010 by Rick_S |
Rick RolledMy son had to explain that one to me :D |
July 30, 2010 by Rick_S |
BTW, Humberto, Not a problem... It was early here too :D I think we're in the same time zone... |
July 31, 2010 by norby31 |
Thanks. Cool, I'm gonna try it. I definitely understand changing the ADMUX, that makes perfect sense. I'll get back to y'all after I try it. |
Please log in to post a reply.
Did you know that microcontrollers have two different kinds of memory, program space (flash) and SRAM? Learn more...
|