AVR code help (newbie) (adc_vect)

Hello everyone! I’m just starting out with AVR and AVR programming. My first project seems pretty simple (on paper).

The Project:

It’s a Halloween project for my father who’s a huge Halloween buff, we’re building a coffin that’s door opens when people walk by, it’s going to use a photo-resistor and a variable resistor, when someone walks in between the photo-resistor and the spotlight aimed at it the door will open and close. The variable resistor would allow the reference voltage to be changed to adjust for ambient light. When the photo-resistor’s resistance is higher (there is less light) the voltage on that pin will be lower than that on the variable resistor’s pin and that will cause the AVR to light up a LED and drive the motor for a select time via a transistor/ relay.

Hardware:

For this project I am using an ATtiny 13

5 inputs / outputs

4 of which have built in 8 bit ADC

Code:

I’m not sure about the code because I’m just starting and everywhere I look people seems to do it differently Well here it is anyway:

Also a few parts are copied and pasted so there might be some strange chunks, I need all the help I can get.

/* ~~~~~ Coffin Program ~~~~~ /

/* Includes: Adds functionality */
#include <avr/interrupt.h>
#include <avr/io.h>
#include <util/delay.h>

#define F_CPU 100000UL //Defines the delay (what this means I don't know)
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) //Sets pin LOW: 0 volts
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit) //Sets pin HIGH: 5 volts

/* Code body: */

SIGNAL (adc_vect)
{
//stuff
}

int main()
{
//define output pins
DDRD = _BV(PB0);
DDRD + _BV(PB1);
//define pins here

cli() //Disables gloal interrupts

//Quick blinking to signify program load

sbi(PORTB, PB1);
_delay_ms(1000);
cbi(PORTB, PB1);
_delay_ms(500);
sbi(PORTB, PB1);
_delay_ms(500);
cbi(PORTB, PB1);

sei() //Enables global interrupts

//begin looking at inputs

}

You may now collect all of your hair that fell out from reading that.

So I need all of the help I can get, how do I take in an analog reading from two pins compare them and if the comparison yields the correct result a routine is run. I realize that this isn’t the simplest project to start with but if I can do this correctly then I’ll have the format (basic at least) fore code down and I’ll know how to use ADC which is really useful.

Let2692@rit.edu

Thanks for any help!

First off, the datasheet is your best friend when it comes to setting up hardware:

http://atmel.com/dyn/resources/prod_doc … oc8126.pdf

Section 14 covers the ADC.

Here is a quick example of using the ADC without interrupts:

ADCSRA = (1 << 7); /* Enable the ADC */
ADMUX = 0; /* Read from ADC0 / PB5, use VCC as reference */
ADCSRA |= (1 << 6); /* Set bit 6, start a conversion */
while(ADCSRA & (1 << 4) == 0) { } /* Busy wait while the ADC does its thing */
uint16_t result = (ADCL);
result += (ADCH << 8); /* Read from ADCL and ADCH for all 10 bits */

I haven’t tested it, but it should help you along. You can change up a few things, such as using names for registers instead of numbers.