STM32 generates out-of-phase square waves

Publisher:zcyzwjLatest update time:2019-01-07 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

I have been working on the past questions of the Blue Bridge Cup Embedded Development these days. In the mock test of the seventh final, there is a requirement that PA6 and PA7 generate two square waves with different phases and adjustable frequency. Later, I studied the Output comare mode of STM32.


Because I want to adjust the frequency, I choose the toggle mode to make the square wave out of phase. Just set it in the interrupt. First, the diagram in the manual


That's an old picture.



Let's take a look at the interrupt function in the OCToggle mode of the official StdLib3.5


void TIM1_BRK_TIM9_IRQHandler(void)

{

  /* TIM9_CH1 toggling with frequency = 366.2 Hz */

  if (TIM_GetITStatus(TIM9, TIM_IT_CC1) != RESET)

  {

    TIM_ClearITPendingBit(TIM9, TIM_IT_CC1 );

    capture = TIM_GetCapture1(TIM9);

    TIM_SetCompare1(TIM9, capture + CCR1Val );

  }

 

  /* TIM9_CH2 toggling with frequency = 732.4 Hz */

  if (TIM_GetITStatus(TIM9, TIM_IT_CC2) != RESET)

  {

    TIM_ClearITPendingBit(TIM9, TIM_IT_CC2);

    capture = TIM_GetCapture2(TIM9);

    TIM_SetCompare2(TIM9, capture + CCR2Val);

  }

}


OCToggle basically works like this:

If TIMx_CNT = TIM_CCR1, then the above interrupt will occur. In the interrupt, it will add the value of CCR1 to the CCR1Val value you set and assign it to CCR1. Each time an interrupt occurs, the level will be flipped (toggle)


Because the frequencies of the two channels are the same, the interruptions of the two channels should also occur close together. 


So in order to maintain the phase difference between the two channels, when channel 1 is interrupted, add the corresponding value based on your CCR2Val value and assign it to CCR2. This is equivalent to delaying the timing of channel 2 for a while when channel 1 is in the middle.


Like this


void TIM3_IRQHandler(void)

{

 

  if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)

  {

    TIM_ClearITPendingBit(TIM3, TIM_IT_CC1 );

    capture = TIM_GetCapture1(TIM3);

    TIM_SetCompare1(TIM3, capture + CCR1Val );

TIM_SetCompare2(TIM3, capture +200);

  }

 

 

  if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)

  {

    TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);

    capture = TIM_GetCapture2(TIM3);

    TIM_SetCompare2(TIM3, capture + CCR2Val);

  }

}


I set CCR1Val and CCR2Val to 1000, and after adding 200, CH2 has a phase shift



The principle is that different initial values ​​can be assigned during initialization, so there is no need to open an interrupt for toggle


Keywords:STM32 Reference address:STM32 generates out-of-phase square waves

Previous article:STM32 serial port idle interrupt problem
Next article:Notes on using ST_Link with the STM32F103C8T6

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号