Bardia Shahrestani
Bardia Shahrestani

Reputation: 9

How do I implement a timer which turn a signal on and off (PWG) every few seconds on an Atmega324a microcontroller?

I'm programming an Atmega324a Microcontroller and I'm trying to implement a timer (in this case Timer1) which supposed to make a second led connected to my board blink. I also need to know how to identify the pin the led is attached to I've found the data sheet: http://ww1.microchip.com/downloads/en/DeviceDoc/ATmega164A_PA-324A_PA-644A_PA-1284_P_Data-Sheet-40002070A.pdf but the details are too technical for me to understand and I don't know where to start looking and most importantly, get to the result, which is the code itself.

Also, What does the ISR function do?

Down below is the current Init_timer function for Timer 0. Is it possible for me to enable both timers at the same time?

static void init_timer(void)
{
    // Configure Timer0 for CTC mode, 64x prescaler for 1 ms interval
    TCCR0A = _BV(WGM01);
    TCCR0B = _BV(CS01) | _BV(CS00);
    OCR0A = 124;
    TIMSK0 = _BV(OCIE0A);
}

int main(void){
    MCUSR = 0;
    wdt_disable();

    init_pins();        // Reset all pins to default state
    init_timer();       // Initialize 1 msec timer interrupt

    configure_as_output(LOAD_ON);
    configure_as_output(LED1);
    configure_as_output(LED2);

    sei();
        .
        .
        .
}

ISR(TIMER0_COMPA_vect)
{
    static uint16_t ms_count = 0;

    ms_count++;             // milliseconds counter

    if (ms_count == TMP107_POLL_PERIOD)
    {
        tmp107_command();   // send command to temperature sensor
        toggle(LED1);       // blink status led

        ms_count = 0;
    }
}

Upvotes: 0

Views: 208

Answers (1)

the busybee
the busybee

Reputation: 12673

First of all: StackOverflow is a site to ask questions around source code, it is not a service delivering solutions. Please take the tour, it will help you to get satisfactory answers.

But nevermind, because you're new:

For example, you can implement a timer for a pulse width generator in these steps:

  1. Learn to read data sheets. Nobody can relieve you of this burden.
  2. Learn how to use output pins.
  3. Write some tests to make sure you understand output pins.
  4. Select a timer to measure the clock cycles. Apparently you did that already.
  5. Learn to use this timer. Some timers can generate PWM (pulse width modulated) signals in hardware. However, the output pin is likely to be in a fixed location and the range of possible periods may not meet your requirements.
  6. Write some tests to make sure you understand timers and interrupts.
  7. If the required pulse period is too long for the timer, you can add an extra variable to scale down, for example.
  8. Implement the rest of it.

Also, What does the ISR function do?

This function is called "magically" by hardware when the conditions for the interrupt are met. In the case shown, tmp107_command() and toggle(LED1) are called only every TMP107_POLL_PERIOD times.

Is it possible for me to enable both timers at the same time?

Sure.

Upvotes: 1

Related Questions