Go Down

Topic: 38khz IR Signal using ATTINY85 (Read 2690 times) previous topic - next topic

rreddy18

I am currently using the code available at ,

http://www.ernstc.dk/arduino/38khz_timer.htm

void setup()   { 
DDRB |= (1<<PB0); //Set pin PB0 as output
TCNT0 = 0;
TCCR0A=0;
TCCR0B=0;

TCCR0A |=(1<<COM0A0); //Timer0 in toggle mode Table 11-2
TCCR0A |=(1<<WGM01); //Start timer 1 in CTC mode Table 11.5
TCCR0B |= (1 << CS00);// Prescaler = 64 Table 11.6
OCR0A=105; //CTC Compare value

}

void loop()                   
{
}

Looks like this code constantly send a  wave with a frequency of 38khz  on PIN PB0 (physical Pin 5)

I would like to send the signal as ,
pulse 400 (on for say 400 u sec's)
space 500 (off for 500 u sec's)

I tried to use ,

digitalWrite(0, HIGH); and  digitalWrite(0, LOW);
pinMode(0, OUTPUT); and  pinMode(0, INPUT);
and
DDRB &= ~(1 << PB1); etc  , but nothing is working.

I am very new to this field , any help is appreciated.


Erni

One way to do it is to connect your IR-diode between pin 3 and pin 0 (with a resitor in serie)
That way the diode is on only when pin 3 is LOW.
Don't expect too much precision, partly bescause you don't use a x-tal (asumption) and partly because there are some overhead in the loop.
BTW the code asumes the Attiny85 is running @ 8 MHZ.

Code: [Select]
/*
Anode (long leg connected) to 0
Cathode (short leg to 3 through resistor)
*/
void setup(){
  TCNT0 = 0;
  TCCR0A=0;
  TCCR0B=0;

  TCCR0A |=(1<<COM0A0); //Timer0 in toggle mode Table 11.2
  TCCR0A |=(1<<WGM01); //Start timer 0 in CTC mode Table 11.5
  TCCR0B |= (1 << CS00);// Prescaler Table 11.6
  OCR0A=105; //CTC Compare value
  pinMode(0,OUTPUT);
  pinMode(3,OUTPUT);
}

void loop(){
  PORTB |=_BV(3);  //off
  delayMicroseconds(500);
  PORTB &=~_BV(3);  //on
  delayMicroseconds(400);
}

rreddy18

Thank you for the reply.

Pin 0 is the reset pin , I connected through a receiver , but no data is being generated.

I am using ardunio with an IR Receiver (ISOP1738) to decode the IR data that is being sent by Attiny85. With this code changes I am not seeing any data coming to the IRReceiver.

When I change the code to

void loop(){
_delay_ms(3000);
  PORTB |=_BV(0);  //off
  _delay_us(500);
  PORTB &=~_BV(0);  //on
_delay_us(400);
}

and if I obstruct the IR led with my hand or say paper some data is being generated. Looks like the IR led is sending the 38khz wave constantly and when I obstruct with my hand I could see some data.



Erni

Quote
Pin 0 is the reset pin


No, take a look at the pinout

Code: [Select]
       +-\/-+
PB5  1|    |8   VCC
PB3  2|    |7   PB2
PB4  3|    |6   PB1
GND  4|    |5   PB0
       +----+


If you connect the IR diode as I suggested the output will be like the picture from my logic analyzer.

I think it would be easyer to check the output if you use a longer delay, f.ex. 1 second on/off



rreddy18

#4
Mar 03, 2014, 01:28 pm Last Edit: Mar 03, 2014, 02:01 pm by rreddy18 Reason: 1
Thank you Erni for testing this one and also for the gif file of the signal from your logic analyser.

You mention that ,
Anode (long leg connected) to 0
Cathode (short leg to 3 through resistor)

When you say 0 it is PB0 (physical pin 5) and 3 means PB3 (physical pin 2) right. Sorry for the basic questions.

Can you please clarify this.

Erni

Quote
When you say 0 it is PB0 (physical pin 5) and 3 means PB3 (physical pin 2) right. S


Yes that was what I meant.

Actually this might be easyer. This sketch will output on pin 0 (PB0), so you just connect your IR-led like the drawing.

Code: [Select]
//Attiny85 IR-test
//Attiny85 , running @ 8MHZ
// ATMEL ATTINY45 / ARDUINO
//
//                           +-\/-+
//  Ain0       (D  5)  PB5  1|    |8   VCC
//  Ain3       (D  3)  PB3  2|    |7   PB2  (D  2)  INT0  Ain1
//  Ain2       (D  4)  PB4  3|    |6   PB1  (D  1)        pwm1
//                     GND  4|    |5   PB0  (D  0)        pwm0
//                           +----+
void setup(){
  DDRB |= (1<<PB0); //Set pin PB0 as output
  TCNT0 = 0;
  TCCR0A=0;
  TCCR0B=0;

  TCCR0A |=(1<<COM0A0); //Timer0 in toggle mode Table 11-2
  TCCR0A |=(1<<WGM01); //Start timer 1 in CTC mode Table 11.5
  TCCR0B |= (1 << CS00);// Prescaler table 11.6
  OCR0A=104; //CTC Compare value
}

void loop(){
  TCCR0A &= ~(1<<COM0A0); //off
  delay(1000);
  TCCR0A |=(1<<COM0A0); //on
  delay(1000);
}

rreddy18

Thank you very much Erni , it is working fine now.

Go Up