go_away

Author Topic: IR Sensor Help with ATmega 169  (Read 1753 times)

0 Members and 1 Guest are viewing this topic.

Offline poxusaTopic starter

  • Beginner
  • *
  • Posts: 3
  • Helpful? 0
IR Sensor Help with ATmega 169
« on: June 28, 2010, 08:43:17 AM »

Hello, the code below runs left and right motors. It is user controlled. We have connected a Sharp IR sensor (model 2Y0A02). We connecting this to an AVR Butterfly with an Atmega 169 microcontroller. We are not sure where and what to insert in order to read the analog voltage from the sensor and stop the motors at 1 volt (2 feet). If you could please help us insert it, it would be greatly appreciated. Thank you.

Code: [Select]
/*
main.c
Main program

Project 672
PWM DC Motor Controller
Author: Lewin A.R.W. Edwards
Rev: 1.00 (5/28/2010)
*/

#include <avr/io.h>
#include <avr/interrupt.h>

/*
I/O definitions
*/
#define MOTOR_R (PD0) // right motor drive
#define MOTOR_L (PD1) // left motor drive
#define BUT_FAST (PD2) // FAST button
// (PD3) not used
#define BUT_SLOW (PD4) // SLOW button
// (PD5) not used
#define BUT_STOP (PD6) // STOP button
#define LED (PD7) // Activity LED
#define IRSensor                 (PF0)   // Added for IR Sensor.
#define PPS_R (PG3) // external position encoder
#define PPS_L (PG4) // external position encoder

/*
Miscellaneous definitions
*/
#define CLK_SPEED 8000000 // Clock speed in Hz
#define TMR2_COMPARE 100 // Timer 2 reset @ cnt=100
#define TMR2_SPEED ((CLK_SPEED / 8) / (TMR2_COMPARE)) // ticks per second
#define PPS_SAMPLE_RATE (TMR2_SPEED / 10) // 100ms


/*
Variables
*/
volatile unsigned int wallclock = 0; // used for time comparisons
volatile unsigned int pwmclock = 0; // used to manage PWM duty cycle
volatile unsigned int ppstimer = PPS_SAMPLE_RATE; // countdown timer for PPS sampling
volatile unsigned int right_duty = 0; // duty cycle, right motor
volatile unsigned int left_duty = 0; // duty cycle, left motor
volatile unsigned int right_pps = 0; // pulses per second on right shaft
volatile unsigned int left_pps = 0; // pulses per second on left shaft
volatile unsigned int distance = 0;     // added to measure voltages
/*
Timer 2 ISR - Runs wallclock and soft-PWMs; handles
various soft time tasks.
Per definitions above, we get 10kHz interrupts out of this
*/
ISR(TIMER2_COMP_vect)
{
wallclock++;

// The PWM clock can't be run off the wallclock, because wallclock won't
// wrap at a multiple of 100.
pwmclock;
pwmclock = pwmclock % 100;

// update right motor
if (right_duty > pwmclock)
PORTD |= (1 << MOTOR_R);
else
PORTD &= ~(1 << MOTOR_R);

// update left motor
if (left_duty > pwmclock)
PORTD |= (1 << MOTOR_L);
else
PORTD &= ~(1 << MOTOR_L);

// resample PPS timers every 100ms
ppstimer--;
if (!ppstimer) {
left_pps = TCNT0 * (TMR2_SPEED / PPS_SAMPLE_RATE);
right_pps = TCNT1 * (TMR2_SPEED / PPS_SAMPLE_RATE);
TCNT0 = 0;
TCNT1 = 0;
ppstimer = PPS_SAMPLE_RATE;
}
}

/*
Main function - Init I/O and timers, run main loop
*/
int main(void)
{
PORTD = (1 << BUT_STOP) | (1 << BUT_SLOW) | (1 << BUT_FAST);
DDRD = (1 << LED) | (1 << MOTOR_R) | (1 << MOTOR_L);
PORTG = (1 << PPS_L) | (1 << PPS_R);

/*
Set up timers 0 and 1 as counters
*/
TCCR0A = (1 << CS02) | (1 << CS01);// clock from pin T0, falling edge
TCNT0 = 0;
TCCR1A = 0;
TCCR1B = (1 << CS12) | (1 << CS11);// clock from pin T1, falling edge
TCCR1C = 0;
TCNT1 = 0;

/*
Set up timer 2 for wallclock and enable interrupts
*/
TCCR2A = (1 << WGM21) | (1 << CS21);// Count to OCR2A CLK/8 (1MHz)
TCNT2 = 0x00; // reset timer
OCR2A = TMR2_COMPARE; // set max value for timer
TIFR2 |= (1 << OCF2A); // clear pending compare if any
TIMSK2 |= (1 << OCIE2A);// enable compare interrupt

sei();


while (1) {
// Stop button scrams motors immediately
if (!(PIND & (1 << BUT_STOP))) {
PORTD &= ~((1 << MOTOR_R) | (1 << MOTOR_L) | (1 << LED));
right_duty = 0;
left_duty = 0;
}

// SLOW button sets 50% duty cycle
else if (!(PIND & (1 << BUT_SLOW))) {
PORTD |= (1 << LED);
right_duty = 50;
left_duty = 50;
}

// FAST button sets 100% duty cycle
else if (!(PIND & (1 << BUT_FAST))) {
PORTD |= (1 << LED);
right_duty = 100;
left_duty = 100;
}
}

return 0; // dummy
}

 


Get Your Ad Here

data_list