You are on page 1of 2

Cockroach C Programming Cheat Sheet #2

A2D (Analog to Digital Conversion):


Analog to digital conversion is used to convert an analog signal such as the one below (Yaxis voltage, X-axis time)

to a digital signal such as this one:

The reason you need to do this is that computers are digital and work around clock rates.
A purely analog signal, such as the first one, cannot be read because the divisions are
infinitely small; smaller than our clock division, so the computer cant read it. This is
why we perform an estimate, which breaks the analog signal up into lots of
approximations at a sampling rate that the microcontroller can handle. You can look in
the Atmega32 manual to see what sampling frequency to set when you are settin up your
A2D.
When do we need A2D conversion? Well, anytime we need to read a sensor you will need
to use this. Most sensors work on a 4-20mA, 0-5V or 0 to 10V analog range.
The example program, A2Dtest.c in the examples directory is a great start for A2D
conversion. Any port can be setup for A2D.

Interrupts:
An interrupt is a smart way to handle an event, meaning it runs in the background and
does not slow your program down. Lets take the counter example that we are making in
Lab. Instead of running loops in your main() function, you can attach a counter read
function (or whatever you want to call it), to your interrupt. What this means is, you will
attach an operation to a timer that will keep tabs on a value or operation. When it
changes it will execute a certain operation. There are different ways to do this, but I
believe the easiest is to use the:
//----- Include Files --------------------------------------------------------#include <avr/io.h>
// include I/O definitions (port names, pin names, etc)
#include <avr/signal.h>
// include "signal" names (interrupt names)
#include <avr/interrupt.h> // include interrupt support
#include "global.h"
#include "timer.h"

// include our global settings


// include timer function library (timing, PWM, etc)

Int main(void){
//setup timing of interrupt
timer1SetPrescaler(TIMER_CLK_DIV8);
// attach interrupt service function
timerAttach(TIMER1OVERFLOW_INT, someFunction);
}
Void someFunction(void)
{
Do interrupt crap
}

You might also like