Society of Robots - Robot Forum

Software => Software => Topic started by: pomprocker on July 13, 2008, 07:35:59 PM

Title: atmega8 port programming
Post by: pomprocker on July 13, 2008, 07:35:59 PM
How would you change a pin on portc from analog input to digital i/o on an atmega8 using C programming (avrlib/winavr)?


Title: Re: atmega8 port programming
Post by: bens on July 13, 2008, 07:49:22 PM
The port C pins are digital I/Os (you don't have to do anything special to use them as such).  It just so happens that you can also internally connect them to the mega8's ADC.  For example, it's possible to use the ADC to read the analog voltage on pin PC0 while using PC0 as a digital output.  The result probably wouldn't be very interesting or useful, but you could do it.

So in short, if you want to use the pins on port C as digital I/Os, do the same thing you'd do for the pins on port D or port B.  If you want to use them as analog inputs, set them as digital inputs and configure the ADC module appropriately.

- Ben
Title: Re: atmega8 port programming
Post by: pomprocker on July 13, 2008, 08:01:57 PM
I didn't mention this but I'm fairly new to this, and I'm starting from the $50 robot code where I believe the C ports are set to analog inputs, so i want to set just ONE of those pins to digital I/O because I am trying to interface the parallax ping sonar which operates using PWM which is digital right?


Title: Re: atmega8 port programming
Post by: Admin on July 20, 2008, 09:14:39 AM
This was meant for the Axon, but its almost exactly the same:
http://www.societyofrobots.com/robotforum/index.php?topic=4536.0
http://www.societyofrobots.com/axon/axon_function_list.shtml#digital_input
(that will make a digital input, but if you turn off the pull-up, it will be an output)

Look at SoR_Utils.h in the Axon source code:
http://www.societyofrobots.com/axon/downloads/Axon_Source_070408.zip

and read the comments for port configuration.

Let us know if its still confusing after reading through this material.