15.3.9 PWM modePulse width modulation mode ยอมให้การสร้าง a signal ด้วย a frequency ที่กำหนดโดยค่าของ the TIMx_ARR register และ a duty cycle ที่กำหนดโดยค่าของ the TIMx_CCRx register
The PWM mode สามารถถูกเลือกอย่างอิสระบนแต่ละ channel (one PWM ต่อ OCx output) โดยการเขียน 110 (PWM mode 1) หรือ ‘111 (PWM mode 2) ใน the OCxM bits ใน the TIMx_CCMRx register. ผู้ใช้ต้องเปิดการใช้งาน the corresponding preload register โดยการเซ็ต the OCxPE bit ใน the TIMx_CCMRx register, และในที่สุด the auto-reload preload register
โดยการเซ็ต the ARPE bit ใน the TIMx_CR1 register.
ดังที่ the preload registers ถูกส่งถ่ายไปยัง the shadow registers ได้เพียงเมื่อ an update event เกิดขึ้น, ก่อนการเริ่ม the counter, ผู้ใช้ต้องเริ่มทุก the registers โดยการเซ็ต the UG bit ใน the TIMx_EGR register
OCx polarity เป็น software programmable โดยใช้ the CCxP bit ใน the TIMx_CCER register. มันสามารถถูกโปรแกรมเป็น active high หรือ active low. OCx output ถูกเปิดการใช้งานโดย the CCxE bit ใน the TIMx_CCER register. อ้างอิงถึง the TIMx_CCERx register description สำหรับรายละเอียดที่มากขึ้น.
ใน PWM mode (1 หรือ 2), TIMx_CNT และ TIMx_CCRx เปรียบเทียบกันเสมอเพื่อตัดสินไม่ว่าจะ TIMx_CCRx≤TIMx_CNT or TIMx_CNT≤TIMx_CCRx (ขึ้นอยู่กับทิศทางของ the counter). อย่างไรก็ตาม, เพื่อให้สอดคล้องกับ the ETRF (OCREF สามารถถูกเคลียร์โดย an external event ผ่านทาง the ETR signal จนกระทั่ง the next PWM period), the OCREF signal ถูกถือสิทธิ์ได้เพียง :
• เมื่อผลของ the comparison เปลี่ยนไป, หรือ
• เมื่อ the output compare mode (OCxM bits in TIMx_CCMRx register) สวิทช์จาก the “frozen” configuration (ไม่
comparison, OCxM=‘000) ไปยังหนึ่งของ the PWM modes(OCxM=‘110 or ‘111).
นี้บังคับ the PWM โดย software ขณะที่ the timer กำลังรันอยู่.
The timer สามารถสร้าง PWM ใน edge-aligned mode หรือ center-aligned mode ขึ้นอยู่กับ the CMS bits ใน the TIMx_CR1 register.
PWM edge-aligned modeUpcounting configurationUpcounting ทำงานเมื่อ the DIR bit ใน the TIMx_CR1 register เป็น low. อ้างอิงถึง Upcounting mode.
ในตัวอย่างดังต่อไปนี้, เราพิจารณา PWM mode 1. The reference PWM signal OCxREF เป็น high ตราบเท่าที่ TIMx_CNT <TIMx_CCRx อย่างอื่นมันจะกลายเป็น low. ถ้า the compare value ใน TIMx_CCRx มากกว่า the auto-reload value (ใน TIMx_ARR) เมื่อนั้น OCxREF จะอยู่ที่ ‘1. ถ้า the compare value เป็น 0 เมื่อนั้น OCxREF จะอยู่ที่ ‘0. Figure 130 แสดงบาง edge-aligned PWM waveforms ในตัวอย่างที่ TIMx_ARR=8.
Downcounting configurationDowncounting ทำงานเมื่อ the DIR bit ใน the TIMx_CR1 register เป็น high. อ้างอิงถึง Downcounting mode.
ใน PWM mode 1, the reference signal ocxref เป็น low ตราบเท่าที่ TIMx_CNT>TIMx_CCRx อย่างอื่นมันจะกลายเป็น high. ถ้า the compare value ใน TIMx_CCRx มากกว่า the auto-reload value ใน TIMx_ARR, เมื่อนั้น ocxref จะอยู่ที่ ‘1. 0% PWM ไม่สามารถเป็นได้ในโหมดนี้.
PWM center-aligned modeCenter-aligned mode ทำงานเมื่อ the CMS bits ใน TIMx_CR1 register แตกต่างจาก ‘00 (ทุกการกำหนดที่เหลือมีผลอย่างเดียวกันบน the ocxref/OCx signals). The compare flag ถูกเซ็ตเมื่อ the counter นับขึ้น, เมื่อมันนับลงหรือทั้งเมื่อมันนับขึ้นและลงขึ้นอยู่กับ the CMS bits configuration. The direction bit (DIR) ใน the TIMx_CR1 register ถูกอัปเดตโดย hardware และต้องไม่ถูก
เปลี่ยนโดย software. อ้างอิงถึง Center-aligned mode (up/down counting).
Figure 131 แสดงบาง center-aligned PWM waveforms ในตัวอย่างที่:
• TIMx_ARR=8,
• PWM mode เป็น the PWM mode 1,
• The flag ถูกเซ็ตเมื่อ the counter นับลง ตรงกันกับ the center-aligned mode 1 ที่เลือกสำหรับ CMS=01 ใน TIMx_CR1
register.

คำแนะนำในการใช้ center-aligned mode:
• เมื่อเริ่มต้นใน center-aligned mode, the current up-down configuration ถูกใช้. หมายความว่า the counter นับขึ้นหรือลง
ขึ้นอยู่กับค่าที่เขียนใน the DIR bit ใน the TIMx_CR1 register. ยิ่งไปกว่านั้น, the DIR และ CMS bits ต้องไม่ถูกเปลี่ยนที่เวลา
เดียวกันโดย the software
• การเขียนำไปยัง the counter ขณะกำลังรันใน center-aligned mode ไม่ถูกแนะนำตามที่มันสามารถนำไปสู่ผลที่ไม่คาดคิด. โดย
เฉพาะอย่างยิ่ง :
– The direction ไม่ถูกอัปเดตถ้าผู้ใช้เขียนค่าใน the counter ที่มากกว่า the auto-reload value (TIMx_CNT>TIMx_ARR).
ตัวอย่างเช่น, ถ้า the counter กำลังนับขึ้นอยู่, มันก็ยังคงนับขึ้น.
– The direction ถูกอัปเดตถ้าผู้ใช้เขียน 0 หรือเขียน the TIMx_ARR value ใน the counter แต่ไม่มี Update Event UEV ถูก
สร้าง
• วิธีที่ปลอกภัยที่สุดในการใช้ center-aligned mode คือการสร้างการอัปเดตโดย software (โดยการเซ็ต the UG bit ใน the
TIMx_EGR register) เพียงก่อนการเริ่มต้น the counter และไม่เขียน the counter ขณะที่มันกำลังรันอยู่.
ปล. อากาศกำลังเย็นสบาย เลยได้เยอะหน่อย