STM32 Timers#7 Timer synchronization || Slave Reset mode

Started by tha, February 07, 2023, 08:23:22 AM

Previous topic - Next topic

tha

https://controllerstech.com/stm32-timers-7-timer-synchronization-slave-reset-mode/

ขอแปลหน่อยนะครับ ขอขอบคุณเขามา ณ ที่นี้ด้วย ท่านใดพอจะมีทรัพย์ก็ Donate เขาด้วยนะ ฟังภาษาอังกฤษเขาในยูทูบก็ฟังง่ายดี(เปิดคำบรรยายด้วย)ครับ

Timer synchronization || Slave Reset mode

นี่คือ the 7th tutorial ใน the STM32 Timer series, และวันนี้เราจะมาดูวิธีใช้ the slave Reset mode. นี่เป็นอีกหนึ่งบทช่วยสอนที่ครอบคลุม the timer synchronization และวันนี้เราจะมาดูวิธีซิงโครไนซ์ the timers โดยใช้ the reset mode.

ฉันได้กล่าวถึง the slave trigger mode แล้ว, ซึ่ง the Update Event (UEV) ที่สร้างโดย the master ถูกใช้เพื่อกลับมาทำงานต่อ the counter of the slave timer. Reset mode ก็ทำงานในลักษณะเดียวกันด้วย, ยกเว้น the UEV จะรีเซ็ต the counter of the slave timer.

tha

CubeMX Setup

ก่อนอื่นมาดู the clock setup ก่อน



ที่นี่ ฉันได้กำหนดค่า the external crystal, ซึ่งคือ 8 MHz, เพื่อจัดให้มี the clock, เพื่อให้ระบบรันที่ 90 MHz. นี้เพื่อให้แน่ใจว่าทั้งสอง the APB timer clocks มี the same speeds และดังนั้นทุก the timers จะมี the same base clock.

tha

The Timer Config

TIM2 จะทำงานเป็น the master timer และ TIM3 จะเป็น the slave timer. เมื่อไหร่ที่ the master counter, TIM2, เกิด overflow, มันจะปล่อยออก an UEV signal ซึ่งจะรีเซ็ต the counter of the TIM3.

ที่นี่ผมได้เปิดใช้งาน the PWM บน channel 1 สำหรับทั้งสอง the timers เพื่อให้เราสามารถเห็น output ของพวกมัน.



     •  ทั้งสอง the timers ถูกให้สัญญานนาฬิกาโดย the APB clock, ดังนั้น the base frequency คือ 90 MHz. สิ่งนี้จะลดลงอีกโดยใช้
         the ARR.
          •  TIM2 จะรันที่ 90MHz/10000 = 9KHz ที่มี the duty cycle of 0.4%
          •  TIM3 จะรันที่ 90MHz/1234 = 73KHz ที่มี duty cycle of 24%
     •  TIM2 เป็น the master timer, ดังนั้น the trigger event selection จะถูกเซ็ตเป็น Update Event (UEV). TIM2 จะปล่อย
         ออก UEV signal นี้เมื่อ the counter เกิด overflows.
     •  TIM3 เป็น the slave ของ the TIM2 และกำลังถูกใช้ใน the RESET Mode. The trigger source คือ ITR1.

โดยทั่วไปใน the RESET MODE, เมื่อไหร่ที่ the TIM3 ได้รับ the UEV signal จาก TIM2, the TIM3's counter จะรีเซ็ตกลับไปเป็น 0. เราจะเห็นสิ่งนี้เกิดขึ้นใน the working section.


tha

Working

The code



ที่นี่ใน the main function เราจะสตาร์ทง่ายๆ the PWM สำหรับทั้งสอง the timers.

tha

The output

ข้างล่างคือ the PWM signals ที่สร้างโดยทั้งสอง the timers.



ในภาพด้านบน คุณจะเห็น the TIM2 PWM signal มี the frequency of 9KHz และ the Duty of 0.4%. ในทำนองเดียวกัน TIM3 PWM มี the frequency of 73KHz และ the Duty of 24%.
นี่เป็นตามการตั้งค่าที่เราทำกับ the Timers.

หากเราวิเคราะห์ the TIM3 PWM signal ให้ละเอียดยิ่งขึ้น จะเห็นได้ว่า the signal ยังคง HIGH ในช่วง 3.33us. เป็นเช่นนี้เสมอ ยกเว้น ณ จุดที่ the TIM2's counter เกิด overflows. สิ่งนี้ถูกแสดงไว้ข้างล่าง.



ที่จุดที่ the TIM2's counter เกิด overflows และสร้าง the UEV, the TIM3's signal ยังคง high เป็นเวลา 4.75us.

มาทำความเข้าใจว่าอะไรเกิดขึ้นจริงๆที่จุดนี้



     •  ที่นี่คุณจะเห็น the TIM3 counter นับขึ้นจาก 0 และในระหว่างนั้นได้รับ the trigger signal จาก the TIM2.
     •  ที่จุดนี้ the counter รีเซ็ตกลับไปเป็น 0 และสตาร์ทการนับขึ้นอีกครั้ง.
     •  นั่นเป็นสาเหตุที่สัญญาณยังคงเป็น HIGH เป็นเวลา the additional time.

หมายเหตุ:- รูปภาพด้านบนถือว่า the CCR(Pulse) value ถูกเซ็ตเป็น 4, ดังนั้น the signal ยังคง high จนกระทั่ง the counter ถึง the value 4.

หากเราวัดความแตกต่างของเวลาระหว่างเวลาที่ the UEV ถูกปล่อยออกและเวลาที่ the TIM3 pulse ไปเป็น LOW, มันจะเท่ากับ the pulse width ของ the signal โดยประมาณ.