~38KHz output Timer1 (ATmega48)

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi there,

I am using the ATmega48PA. My application uses a lot of interrupt vectors. I am also using the Timer1 OC1A pin to output a square wave to control an high constant current circuit that drives an IR LED. I have already read the specific ATMEL ApNote, but it used a 455KHz resonator. In my design I am using an 8MHz crystal.

In continuous of this Topic:

https://www.avrfreaks.net/index.p...

would you be able to tell me an automatic way to toggle the OC1A output?

Which is the right way to produce a 50% Square wave, at ~38KHz, but without Interrupt.

In PWM mode, the Timer must count 210 pulses (8MHz / 38KHz = ~210, without prescaller) and the OCR1A register must contain the value of 149 (or 150) in order to have a 50% Duty Cycle, but I need to use the OVF interrupt in order to reload the TCNT1 register with 255-210 = 45. That way I have an output jitter even if I se all other interrupts as nested interrupts.

Is there any other way (I don't know) to do this without any interrupt? This is becaue I have

Thank you.

Michael.

User of:
IAR Embedded Workbench C/C++ Compiler
Altium Designer

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

CTC mode and enable toggle for the output.

//TIMER1 initialize - prescale:1
// WGM: 4) CTC, TOP=OCRnA
// desired value: 13.8uSec
// actual value: 13.750uSec (0.4%)
void timer1_init(void)
{
 TCCR1B = 0x00; //stop
 TCNT1H = 0xFF; //setup
 TCNT1L = 0x93;
 OCR1AH = 0x00;
 OCR1AL = 0x6D;
 OCR1BH = 0x00;
 OCR1BL = 0x6D;
 ICR1H  = 0x00;
 ICR1L  = 0x6D;
 TCCR1A = 0x40;
 TCCR1B = 0x09; //start Timer
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

THANK YOUuuuuuuuuuuuuuuuuuu...........................

Michael.

User of:
IAR Embedded Workbench C/C++ Compiler
Altium Designer

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Excuse me. Does this CTC mode code that you gave me outputs anything in the real world (I mean a pin output, OC1A let's say), or I have to enable the specific interrupt in order to manually toggle the digital pin in the ISR?

This is very critical for me, because if an interrupt is used AND because other interrupts are used (in the same application), this will have the effect of a jitter out the output (even if all other interrupts are used as nested interrupts).

Michael.

User of:
IAR Embedded Workbench C/C++ Compiler
Altium Designer

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ah....OK sory I show it in the datasheet.

YES...IT AUTOMATIC TOGGLES THE OCnA pin.

Many thanks for your help.

Michael.

User of:
IAR Embedded Workbench C/C++ Compiler
Altium Designer

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can thank the appbuilder in the Imagecraft compiler for that bit of code.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ha ha ha ha..........

Thank you appbuilder.

Michael.

User of:
IAR Embedded Workbench C/C++ Compiler
Altium Designer

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

CTC mode is perfect for you if you want to drive the IR led with 50% duty.

However, you can get better if you use smaller duty like 33% or 25% and use larger current.

The timer can do this automatically as well, I think it is called Fast PWM mode. Anyway, you set the timer to count only up to 8M/38k=210 which sets the period, and set the width to be 210/4=52.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you Japael.

The CTC mode works perfect. I have already finished with the IR transmitter.

Michael.

User of:
IAR Embedded Workbench C/C++ Compiler
Altium Designer