How to synchronize ATmega 328 Timer1, Timer2 and Timer3 when operating in PWM Correct Phase Mode and prescaler 1:1Using the Timer2 of a PIC32 to Set a DelayTimer to generate overflow every 4msWhy Arduino uses interrupt every 1.024ms in millis function?uC/OS-III Systick & Peripheral timer0 for PWM interfereHow can I avoid this off-by-one error when adding an offset to a prescaled hardware timer?AVR 8 bit timer - what to do when compare value doesn't fit in register?How to set frequency and duty cycle of Timer1 in 16bit P&F correct PWMErroneous Pulsetrain TimingGenerating 200khz pulse on Arduino Uno in normal modeSTM32 TIM+GPIO unexpected phase shift (HAL library)

How exactly does artificial gravity work?

Program which behaves differently in/out of a debugger

How can dragons propel their breath attacks to a long distance

Is taking modulus on both sides of an equation valid?

Can someone explain homicide-related death rates?

Stuck filament in the extruder of Infitary M508

Does SQL Server allow (make visible) DDL inside a transaction to the transaction prior to commit?

Jesus' words on the Jews

Was this character’s old age look CGI or make-up?

What is the largest number of identical satellites launched together?

How do I tell my supervisor that he is choosing poor replacements for me while I am on maternity leave?

Extracting sublists that contain similar elements

How can I answer high-school writing prompts without sounding weird and fake?

How to cope with regret and shame about not fully utilizing opportunities during PhD?

Anabelian geometry ~ higher category theory

Tikz draw contour without some edges, and fill

Earliest use of "rookie"?

Is Germany still exporting arms to countries involved in Yemen?

Why in the below sentence dative "dem" is used instead of nominative "der"?

How does emacs `shell-mode` know to prompt for sudo?

What is the best way for a skeleton to impersonate human without using magic?

What are the components of a legend (in the sense of a tale, not a figure legend)?

Non-deterministic Finite Automata | Sipser Example 1.16

How do I extract the X and Y coordinates from a .gpx file using QGIS?



How to synchronize ATmega 328 Timer1, Timer2 and Timer3 when operating in PWM Correct Phase Mode and prescaler 1:1


Using the Timer2 of a PIC32 to Set a DelayTimer to generate overflow every 4msWhy Arduino uses interrupt every 1.024ms in millis function?uC/OS-III Systick & Peripheral timer0 for PWM interfereHow can I avoid this off-by-one error when adding an offset to a prescaled hardware timer?AVR 8 bit timer - what to do when compare value doesn't fit in register?How to set frequency and duty cycle of Timer1 in 16bit P&F correct PWMErroneous Pulsetrain TimingGenerating 200khz pulse on Arduino Uno in normal modeSTM32 TIM+GPIO unexpected phase shift (HAL library)






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















The goal of my application is to use an ATMEGA328P as the core of a Digital Direct Synthesis 400 Hertz sinusoidal generator. Pulses from the microprocessor are sent to the gates of four high-voltage N-MOSFET connected as a full H bridge. 16-bitTimer1 uses WGM mode 10 and commands the Northeast and Northwest MOSFET. 8-bitTimer0 and 8-bitTimer2 use mode 5 and command the Southeast and Southwest MOSFET respectively. I was able to generate the pulses with the correct widths but I failed to get them correctly synchronized. There is one or two clock slippage between the Timer1 pulses and the corresponding ones from Timer0 and Timer2.



I had no success when tried to compensate this delay by setting Timer0 and Timer2 initial counts.



#define CPU_FREQ 16000000UL
#define OUT_FREQ 400UL
#define SAMPLE_FREQ 40000UL
#define SAMPLE_COUNT SAMPLE_FREQ/OUT_FREQ/2
#define SAMPLE_MAX CPU_FREQ/SAMPLE_FREQ/2 - 1
#define TIMER0_MAX 255
#define TIMER2_MAX 255
#define TIMER0_DELAY 1
#define TIMER2_DELAY 1

void setupTimers() bit( PSRSYNC);
setupTimer0();
setupTimer1();
setupTimer2();
sei(); // enable global interrupts;
GTCCR = 0;


void setupTimer0()
// Waveform Generation Mode 5, PWM, Phase Correct
// TOP = OCR0A, OCR1B updated at TOP, TOV1 Flag set on BOTTOM
// Set OC0B on Compare Match when up-counting.
// Clear OC0B on Compare Match when down-counting
// Clock Source Mode 1, No prescaling
// OCIE0B, OCIE0A, TOIE0 interrupts disaabled
// No Force Output Compare A or B
// TCNT0 initial count compensation for OCR0A as TOP delay

TCCR0A = bit(COM0B1)

void setupTimer1() bit(COM1B1)

void setupTimer2()
// Waveform Generation Mode 5, PWM, Phase Correct
// TOP = OCR0A, OCR1B updated at TOP, TOV1 Flag set on BOTTOM
// Set OC0B on Compare Match when up-counting.
// Clear OC0B on Compare Match when down-counting
// Clock Source Mode 1, No prescaling
// OCIE0B, OCIE0A, TOIE0 interrupts disaabled
// No Force Output Compare A or B
// TCNT2 initial count compensation for OCR2A as TOP delay

