In this experiment, we managed to connect infrared LEDs to the ATTINY. The other one ( infrared receptor seem like LED) is used for read rebounding object light form a infrared led, acting as a simple proximity sensor. ( 3 feets ...)
___________________________[ Avr Code ]__________________________
/* -----------------------------------------------------------------------
* Title: Asynchronous data transmit input (auto timing pulse detection)
* Author: Samuel St-Aubin (samuel.st-aubin@sympatico.ca)
* Date: 15.1.2008
* Hardware: ATtiny13
* Software: WinAVR 20060421
-----------------------------------------------------------------------*/
#define F_CPU 1200000
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#define SD_DATAIN PB0
#define LED PB4
volatile int Data ;
char timer;
int i;
int x;
int main(void) {
GIMSK = _BV (INT0); // int - Enable external interrupts int0
MCUCR = _BV (ISC01); // int - INT0 is falling edge
sei(); // int - Global enable interrupts
Data = 50;
DDRB &= ~(1<<SD_DATAIN); // use PortD for input (switches)
DDRB |= (1 << LED); // Set direction register output
for (;;) // Main loop (endless)
{
// interrupts do the job because he's cool
}
return 0;
}
ISR (INT0_vect) // Interrupt on Int0 vector
{
while (bit_is_set(PINB, SD_DATAIN)) // Calculated one pulse timing
{ //
_delay_ms(1); //
timer++; //
} //
for ( x = 0 ; x < (3 * timer); x++) // Wait 1.5 pulse ( 3 divided by 2)
{ // (.5 for half pulse synchronysing)
_delay_ms(1); //
x++; // double incrementation
} //
Data = 0; // Initialize data register
for (i = 0 ; i < 7 ; i++) // For loop 0 to 7
{ //
if bit_is_set(PINB,SD_DATAIN) // Bit detector on Sd_Data input
{ //
Data ^= (1<<i); // Update data register (one by one bit )
} //
for ( x = 0 ; x < timer; x++) // One pulse time calculation
{ //
_delay_ms(1); //
x++; //
} //
}
timer = (timer * 10); // Show result of "timer" variable
for ( x = 0 ; x < timer ; x++ ) // 0 = 10 usec, 255 = 2550 usec
{ //
PORTB ^= (1 << Piezo); // Inverte led bit
_delay_us (10); //
}
}