1

I want to use timer 0 to generate a rectangular signal on OC0A (D11) without the use of Arduino specific functions (that use timers for themselves). When I toggle D11 manually using a loop, I hear a sound in the connected piezo, but with my sample code, I don't hear something:

#define ALARM_PIN 11

void setup() {
  digitalWrite(ALARM_PIN, LOW);
  pinMode(ALARM_PIN, OUTPUT);
  noInterrupts();

  OCR0A = 60;
  // Toggle OC0A on compare match
  TCCR0A = (0 << COM0A1)
         | (1 << COM0A0)
  // CTC mode
         | (1 << WGM01)
         | (0 << WGM00);
  // prescaler = 256
  TCCR0B = (0 << WGM02)
         | (1 << CS02);
}

void loop() {
}

Any idea, what could be wrong?

Update

I've played with it a little bit more:

#define LED_PIN 9

void setup() {
  pinMode(LED_PIN, OUTPUT);
  TCCR0B = (1 << CS02);
}

void loop() {
  digitalWrite(LED_PIN, TCNT0 > 250);
}

This causes my LED to light very dim, so the timer is running up to 255. But as soon as I set TCCR0A, nothing is lighting up:

#define LED_PIN 9

void setup() {
  pinMode(LED_PIN, OUTPUT);
  OCR0A = 60;
  TCCR0A = (1 << WGM01);
  TCCR0B = (1 << CS02);
}

void loop() {
  digitalWrite(LED_PIN, TCNT0 > 0);
}
1
  • What happens when you connect an led to pin 11? What happens if you flip the polarity of the led around? (So pin11--resistor--led--ground and pin11--resistor--led--vcc). If pwm is working then the led should be lid either way.
    – Gerben
    Commented Jul 21, 2014 at 11:40

2 Answers 2

2

You should re-enable interrupts after you finish setting up. Use either

interrupts(); 

or

sei();

If you want to don't want to use the arduino specific commands. Even if the code does not need interrupts, background code does use them.

Timer 0 is used in arduino for some basic "house keeping" function like keeping time. I'm not sure if that could be part of your problem. you could try using a different timer.

3
  • Thanks for the answer, but I don't need the interrupt, the port pin should be toggled automatically on compare match.
    – Thomas S.
    Commented Jul 20, 2014 at 17:49
  • You are correct. I thought at the time that the bits would also control the compare match output, in any case you might not want to leave interrupts off. Commented Jul 20, 2014 at 18:20
  • 1
    Enabling interrupts at the end of setup() using sei(); does not make any difference.
    – Thomas S.
    Commented Jul 20, 2014 at 18:36
0

Your value of 60 for OCRA will give you tone of around 16666hz which isn't audible to us humans. Try setting it to 255. That should give you a 4000hz tone, which is audible. (Or get your neighbors dog, and see if he dislikes hearing it)

You can use this online calculator to help you get the correct value for OCRA.

Note that you need 2 cycles to get a full wavelength (1 low and one high), so two toggles.

3
  • 16MHz + 256 prescaler and the OCRA set to 60 should give me ~500Hz. I've tried different values, even your 255 but without success.
    – Thomas S.
    Commented Jul 20, 2014 at 18:31
  • 1
    A decent-sized portion of the population can hear 17kHz sounds. 19kHz would be a bit of a stretch though. Commented Jul 20, 2014 at 20:28
  • My mistake. I don't know where I got that 8-prescaler from.
    – Gerben
    Commented Jul 21, 2014 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.