Loading...
Pages: [1]   Go Down
Author Topic: Attiny 45 38khz flash  (Read 576 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all

I am new to Arduino and the forum.
I am trying to make a virtual wall for my Roomba robot.
It requires a 38kHz pulse with an IR led.
I managed this with my Arduino uno but I am having problems with my Attiny 45.
I have sucessfully downloaded the LED blink sketch to the Attiny 45 and it is blinking at 1Hz.
When I use the delayMicroseconds function it doesn't flash faster than approximately 100 ms.

void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delayMicroseconds(13)               // wait for 13 ms
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delayMicroseconds(13)               // wait for 13 ms
}

Could the problem be that I haven't been able to set the clock frequency to 8 MHz?
Thanks

Logged

Valencia, Spain
Offline Offline
Edison Member
*
Karma: 65
Posts: 2246
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Could the problem be that I haven't been able to set the clock frequency to 8 MHz?
Thanks

Maybe.

The "Burn bootloader" option in the Tools menu will sets the clock frequency of the chip to whatever's selected as the current board.

Select "Tiny45 8MHz" then do that and you should be running at 8mHz.


OTOH I don't think your program will ever work like that. The loop(), the digitalWrite()s, even the delayMicroseconds(), they all take time to execute.
« Last Edit: December 29, 2012, 03:34:37 pm by fungus » Logged

SE USA
Offline Offline
Faraday Member
**
Karma: 33
Posts: 3621
@ssh0le
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

your going from a arduino clocked at 16Mhz to a tiny clocked at 1Mhz, yes that would have an effect on timers, though if you were at 1Mhz it should be more like 200ms
Logged


United Kingdom
Offline Offline
Faraday Member
**
Karma: 131
Posts: 4670
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

To generate 38KHz it's better to use one of the timer/counters. I use timer 1 so that timer 0 is still available for supporting the micros() function. This is the code I use:

Code:
void setup()
{
  PORTB = 0;
  DDRB =  0b00000010; // set PB1 (= OCR1A) to be an output
}

// Set the frequency that we will get on pin OCR1A but don't turn it on
void setFrequency(uint16_t freq)
{
  uint32_t requiredDivisor = (F_CPU/2)/(uint32_t)freq;

  uint16_t prescalerVal = 1;
  uint8_t prescalerBits = 1;
  while ((requiredDivisor + prescalerVal/2)/prescalerVal > 256)
  {
    ++prescalerBits;
    prescalerVal <<= 1;
  }
  
  uint8_t top = ((requiredDivisor + (prescalerVal/2))/prescalerVal) - 1;
  TCCR1 = (1 << CTC1) | prescalerBits;
  GTCCR = 0;
  OCR1C = top;
}

// Turn the frequency on
void on()
{
  TCNT1 = 0;
  TCCR1 |= (1 << COM1A0);
}

// Turn the frequency off and turn off the IR LED.
// We let the counter continue running, we just turn off the OCR1A pin.
void off()
{
  TCCR1 &= ~(1 << COM1A0);
}
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. http://www.eschertech.com

Valencia, Spain
Offline Offline
Edison Member
*
Karma: 65
Posts: 2246
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe the "tone()" function will work, though I don't know its limitations...

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi!

Thanks for all your suggestions, I will try this.

Happy new year!

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi!

Thanks for the help!
I used the Timer that dc 42 suggested an it worked fine.
I added 1 ms delay in the loop function as below:

void loop() {
 on ();
delay (1);
off ();
delay (1);
}

It worked like a charm as a virtual wall for my Roomba robot.

Thanks again!
Logged

Pages: [1]   Go Up
Print
 
Jump to: