Analog to Digital Conversion with the Atmega324p on OSX with CrossPack
Analog to digital conversion is used for things like sensors, potentiometers, and other input devices that interface with our non-digital world. The signals they gather are then converted to a digital representation by the microcontroller so you can do something meaningful with those signals.
What you will need:
Black, Red, & one other color of breadboard wire
Completed Hello World!
Completed UART
Completed Power Regulator
Step 1 - Wire your potentiometer to your breadboard and Atmega324p.
This step is simple, you need to wire the three pins of the potentiometer. The outside pins are power (right) and ground (left). The center is the pin-out to the Atmega324p.
1. Ground: Use a black wire to connect this to you ground rail on your breadboard.
2. Pin-out: Wire this to Pin 20 on the Atmega324p.
3. VCC: use a red wire to connect this to the power rail on your breadboard.
4. Connect the AREF pin on the Atmega324p to the power rail on your breadboard.
Step 2 - Writing the ADC code
Note: for details on this code please see the More Details section on the ADC.
a. The adc.c and adc.h files: create a new file for the ADC liek you have for the previous projects using vi and create the following files:
adc.h - this is the header file (thus the .h file extension) for the following adc.c file. For more on header
files see the K&R book for ANSI C and the tutorials found here by Kernighan. Its the C Programming "Bible".
adc.c - this is where we "fill out" all the methods defined in the header file. For details beyond the comments
in blue you can see the More Details section for the ADC Registers. Don't forget to include your header file
in quotes as seen here or you will get complaints by the compiler later that it does not know what these methods
are.
b. You will need to edit your Makefile so that it also "knows" about your new adc file:
See the highlighted line? The object files are created by the complilation process. You need to add the
adc.o line to your Makefile as seen above. You do this any time you create new files that are included
in your project.
c. Additions need to be made to your main.c file to utilize your new ADC methods and your UART serial
communication to print your adc conversions using what is called VT100 terminal emulation.
main.c with the additions for geting your potentiometer reads, converting the data to a digital value and
printing to the screen with UART and VT100 terminal emulation
It is important to note the addition of the inclusion of the #include "adc.h" at the top and the order of the VT100 commands.
It is pretty easy to accidentally put some of those commands in the while loop. If you do so, you'll overwrite values and text
unnecessarily. VT100 terminal emulation is powerful and useful for creating menus. This will give you an idea of what it can do.
Using the itoa() method, which stands for 'integer to ascii', allows us to pass the UART_Print() function a string. itoa() is a
standard C library function, which is why we included the stdlib.h header file at the beginning of the file. The itoa() function expects
an integer (returned by the ADC_Get() call), a buffer (char buf[8]), and a base. The base, in this case base 10, is the normal number system
we are used to using i.e. 0-9. the conversion to ascii is put in the buffer and that that buffer is handed to the UART_Print() function to be
iterated over and printed. This buffer length is 8 'characters' but coud easily be smaller since our largest possible value from the potentiometer
is 1024, or four characters wide.
Step 4 - Compile, Download, and Run Your Code
a. Compile your code as you have before with:
ykk$ make clean
You should see:
ykk$ make
You should see:
b. Download your new code to the Atmega324p with:
ykk$ avrdude -c dragon_isp -B4 -b 4800 -p m324p -P usb -U flash:w:main.hex:i
You should see:
c. Run your code with a serial console with:
ykk$ screen /dev/tty.usbserial-FTF539ZI 4800
You should see:
NOTE: This does not work with the Arduino console - which will read out the VT00 commands
instead of the above.
Here, if you manipulate the potentiometer's thumb wheel, the converted digital value will update
where the value 205 is positioned above- which in turn corresponds to the VT100 command
\x1b[5;15H
used in the main.c file. This program can also be written to only update the value if it is different
than the current value saving cycles.