Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 7 of 7

Thread: AC Mains Cycle Phase Control

  1. #1
    Senior Member
    Join Date
    Feb 2017
    Posts
    247

    AC Mains Cycle Phase Control

    Hi. Iím wondering if itís possible to do 60 Hz AC mains phase control (i.e. incandescent light dimming) using the T3.x FlexTimer Module (FTM) without direct software intervention at the per-cycle time scale.

    Iíve scanned the FTM section in the datasheet and see that it has lots of different modes that Iíll go back and study in detail. But, if someone can say for certain that it CANíT do what Iím proposing, then Iíll save the effort for now.

    My basic idea is to start the FTM counting (from 0) when the AC cycle zero crossing is detected. The match value (CnV) and terminal count (MOD) will have already been set up by software based on the amount of dimming desired. When the FTM counter reaches CnV, an output pulse will be begun to trigger the triacís gate at the desired phase of the AC cycle. Shortly thereafter, MOD count will be reached, and the timer will overflow. This will terminate the output pulse so that the triacís gate is not asserted for when the current half cycle ends.

    Key to this idea is being able to sync the start of FTM counting to the AC cycle zero crossing and to ensure that counting does not start again after overflow until the next crossing occurs.

    As an alternative, Iím wonder if I could sync one of the PWM modes to the AC cycle zero crossing and adjust the PWM output characteristics to get the same behavior.

    So, does anyone know for sure that this is not possible?

    Thanks.

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,258
    Sounds like it could probably work, restarting the FTM when you detect the zero cross.

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,258
    You might also consider just doing it all by software. Here's a project I did 4 years ago, which ran on Teensy 2.0, controlling 4 dimmers using AC zero cross detection and just computing everything in terms of microseconds.

    https://www.pjrc.com/embrace-heart-lighting/

    About half way down the page is a video of it fading the bulbs.

  4. #4
    Senior Member
    Join Date
    Feb 2017
    Posts
    247
    Hi Paul, thanks for the reply. Yes, Iím sure T3.x is more than capable of doing this in software. Iím pursuing the hardware technique as somewhat of a learning exercise.

    Do you know if FTM can be operated in the mode I outlined? Mainly:

    * Start counting when triggered by external pulse.

    * Stop counting after overflow and donít start again until the next trigger.

  5. #5
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,258
    Quote Originally Posted by gfvalvo View Post
    * Start counting when triggered by external pulse.
    Maybe, but I have not ever tried this. I would use an interrupt and have the interrupt code configure the timer.

    The FTM timers do have special fault inputs. I've never used them, so I really can't suggest anything more specific than reading the FTM chapter carefully.


    * Stop counting after overflow and don’t start again until the next trigger.
    Again, I've almost always used the FTM timers in free running mode, so really not sure about this.

    Personally, I would not do things this way, starting and stopping the timer. I would configure it to count from 0 to 0xFFFF and roll over. Then I'd use 1 channel as input capture, to capture the free-running count at the zero cross detection. Then I'd use another channel as output compare, to generate the pulse at a desired time in the future... where the input capture interrupt programs the output compare channel. Then I'd have that channel interrupt, and in that ISR I'd reprogram it to again change the pin in the future, for the other edge of the pulse. This way does involve interrupts for every event, but the hardware does the actual work precisely aligned to the timer count and the interrupts clean up after each event and program what happen at the next event.

  6. #6
    Senior Member
    Join Date
    Feb 2017
    Posts
    247
    Thanks for the suggestion, Paul.

    I do see at least one advantage to doing it your way - I could use the FTM counter value captured at every zero crossing to determine the half-cycle period of the AC line in units of counter ticks. That would make it easy to scale the desired brightness into counts for the output compare event. This way I would not have to determine a priori the number of counts that occur during a half cycle. It would also automatically account for drift and unit-to-unit variation of the T3.x’s clock rate.

  7. #7
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    2,187
    Quote Originally Posted by PaulStoffregen View Post
    Personally, I would not do things this way, starting and stopping the timer. I would configure it to count from 0 to 0xFFFF and roll over. Then I'd use 1 channel as input capture, to capture the free-running count at the zero cross detection. Then I'd use another channel as output compare, to generate the pulse at a desired time in the future... where the input capture interrupt programs the output compare channel. Then I'd have that channel interrupt, and in that ISR I'd reprogram it to again change the pin in the future, for the other edge of the pulse. This way does involve interrupts for every event, but the hardware does the actual work precisely aligned to the timer count and the interrupts clean up after each event and program what happen at the next event.
    Sounds like the most reliable solution to me.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •