STM32 Timers#6 Timer sync || Generate 3 Phase PWM

Started by tha, February 06, 2023, 05:58:42 AM

Previous topic - Next topic

tha

https://controllerstech.com/stm32-timers-6-timer-synchronization-generate-3-phase-pwm/

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

How to Generate 3 Phase PWM

นี้คือ 6th tutorial ใน the STM32 Timer series, และวันนี้เราจะกล่าวถึง Timer synchronization feature อีกแบบหนึ่งซึ่งเราจะสร้าง a 3 phase PWM waveform.

ในบทช่วยสอนก่อนหน้านี้เราได้เห็นวิธีที่ the Trigger mode สามารถถูกใช้เพื่อสตาร์ท the counter ของ the slave timer ได้อย่างไร. The slave counter จะสตาร์ทก็ต่อเมื่อ the counter of the master timer เกิด overflows. แต่สมมติว่าสถานะการณ์ที่เราต้องการสตาร์ท the slave counter เมื่อ the master counter มีค่าถึงค่าหนึ่ง. นี่เป็นความต้องการพื้นฐานสำหรับการสร้าง a 3 phase or 2 phase PWM, และเราไม่สามารถบรรลุได้ด้วยวิธีการก่อนหน้านี้ที่เราใช้.

ดังนั้นวันนี้ในบทช่วยสอนนี้ เราจะมาดูกันว่าเราจะควบคุมการสตาร์ทของ the slave counter ได้อย่างไรโดยการเซ็ตขอบเขตสำหรับ the master counter.

tha

Setup

ข้างล่างคือ the clock setup สำหรับ the project.



ที่นี่ทั้งสอง the APB Timer clocks รันที่ 90 MHz. เราจะใช้ the timers 1, 2 and 3 เพื่อสร้าง the 3 PWM signals. ที่นี่ TIM1 ถูกต่อกับ the APB2 bus และ TIM2 and 3 ถูกต่อกับ the APB1 bus. ผมจะเก็บ the clock ให้เท่ากันสำหรับ buses เหล่านี้ดังนั้นเราสามารถใช้การกำหนดค่าเดียวกันสำหรับ the timers.

The internal Trigger system สำหรับ Timers เหล่านี้ถูกแสดงไว้ข้างล่าง



ที่นี่ คุณจะเห็น the TIM2 เป็น a slave สามารถถูกควบคุมโดย the master TIM1 โดยใช้ the ITR0 signal. ในทำนองเดียวกัน, the TIM3 สามารถถูกควบคุมโดย the TIM2 โดยใช้ the ITR1 signal.

เราจะใช้สัญญานเหล่านี้เพื่อทริก the counter ของ the slave Timers, เมื่อ the master counter ถึงค่าที่กำหนดไว้ล่วงหน้า.



     •  โดยพื้นฐานแล้ว the TIM1 จะสร้าง a PWM signal บน channel 1
     •  เมื่อ the counter 1 จะถึง 33% ของ the ARR value, TIM1 Output Compare บน channel 2 จะไปเป็น high.
     •  นี้จะทริก the ITR0 signal, และ TIM2's counter จะสตาร์ทในตอนนั้น.
     •  ในทำนองเดียวกัน, เมื่อ the counter 2 จะถึง 33% ของ the ARR value, TIM2 Output compare บน channel 2 จะไปเป็น
         high และจะทริก the ITR1 signal.
     •  TIM3 counter จะสตาร์ทในตอนนั้น.

tha

TIMER Setup

ข้างล่างคือการตั้งค่าสำหรับสาม Timers ทั้งหมด



     •  ที่นี่ the TIM1 เป็น the master สำหรับ TIM2, ดังนั้นไม่มี slave mode สำหรับมัน.
     •  TIM2 เป็น the slave สำหรับ TIM1, ซึ่งสามารถถูกทริกโดย the ITR0 signal. มันยังเป็น the master สำหรับ TIM3 ด้วย.
     •  TIM3 เป็น the slave สำหรับ TIM2 เท่านั้น, ซึ่งสามารถถูกทริกโดย the ITR1.

ทุก the timers มี the channel 1 ที่เซ็ตเป็น the PWM output, และ channel 2 เป็น the Output compare (ยกเว้น TIM3 เนื่องจากมันไม่ใช่ the master).
The output compare signal ถูกใช้เป็น the trigger source สำหรับ the next Timer ในสาย.

เนื่องจากทั้งสอง the APB Timer clocks จะรันที่ the same 90MHz frequency, ผมเลยใช้การกำหนดค่าที่คล้ายกันสำหรับสาม timers ทั้งหมด.
ที่นี่ the timers จะรันที่ 100 Hz frequency ที่มี the PWM duty เป็น 40%.
คุณสามารถเช็ค the PWM tutorial ถ้าคุณไม่เข้าใจสิ่งนี้ https://controllerstech.com/pwm-in-stm32/

The pulse value (AKA CCR value) สำหรับ the output compare channel จะถูกเซ็ตไว้ที่ 3333. นี้โดยพื้นฐานคือ the 33% ของ the ARR value, ซึ่งคือ 9999.


tha

The Pinout

The pinout ถูกแสดงไว้ข้างล่าง



3 pins เหล่านี้โดยพื้นฐานเป็น the PWM pins จากแต่ละ timer. ผมได้ต่อพวกมันเข้ากับ the 3 channels ของ the Logic Analyzer.

tha

Some insight into the code

ไม่มีอะไรต้องทำมากใน the coding part. เนื่องจาากเรากำลังใช้ the PWM และ output compare, เราจะสตาร์ทง่ายๆทั้งสองฟังชั่นสำหรับ the timers.



ที่นี่ผมได้สตาร์ท the PWM สำหรับ channel 1 และ Output Compare สำหรับ channel 2 สำหรับ TIM1 และ TIM2. เนื่องจาก TIM3 ไม่เป็น the master สำหรับ timer ใดๆ, เราจะสตาร์ทเฉพาะ the PWM สำหรับมัน.

tha

Result

ก่อนอื่นมาดู the frequency และ duty cycle สำหรับ the PWM signal.



ที่นี่คุณจะเห็น the frequency ประมาณ 100 Hz ที่มี the duty of 40%. นี้เป็นตาม the configuration ที่เราทำสำหรับ the PWM signal.

tha

ตอนนี้เราจะเห็น the three waveforms ที่เราได้.



ที่นี่ผมได้เซ็ต the three markers ที่ the rising edges ของ three PWMs ทั้งหมด.
ทางด้านขวาคุณจะเห็นการประทับเวลาของ rising edges เหล่านี้ที่สัมพันธ์กับ the TIM1.

เราทราบว่า the time period ของ the PWM signal คือ 10ms, ดังนั้น the PWM2 จะสตาร์ทที่ 3.3ms นั่นคือ 33% ของ 10ms. ในทำนองเดียวกัน the PWM3 จะสตาร์ทที่ 6.6ms นั่นคือ 66% ของ 10ms.

ความแตกต่างนี้จะยังคงคงที่ตลอด