cockroach c programming cheat sheet

3
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 (Y-axis 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 can’t 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.

Upload: geouno

Post on 07-Nov-2015

9 views

Category:

Documents


4 download

DESCRIPTION

C Programming Cheat Sheet

TRANSCRIPT

Cockroach C Programming Cheat Sheet

Cockroach C Programming Cheat Sheet #2A2D (Analog to Digital Conversion):

Analog to digital conversion is used to convert an analog signal such as the one below (Y-axis 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

// include I/O definitions (port names, pin names, etc)

#include // include "signal" names (interrupt names)

#include // include interrupt support

#include "global.h"

// include our global settings

#include "timer.h"

// 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

}