Up: Hands On AVR-GCC programming
Analog Inputs
This example shows analog inputs and discrete outputs using AVRLIB on
Wiring . The idea is to measure values coming from the sensor, and
categorize them to be used with the colored LEDs from the previous
example. LEDs will light to signal low, medium or high values
accrodingly. In most situations sensors give countinous values, and
thus they are used for getting analog values to ATMega analog inputs.
Analog values are continuous values that go from 0 to 5 Volts. In
order to be used on the ATMega, they need to be converted to discrete
(digital) values. The function 'a2dConvert10bit()', part of
AVRLIB can be used to do this
conversion. 'a2dConvert10bit()' will return discrete values
that go from 0 to 1024. Further signal processing
can be done after. In this case we are using a ``step function'' to
create ranges which can help signal information or something.
After reading and understanding this program, do the circuit with a
photo-resistor, an FSR
or another type of sensor using a
breadboard. As with the other examples there are diagrams and
descriptions for doing a FSR circuit
on the
Wiring documentation. Try to follow the steps outlined on the previous
examples.
Create an 'analogin' directory, copy a 'makefile'
from previous examples an edit it. It should contain the name of this
program 'analogin.c', and it should knows about the libraries
being used here. The 'SRC' line should look like:
SRC = $(AVRLIB)/buffer.c $(AVRLIB)/timer128.c $(AVRLIB)/a2d.c $(TRG).c
|
|
Compile'analogin.c' by issuing a 'make' command. If
there are no errors, press the '<reset>' button on the Wiring
board, and after three(3) seconds type 'make-load' command.
Press the '<reset>' button again and after a few moments you
should see the LEDs ON and OFF. Manipulate the sensors to see which
LED is ON and guess what value it corresponds to.
//------------------------------------------------------
//------------------------------------------------------
//
// AVRLIB-DEMO
// For avrlib and Wiring development board.
//
//
// File: analogin.c
// Author: Juan Reyes juanig-at-ccrma-stanford_edu
//
// Date: July 3, 2008
//
// Notes:
//-----------------------------------------------------
// USING THE WIRING DEVELOPMENT BOARD, ANALOG INPUTS
// PORTF IN ATMEGA128 TO PROCESS SENSOR DATA AND
// MEASURE IT USING DIGITAL OUTPUTS
//
//-----------------------------------------------------
// Layout of ports in Wiring board correspond with
// ATMega128 as follows:
//
// ATMega128 WIRING
// ========= ======
//
// PORTF ANALOG IN
//-----------------------------------------------------
// Layout of analog pins in Wiring board correspond with
// ATMega128 as follows:
//
// Ch0 W-APIN0
// Ch1 W-APIN1
// . .
// Ch7 W-APIN7
//
// Function a2dConvert10bit(Ch) reads and converts analog
// values.
//
//-----------------------------------------------------
// More information about Wiring board:
//
// http://wiring.org.co/hardware/index.html
//
//-----------------------------------------------------
// Information about functions used here and avrlib:
//
// ~/avrlib/examples/a2d/a2dtest.c
//
//*********************************************************
// Description:
// ============
//
// This program will read sensor data on pin0 (Ch 0) of
// PORTA, analog port on Wiring board. If values are low
// it will light a GREEN LED. If values are in the middle
// range it will light a YELLOW LED.If values are too high,
// it will light a RED LED. LEDS are connected on PORTD
// pin0, pin1, pin2.You can use a photo resistor or a POT
// for this purpose.
//
//
// Information about analog sensors and Wiring:
// http://wiring.org.co/learning/examples/
//
//
//*********************************************************
#include <avr/io.h>
#include <avr/pgmspace.h>
#include "global.h"
#include "timer128.h" // use timer128.h for ATMega128
#include "a2d.h"
int main(void)
{
u16 a2dval;
u08 lightn;
// Initialize libraries
timerInit();
// ATOD STUFF
a2dInit();
// set PORTF pins to input (for ADC)
outb(DDRF, 0x00);
// turn pull-ups off
outb(PORTF, 0x00);
// set PORTD as OUTPUT
outb(DDRD, 0xFF);
// set pull-ups
outb(PORTD, 0xF0);
while(1) {
// Read sensor value using a2dConvert10bit(ch).
//
// a2dConvert10bit(ch) converts on A/D channel# ch,
// and returns 10-bit values from 0 to 1024
a2dval = a2dConvert10bit(0);
// Check for thresholds in sensor values
//
if (a2dval < 500) lightn=0;
if ((a2dval >= 500) && (a2dval < 850)) {
lightn=1;
}
if (a2dval >= 850) lightn=2;
//
// Turn ON and OFF LEDS according to
// thresholds
switch(lightn) { // switch 'C' statement
case 0 :
sbi(PORTD,2); // Turn GREEN LED ON
cbi(PORTD,1); // Others OFF
cbi(PORTD,0);
timerPause(10);
break;
case 1 :
sbi(PORTD,1); // Turn YELLOW LED ON
cbi(PORTD,0); // Others OFF
cbi(PORTD,2);
timerPause(10);
break;
case 2 :
sbi(PORTD,0); // Turn RED LED ON
cbi(PORTD,1); // Others OFF
cbi(PORTD,2);
timerPause(10);
break;
default :
break;
}
}
return 0;
}
|
|
On the 'analogin' directory these are the steps:
Compile:
Press the '<reset>' button.
Load to ATMega:
Press the '<reset>' button. If you see the progress output,
and everything goes fine, press the '<reset>' button again.
Manipulate the sensor and look which LED(s) lights ON.
Next: Analog Inputs. Using (OSC )
Up: Hands On AVR-GCC programming
Previous: Button
© Copyright 2001-2008 CCRMA, Stanford University. All rights reserved.
Created and Mantained by Juan Reyes