TCCR2A = bit(COM2B1)


Expected results: synchronized pulses
Actual results: non synchronized pulses










share|improve this question






























    0















    The goal of my application is to use an ATMEGA328P as the core of a Digital Direct Synthesis 400 Hertz sinusoidal generator. Pulses from the microprocessor are sent to the gates of four high-voltage N-MOSFET connected as a full H bridge. 16-bitTimer1 uses WGM mode 10 and commands the Northeast and Northwest MOSFET. 8-bitTimer0 and 8-bitTimer2 use mode 5 and command the Southeast and Southwest MOSFET respectively. I was able to generate the pulses with the correct widths but I failed to get them correctly synchronized. There is one or two clock slippage between the Timer1 pulses and the corresponding ones from Timer0 and Timer2.



    I had no success when tried to compensate this delay by setting Timer0 and Timer2 initial counts.



    #define CPU_FREQ 16000000UL
    #define OUT_FREQ 400UL
    #define SAMPLE_FREQ 40000UL
    #define SAMPLE_COUNT SAMPLE_FREQ/OUT_FREQ/2
    #define SAMPLE_MAX CPU_FREQ/SAMPLE_FREQ/2 - 1
    #define TIMER0_MAX 255
    #define TIMER2_MAX 255
    #define TIMER0_DELAY 1
    #define TIMER2_DELAY 1

    void setupTimers() bit( PSRSYNC);
    setupTimer0();
    setupTimer1();
    setupTimer2();
    sei(); // enable global interrupts;
    GTCCR = 0;


    void setupTimer0()
    // Waveform Generation Mode 5, PWM, Phase Correct
    // TOP = OCR0A, OCR1B updated at TOP, TOV1 Flag set on BOTTOM
    // Set OC0B on Compare Match when up-counting.
    // Clear OC0B on Compare Match when down-counting
    // Clock Source Mode 1, No prescaling
    // OCIE0B, OCIE0A, TOIE0 interrupts disaabled
    // No Force Output Compare A or B
    // TCNT0 initial count compensation for OCR0A as TOP delay

    TCCR0A = bit(COM0B1)

    void setupTimer1() bit(COM1B1)

    void setupTimer2()
    // Waveform Generation Mode 5, PWM, Phase Correct
    // TOP = OCR0A, OCR1B updated at TOP, TOV1 Flag set on BOTTOM
    // Set OC0B on Compare Match when up-counting.
    // Clear OC0B on Compare Match when down-counting
    // Clock Source Mode 1, No prescaling
    // OCIE0B, OCIE0A, TOIE0 interrupts disaabled
    // No Force Output Compare A or B
    // TCNT2 initial count compensation for OCR2A as TOP delay

    TCCR2A = bit(COM2B1)


    Expected results: synchronized pulses
    Actual results: non synchronized pulses










    share|improve this question


























      0












      0








      0








      The goal of my application is to use an ATMEGA328P as the core of a Digital Direct Synthesis 400 Hertz sinusoidal generator. Pulses from the microprocessor are sent to the gates of four high-voltage N-MOSFET connected as a full H bridge. 16-bitTimer1 uses WGM mode 10 and commands the Northeast and Northwest MOSFET. 8-bitTimer0 and 8-bitTimer2 use mode 5 and command the Southeast and Southwest MOSFET respectively. I was able to generate the pulses with the correct widths but I failed to get them correctly synchronized. There is one or two clock slippage between the Timer1 pulses and the corresponding ones from Timer0 and Timer2.



      I had no success when tried to compensate this delay by setting Timer0 and Timer2 initial counts.



      #define CPU_FREQ 16000000UL
      #define OUT_FREQ 400UL
      #define SAMPLE_FREQ 40000UL
      #define SAMPLE_COUNT SAMPLE_FREQ/OUT_FREQ/2
      #define SAMPLE_MAX CPU_FREQ/SAMPLE_FREQ/2 - 1
      #define TIMER0_MAX 255
      #define TIMER2_MAX 255
      #define TIMER0_DELAY 1
      #define TIMER2_DELAY 1

      void setupTimers() bit( PSRSYNC);
      setupTimer0();
      setupTimer1();
      setupTimer2();
      sei(); // enable global interrupts;
      GTCCR = 0;


      void setupTimer0()
      // Waveform Generation Mode 5, PWM, Phase Correct
      // TOP = OCR0A, OCR1B updated at TOP, TOV1 Flag set on BOTTOM
      // Set OC0B on Compare Match when up-counting.
      // Clear OC0B on Compare Match when down-counting
      // Clock Source Mode 1, No prescaling
      // OCIE0B, OCIE0A, TOIE0 interrupts disaabled
      // No Force Output Compare A or B
      // TCNT0 initial count compensation for OCR0A as TOP delay

      TCCR0A = bit(COM0B1)

      void setupTimer1() bit(COM1B1)

      void setupTimer2()
      // Waveform Generation Mode 5, PWM, Phase Correct
      // TOP = OCR0A, OCR1B updated at TOP, TOV1 Flag set on BOTTOM
      // Set OC0B on Compare Match when up-counting.
      // Clear OC0B on Compare Match when down-counting
      // Clock Source Mode 1, No prescaling
      // OCIE0B, OCIE0A, TOIE0 interrupts disaabled
      // No Force Output Compare A or B
      // TCNT2 initial count compensation for OCR2A as TOP delay

      TCCR2A = bit(COM2B1)


      Expected results: synchronized pulses
      Actual results: non synchronized pulses










      share|improve this question
















      The goal of my application is to use an ATMEGA328P as the core of a Digital Direct Synthesis 400 Hertz sinusoidal generator. Pulses from the microprocessor are sent to the gates of four high-voltage N-MOSFET connected as a full H bridge. 16-bitTimer1 uses WGM mode 10 and commands the Northeast and Northwest MOSFET. 8-bitTimer0 and 8-bitTimer2 use mode 5 and command the Southeast and Southwest MOSFET respectively. I was able to generate the pulses with the correct widths but I failed to get them correctly synchronized. There is one or two clock slippage between the Timer1 pulses and the corresponding ones from Timer0 and Timer2.



      I had no success when tried to compensate this delay by setting Timer0 and Timer2 initial counts.



      #define CPU_FREQ 16000000UL
      #define OUT_FREQ 400UL
      #define SAMPLE_FREQ 40000UL
      #define SAMPLE_COUNT SAMPLE_FREQ/OUT_FREQ/2
      #define SAMPLE_MAX CPU_FREQ/SAMPLE_FREQ/2 - 1
      #define TIMER0_MAX 255
      #define TIMER2_MAX 255
      #define TIMER0_DELAY 1
      #define TIMER2_DELAY 1

      void setupTimers() bit( PSRSYNC);
      setupTimer0();
      setupTimer1();
      setupTimer2();
      sei(); // enable global interrupts;
      GTCCR = 0;


      void setupTimer0()
      // Waveform Generation Mode 5, PWM, Phase Correct
      // TOP = OCR0A, OCR1B updated at TOP, TOV1 Flag set on BOTTOM
      // Set OC0B on Compare Match when up-counting.
      // Clear OC0B on Compare Match when down-counting
      // Clock Source Mode 1, No prescaling
      // OCIE0B, OCIE0A, TOIE0 interrupts disaabled
      // No Force Output Compare A or B
      // TCNT0 initial count compensation for OCR0A as TOP delay

      TCCR0A = bit(COM0B1)

      void setupTimer1() bit(COM1B1)

      void setupTimer2()
      // Waveform Generation Mode 5, PWM, Phase Correct
      // TOP = OCR0A, OCR1B updated at TOP, TOV1 Flag set on BOTTOM
      // Set OC0B on Compare Match when up-counting.
      // Clear OC0B on Compare Match when down-counting
      // Clock Source Mode 1, No prescaling
      // OCIE0B, OCIE0A, TOIE0 interrupts disaabled
      // No Force Output Compare A or B
      // TCNT2 initial count compensation for OCR2A as TOP delay

      TCCR2A = bit(COM2B1)


      Expected results: synchronized pulses
      Actual results: non synchronized pulses







      c++ timer synchronization arduino-uno






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 23 at 17:37







      santosgcot

















      asked Mar 23 at 13:02









      santosgcotsantosgcot

      11




      11






















          1 Answer
          1






          active

          oldest

          votes


















          0














          By trial and error I managed to find a combination of TIMER0_DELAY, TIMER1_DELAY and TIMER2_DELAY that solved my problem.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55313992%2fhow-to-synchronize-atmega-328-timer1-timer2-and-timer3-when-operating-in-pwm-co%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            By trial and error I managed to find a combination of TIMER0_DELAY, TIMER1_DELAY and TIMER2_DELAY that solved my problem.






            share|improve this answer



























              0














              By trial and error I managed to find a combination of TIMER0_DELAY, TIMER1_DELAY and TIMER2_DELAY that solved my problem.






              share|improve this answer

























                0












                0








                0







                By trial and error I managed to find a combination of TIMER0_DELAY, TIMER1_DELAY and TIMER2_DELAY that solved my problem.






                share|improve this answer













                By trial and error I managed to find a combination of TIMER0_DELAY, TIMER1_DELAY and TIMER2_DELAY that solved my problem.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 23 at 21:14









                santosgcotsantosgcot

                11




                11





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55313992%2fhow-to-synchronize-atmega-328-timer1-timer2-and-timer3-when-operating-in-pwm-co%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Obelisk of Theodosius Contents History Description Notes Bibliography Further reading External links Navigation menuAge of spirituality : late antique and early Christian art, third to seventh centuryOver 60 picturesObelisks of the World41°00′21.24″N 28°58′31.43″E / 41.0059000°N 28.9753972°E / 41.0059000; 28.97539727724550-7235741376235741376

                    밀양 대씨 역사 각주 함께 보기 둘러보기 메뉴밀양 대씨

                    1973년 목차 사건 문화 탄생 사망 노벨상 달력 둘러보기 메뉴