Electoday 2025

ไมโครคอนโทรลเลอร์ => ARM Processors => Topic started by: tha on November 21, 2020, 08:18:38 AM

Title: STM32F1 TIM1 & TIM8
Post by: tha on November 21, 2020, 08:18:38 AM
14 Advanced-control timers (TIM1 and TIM8)
Low-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 16 and 32 Kbytes.
Medium-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 64 and 128 Kbytes.
High-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 256 and 512 Kbytes.
XL-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 768 Kbytes and 1 Mbyte.
Connectivity line devices are STM32F105xx and STM32F107xx microcontrollers.

Low- and medium-density STM32F103xx devices, and the STM32F105xx/STM32F107xx
connectivity line devices, บรรจุ one advanced-control timer (TIM1) ขณะที่ high-density
and XL-density STM32F103xx devices มีคุณลักษณะ two advance-control timers (TIM1 and
TIM8).

14.1 TIM1 and TIM8 introduction
The advanced-control timers (TIM1 and TIM8) ประกอบด้วย a 16-bit auto-reload counter ที่ถูกขับโดย a programmable prescaler.

มันอาจถูกใช้สำหรับวัตถุประสงค์ที่หลากหลาย, รวมทั้งวัดความยาวพลัสของ input signals (input capture) หรือสร้าง output waveforms (output compare, PWM, complementary PWM ที่มี dead-time insertion).

Pulse lengths และ waveform periods สามารถถูกปรับจาก microseconds เล็กน้อยไปถึงหลายๆ milliseconds โดยใช้ the timer prescaler และ the RCC clock controller prescalers.

The advanced-control (TIM1 and TIM8) and general-purpose (TIMx) timers  เป็นอิสระโดยสมบูรณ์, และไม่ได้ใช้ทรัพยากรใดๆร่วมกัน. สามารถถูกซิงโครไนซ์เข้าด้วยกันได้ตามที่อธิบายไว้ใน Section 14.3.20.
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on November 21, 2020, 01:31:42 PM
14.2 TIM1 and TIM8 main features
TIM1 and TIM8 timer features ประกอบด้วย :
   • 16-bit up, down, up/down auto-reload counter.
   • 16-bit programmable prescaler ยอมให้การหาร ("ได้ทันที" อีกด้วย) the counter clock frequency โดยแฟ็คเตอร์ใดแฟ็ค
      เตอร์หนึ่งระหว่าง 1 และ 65536.
   • มากถึง 4 independent channels สำหรับ:
     – Input capture
     – Output compare
     – PWM generation (Edge- and Center-aligned modes)
     – One-pulse mode output
   • Complementary outputs พร้อม programmable dead-time
   • Synchronization circuit เพื่อควบคุม the timer ด้วย external signals และเชื่อมต่อ timers หลายตัวเข้าด้วยกัน.
   • ตัวนับการทำซ้ำ(Repetition counter)เพื่ออัปเดต the timer registers เพียงหลังจากจำนวนที่ให้ไว้ของรอบของ the counter
   • Break input เพื่อผลัก the timer's output signals อยู่ใน reset state หรืออยู่ใน a known state.
   • Interrupt/DMA generation ตามเหตุการณ์ดังต่อไปนี้:
     – Update: counter overflow/underflow, counter initialization (โดย software หรือ internal/external trigger)
     – Trigger event (counter start, stop, initialization หรือนับโดย internal/external trigger)
     – Input capture
     – Output compare
     – Break input
   • รองรับ incremental (quadrature) encoder และ hall-sensor circuitry สำหรับ positioning purposes
   • Trigger input สำหรับ external clock หรือ cycle-by-cycle current management

(https://i.imgur.com/MX18ufM.png)
Preload registers ส่งถ่ายไปยัง active registers บน U event สอดคล้องกับ control bit
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 07, 2020, 07:10:57 AM
14.3 TIM1 and TIM8 functional description

14.3.1 Time-base unit
The main block ของ the programmable advanced-control timer คือ a 16-bit counter พร้อมด้วย auto-reload register ที่เกี่ยวข้องของมัน. The counter สามารถ count up, down หรือทั้ง up และ down. The counter clock สามารถถูกหารโดย a prescaler.

The counter, the auto-reload register และ the prescaler register สามารถถูกเขียนหรืออ่านโดย software. นี่เป็นจริงแม้ว่า the counter กำลังทำงานอยู่.

The time-base unit ประกอบด้วย :
• Counter register (TIMx_CNT)
• Prescaler register (TIMx_PSC)
• Auto-reload register (TIMx_ARR)
• Repetition counter register (TIMx_RCR)

The auto-reload register ถูกโหลดไว้ล่วงหน้า. การเขียนไปยังหรือการอ่านจาก the auto-reload register จะเข้าถึง the preload register. The content ของ the preload register ถูกส่งถ่ายลงใน the shadow register อย่างถาวรหรือที่แต่ละ update event (UEV), ขึ้นอยู่กับ the auto-reload preload enable bit (ARPE) ใน TIMx_CR1 register. The update event ถูกส่งเมื่อ the counter ถึง the overflow (หรือ underflow เมื่อนับลง) และถ้า the UDIS bit เท่ากับ 0 ใน the TIMx_CR1 register. มันสามารถถูกสร้างโดย software. The generation ของ the update event ถูกอธิบายในรายละเอียดสำหรับแต่ละ configuration.

The counter ถูก clocked โดย the prescaler output CK_CNT, ซึ่งถูกเปิดการใช้งานได้เพียงเมื่อ the counter enable bit (CEN) ใน TIMx_CR1 register ถูกเซ็ต (อ้างอิงอีกด้วยถึง the slave mode controller description เพื่อให้ได้รายละเอียดที่มากขึ้นบน counter enabling).

บันทึกไว้ว่า the counter เริ่มการนับ 1 clock cycle หลังจากการเซ็ต the CEN bit ใน the TIMx_CR1

Prescaler description
The prescaler สามารถหาร the counter clock frequency โดยแฟคเตอร์ใดแฟคเตอร์หนึ่งระหว่าง 1 และ 65536. มันขึ้นอยู่กับ a 16-bit counter ถูกควบคุมผ่าน  a 16-bit register (ใน the TIMx_PSC register). มันสามารถถูกเปลี่ยนได้ในทันทีเนื่องจาก control register นี้ถูก buffered. The new prescaler ratio ถูกนำมาคิดคำนึงถึงที่ the next update event.

Figure 53 and Figure 54 แสดงบางตัวอย่างของ the counter behavior เมื่อ the prescaler ratio ถูกเปลี่ยนแปลงทันที :

(https://i.imgur.com/3HCvUWH.png)
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 07, 2020, 08:44:40 AM
14.3.2 Counter modes

Upcounting mode
In upcounting mode, the counter นับจาก 0 จนถึง the auto-reload value (ที่อยู่ภายในของ the TIMx_ARR register), จากนั้นเริ่มใหม่จาก 0 และสร้าง a counter overflow event.

ถ้า the repetition counter ถูกใช้, the update event (UEV) ถูกสร้างหลังจาก upcounting ถูกซ้ำเป็นจำนวนครั้งที่โปรแกรมใน the repetition counter register บวกหนึ่ง (TIMx_RCR+1). ถ้าเป็นอย่างอื่น the update event ถูกสร้างที่แต่ละ counter overflow.

การเซ็ต the UG bit ใน the TIMx_EGR register (โดย software หรือโดยการใช้ the slave mode controller). ก็สร้าง an update event อีกด้วย

The UEV event สามารถถูกปิดการใช้งานโดย software โดยการเซ็ต the UDIS bit ใน TIMx_CR1 register. นี้เพื่อหลีกเลี่ยงการอัปเดต the shadow registers ขณะที่เขียนค่าใหม่ใน the preload registers. จากน้้นไม่มีการอัปเดต event เกิดขึนจนกระทั่ง the UDIS bit ถูกเขียนให้เป็น 0 แล้ว. อย่างไรก็ตาม, the counter จะเริ่มใหม่จาก 0, เช่นเดียวกันกับ the counter ของ the prescaler
(แต่ the prescale rate ไม่ได้เปลี่ยนแปลง). นอกจากนี้, หาก the URS bit (update request selection) ใน TIMx_CR1 register ถูกเซ็ต, การเซ็ต the UG bit จะสร้าง an update event UEV แต่ปราศจากการเซ็ต the UIF flag (ดังนั้นไม่มี interrupt หรือ DMA request ถูกส่ง). นี้เพื่อหลีกเลี่ยงการสร้างทั้ง update และ capture interrupts เมื่อมีการเคลียร์ the counter บน the capture event.

เมื่อ an update event เกิดขึ้น, ทุก the registers ถูกอัปเดตและ the update flag (UIF bit ใน TIMx_SR register) ถูกเซ็ต (ขึ้นอยู่กับ the URS bit):
•    The repetition counter ถูกโหลดซ้ำใหม่ด้วย the content ของ TIMx_RCR register,
•    The buffer ของ the prescaler ถูกโหลดใหม่ด้วย the preload value (ที่อยู่ภายในของ the TIMx_PSC register)
•    The auto-reload shadow register ถูกอัปเดตด้วย the preload value (TIMx_ARR)

รูปดังต่อไปนี้แสดงบางตัวอย่างของ the counter behavior สำหรับ different clock frequencies เมื่อ TIMx_ARR=0x36.

(https://i.imgur.com/PUKne7n.png)
(https://i.imgur.com/GtlGYD8.png)
(https://i.imgur.com/DyIbw8R.png)
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 07, 2020, 09:41:08 AM
Downcounting mode
ใน downcounting mode, the counter นับจาก the auto-reload value (ที่อยู่ภายในของ the TIMx_ARR register) ลงไปถึง 0, จากนั้นเริ่มใหม่จาก the auto-reload value และสร้าง a counter underflow event

ถ้า the repetition counter ถูกใช้, the update event (UEV) ถูกสร้างหลังจาก downcounting ถูกซ้ำเป็นจำนวนครั้งที่โปรแกรมใน the repetition counter register บวกหนึ่ง (TIMx_RCR+1). ถ้าเป็นอย่างอื่น the update event ถูกสร้างที่แต่ละ counter underflow.

การเซ็ต the UG bit ใน the TIMx_EGR register (โดย software หรือโดยการใช้ the slave mode controller). ก็สร้าง an update event ได้อีกด้วย

The UEV event สามารถถูกปิดการใช้งานโดย software โดยการเซ็ต the UDIS bit ใน TIMx_CR1 register. นี้เพื่อหลีกเลี่ยงการอัปเดต the shadow registers ขณะที่เขียนค่าใหม่ใน the preload registers. จากน้้นไม่มีการอัปเดต event เกิดขึนจนกระทั่ง the UDIS bit ถูกเขียนให้เป็น 0 แล้ว. อย่างไรก็ตาม, the counter จะเริ่มใหม่จาก the current auto-reload value, ในขณะที่ the counter ของ the prescaler เริ่มใหม่จาก 0 (แต่ the prescale rate ไม่ได้เปลี่ยนแปลง).

นอกจากนี้, หาก the URS bit (update request selection) ใน TIMx_CR1 register ถูกเซ็ต, การเซ็ต the UG bit จะสร้าง an update event UEV แต่ปราศจากการเซ็ต the UIF flag (ดังนั้นไม่มี interrupt หรือ DMA request ถูกส่ง). นี้เพื่อหลีกเลี่ยงการสร้างทั้ง update และ capture interrupts เมื่อมีการเคลียร์ the counter บน the capture event.

เมื่อ an update event เกิดขึ้น, ทุก the registers ถูกอัปเดตและ the update flag (UIF bit ใน TIMx_SR register) ถูกเซ็ต (ขึ้นอยู่กับ the URS bit):
•    The repetition counter ถูกโหลดซ้ำใหม่ด้วย the content ของ TIMx_RCR register.
•    The buffer ของ the prescaler ถูกโหลดซ้ำใหม่ด้วย the preload value (ที่อยู่ภายในของ the TIMx_PSC register)
•    The auto-reload active register ถูกอัปเดตด้วย the preload value (TIMx_ARR) บันทึกไว้ว่า the auto-reload ถูกอัปเดต
      ก่อน the counter ถูกโหลดซ้ำใหม่, ดังนั้น the next period คือสิ่งที่คาดหวัง(the expected one)

รูปดังต่อไปนี้แสดงบางตัวอย่างของ the counter behavior สำหรับ different clock frequencies เมื่อ TIMx_ARR=0x36.

(https://i.imgur.com/BaoJX8c.png)
(https://i.imgur.com/WqzQSAB.png)
(https://i.imgur.com/Jx92dI2.png)
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 07, 2020, 10:16:46 AM
Center-aligned mode (up/down counting)
ใน center-aligned mode, the counter นับจาก 0 ไปยัง the auto-reload value (ที่อยู่ภายในของ the TIMx_ARR register) – 1, สร้าง a counter overflow event, จากนั้นนับจาก the autoreload value ลงไปยัง 1 และสร้าง a counter underflow event. จากนั้นมันเริ่มใหม่การนับจาก 0.

Center-aligned mode ถูกทำให้ทำงานเมื่อ the CMS bits ใน TIMx_CR1 register ไม่เท่ากับ '00'. The Output compare interrupt flag ของ channels ที่กำหนดค่าใน output ถูกเซ็ตเมื่อ : the counter นับลง (Center aligned mode 1, CMS = "01"), the counter นับขึ้น (Center aligned mode 2, CMS = "10") the counter นับขึ้นและลง (Center aligned mode 3, CMS = "11").

ในโหมดนี้, the direction bit (DIR จาก TIMx_CR1 register) ไม่สามารถถูกเขียน. มันถูกอัปเดตโดย hardware และให้ทิศทางปัจจุบันของ the counter

The update event สามารถถูกสร้างที่แต่ละ counter overflow และที่แต่ละ counter underflow หรือโดยการเซ็ต the UG bit ใน the TIMx_EGR register (โดย software หรือโดยการใช้ the slave mode controller) ก็สร้าง an update event ได้อีกด้วย. ในกรณีนี้, the counter เริ่มใหม่การนับจาก 0, เช่นเดียวกันกับ the counter ของ the prescaler.

The UEV event สามารถถูกปิดการใช้งานโดย software โดยการเซ็ต the UDIS bit ใน TIMx_CR1 register. นี้เพื่อหลีกเลี่ยงการอัปเดต the shadow registers ขณะที่เขียนค่าใหม่ใน the preload registers. จากน้้นไม่มีการอัปเดต event เกิดขึนจนกระทั่ง the UDIS bit ถูกเขียนให้เป็น 0 แล้ว. อย่างไรก็ตาม, the counter ยังคง counting up และ down, ตาม the current auto-reload
value.

นอกจากนี้, หาก the URS bit (update request selection) ใน TIMx_CR1 register ถูกเซ็ต, การเซ็ต the UG bit จะสร้าง an update event UEV แต่ปราศจากการเซ็ต the UIF flag (ดังนั้นไม่มี interrupt หรือ DMA request ถูกส่ง). นี้เพื่อหลีกเลี่ยงการสร้างทั้ง update และ capture interrupts เมื่อมีการเคลียร์ the counter บน the capture event.

เมื่อ an update event เกิดขึ้น, ทุก the registers ถูกอัปเดตและ the update flag (UIF bit ใน TIMx_SR register) ถูกเซ็ต (ขึ้นอยู่กับ the URS bit):
•    The repetition counter ถูกโหลดซ้ำใหม่ด้วย the content ของ TIMx_RCR register.
•    The buffer ของ the prescaler ถูกโหลดซ้ำใหม่ด้วย the preload value (ที่อยู่ภายในของ the TIMx_PSC register)
•    The auto-reload active register ถูกอัปเดตด้วย the preload value (ที่อยู่ภายในของ the TIMx_ARR register) บันทึกไว้ว่า
      ถ้า the update source คือ a counter overflow, the auto-reload ถูกอัปเดตก่อน the counter ถูกโหลดซ้ำใหม่, ดังนั้น the
      next period คือสิ่งที่คาดหวัง(the expected one) (the counter ถูกโหลดด้วย the new value).

รูปดังต่อไปนี้แสดงบางตัวอย่างของ the counter behavior สำหรับ different clock frequencies

(https://i.imgur.com/YXF2ZTG.png)
(https://i.imgur.com/7E0YB2f.png)
(https://i.imgur.com/L6MFsDW.png)
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 08, 2020, 06:42:16 AM
14.3.3 Repetition counter
Section 14.3.1: Time-base unit อธิบายวิธีที่ the update event (UEV) ถูกสร้างด้วยความเคารพต่อ the counter overflows/underflows. มันถูกสร้างได้จริงเพียงเมื่อ the repetition counter ถึงศูนย์แล้วเท่านั้น. นี้เป็นประโยชน์เมื่อสร้าง PWM signals.

นี้หมายความว่า data ถูกส่งถ่ายจาก the preload registers ไปยัง the shadow registers (TIMx_ARR auto-reload register, TIMx_PSC prescaler register, แต่ TIMx_CCRx capture/compare registers ใน compare mode อีกด้วย) ทุกๆ N+1 counter overflows หรือ underflows, ที่ N คือ the value ใน the TIMx_RCR repetition counter register.

The repetition counter ถูกลดลง:
•    ที่แต่ละ counter overflow ใน upcounting mode,
•    ที่แต่ละ counter underflow ใน downcounting mode,
•    ที่แต่ละ counter overflow และที่แต่ละ counter underflow ใน center-aligned mode. แม้ว่าจะจำกัด the maximum
      number ของ repetition ไว้ที่ 128 PWM cycles, มันทำให้มันสามารถเป็นได้ที่จะอัปเดต the duty cycle สองครั้งต่อ PWM
      period. เมื่อรีเฟรช compare registers เพียงครั้งเดียวต่อ PWM period ใน center-aligned mode, maximum resolution
      คือ 2xTck, เนื่องจากความสมมาตรของรูปแบบ.

The repetition counter เป็น an auto-reload type; the repetition rate ถุกคงไว้ตามที่กำหนดโดย the TIMx_RCR register value (อ้างอิงถึง Figure 72). เมื่อ the update event ถูกสร้างโดย software (โดยการเซ็ต the UG bit ใน TIMx_EGR register) หรือโดย hardware ผ่านทาง the slave mode controller, มันเกิดขึ้นโดยทันทีไม่ว่า the value ของ the repetition counter คืออะไรและ the repetition counter ถูกโหลดลงใหม่ด้วย the content ของ the TIMx_RCR register.

ใน center-aligned mode, สำหรับค่าคี่ของ RCR, the update event เกิดขึ้นทั้งบน the overflow หรือบน the underflow ขึ้นอยู่กับเมื่อไหร่ the RCR register ถูกเขียนและเมื่อไหร่ the counter ถูกสตาร์ท. ถ้าIf the RCR ถูกเขียนก่อนการสตาร์ท the counter, the UEV จะเกิดขึ้นบน the overflow. ถ้า the RCR ถูกเขียนหลังจากการสตาร์ท the counter, the UEV จะเกิดขึ้นบน the underflow. ตัวอย่างเช่นสำหรับ RCR = 3, the UEV ถูกสร้างบนแต่ละh 4th overflow หรือ underflow event ขึ้นอยู่กับเมื่อไหร่ RCR ถูกเขียน.

(https://i.imgur.com/eZ0s0GT.png)
Update event : Preload register ส่งถ่ายถึง active register และ update interrupt ถูกสร้าง
Update event : ถ้า the repetition counter underflow เกิดขึ้นเมื่อ the counter เท่ากันกับ the auto-reload value
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 08, 2020, 06:48:58 AM
14.3.4 Clock selection
The counter clock สามารถถูกจัดให้มีโดย the following clock sources:
•    Internal clock (CK_INT)
•    External clock mode1: external input pin (TIx)
•    External clock mode2: external trigger input (ETR).
•    Internal trigger inputs (ITRx): การใช้ one timer เป็น prescaler สำหรับ timer อื่น, ตัวอย่างเช่น, Timer1 สามารถถูกกำหนด
     ค่าเพื่อทำงานเป็น a prescaler สำหรับ Timer 2. อ้างอิงถึง Using one timer as prescaler for another timer สำหรับราย
     ละเอียดที่มากขึ้น.

Internal clock source (CK_INT)
หาก the slave mode controller ถูกปิดการใช้งาน (SMS=000), ดังนั้น the CEN, DIR (ใน the TIMx_CR1 register) และ UG bits (ใน the TIMx_EGR register) เป็น actual control bits และสามารถถูกเปลี่ยนได้เพียงโดย software (ยกเว้น UG ซึ่งยังคงเคลียร์โดยอัตโนมัติ). ทันทีที่ the CEN bit ถูกเขียนเป็น 1, the prescaler ถูก clocked โดย the internal clock C K_INT.

Figure 73 แสดงลักษณะการทำงานของ the control circuit และ the upcounter ใน normal mode, โดยไม่มี prescaler.

(https://i.imgur.com/0EBk7RW.png)
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 08, 2020, 07:47:06 AM
External clock source mode 1
โหมดนี้ถูกเลือกเมื่อ SMS=111 ใน the TIMx_SMCR register. The counter สามารถนับที่แต่ละ rising หรือ falling edge บน a selected input.

(https://i.imgur.com/FN8LSJm.png)

ตัวอย่างเช่น, ในการกำหนดค่า the upcounter เพื่อนับที่ตอบสนองต่อ a rising edge บน the TI2 input, ใช้ขบวนการดังต่อไปนี้:
1.    กำหนดค่า channel 2 เพื่อตรวจจับ rising edges บน the TI2 input โดยการเขียน CC2S= '01 ใน the TIMx_CCMR1
       register.
2.    กำหนดค่า the input filter duration โดยการเขียน the IC2F[3:0] bits ใน the TIMx_CCMR1 register (ถ้าไม่มี filter ถูก
       ต้องการ, ให้เก็บ IC2F=0000).
3.    เลือก rising edge polarity โดยการเขียน CC2P=0 ใน the TIMx_CCER register
4.    กำหนดค่า the timer ใน external clock mode 1 โดยการเขียน SMS=111 ใน the TIMx_SMCR register.
5.    เลือก TI2 เป็น the input source โดยการเขียน TS=110 ใน the TIMx_SMCR register.
6.    เปิดการใช้งาน the counter โดยการเขียน CEN=1 ใน the TIMx_CR1 register

Note: The capture prescaler ไม่ได้ถูกใช้สำหรับการทริกเกอร์, ดังนั้นไม่มีความจำเป็นที่จะกำหนดค่ามัน.

เมื่อ a rising edge เกิดขึ้นบน TI2, the counter นับหนึ่งครั้งและ the TIF flag ถูกเซ็ต.

The delay ระหว่าง the rising edge บน TI2 และ the actual clock ของ the counter เกิดจาก the resynchronization circuit บน TI2 input

(https://i.imgur.com/itceEf5.png)
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 08, 2020, 08:32:46 AM
External clock source mode 2
โหมดนี้ถูกเลือกโดยการเขียน ECE=1 ใน the TIMx_SMCR register.

The counter สามารถนับที่แต่ละ rising หรือ falling edge บน the external trigger input ETR.

Figure 76 แสดงภาพรวมของ the external trigger input block.

(https://i.imgur.com/gDU11Zy.png)

ตัวอย่างเช่น, ในการกำหนดค่า the upcounter เพื่อนับแต่ละ 2 rising edges บน ETR, ใช้ขบวนการดังต่อไปนี้:
1.    ตามที่ไม่มี filter ถูกต้องการในตัวอย่างนี้, เขียน ETF[3:0]=0000 ใน the TIMx_SMCR register
2.    เซ็ต the prescaler โดยการเขียน ETPS[1:0]=01 ใน the TIMx_SMCR register
3.    เลือก rising edge detection บน the ETR pin โดยการเขียน ETP=0 ใน the TIMx_SMCR register
4.    เปิดการใช้งาน external clock mode 2 โดยการเขียน ECE=1 ใน the TIMx_SMCR register.
5.    เปิดการใช้งาน the counter โดยการเขียน CEN=1 ใน the TIMx_CR1 register.

The counter นับหนึ่งครั้งแต่ละ 2 ETR rising edges

The delay ระหว่าง the rising edge บน  ETR  และ the actual clock ของ the counter เกิดจาก the resynchronization circuit บน the ETRP signal

(https://i.imgur.com/PHF1COQ.png)
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 09, 2020, 07:26:05 AM
14.3.5 Capture/compare channels
แต่ละ Capture/Compare channel (ดู Figure 125) ถูกสร้างขึ้นจาก a capture/compare register (รวมถึง a shadow register), an input stage สำหรับ capture (พร้อมด้วย digital filter, multiplexing และ prescaler) และ an output stage (พร้อมด้วย comparator and output control).

Figure 78 ถึง Figure 81 แสดงภาพรวมของ one Capture/Compare channel.

The input stage สุ่ม the corresponding TIx input เพื่อสร้าง a filtered signal TIxF. จากนั้น, an edge detector ที่มี polarity selection สร้าง a signal (TIxFPx) ซึ่งสามารถถูกใช้เป็น trigger input โดย the slave mode controller หรือเป็น the capture command. มันถูก prescaled ก่อน the capture register (ICxPS).

(https://i.imgur.com/0xYfQzC.png)

The output stage สร้าง an intermediate waveform ซึ่งจากนั้นถูกใช้สำหรับ reference : OCxRef (active high). The polarity ทำงานที่ the end ของ the chain.

(https://i.imgur.com/gY8uXzB.png)

(https://i.imgur.com/8bjiV3d.png)

The capture/compare block ถูกทำจาก one preload register และ one shadow register. เขียนและอ่านจะเข้าถึง the preload register เสมอ.

ใน capture mode, captures ถูกทำจริงใน the shadow register, ซึ่งถูกคัดลอกลงใน the preload register.

ใน compare mode, ที่เก็บอยู่ในของ the preload register ถูกคัดลอกลงใน the shadow register ซึ่งถูกเปรียบเทียบกับ the counter.

ปล. Input capture ถ้ามีสัญญาณ input capture มา ก็จะโหลดค่าจาก counter ไปยัง capture register แล้วเราก็อ่านไปใช้อีกที ส่วน output compare เราเอาค่ามาใส่ compare register แล้ว counter ก็นับ ถ้าค่ามาเท่ากันแล้ว ก็จะส่งสัญญาณ output ออกมา
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 09, 2020, 08:49:31 AM
14.3.6 Input capture mode
ใน Input capture mode, the Capture/Compare Registers (TIMx_CCRx) ถูกใช้เพื่อค้าง(latch)ค่าของ the counter หลังจากการเปลี่ยนแปลงถูกตรวจพบโดย the corresponding ICx signal. เมื่อ a capture เกิดขึ้น, the corresponding CCXIF flag (TIMx_SR register) ถูกเซ็ตและ an interrupt หรือ a DMA request สามารถถูกส่งถ้าพวกมันถูกเปิดการใช้งาน. ถ้า a capture เกิดขึ้นขณะที่ the CCxIF flag เป็น high อยู่แล้ว, จากนั้น the over-capture flag CCxOF (TIMx_SR register) ถูกเซ็ต. CCxIF สามารถถูกเคลียร์โดย software โดยการเขียนมันให้เป็น 0 หรือโดยการอ่าน the captured data ที่เก็บอยู่ใน the TIMx_CCRx register. CCxOF ถูกเคลียร์เมื่อเขียนให้เป็น 0.

ตัวอย่างต่อไปนี้แสดงวิธีจับ the counter value ใน TIMx_CCR1 เมื่อ TI1 input สูงขึ้น. เพื่อทำนี้, ใช้ขั้นตอนดังต่อไปนี้
•    เลือก the active input: TIMx_CCR1 ต้องถูกเชื่อมโยงกับ the TI1 input, ดังนั้นต้องเขียน the CC1S bits เป็น 01 ใน the
      TIMx_CCMR1 register. ทันทีที่ CC1S แตกต่างไปจาก 00, the channel ถูกกำหนดค่าใน input และ the TIMx_CCR1
      register จะกลายเป็นอ่านได้เพียงอย่างเดียว.
•    โปรแกรม the needed input filter duration โดยคำนึงถึง the signal ที่ต่อถึง the timer (โดยการโปรแกรม the ICxF bits ใน
      the TIMx_CCMRx register ถ้า the input คือหนึ่งของ the TIx inputs). ลองนึกภาพว่า, เมื่อมีการสลับ, the input signal ไม่มี
      ความเสถียรระหว่างที่ต้อง five internal clock cycles. เราต้องโปรแกรม a filter duration ให้ยาวกว่า five clock cycles นี้. เรา
      สามารถตรวจสอบความถูกต้องของการเปลี่ยนแปลงบน TI1 เมื่อแปดการสุ่มต่อเนื่องพร้อมด้วย the new level ถูกตรวจจับแล้ว (สุ่มที่
      fDTS frequency). จากนั้นเขียน IC1F bits เป็น 0011 in the TIMx_CCMR1 register
•    เลือก the edge ของ the active transition บน the TI1 channel โดยการเขียน the CC1P bit เป็น 0 ใน the TIMx_CCER
      register (rising edge ในกรณีนี้).
•    โปรแกรม the input prescaler. ในตัวอย่างของเรา, เราอยากให้ the capture ถูกกระทำที่แต่ละ valid transition, ดังนั้น the
      prescaler ถูกปิดการใช้งาน (เขียน IC1PS bits เป็น 00 ใน the TIMx_CCMR1 register).
•    เปิดการใช้งาน capture จาก the counter ลงใน the capture register โดยการเซ็ต the CC1E bit ใน the TIMx_CCER
      register.
•    ถ้าต้องการ, เปิดการใช้งาน the related interrupt request โดยการเซ็ต the CC1IE bit ใน the TIMx_DIER register, และ/
      หรือ the DMA request โดยการเซ็ต the CC1DE bit ใน the TIMx_DIER register.

เมื่อ an input capture เกิดขึ้น :
•    The TIMx_CCR1 register รับค่าของ the counter บนการเปลี่ยนแปลงที่ให้ทำงาน(the active transition).
•    CC1IF flag ถูกเซ็ต(interrupt flag). CC1OF ก็ถูกเซ็ตด้วยถ้าอย่างน้อย 2 captures ที่ต่อเนื่องกันเกิดขึ้นในขณะที่ the flag ไม่ได้
     ถูกเคลียร์อยู่.
•    An interrupt ถูกสร้างขึ้นอยู่กับ the CC1IE bit.
•    A DMA request ถูกสร้างขึ้นอยู่กับ  the CC1DE bit.

เพื่อรับมือ the overcapture, ขอแนะนำให้อ่าน the data ก่อน the overcapture flag. นี้เพื่อหลีกเลี่ยงไม่ให้พลาด an overcapture ซึ่งอาจเกิดขึ้นได้หลังจากการอ่าน the flag และก่อนการอ่าน the data.

Note: IC interrupt และ/หรือ DMA requests สามารถถูกสร้างโดย software โดยการเซ็ต the corresponding CCxG bit ใน the TIMx_EGR register.
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 09, 2020, 08:55:03 AM
-
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 10, 2020, 06:48:38 AM
14.3.7 PWM input mode
โหมดนี้เป็นกรณีเฉพาะของ input capture mode. ขั้นตอนเหมือนกันยกเว้น:
•    Two ICx signals ถูก mapped บน the same TIx input.
•    2 ICx signals นี้ทำงานบน edges ที่มีขั้วตรงกันข้ามกัน.
•    หนึ่งของ the two TIxFP signals นี้ถูกเลือกเป็น trigger input และ the slave mode controller ถูกกำหนดใน reset mode.

ตัวอย่างเช่น, ผู้ใช้สามารถวัด the period (ใน TIMx_CCR1 register) และ the duty cycle (ใน TIMx_CCR2 register) ของ the PWM ที่ใช้กับ TI1 โดยใช้ขั้นตอนดังต่อไปนี้ (ขึ้นอยู่กับ CK_INT frequency และ prescaler value):
•    เลือก the active input สำหรับ TIMx_CCR1: เขียน the CC1S bits เป็น 01 ใน the TIMx_CCMR1 register (TI1 ถูกเลือก).
•    เลือก the active polarity สำหรับ TI1FP1 (ใช้ทั้งสำหรับ capture ใน TIMx_CCR1 และ เคลียร์ counter): เขียน the CC1P เป็น
      '0' (ทำงานบน rising edge)
•    เลือก the active input สำหรับ TIMx_CCR2: เขียน the CC2S bits เป็น 10 ใน the TIMx_CCMR1 register (TI1 ถูกเลือก).
•    เลือก the active polarity สำหรับ TI1FP2 (ใช้สำหรับ capture ใน TIMx_CCR2): เขียน the CC2P bit เป็น '1' (ทำงานบน
      falling edge).
•    เลือก the valid trigger input: เขียน the TS bits เป็น 101 ใน the TIMx_SMCR register (TI1FP1 ถูกเลือก).
•    กำหนด the slave mode controller ใน reset mode: เขียน the SMS bits เป็น 100 ใน the TIMx_SMCR register.
•    เปิดการใช้งาน the captures: เขียน the CC1E และ CC2E bits เป็น '1 ใน the TIMx_CCER register

(https://i.imgur.com/iCkRA2d.png)
1. The PWM input mode สามารถถูกใช้เพียงกับ the TIMx_CH1/TIMx_CH2 signals เนื่องจากความจริงแล้วมีเพียง TI1FP1 และ TI2FP2 ที่ถูกต่อกับ the slave mode controller.
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 10, 2020, 07:02:23 AM
14.3.8 Forced output mode
ใน output mode (CCxS bits = 00 ใน the TIMx_CCMRx register), แต่ละ output compare signal (OCxREF และจากนั้น OCx) สามารถถูกบังคับให้ active หรือ inactive level โดยตรงโดย software, เป็นอิสระจาก comparison ใดๆระหว่าง the output compare register และ the counter.

เพื่อบังคับ an output compare signal (ocxref/OCx) ให้ active level ของมัน, ผู้ใช้เพียงต้องการเขียน 101 ใน the OCxM bits ใน the corresponding TIMx_CCMRx register. ด้วยประการฉะนี้ ocxref ถูกบังคับ high (OCxREF active high เสมอ) และ OCx ได้รับค่าที่ตรงกันข้ามกับ CCxP polarity bit.

ตัวอย่างเช่น : CCxP=0 (OCx active high) => OCx ถูกบังคับเป็น high level.

ocxref signal สามารถถูกบังคับเป็น low โดยการเขียน the OCxM bits เป็น 100 ใน the TIMx_CCMRx register.

อย่างไรก็ตาม, the comparison ระหว่าง the TIMx_CCRx shadow register และ the counter ยังคงถูกกระทำและยอมให้ the flag ถูกเซ็ต. Interrupt และ DMA requests สามารถถูกส่งตามนั้น. นี้ถูกอธิบายใน the Output Compare Mode section.
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 10, 2020, 07:18:41 AM
14.3.9 Output compare mode
function นี้ถูกใช้เพื่อควบคุม an output waveform หรือแสดงเมื่อ a period ของ time ได้ผ่านไป.

เมื่อการเท่ากันถูกพบระหว่าง the capture/compare register และ the counter, the output compare function:
•    ระบุ the corresponding output pin ให้เป็น a programmable value กำหนดโดย the output compare mode (OCxM bits
      ใน the TIMx_CCMRx register) และ the output polarity (CCxP bit ใน the TIMx_CCER register). The output pin
      สามารถรักษาระดับของมัน (OCXM=000), ถูกเซ็ต active (OCxM=001), ถูกเซ็ต inactive (OCxM=010) หรือสามารถ toggle
      (OCxM=011) เมื่อเท่ากัน.
•    เซ็ต a flag ใน the interrupt status register (CCxIF bit ใน the TIMx_SR register).
•    สร้าง an interrupt ถ้า the corresponding interrupt mask ถูกเซ็ต (CCXIE bit ใน the TIMx_DIER register).
•    ส่ง a DMA request ถ้า the corresponding enable bit ถูกเซ็ต (CCxDE bit ใน the TIMx_DIER register, CCDS bit ใน
      the TIMx_CR2 register สำหรับ the DMA request selection).

The TIMx_CCRx registers สามารถถูกโปรแกรมโดยมีหรือโดยไม่มี preload registers โดยใช้ the OCxPE bit ใน the TIMx_CCMRx register

ใน output compare mode, the update event UEV ไม่มีผลกับ ocxref และ OCx output. The timing resolution คือ one count ของ the counter. Output compare mode สามารถถูกใช้เพื่อเอ๊าพุท a single pulse อีกด้วย (ใน One-pulse mode).

ขั้นตอน:
1. เลือก the counter clock (internal, external, prescaler).
2. เขียน the desired data ใน the TIMx_ARR และ TIMx_CCRx registers.
3. เซ็ต the CCxIE และ/หรือ CCxDE bits ถ้า an interrupt และ/หรือ a DMA request ถูกต้องการสร้าง.
4. เลือก the output mode. ตัวอย่างเช่น:
    – เขียน OCxM = 011 เพื่อสลับ OCx output pin เมื่อ CNT เท่ากันกับ CCRx
    – เขียน OCxPE = 0 เพื่อปิดการใช้งาน preload register
    – เขียน CCxP = 0 เพื่อเลือก active high polarity
    – เขียน CCxE = 1 เพื่อเปิดการใช้งาน the output
5. เปิดการใช้งาน the counter โดยการเซ็ต the CEN bit ใน the TIMx_CR1 register.

The TIMx_CCRx register สามารถถูกอัปเดตได้ตลอกเวลาโดย software เพื่อควบคุม the output waveform, จัดให้ว่า the preload register ไม่ถูกเปิดการใช้งาน (OCxPE=0, มิฉะนั้น TIMx_CCRx shadow register ถูกอัปเดตได้เพียงที่ the next update event UEV). ตัวอย่างถูกให้ใน Figure 83.

(https://i.imgur.com/WmzYw5U.png)
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 10, 2020, 10:04:26 AM
14.3.10 PWM mode
Pulse 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, CCxNE, MOE, OSSI และ OSSR bits (TIMx_CCER และ TIMx_BDTR registers). อ้างอิงถึง the TIMx_CCERx register description สำหรับรายละเอียดที่มากขึ้น.

ใน PWM mode (1 หรือ 2), TIMx_CNT และ TIMx_CCRx เปรียบเทียบกันเสมอเพื่อตัดสินไม่ว่าจะ TIMx_CCRx≤TIMx_CNT หรือ TIMx_CNT≤TIMx_CCRx (ขึ้นอยู่กับทิศทางของ the counter).

The timer สามารถสร้าง PWM ใน edge-aligned mode หรือ center-aligned mode ขึ้นอยู่กับ the CMS bits ใน the TIMx_CR1 register.

PWM edge-aligned mode

•    Upcounting configuration
      Upcounting จะ active เมื่อ 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 84 แสดงบาง
      edge-aligned PWM waveforms ในตัวอย่างที่ TIMx_ARR=8.

(https://i.imgur.com/rPmXOge.png)

•    Downcounting configuration
      Downcounting ทำงานเมื่อ 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 mode
Center-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 85 แสดงบาง 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.

(https://i.imgur.com/IDGqGzF.png)

คำแนะนำในการใช้ 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 ขณะที่มันกำลังรันอยู่.

ปล. ไปเร็วไปมั๊ย พอดีมันซ้ำๆกับ TIM2&5 อยู่ เลยก๊อบปี้เอา
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 11, 2020, 06:58:50 AM
14.3.11 Complementary(ทำให้ครบ, ส่วนประกอบ, เต็มเติม) outputs and dead-time insertion
The advanced-control timers (TIM1 and TIM8) สามารถส่งออก two complementary signals และจัดการ the switching-off และ the switching-on ทันทีทันใดของ the outputs

time นี้รู้โดยทั่วไปว่า dead-time และมันต้องถูกปรับมันขึ้นอยู่กับ the devices ที่ต่อกับ the outputs และคุณลักษณะของมัน (intrinsic delays ของ level-shifters, delays เนื่องจาก power switches...)

ผู้ใช้สามารถเลือก the polarity ของ the outputs (main output OCx หรือ complementary OCxN) เป็นอิสระสำหรับแต่ละ output. นี้ถูกทำโดยการเขียนไปยัง the CCxP และ CCxNP bits ใน the TIMx_CCER register

The complementary signals OCx และ OCxN ถูกทำให้ทำงานโดยการรวมของหลาย control bits: the CCxE และ CCxNE bits ใน the TIMx_CCER register และ the MOE, OISx, OISxN, OSSI และ OSSR bits ใน the TIMx_BDTR และ TIMx_CR2 registers. อ้างอิงถึง Table 83 สำหรับรายละเอียดที่มากขึ้น. โดยเฉพาะอย่างยิ่ง, the dead-time ถูกทำให้ทำงานเมื่อสวิทช์ไปยัง the IDLE state (MOE ตกลงมา 0).

Dead-time insertion ถูกเปิดการใช้งานโดยการเซ็ตทั้ง CCxE และ CCxNE bits, และ the MOE bit ถ้า the break circuit มีอยู่. DTG[7:0] bits ของ the TIMx_BDTR register ถูกใช้เพื่อควบคุม the dead-time generation สำหรับ channels ทั้งหมด. จาก a reference waveform OCxREF, มันสร้าง 2 outputs OCx และ OCxN. ถ้า OCx และ OCxN เป็น active high:
•    The OCx output signal เป็นเหมือนอย่าง the reference signal ยกเว้นสำหรับ the rising edge, ซึ่งถูก delayed เกี่ยวข้องกับ
      the reference rising edge.
•    The OCxN output signal เป็นตรงกันข้ามของ the reference signal ยกเว้นสำหรับ the rising edge, ซึ่งถูก delayed เกี่ยวข้อง
      กับ the reference falling edge

ถ้า the delay ใหญ่กว่าความกว้างของ the active output (OCx or OCxN) ดังนั้น the corresponding pulse จะไม่ถูกสร้าง.

รูปต่อไปนี้แสดงความสัมพันธ์ระหว่าง the output signals ของ the dead-time generator และ the reference signal OCxREF. (เราสมมุติว่า CCxP=0, CCxNP=0, MOE=1, CCxE=1 และ CCxNE=1 ในตัวอย่างเหล่านี้)

(https://i.imgur.com/TIVfBqk.png)
(https://i.imgur.com/IfVp2GD.png)

The dead-time delay เป็นเหมือนกันสำหรับแต่ละของ the channels และโปรแกรมได้ด้วย the DTG bits ใน the TIMx_BDTR register. อ้างอิงถึง Section 14.4.18: TIM1 and TIM8 break and dead-time register (TIMx_BDTR) สำหรับการคำนวน delay

Re-directing OCxREF to OCx or OCxN
ใน output mode (forced, output compare or PWM), OCxREF สามารถถูกเปลี่ยนทิศทางไปยัง the OCx output หรือไปยัง OCxN output โดยการกำหนดค่า the CCxE และ CCxNE bits ใน the TIMx_CCER register.

นี้ยอมให้ผู้ใช้ส่ง a specific waveform (อย่างเช่น PWM หรือ static active level) บน one output ขณะที่ตัวเติมเต็ม (the complementary) ยังคงอยู่ที่ inactive level ของมัน. ความเป็นไปได้อื่นๆคือมีทั้งสอง outputs ที่ inactive level หรือทั้งสอง outputs ที่ active และเสริมด้วย dead-time.

Note: เมื่อมีเพียง OCxN ถูกเปิดการใช้งาน (CCxE=0, CCxNE=1), มันไม่ได้ถูกเติมเต็มและกลายเป็น active ทันทีที่ OCxREF
           เป็น high. ตัวอย่างเช่น, ถ้า CCxNP=0 ดังนั้น OCxN=OCxRef. ในทางกลับกัน, เมื่อทั้ง OCx และ OCxN ถูกเปิดการใช้งาน
           (CCxE=CCxNE=1) OCx กลายเป็น active เมื่อ OCxREF เป็น high ขณะที่ OCxN ถูกเติมเต็มและกลายเป็น active เมื่อ
           OCxREF เป็น low.

ปล. ท่านใดจะเอาไปขับโหลดอย่าง DC Motor ผ่านทางทรายซิสเตอร์ โดย OCx ขับ PNP แล้วอีกทาง OCxN ไปขับ NPN ก็ได้ หรือเอาไปทำ inverter ได้มั๊ย มี 3 channels พอดี ไปปรับความเร็วมอเตอร์สามเฟส ท่านใดทำได้มั่ง
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 11, 2020, 11:27:17 AM
14.3.12 Using the break function
เมื่อใช้ the break function, the output enable signals และ inactive levels ถูกแก้ไขสอดคล้องกับ additional control bits (MOE, OSSI และ OSSR bits ใน the TIMx_BDTR register, OISx และ OISxN bits ใน the TIMx_CR2 register). ไม่ว่าในกรณีใด, the OCx และ OCxN outputs ไม่สามารถถูกเซ้ตทั้งคู่เป็น active level ที่ a given time. อ้างอิงถึง Table 83 สำหรับรายละเอียดที่มากขึ้น

The break source สามารถเป็นอย่างใดอย่างหนึ่ง the break input pin หรือ a clock failure event, ที่สร้างโดย the Clock Security System (CSS), จาก the Reset Clock Controller. สำหรับข้อมูลเพิ่มเติมบน the Clock Security System, อ้างอิงถึง Section 7.2.7: Clock security system (CSS).

เมื่อออกจาก reset, the break circuit ถูกปิดการใช้งานและ the MOE bit เป็น low. ผู้ใช้สามารถเปิดการใช้งาน the break function โดยการเซ็ต the BKE bit ใน the TIMx_BDTR register. The break input polarity สามารถถูกเลือกโดยการกำหนดค่า the BKP bit ใน the same register. BKE และ BKP สามารถถูกแก้ไขที่เวลาเดียวกัน. เมื่อ the BKE และ BKP bits ถูกเขียน, a delay ของ 1 APB clock cycle ถูกใช้ก่อนการเขียนจะเป็นผล. ด้วยเหตุนี้, มันมีความจำเป็นที่ต้องคอย 1 APB clock period เพื่อการอ่านกลับบิตอย่างถูกต้องหลังจาก the write operation

เพราะว่า MOE falling edge สามารถถูก asynchronous, a resynchronization circuit ถูกแทรกระหว่าง the actual signal (ทำงานบน the outputs) และ the synchronous control bit (เข้าถึงใน the TIMx_BDTR register). ส่งผลให้เกิดการ delays ระหว่าง the asynchronous และ the synchronous signals.  โดยเฉพาะอย่างยิ่ง, ถ้า MOE ถูกเขียนเป็น 1 ในขณะที่มันเป็น low, a delay
(dummy instruction) ต้องถูกใส่ก่อนการอ่านมันได้อย่างถูกต้อง. นี้ก็เพราะว่าผู้ใช้เขียน an asynchronous signal, แต่อ่าน a synchronous signal

เมื่อ a break เกิดขึ้น (ระดับที่เลือกบน the break input):
•    The MOE bit ถูกเคลียร์ไม่พร้อมกัน(asynchronously), ผลัก the outputs ลงใน inactive state, idle state หรือลงใน reset
      state (ที่เลือกโดย the OSSI bit).  คุณสมบัตินี้ทำงานได้แม้ว่า the MCU oscillator ถูก off.
•    แต่ละ output channel ถูกขับด้วย the level ที่โปรแกรมใน the OISx bit ใน the TIMx_CR2 register ทันทีที่ MOE=0. ถ้า
      OSSI=0 จากนั้น the timer จะปลดปล่อย the enable output ถ้าเป็นอย่างอื่น the enable output ยังคง high.
•    เมื่อ complementary outputs ถูกใช้:
      – The outputs ถูกใส่เป็นอันดับแรกใน reset state inactive state (ขึ้นอยู่กับ the polarity). นี้ถูกทำแบบ asynchronous ดัง
         นั้นมันทำงานได้แม้ว่าไม่มี clock ถูกจัดให้มีถึง the timer.
      – ถ้า the timer clock ยังคงมีอยู่, ดังนั้น the dead-time generator ถูกทำให้ทำงานใหม่เพื่อขับ the outputs ด้วย the level ที่
         ถูกโปรแกรมใน the OISx และ OISxN bits หลังจาก a dead-time. แม้ในกรณีนี้ , OCx และ OCxN ไม่สามารถถูกขับถึง active
         level ของมันร่วมกัน. โปรดสังเกตว่าเนื่องจาก the resynchronization บน MOE, the dead-time duration นานกว่าปกติเล็ก
         น้อย (ประมาณ 2 ck_tim clock cycles).
      – ถ้า OSSI=0 ดังนั้น the timer จะปลดปล่อย the enable outputs ถ้าเป็นอย่างอื่น the enable outputs ยังคงหรือกลายเป็น
         high ทันทีที่หนึ่งใน the CCxE หรือ CCxNE bits เป็น high.
•    The break status flag (BIF bit ใน the TIMx_SR register) ถูก set. An interrupt สามารถถูกสร้างถ้า the BIE bit ใน the
      TIMx_DIER register ถูก set. A DMA request สามารถถูกส่งถ้า the BDE bit ใน the TIMx_DIER register ถูก set.
.•    ถ้า the AOE bit ใน the TIMx_BDTR register ถูก set, the MOE bit ถูกเซ็ตโดยอัตโนมัติอีกครั้งที่ the next update event
      UEV. นี้สามารถถูกใช้เพื่อกระทำตามระเบียบข้อบังคับ, อย่างเช่น. ถ้าเป็นอย่างอื่น, MOE ยังคงเป็น low จนกระทั่งถูกเขียนเป็น '1' อีก
      ครั้ง. ในกรณีนี้, มันสามารถถูกใช้สำหรับความปลอดภัยและ the break input สามารถถูกต่อไปยังสัญญาณเตือนจาก power drivers,
      thermal sensors หรือ security components ใดๆ

Note: The break inputs กำลังทำงานบน level. ดังนั้น, the MOE ไม่สามารถถูกเซ้ตในขณะที่ the break input เป็น active
           (ไม่ว่าโดยอัตโนมัติหรือโดย software). ในระหว่างนี้, the status flag BIF ไม่สามารถถูกเเคลียร์.

The break สามารถถูกสร้างโดย the BRK input ซึ่งมี a programmable polarity และ an enable bit BKE ใน the TIMx_BDTR Register.

มีสองวิธีในการสร้าง a break:
•    โดยการใช้ the BRK input ซึ่งมี a programmable polarity และ an enable bit BKE ใน the TIMx_BDTR register
•    โดย software ผ่านทาง the BG bit ของ the TIMx_EGR register.

นอกจาก the break input และ the output management, a write protection ถูกนำมาใช้ภายใน the break circuit เพื่อป้องกัน the application. มันยอมให้การแช่แข็ง the configuration ของ several parameters (dead-time duration, OCx/OCxN polarities และ state เมื่อปิดการใช้งาน, OCxM configurations, break enable and polarity). ผู้ใช้สามารถเลือก จากสามระดับของ
protection ที่เลือกโดย the LOCK bits ใน the TIMx_BDTR register. อ้างอิงถึง Section 14.4.18: TIM1 and TIM8 break and dead-time register (TIMx_BDTR). The LOCK bits สามารถถูกเขียนเพียงครั้งเดียวหลังจาก an MCU reset.

Figure 89 แสดงตัวอย่างของ behavior ของ the outputs ในการตอบสนองต่อ a break.

(https://i.imgur.com/TbYwTY2.png)
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 12, 2020, 06:57:53 AM
14.3.13 Clearing the OCxREF signal on an external event
The OCxREF signal สำหรับ a given channel สามารถถูกขับ Low โดยการใช้ a High level ไปยัง the ETRF input (OCxCE enable bit ของ the corresponding TIMx_CCMRx register เซ็ตเป็น '1'). The OCxREF signal ยังคง Low จนกระทั่ง the next update event, UEV, เกิดขึ้น.

function นี้สามารถถูกใช้เพียงใน output compare และ PWM modes, และไม่ทำงานใน forced mode.

ตัวอย่างเช่น, the ETR signal สามารถถูกต่อถึง the output ของ a comparator ที่ถูกใช้สำหรับการจัดการปัจจุบัน(current handling). ในกรณีนี้, ETR ต้องถูกกำหนดค่าดังต่อไปนี้ :
1.    The external trigger prescaler ควรถูกปิด: bits ETPS[1:0] ใน the TIMx_SMCR register ถูกเคลียร์เป็น 00.
2.    The external clock mode 2 ต้องถูกปิดการใช้งาน: bit ECE ใน the TIM1_SMCR register ถูกเคลียร์เป็น 0.
3.    The external trigger polarity (ETP) และ the external trigger filter (ETF) สามารถถูกกำหนดให้สอดคล้องกับ the
       application's needs.

Figure 90 แสดง the behavior ของ the OCxREF signal เมื่อ the ETRF input กลายเป็น high, สำหรับทั้งสองค่าของ the OCxCE enable bit. ในตัวอย่างนี้, the timer TIMx ถูกโปรแกรมใน PWM mode

(https://i.imgur.com/LAkce1A.png)
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 12, 2020, 07:56:32 AM
14.3.14 6-step PWM generation
เมื่อ complementary outputs ถูกใช้บน a channel, preload bits มีให้ใช้บน the OCxM, CCxE and CCxNE bits. The preload bits ถูกส่งถ่ายไปยัง the shadow bits ที่ the COM commutation event. ผู้ใช้สามารถดังนั้นโปรแกรมล่วงหน้า the configuration สำหรับ the next step และเปลี่ยน the configuration ของ the channels ทั้งหมดในเวลาเดียวกัน. COM สามารถถูกสร้างโดย
software โดยการเซ็ต the COM bit ใน the TIMx_EGR register หรือโดย hardware (บน TRGI rising edge).

A flag ถูกเซ็ตเมื่อ the COM event เกิดขึ้น (COMIF bit ใน the TIMx_SR register), ซึ่งสามารถสร้าง an interrupt (ถ้า the COMIE bit ถูกเซ็ตใน the TIMx_DIER register) หรือ a DMA request (ถ้า the COMDE bit ถูกเซ็ตใน the TIMx_DIER register).

Figure 91 อธิบาย the behavior ของ the OCx และ OCxN outputs เมื่อ a COM event เกิดขึ้น, ใน 3 ตัวอย่างที่ต่างกันของ programmed configurations

(https://i.imgur.com/bdPvnHv.png)
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 12, 2020, 08:48:21 AM
14.3.15 One-pulse mode
One-pulse mode (OPM) เป็นกรณีเฉพาะของโหมดก่อนหน้านี้. มันยอมให้ the counter ถูกเริ่มต้นเพื่อตอบสนองต่อสิ่งกระตุ้นและเพื่อสร้าง a pulse ด้วย a programmable length หลังจาก a programmable delay.

การเริ่มต้น the counter สามารถถูกควบคุมผ่านทาง the slave mode controller. การสร้าง the waveform สามารถถูกกระทำใน output compare mode หรือ PWM mode. เลือก One-pulse mode โดยการเซ็ต the OPM bit ใน the TIMx_CR1 register. นี้ทำให้ the counter หยุดโดยอัตโนมัติที่ the next update event UEV.

A pulse สามารถถูกสร้างอย่างถูกต้องเพียงถ้า the compare value แตกต่างจาก the counter initial value. ก่อนการเริ่มต้น (เมือ่ the timer กำลังรอสำหรับ the trigger), the configuration ต้องถูก:
•    ใน upcounting: CNT<CCRx ≤ ARR (โดยเฉพาะ, 0<CCRx),
•    ใน downcounting: CNT>CCRx.

(https://i.imgur.com/J9sXrlK.png)

ตัวอย่างเช่นผู้ใช้ต้องการสร้าง a positive pulse บน OC1 ด้วยความยาวเป็น tPULSE และหลังจาก a delay ของ tDELAY ทันใดนั้น a positive edge ถูกตรวจพบบน the TI2 input pin

มาใช้ TI2FP2 เป็น trigger 1:
•    แมป TI2FP2 เข้ากับ TI2 โดยการเขียน CC2S=01 ใน the TIMx_CCMR1 register.
•    TI2FP2 ต้องตรวจจับ a rising edge, เขียน CC2P=0 ใน the TIMx_CCER register.
•    กำหนด TI2FP2 เป็น trigger สำหรับ the slave mode controller (TRGI) โดยการเขียน TS=110 ใน the TIMx_SMCR
      register.
•    TI2FP2 ถูกใช้เพื่อเริ่มต้น the counter โดยการเขียน SMS เป็น '110 ใน the TIMx_SMCR register (trigger mode).

The OPM waveform ถูกกำหนดโดยการเขียน the compare registers (โดยคำนึงถึง the clock frequency และ the counter prescaler).
•    The tDELAY ถูกกำหนดโดยค่าที่เขียนใน the TIMx_CCR1 register
•    The tPULSE ถูกกำหนดโดยความแตกต่างระหว่าง the auto-reload value และ the compare value (TIMx_ARR -
      TIMx_CCR + 1).
•    ให้เราบอกผู้ใช้ที่ต้องการสร้าง a waveform ด้วยการเปลี่ยนจาก '0 ไปเป็น '1 เมื่อ a compare match เกิดขึ้นและการเปลี่ยนจาก '1
      ไปเป็น '0 เมื่อ the counter ถึง the auto-reload value. ในการดำเนินการนี้ให้เปิดการใช้งาน PWM mode 2 โดยการเขียน
      OC1M=111 ใน the TIMx_CCMR1 register. ผู้ใช้สามารถมีทางเลือกที่จะเปิดการใช้งาน the preload registers โดยการเขียน
      OC1PE=1 ใน the TIMx_CCMR1 register และ ARPE ใน the TIMx_CR1 register. ในกรณีนี้เขียน the compare value ใน
      the TIMx_CCR1 register, the auto-reload value ใน the TIMx_ARR register, สร้าง an update โดยการเซ็ต the UG
      bit และคอยสำหรับ external trigger event บน TI2. CC1P ถูกเขียนเป็น '0 ในตัวอย่างนี้.

ในตัวอย่างของเรา, the DIR และ CMS bits ใน the TIMx_CR1 register ควรเป็น low.

ผู้ใช้ต้องการเพียง one pulse (Single mode), ดังนั้นให้เขียน '1 ใน the OPM bit ใน the TIMx_CR1 register เพื่อหยุด the counter ที่ the next update event (เมื่อ the counter ม้วนวนจาก the auto-reload value กลับไปยัง 0). เมื่อ OPM bit ใน the TIMx_CR1 register ถูกเซ็ตเป็น '0', ดังนั้น the Repetitive Mode ถูกเลือก.

Particular case: OCx fast enable:
ใน One-pulse mode, the edge detection บน TIx input เซ็ต the CEN bit ซึ่งเปิดการใช้งาน the counter. จากนั้น the comparison ระหว่าง the counter และ the compare value ทำให้ the output toggle. แต่ several clock cycles ถูกต้องการสำหรับการทำงานเหล่านี้และมันจำกัด the minimum delay tDELAY เป็นนาทีที่เราสามารถจะได้.

เพื่อส่งออก a waveform ที่มี the minimum delay, ผู้ใช้สามารถเซ็ต the OCxFE bit ใน the TIMx_CCMRx register. จากนั้น OCxRef (และ OCx) ถูกบังคับให้ตอบสนองต่อสิ่งกระตุ้นโดยไม่คำนึงถึง the comparison. new level ของมันเป็นเหมือนกับ a compare match ที่เกิดขึ้นแล้ว. OCxFE ทำงานก็ต่อเมื่อ the channel ถูกกำหนดใน PWM1 หรือ PWM2 mode.
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 12, 2020, 10:56:44 AM
14.3.16 Encoder interface mode
เพื่อเลือก Encoder Interface mode เขียน SMS='001 ใน the TIMx_SMCR register ถ้า the counter กำลังนับบน TI2 edges เพียงอย่างเดียว, SMS=010 ถ้ามันกำลังนับบน TI1 edges เพียงอย่างเดียวและ SMS=011 ถ้ามันกำลังนับบนทั้ง TI1 and TI2 edges.

เลือก the TI1 และ TI2 polarity โดยการโปรแกรม the CC1P และ CC2P bits ใน the TIMx_CCER register. เมื่อจำเป็น, ก็โปรแกรม the input filter ด้วย.

The two inputs TI1 และ TI2 ถูกใช้เพื่อ interface กับ an incremental encoder. อ้างอิงถึง Table 81. The counter ถูก clocked โดยแต่ละ valid transition บน TI1FP1 หรือ TI2FP2 (TI1 และ TI2 หลังจาก input filter และ polarity selection, TI1FP1=TI1 ถ้าไม่ filtered และไม่ inverted, TI2FP2=TI2 ถ้าไม่ filtered และไม่ inverted) ทึกทักเอาว่ามันถูกเปิดการใช้งาน (CEN bit ใน TIMx_CR1 register ถูกเขียนเป็น '1). The sequence ของ transitions ของ the two inputs ถูกตีค่าและสร้าง count pulses เช่นเดียวกันกับ the direction signal. ขึ้นอยู่กับ the sequence the counter นับขึ้นหรือลง, the DIR bit ใน the TIMx_CR1 register ถูกแก้ไขโดย hardware สอดคล้องกัน. The DIR bit ถูกคำนวณที่แต่ละ transition บน input ใดๆ (TI1 หรือ TI2), อะไรก็ได้ the counter นับบน TI1 เท่านั้น, TI2 เท่านั้นหรือทั้งคู่ TI1 และ TI2.

Encoder interface mode ทำงานอย่างง่ายๆคล้าย an external clock ที่มี direction selection. นี้หมายความว่า the counter เพียงนับต่อเนื่องไประหว่าง 0 และ the auto-reload value ใน the TIMx_ARR register (0 ไปยัง ARR หรือ ARR ลงไป 0 ขึ้นอยู่กับ the direction). ดังนั้นผู้ใช้ต้องกำหนด TIMx_ARR ก่อนการสตาร์ท. ในทำนองเดียวกัน, the capture, compare, prescaler, trigger output features ยังคงทำงานได้ตามปกติ. Encoder mode และ External clock mode 2 เข้ากันไม่ได้และต้องไม่ถูกเลือกเข้าด้วยกัน.

ในโหมดนี้, the counter ถูกปรับเปลี่ยนโดยอัตโนมัติตาม the speed และ the direction ของ the incremental encoder และสิ่งที่อยู่ภายในของมัน, ดังนั้น,จึงแสดง the encoder's position เสมอ. The count direction ตรงกันกับ the rotation direction ของ the connected sensor.

Table 81 สรุป the possible combinations, ทึกทักเอาว่า TI1 และ TI2 ไม่สวิทช์ในเวลาเดียวกัน.

(https://i.imgur.com/u3fVp2p.png)

An external incremental encoder สามารถถูกต่อโดยตรงเข้ากับ the MCU โดยปราศจาก external interface logic. อย่างไรก็ตาม, comparators โดยปกติถูกใช้แปลง the encoder's differential outputs ไปเป็น digital signals. สิ่งนี้เพิ่ม noise immunity อย่างมาก. The third encoder output ซึ่งแสดง the mechanical zero position, อาจถูกต่อถึง an external interrupt input และทริกเกอร์ a counter reset.

Figure 93 แสดงตัวอย่างของ counter operation, แสดง count signal generation และ direction control. มันยังแสดงวิธีที่ input jitter ถูกชดเชยในที่ๆทั้งสอง edges ถูกเลือก. สิ่งนี้อาจเกิดขึ้นหาก the sensor อยู่ในตำแหน่งใกล้กับ the switching points จุดใดจุดหนึ่ง. สำหรับตัวอย่างนี้เราถือว่าการกำหนดค่ามีดังต่อไปนี้:
•    CC1S= '01' (TIMx_CCMR1 register, TI1FP1 mapped on TI1)
•    CC2S= '01' (TIMx_CCMR2 register, TI2FP2 mapped on TI2)
•    CC1P= '0', CC1NP = '0', IC1F ='0000' (TIMx_CCER register, TI1FP1 noninverted, TI1FP1=TI1)
•    CC2P= '0', CC2NP = '0', IC2F ='0000' (TIMx_CCER register, TI2FP2 noninverted, TI2FP2=TI2)
•    SMS= '011' (TIMx_SMCR register, ทั้งสอง inputs ทำงานบนทั้ง rising และ falling edges)
•    CEN = 1 (TIMx_CR1 register, Counter ถูกเปิดการใช้งาน)

(https://i.imgur.com/8roZR3n.png)

Figure 94 แสดงตัวอย่างของ counter behavior เมื่อ TI1FP1 polarity ถูกกลับ (การกำหนดเหมือนอย่างข้างบน ยกเว้น CC1P=1).

(https://i.imgur.com/hjDVNaZ.png)

The timer, เมื่อกำหนดค่าใน Encoder Interface mode จัดให้มี information บน the sensor's current position. ผู้ใช้สามารถได้รับ dynamic information (speed, acceleration, deceleration) โดยการวัด the period ระหว่าง two encoder events โดยใช้ a second timer กำหนดค่าใน capture mode. The output ของ the encoder ซึ่งแสดง the mechanical zero สามารถถูกใช้สำหรับจุดมุ่งหมายนี้. ขึ้นอยู่กับ the time ระหว่าง two events, the counter สามารถถูกอ่านที่ regular times ได้อีกด้วย. ผู้ใช้สามารถทำการนี้โดยล็อค(latching) the counter value ลงใน a third input capture register ถ้ามีให้ใช้ (จากนั้น the capture signal ต้องถูกทำเป็นระยะๆและสามารถถูกสร้างโดย timer อื่น). เมื่อมีให้ใช้, มันเป็นไปได้ที่จะอ่านค่าของมันได้อีกด้วยผ่านทาง a DMA request ที่สร้างโดย a Real-Time clock.
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 13, 2020, 07:05:35 AM
14.3.17 Timer input XOR function
The TI1S bit ใน the TIM1_CR2 register, ยอมให้ the input filter ของ channel 1 ถูกต่อถึง the output ของ a XOR gate, รวม the three input pins TIMx_CH1, TIMx_CH2 และ TIMx_CH3.

The XOR output สามารถถูกใช้กับทุก the timer input functions อย่างเช่น trigger หรือ input capture.

The XOR output สามารถถูกใช้กับทุก the timer input functions อย่างเช่น trigger หรือ input capture. ตัวอย่างของคุณลักษณะนี้ใช้เพื่ออินเตอร์เฟส Hall sensors ถูกให้ไว้ใน Section 14.3.18.
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 13, 2020, 07:31:04 AM
14.3.18 Interfacing with Hall sensors
นี้ทำได้โดยใช้ the advanced-control timers (TIM1 or TIM8) สร้าง PWM signals เพื่อขับ the motor และ timer TIMx อื่นๆ (TIM2, TIM3, TIM4 หรือ TIM5) เรียกว่า "interfacing timer" ใน Figure 95. The "interfacing timer" จับ the 3 timer input pins (TIMx_CH1, TIMx_CH2, และ TIMx_CH3) ที่ต่อผ่านทาง a XOR ไปยัง the TI1 input channel (ที่เลือกโดยการเซ็ต the TI1S bit ใน the TIMx_CR2 register).

The slave mode controller ถูกกำหนดใน reset mode; the slave input คือ TI1F_ED. ดังนั้น, แต่ละเวลาที่หนึ่งของ the 3 inputs สลับ, the counter สตาร์ทการนับใหม่จาก 0. นี้สร้าง a time base ทริกเกอร์โดยการเปลี่ยนใดๆบน the Hall inputs.

บน the "interfacing timer", capture/compare channel 1 ถูกกำหนดใน capture mode, capture signal คือ TRC (ดู Figure 78). The captured value, ซึ่งตรงกันกับเวลาที่ผ่านไประหว่าง 2 การเปลี่ยนบน the inputs, ให้ข้อมูลเกี่ยวกับ motor speed.

The "interfacing timer" สามารถถูกใช้ใน output mode เพื่อสร้าง a pulse ซึ่งเปลี่ยนการกำหนดค่าของ the channels ของ the advanced-control timer (TIM1 or TIM8) (โดยการทริกเกอร์ a COM event). The TIM1 timer ถูกใช้เพื่อสร้าง PWM signals เพื่อขับ the motor. เพื่อทำการนี้, the interfacing timer channel ต้องถูกโปรแกรมเพื่อว่า a positive pulse ถูกสร้างหลังจาก a programmed delay (ใน output compare หรือ PWM mode). pulse นี้ถูกส่งไปยัง the advanced-control timer (TIM1 or TIM8) ผ่านทาง the TRGO output.

ตัวอย่าง: ผู้ใช้ต้องการเปลี่ยน the PWM configuration ของ the advanced-control timer TIM1 หลังจาก a programmed delay แต่ละเวลาการเปลี่ยนเกิดขึ้นบน the Hall inputs ที่ต่อถึงหนึ่งของ the TIMx timers.
•    กำหนด 3 timer inputs ORed ไปยัง the TI1 input channel โดยการเขียน the TI1S bit ใน the TIMx_CR2 register เป็น
      '1',
•    โปรแกรม the time base: เขียน the TIMx_ARR เป็น the max value (the counter ต้องถูกเคลียร์โดย the TI1 change. เซ็ต
      the prescaler เพื่อให้ได้ a maximum counter period ที่ยาวนานกว่าเวลาระหว่าง 2 การเปลี่ยนบน the sensors,
•    โปรแกรม channel 1 ใน capture mode (TRC ถูกเลือก): เขียน the CC1S bits ใน the TIMx_CCMR1 register เป็น '11'. ผู้
      ใช้สามารถโปรแกรม the digital filter อีกด้วยถ้าต้องการ,
•    โปรแกรม channel 2 ใน PWM 2 mode พร้อมด้วย the desired delay: เขียน the OC2M bits เป็น '111' และ the CC2S bits
      เป็น '00' ใน the TIMx_CCMR1 register,
•    เลือก OC2REF เป็น trigger output บน TRGO: เขียน the MMS bits ใน the TIMx_CR2 register เป็น '101',

ใน the advanced-control timer TIM1, the right ITR input ต้องถูกเลือกเป็น trigger input, the timer ถูกโปรแกรมเพื่อสร้าง PWM signals, the capture/compare control signals ถูกโหลดไว้ล่วงหน้า (CCPC=1 ใน the TIMx_CR2 register) และ the COM event ถูกควบคุมโดย the trigger input (CCUS=1 ใน the TIMx_CR2 register). The PWM control bits (CCxE, OCxM) ถูกเขียนหลังจาก a COM event สำหรับ the next step (นี้สามารถถูกทำใน an interrupt subroutine ที่สร้างโดย the rising edge ของ OC2REF).

Figure 95 อธิบายตัวอย่างนี้.

(https://i.imgur.com/Gny9hiG.png)
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 13, 2020, 08:01:02 AM
14.3.19 Timers and external trigger synchronization
The TIMx Timers สามารถถูก synchronized กับ an external trigger ใน several modes: Reset mode, Gated mode และ Trigger mode.

Slave mode: Reset mode
The counter และ prescaler ของมันสามารถถูกเริ่มต้นใหม่โดยตอบสนองต่อ an event บน a trigger input. ยิ่งไปกว่านั้น, ถ้า the URS bit จาก the TIMx_CR1 register เป็น low, an update event UEV จะถูกสร้าง. จากนั้นทุก the preloaded registers (TIMx_ARR, TIMx_CCRx) ถูกอัปเดต.

ในตัวอย่างต่อไปนี้, the upcounter ถูกเคลียร์เพื่อตอบสนองต่อ a rising edge บน TI1 input:
•    กำหนดค่า the channel 1 เพื่อตรวจจับ rising edges บน TI1. กำหนดค่า the input filter duration (ในตัวอย่างนี้, เราไม่ต้องการ
      filter ใดๆ, ดังนั้นเราเก็บ IC1F=0000). The capture prescaler ไม่ถูกใช้สำหรับการทริกเกอร์, ดังนั้นผู้ใช้ไม่ต้องการกำหนดค่ามัน.
      The CC1S bits เลือก the input capture source เท่านั้น, CC1S = 01 ใน the TIMx_CCMR1 register. เขียน CC1P=0 ใน
      TIMx_CCER register เพื่อตรวจสอบ the polarity (และตรวจจับ rising edges เท่านั้น).
•    กำหนดค่า the timer ใน reset mode โดยการเขียน SMS=100 ใน TIMx_SMCR register. เลือก TI1 เป็น the input source
      โดยการเขียน TS=101 ใน TIMx_SMCR register.
•    เริ่ม the counter โดยการเขียน CEN=1 ใน the TIMx_CR1 register.

The counter เริ่มการนับบน the internal clock, จากนั้นจะทำงานตามปกติจนกระทั่ง TI1 rising edge. เมื่อ TI1 พุ่งขึ้น, the counter ถูกเคลียร์และเริ่มใหม่จาก 0. ในขณะเดียวกัน, the trigger flag ถูกเซ็ต (TIF bit ใน the TIMx_SR register) และ an interrupt request, หรือ a DMA request สามารถถูกส่งถ้าเปิดการใช้งาน (ขึ้นอยู่กับ the TIE และ TDE bits ใน TIMx_DIER register).

รูปที่ 96 แสดงพฤติกรรมนี้เมื่อ the auto-reload register TIMx_ARR=0x36. The delay ระหว่าง the rising edge บน TI1 และ the actual reset ของ the counter เนื่องมาจาก the resynchronization circuit บน TI1 input.

(https://i.imgur.com/WOu9L93.png)
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 13, 2020, 08:36:57 AM
Slave mode: Gated mode
The counter สามารถถูกเปิดการใช้งานขึ้นอยู่กับ the level ของ a selected input.

ในตัวอย่างต่อไปนี้, the upcounter นับเพียงเมื่อ TI1 input เป็น low เท่านั้น:
•    กำหนดค่า the channel 1 เพื่อตรวจจับ low levels บน TI1. กำหนดค่า the input filter duration (ในตัวอย่างนี้, เราไม่ต้องการ
      filter ใดๆ, ดังนั้นเราเก็บ IC1F=0000). The capture prescaler ไม่ถูกใช้สำหรับการทริกเกอร์, ดังนั้นผู้ใช้ไม่ต้องการกำหนดค่ามัน.
      The CC1S bits เลือก the input capture source เท่านั้น, CC1S = 01 ใน the TIMx_CCMR1 register. เขียน CC1P=1 ใน
      TIMx_CCER register เพื่อตรวจสอบ the polarity (และตรวจจับ low level เท่านั้น).
•    กำหนดค่า the timer ใน gated mode โดยการเขียน SMS=101 ใน TIMx_SMCR register. เลือก TI1 เป็น the input source
      โดยการเขียน TS=101 ใน TIMx_SMCR register
•    เปิดการใช้งาน the counter โดยการเขียน CEN=1 ใน the TIMx_CR1 register (ใน gated mode, the counter จะไม่เริ่มถ้า
      CEN=0, ไม่ว่า the trigger input level คืออะไรก็ตาม).

The counter เริ่มการนับบน the internal clock ตราบเท่าที่ TI1 เป็น low และหยุดทันทีที่ TI1 มาเป็น high. The TIF flag ใน the TIMx_SR register ถูกเซ็ตทั้งเมื่อ the counter เริ่มหรือหยุด.

The delay ระหว่าง the rising edge บน TI1 และ the actual stop ของ the counter เนื่องมาจาก the resynchronization circuit บน TI1 input.

(https://i.imgur.com/z4JwwRt.png)
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 13, 2020, 08:41:39 AM
Slave mode: Trigger mode
The counter สามารถเริ่มโดยตอบสนองต่อ an event บน a selected input.

ในตัวอย่างต่อไปนี้, the upcounter เริ่มโดยตอบสนองต่อ a rising edge บน TI2 input:
•    กำหนดค่า the channel 2 เพื่อตรวจจับ low levels บน TI2. กำหนดค่า the input filter duration (ในตัวอย่างนี้, เราไม่ต้องการ
      filter ใดๆ, ดังนั้นเราเก็บ IC2F=0000). The capture prescaler ไม่ถูกใช้สำหรับการทริกเกอร์, ดังนั้นผู้ใช้ไม่ต้องการกำหนดค่ามัน.
      The CC2S bits เลือก the input capture source เท่านั้น, CC2S = 01 ใน the TIMx_CCMR1 register. เขียน CC2P=1 ใน
      TIMx_CCER register เพื่อตรวจสอบ the polarity (และตรวจจับ low level เท่านั้น).
•    กำหนดค่า the timer ใน trigger mode โดยการเขียน SMS=110 ใน TIMx_SMCR register. เลือก TI2 เป็น the input
      source โดยการเขียน TS=110 ใน TIMx_SMCR register.

เมื่อ a rising edge เกิดขึ้นบน TI2, the counter เริ่มการนับบน the internal clock และ the TIF flag ถูกเซ็ต

The delay ระหว่าง the rising edge บน TI2 และ the actual start ของ the counter เนื่องมาจาก the resynchronization circuit บน TI2 input.

(https://i.imgur.com/UPDPWma.png)
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 13, 2020, 08:45:52 AM
Slave mode: External Clock mode 2 + trigger mode
The external clock mode 2 สามารถถูกใช้พร้อมทั้ง slave mode อื่นๆ (ยกเว้น external clock mode 1 และ encoder mode). ในกรณีนี้, the ETR signal ถูกใช้เป็น external clock input, และ input อื่นสามารถถูกเลือกเป็น trigger input เมื่อทำงานใน reset mode, gated mode หรือ trigger mode. แนะนำไม่ให้เลือก ETR เป็น TRGI ผ่านทาง the TS bits ของ TIMx_SMCR register.

ในตัวอย่างต่อไปนี้, the upcounter ถูกเพิ่มขึ้นที่แต่ละ rising edge ของ the ETR signal ทันทีที่ a rising edge ของ TI1 เกิดขึ้น
1.    กำหนดค่า the external trigger input circuit โดยการโปรแกรม the TIMx_SMCR register ดังต่อไปนี้:
      – ETF = 0000: ไม่มี filter
      – ETPS = 00: prescaler ปิดการใช้งาน
      – ETP = 0: การตรวจจับของ rising edges บน ETR และ ECE=1 เพื่อเปิดการใช้งาน the external clock mode 2.
2.    กำหนดค่า the channel 1 ดังต่อไปนี้, เพื่อตรวจจับ rising edges บน TI:
      – IC1F = 0000: ไม่มี filter.
      – The capture prescaler ไม่ถูกใช้สำหรับการทริกเกอร์และไม่ต้องการกำหนดค่า.
      – CC1S = 01 ใน TIMx_CCMR1 register เพื่อเลือกเพียง the input capture source
      – CC1P = 0 ใน TIMx_CCER register เพื่อตรวจสอบ the polarity (และตรวจจับ rising edge เท่านั้น).
3.    กำหนดค่า the timer ใน trigger mode โดยการเขียน SMS=110 ใน TIMx_SMCR register. เลือก TI1 เป็น the input
       source โดยการเขียน TS=101 ใน TIMx_SMCR register

A rising edge บน TI1 เปิดการใช้งาน the counter และเซ็ต the TIF flag. The counter จากนั้นนับบน ETR rising edges.

The delay ระหว่าง the rising edge ของ the ETR signal และ the actual reset ของ the counter เนื่องมาจาก the resynchronization circuit บน ETRP input.

(https://i.imgur.com/lfBxjPn.png)
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 13, 2020, 09:10:55 AM
14.3.20 Timer synchronization
The TIMx timers ถูกเชื่อมโยงเข้าด้วยกันภายในสำหรับ timer synchronization หรือ chaining. อ้างอิงถึง
Section 15.3.15: Timer synchronization สำหรับรายละเอียด.

Note: The clock ของ the slave timer ต้องถูกเปิดการใช้งานก่อนเพื่อได้รับ events จาก the master timer, และต้องไม่ถูก
           เปลี่ยนกลางอากาศขณะที่ triggers ถูกได้รับจาก the master timer.
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 13, 2020, 09:25:48 AM
14.3.21 Debug mode
เมื่อ the microcontroller เข้าสู่ debug mode (Cortex®-M3 core - ถูกหยุด), the TIMx counter ยังคงทำงานตามปกติหรือหยุดอย่างใดอย่างหนึ่ง, ขึ้นอยู่กับ DBG_TIMx_STOP configuration bit ใน DBGMCU module. สำหรับรายละเอียดที่มากขึ้น, อ้างอิงถึงo Section 31.16.2: Debug support for timers, watchdog, bxCAN and I2C.

เพื่อความมุ่งหมายด้านปลอดภัย, เมื่อ the counter ถูกหยุด (DBG_TIMx_STOP = 1 ใน DBGMCU_APBx_FZ register), the outputs ถูกปิดการใช้งาน (ราวกับว่า the MOE bit ถูก reset). The outputs สามารถอย่างใดอย่างหนึ่งถูกบังคับเป็น an inactive state (OSSI bit = 1), หรือมีการควบคุมของมันเข้าควบคุมโดย the GPIO controller (OSSI bit = 0) เพื่อ force พวกมันเป็นไฮอิมพิแด๊นท
(Hi-Z).
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 13, 2020, 09:53:08 AM
14.4 TIM1 and TIM8 registers
อ้างอิงถึง Section 2.2 สำหรับรายการของตัวย่อที่ใช้ใน register descriptions.

The peripheral registers สามารถถูกเข้าถึงโดย half-words (16-bit) หรือ words (32-bit).

(https://i.imgur.com/7Pqwdls.png)

Bits 15:10 Reserved, must be kept at reset value.

Bits 9:8 CKD: Clock division
bit-field นี้แสดง the division ratio ระหว่าง the timer clock (CK_INT) frequency และ the dead-time และ sampling clock ที่ถูกใช้โดย the digital filters (ETR, TIx),
      00: tDTS = tCK_INT
      01: tDTS = 2 x tCK_INT
      10: tDTS = 4 x tCK_INT
      11: Reserved

Bit 7 ARPE: Auto-reload preload enable
     0: TIMx_ARR register ไม่ถูก buffered
     1: TIMx_ARR register ถูก buffered

Bits 6:5 CMS: Center-aligned mode selection
     00: Edge-aligned mode. The counter นับขึ้นหรือลงขึ้นอยู่กับ the direction bit (DIR).
     01: Center-aligned mode 1. The counter นับขึ้นและลงอีกทางหนึ่ง. Output compare interrupt flags ของ channels ที่
           กำหนดค่าใน output (CCxS=00 ใน TIMx_CCMRx register) ถูกเซ็ตเพียงเมื่อ the counter กำลังนับลง.
     10: Center-aligned mode 2. The counter นับขึ้นและลงอีกทางหนึ่ง. Output compare interrupt flags ของ channels ที่
           กำหนดค่าใน output (CCxS=00 ใน TIMx_CCMRx register) ถูกเซ็ตเพียงเมื่อ the counter กำลังนับขึ้น.
     11: Center-aligned mode 3. The counter นับขึ้นและลงอีกทางหนึ่ง. Output compare interrupt flags ของ channels ที่
           กำหนดค่าใน output (CCxS=00 ใน TIMx_CCMRx register) ถูกเซ็ตทั้งเมื่อ the counter กำลังนับขึ้นหรือลง.
Note: จะไม่ยอมให้สวิทช์จาก edge-aligned mode ไปยัง center-aligned mode ตราบเท่าที่ the counter ถูก enabled
            (CEN=1)

Bit 4 DIR: Direction
     0: Counter ถูกใช้เป็น upcounter
     1: Counter ถูกใช้เป็น downcounter
Note: บิตนี้ถูกอ่านได้เพียงเมื่อ the timer ถูกกำหนดค่าใน Center-aligned mode หรือ Encoder mode.

Bit 3 OPM: One-pulse mode
     0: Counter ไม่ถูกหยุดที่ update event
     1: Counter หยุดการนับที่ the next update event (โดยการเคลียร์ the bit CEN)

Bit 2 URS: Update request source
บิตนี้ถูกเซ็ตและเคลียร์โดย software เพื่อเลือก the UEV event sources.
     0: the following events ใดๆสร้าง an update interrupt หรือ DMA request ถ้า enabled. events เหล่านี้สามารถเป็น:
         – Counter overflow/underflow
         – การเซ็ต the UG bit
         – Update generation ผ่านทาง the slave mode controller
     1: เพียง counter overflow/underflow เท่านั้นที่สร้าง an update interrupt หรือ DMA request ถ้า enabled.

Bit 1 UDIS: Update disable
บิตนี้ถูกเซ็ตและเคลียร์โดย software เพื่อเปิดการใช้งาน/ปิดการใช้งาน UEV event generation.
     0: UEV ถูกเปิดการใช้งาน. The Update (UEV) event ถูกสร้างโดยหนึ่งของ the following events:
         – Counter overflow/underflow
         – การเซ็ต the UG bit
         – Update generation ผ่านทาง the slave mode controller
     Buffered registers จากนั้นถูกโหลดด้วย preload values ของมัน.
     1: UEV ถูกปิดการใช้งาน. The Update event ไม่ถูกสร้าง, shadow registers เก็บค่าของมัน (ARR, PSC, CCRx). อย่างไรก็ตาม
         the counter และ the prescaler จะถูกเริ่มต้นใหม่ถ้า the UG bit ถูกเซ็ตหรือถ้า a hardware reset ถูกได้รับจาก the slave
         mode controller.

Bit 0 CEN: Counter enable
     0: Counter ถูกปิดการใช้งาน
     1: Counter ถูกเปิดการใช้งาน
Note: External clock, gated mode และ encoder mode สามารถทำงานเพียงถ้า the CEN bit ถูกเซ็ตมาก่อนแล้วโดย
         software. อย่างไรก็ตาม trigger mode สามารถเซ็ต the CEN bit โดยอัตโนมัติโดย hardware.
CEN ถูกเคลียร์โดยอัตโนมัติใน one-pulse mode, เมื่อ an update event เกิดขึ้น.
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 13, 2020, 11:03:35 AM
(https://i.imgur.com/Pwba2r9.png)

Bit 15 Reserved, must be kept at reset value.

Bit 14 OIS4: Output Idle state 4 (OC4 output)
     อ้างอิงถึง OIS1 bit

Bit 13 OIS3N: Output Idle state 3 (OC3N output)
     refer to OIS1N bit

Bit 12 OIS3: Output Idle state 3 (OC3 output)
     refer to OIS1 bit

Bit 11 OIS2N: Output Idle state 2 (OC2N output)
     refer to OIS1N bit

Bit 10 OIS2: Output Idle state 2 (OC2 output)
     refer to OIS1 bit

Bit 9 OIS1N: Output Idle state 1 (OC1N output)
     0: OC1N=0 หลังจาก a dead-time เมื่อ MOE=0
     1: OC1N=1 หลังจาก a dead-time เมื่อ MOE=0
Note: บิตนี้ไม่สามารถถูกแก้ไขตราบเท่าที่ LOCK level 1, 2 หรือ 3 ถูกโปรแกรมแล้ว (LOCK bits ใน TIMx_BDTR register).

Bit 8 OIS1: Output Idle state 1 (OC1 output)
     0: OC1=0 (หลังจาก a dead-time ถ้า OC1N ถูกใช้งาน) เมื่อ MOE=0
     1: OC1=1 (หลังจาก a dead-time ถ้า OC1N ถูกใช้งาน) เมื่อ MOE=0
Note: บิตนี้ไม่สามารถถูกแก้ไขตราบเท่าที่ LOCK level 1, 2 หรือ 3 ถูกโปรแกรมแล้ว (LOCK bits ใน TIMx_BDTR register).

Bit 7 TI1S: TI1 selection
     0: The TIMx_CH1 pin ถูกต่อถึง TI1 input
     1: The TIMx_CH1, CH2 and CH3 pins ถูกต่อถึง the TI1 input (XOR combination)
ดู Section 14.3.18: Interfacing with Hall sensors ด้วย

Bits 6:4 MMS[2:0]: Master mode selection
บิตเหล่านี้ยอมให้เลือก the information ถูกส่งใน master mode ไปยัง slave timers สำหรับ synchronization (TRGO). The combination เป็นดังต่อไปนี้ :
     000: Reset - the UG bit จาก the TIMx_EGR register ถูกใช้เป็น trigger output (TRGO). ถ้า the reset ถูกสร้างโดย the
             trigger input (slave mode controller กำหนดค่าใน reset mode) จากนั้น the signal บน TRGO จะถูก delayed เปรียบ
             เทียบกับ the actual reset.
     001: Enable - the Counter enable signal, CNT_EN, ถูกใช้เป็น trigger output (TRGO). มันมีประโยชน์ในการเริ่ม timers
             หลายตัวในเวลาเดียวกันหรือเพื่อควบคุม a window ที่ซึ่ง a slave timer ถูก enabled. The Counter Enable signal ถูก
             สร้างโดย a logic OR ระหว่าง CEN control bit และ the trigger input เมื่อกำหนดค่าใน gated mode. เมื่อ the Counter
             Enable signal ถูกควบคุมโดย the trigger input, มี a delay บน TRGO, ยกเว้นถ้าf the master/slave mode ถูกเลือก
             (ดู the MSM bit description ใน TIMx_SMCR register).
     010: Update - The update event ถูกเลือกเป็น trigger output (TRGO). ตัวอย่างเช่น a master timer สามารถจากนั้นถูกใช้
             เป็น a prescaler สำหรับ a slave timer.
     011: Compare Pulse - The trigger output ส่ง a positive pulse เมื่อ the CC1IF flag ถูกเซ็ต (แม้ว่ามันเป็น high อยู่แล้ว),
             ทันทีที่ a capture หรือ a compare match เกิดขึ้น. (TRGO)
     100: Compare - OC1REF signal ถูกใช้เป็น trigger output (TRGO)
     101: Compare - OC2REF signal is used as trigger output (TRGO)
     110: Compare - OC3REF signal is used as trigger output (TRGO)
     111: Compare - OC4REF signal is used as trigger output (TRGO)
Note: The clock ของ the slave timer และ ADC ต้องถูก enabled ก่อนที่จะรับ events จาก the master timer, และต้อง
          ไม่ถูกเปลี่ยนกลางอากาศขณะที่ triggers ถูกได้รับจาก the master timer.

Bit 3 CCDS: Capture/compare DMA selection
     0: CCx DMA request ส่งเมื่อ CCx event เกิดขึ้น
     1: CCx DMA requests ส่งเมื่อ update event เกิดขึ้น

Bit 2 CCUS: Capture/compare control update selection
     0: เมื่อ capture/compare control bits ถูกโหลดไว้ก่อนมันถูกอัปเดตโดยการเซ็ต the COMG bit เท่านั้น
     1: เมื่อ capture/compare control bits ถูกโหลดไว้ก่อน (CCPC=1), มันถูกอัปเดตโดยการเซ็ต the COMG bit หรือเมื่อ an
         rising edge เ้นกิดขึนบน TRGI
Note: บิตนี้ทำงานเพียงบน channels ที่มี a complementary output.

Bit 1 Reserved, must be kept at reset value.

Bit 0 CCPC: Capture/compare preloaded control
     0: CCxE, CCxNE and OCxM bits ไม่ถูก preloaded
     1: CCxE, CCxNE and OCxM bits ถูก preloaded, หลังจากเขียนแล้ว, มันจะอัปเดตเพียงเมื่อ a commutation event (COM)
         เกิดขึ้น (COMG bit เซ็ตหรือ rising edge ถูกตรวจพบบน TRGI, ขึ้นอยู่กับ the CCUS bit).
Note: บิตนี้ทำงานเพียงบน channels ที่มี a complementary output.
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 14, 2020, 08:02:03 AM
(https://i.imgur.com/GejMpWK.png)

Bit 15 ETP: External trigger polarity
บิตนี้เลือกว่า ETR หรือ ETR(inverted) ถูกใช้สำหรับ trigger operations
     0: ETR เป็น non-inverted, active ที่ high level หรือ rising edge
     1: ETR เป็น inverted, active ที่ low level หรือ falling edge

Bit 14 ECE: External clock enable
บิตนี้เปิดการใช้งาน External clock mode 2.
     0: External clock mode 2 ถูกปิดการใช้งาน
     1: External clock mode 2 ถูกเปิดการใช้งาน . The counter ถูก clocked โดย active edge ใดๆบน the ETRF signal.

1: การเซ็ต the ECE bit มีผลเดียวกันกับการเลือก external clock mode 1 ด้วย TRGI ถูกต่อกับ ETRF (SMS=111 และ TS=111).
2: เป็นไปได้ที่จะใช้ external clock mode 2 พร้อมด้วย the following slave modes: reset mode, gated mode และ trigger
    mode. อย่างไรก็ตาม, TRGI ต้องไม่ถูกต่อถึง ETRF ในกรณีนี้ (TS bits ต้องไม่เป็น 111).
3: ถ้า external clock mode 1 และ external clock mode 2 ถูกเปิดการใช้งานในเวลาเดียวกัน, the external clock input คือ
    ETRF.

Bits 13:12 ETPS: External trigger prescaler
External trigger signal ETRP frequency ต้องไม่เกิน 1/4 ของ CK_INT frequency. A prescaler สามารถถูกเปิดการใช้งานเพื่อลด ETRP frequency. มันมีประโยชน์เมื่อมีการป้อนเข้า fast external clocks.
     00: Prescaler OFF
     01: ETRP frequency ถูกหารโดย 2
     10: ETRP frequency divided by 4
     11: ETRP frequency divided by 8

Bits 11:8 ETF[3:0]: External trigger filter
bit-field นี้จะกำหนด the frequency ที่ใช้เพื่อสุ่ม ETRP signal และความยาวของ digital filter ที่ใช้กับ ETRP. The digital filter ถูกทำจาก an event counter ที่ซึ่ง N consecutive events ถูกต้องการเพื่อทำให้ถูกต้อง a transition บน the output:
     0000: No filter, การสุ่มถูกทำที่ fDTS
     0001: fSAMPLING=fCK_INT, N=2
     0010: fSAMPLING=fCK_INT, N=4
     0011: fSAMPLING=fCK_INT, N=8²
     0100: fSAMPLING=fDTS/2, N=6
     0101: fSAMPLING=fDTS/2, N=8
     0110: fSAMPLING=fDTS/4, N=6²
     0111: fSAMPLING=fDTS/4, N=8
     1000: fSAMPLING=fDTS/8, N=6
     1001: fSAMPLING=fDTS/8, N=8
     1010: fSAMPLING=fDTS/16, N=5
     1011: fSAMPLING=fDTS/16, N=6
     1100: fSAMPLING=fDTS/16, N=8
     1101: fSAMPLING=fDTS/32, N=5
     1110: fSAMPLING=fDTS/32, N=6
     1111: fSAMPLING=fDTS/32, N=8

Bit 7 MSM: Master/Slave mode
     0: No action
     1: The effect ของ an event บน the trigger input (TRGI) ถูก delayed เพื่อยอมให้ a perfect synchronization ระหว่าง
         the current timer และ slaves ของมัน (ผ่านทาง TRGO). มันมีประโยชน์ถ้าเราต้องการ synchronize หลายๆ timers บน a
         single external event.

Bits 6:4 TS: Trigger selection
bit-field นี้เลือก the trigger input ถูกใช้เพื่อประสานพร้อมกัน the counter.
     000: Internal Trigger 0 (ITR0).
     001: Internal Trigger 1 (ITR1).
     010: Internal Trigger 2 (ITR2).
     011: Internal Trigger 3 (ITR3).
     100: TI1 Edge Detector (TI1F_ED)
     101: Filtered Timer Input 1 (TI1FP1)
     110: Filtered Timer Input 2 (TI2FP2)
     111: External Trigger input (ETRF)
ดู Table 86: TIMx Internal trigger connection สำหรับรายละเอียดที่มากขึ้นบน ITRx ความหมายสำหรับแต่ละ Timer.
Note: บิตเหล่านี้ต้องถูกเปลี่ยนได้เพียงเมื่อมันไม่ถูกใช้ (ตัวอย่างเข่นเมื่อ SMS=000) เพื่อหลีกเลี่ยง wrong edge detections ที่
            the transition

Bit 3 Reserved, must be kept at reset value.

Bits 2:0 SMS: Slave mode selection
เมื่อ external signals ถูกเลือก the active edge ของ the trigger signal (TRGI) ถูกเชื่อมโยงถึง the polarity เลือกบน the external input (ดู Input Control register and Control Register
description.
     000: Slave mode ถูกปิดการใช้งาน - ถ้า CEN = '1 จากนั้น the prescaler ถูก clocked โดยตรงโดย the internal clock.
     001: Encoder mode 1 - Counter นับขึ้น/ลงบน TI2FP1 edge ขึ้นอยู่กับ TI1FP2 level.
     010: Encoder mode 2 - Counter นับขึ้น/ลงบน TI1FP2 edge ขึ้นอยู่กับ TI2FP1 level.
     011: Encoder mode 3 - Counter นับขึ้น/ลงบนทั้ง TI1FP1 และ TI2FP2 edges ขึ้นอยู่กับ the level ของ the other input.
     100: Reset Mode - Rising edge ของ the selected trigger input (TRGI) เริ่มต้นใหม่ the counter และสร้าง an update
             ของ the registers.
     101: Gated Mode - The counter clock ถูก enabled เมื่อ the trigger input (TRGI) เป็น high. The counter หยุด (แต่ไม่
             ถูก reset) ทันทีที่ the trigger กลายมาเป็น low. ทั้ง start และ stop ของ the counter ถูกควบคุม.
     110: Trigger Mode - The counter เริ่มที่ a rising edge ของ the trigger TRGI (แต่ไม่ถูก reset). มีเพียง the start ของ
             the counter เท่านั้นที่ถูกควบคุม.
     111: External Clock Mode 1 - Rising edges ของ the selected trigger (TRGI) ให้สัญญาณนาฬิกา the counter.

Note: The gated mode ต้องไม่ถูกใช้ถ้า TI1F_ED ถูกเลือกเป็น the trigger input (TS=100). อันที่จริง, TI1F_ED
             outputs 1 พัลส์สำหรับแต่ละ transition บน TI1F, ในขณะที่ the gated mode
             ตรวจเช็ค the level ของ the trigger signal.

             The clock ของ the slave timer ต้องถูก enabled ก่อนเพื่อรับ events จาก the master timer, และต้องไม่ถูกเปลี่ยนกลาง
             อากาศขณะที่ triggers ถูกได้รับจาก the master timer.

(https://i.imgur.com/LoZ2O6u.png)
1. เมื่อ a timer ไม่ถูกแสดงใน the product, the corresponding trigger ITRx ก็ไม่มีให้ใช้งาน
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 14, 2020, 04:18:13 PM
(https://i.imgur.com/H3Cf7fw.png)
(https://i.imgur.com/1MH4smL.png)
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 14, 2020, 04:44:28 PM
(https://i.imgur.com/oeNBbdY.png)

Bit 15:13 Reserved, must be kept at reset value.

Bit 12 CC4OF: Capture/Compare 4 overcapture flag
อ้างอิงถึง CC1OF description

Bit 11 CC3OF: Capture/Compare 3 overcapture flag
refer to CC1OF description

Bit 10 CC2OF: Capture/compare 2 overcapture flag
refer to CC1OF description

Bit 9 CC1OF: Capture/Compare 1 overcapture flag
flag นี้ถูกเซ็ตโดย hardware เพียงเมื่อ the corresponding channel ถูกกำหนดค่าใน input capture mode. มันถูกเคลียร์โดย software โดยการเขียนมันเป็น '0'.
     0: ไม่มี overcapture ถูกตรวจจับแล้ว.
     1: The counter value ถูก captured แล้วใน TIMx_CCR1 register ขณะที่ CC1IF flag ถูกเซ็ตอยู่

Bit 8 Reserved, must be kept at reset value.

Bit 7 BIF: Break interrupt flag
flag นี้ถูกเซ็ตโดย hardware ทันทีที่ the break input ไปเป็น active. มันสามารถถูกเคลียร์โดย software ถ้า the break input ไม่ active.
     0: ไม่มี break event เกิดขึ้น.
     1: An active level ถูกตรวจพบบน the break input.

Bit 6 TIF: Trigger interrupt flag
flag นี้ถูกเซ็ตโดย hardware บน trigger event (active edge ถูกตรวจจับได้บน TRGI input เมื่อ the slave mode controller ถูก enabled ในทุก modes แต่ gated mode, ทั้งสอง edges ในกรณีที่ gated mode ถูกเลือก). มันถูกเคลียร์โดย software.
     0: ไม่มี trigger event เกิดขึ้น.
     1: Trigger interrupt ค้างอยู่.

Bit 5 COMIF: COM interrupt flag
flag นี้ถูกเซ็ตโดย hardware บน COM event (เมื่อ Capture/compare Control bits - CCxE, CCxNE, OCxM - ถูกอัปเดตแล้ว). มันถูกเคลียร์โดย software.
     0: ไม่มี COM event เกิดขึ้น.
     1: COM interrupt ค้างอยู่.

Bit 4 CC4IF: Capture/Compare 4 interrupt flag
อ้างอิงถึง CC1IF description

Bit 3 CC3IF: Capture/Compare 3 interrupt flag
refer to CC1IF description

Bit 2 CC2IF: Capture/Compare 2 interrupt flag
refer to CC1IF description

Bit 1 CC1IF: Capture/compare 1 interrupt flag
ถ้า channel CC1 ถูกกำหนดค่าเป็น output:
flag นี้ถูกเซ็ตโดย hardware เมื่อ the counter เท่ากับ the compare value, ด้วยบางข้อยกเว้นใน center-aligned mode (อ้างอิงถึง the CMS bits ใน the TIMx_CR1 register description). มันถูกเคลียร์โดย software.
     0: No match.
     1: The content ของ the counter TIMx_CNT เท่ากันกับ the content ของ the TIMx_CCR1 register.
ถ้า channel CC1 ถูกกำหนดค่าเป็น input:
บิตนี้ถูกเซ็ตโดย hardware บน a capture. มันถูกเคลียร์โดย software หรือโดยการอ่าน the TIMx_CCR1 register.
     0: ไม่มี input capture เกิดขึ้น.
     1: The counter value ถูก captured แล้วใน TIMx_CCR1 register (An edge ถูกตรวจจับได้แล้วบน IC1 ซึ่งเท่ากันกับ the
         selected polarity).

Bit 0 UIF: Update interrupt flag
– บิตนี้ถูกเซ็ตโดย hardware บน an update event. มันถูกเคลียร์โดย software.
     0: ไม่มี update เกิดขึ้น.
     1: Update interrupt ค้างอยู่. บิตนี้ถูกเซ็ตโดย hardware เมื่อ the registers ถูก updated:
– ที่ overflow หรือ underflow และถ้า the UDIS=0 ใน the TIMx_CR1 register.
– เมื่อ CNT ถูกเริ่มต้นใหม่โดย software โดยใช้ the UG bit ใน TIMx_EGR register, ถ้า URS=0 และ UDIS=0 ใน the
   TIMx_CR1 register.
– เมื่อ CNT ถูกเริ่มต้นใหม่โดย a trigger event อ้างอิงถึง Section 14.4.3: TIM1 and TIM8 slave mode control register (TIMx_SMCR), ถ้า URS=0 และ UDIS=0 ใน the TIMx_CR1 register.
   ใน the TIMx_CR1 register.
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 14, 2020, 05:00:31 PM
(https://i.imgur.com/Lpqo2gS.png)

Bits 15:8 Reserved, must be kept at reset value.

Bit 7 BG: Break generation
บิตนี้ถูกเซ็ตโดย software เพื่อสร้าง an event, มันถูกเคลียร์โดยอัตโนมัติโดย hardware.
     0: No action
     1: A break event ถูกสร้าง. MOE bit ถูกเคลียร์และ BIF flag ถูกเซ็ต. Related interrupt หรือ DMA transfer สามารถเกิดขึ้นถ้า
         เปิดการใช้งาน

Bit 6 TG: Trigger generation
บิตนี้ถูกเซ็ตโดย software เพื่อสร้าง an event, มันถูกเคลียร์โดยอัตโนมัติโดย hardware.
     0: No action
     1: The TIF flag ถูกเซ็ตใน TIMx_SR register. interrupt หรือ DMA transfer ที่เกี่ยวข้องสามารถเกิดขึ้นถ้าถูกเปิดการใช้งาน.

Bit 5 Reserved, must be kept at reset value.

Bit 4 CC4G: Capture/compare 4 generation
refer to CC1G description

Bit 3 CC3G: Capture/compare 3 generation
refer to CC1G description

Bit 2 CC2G: Capture/compare 2 generation
refer to CC1G description

Bit 1 CC1G: Capture/compare 1 generation
บิตนี้ถูกเซ็ตโดย software เพื่อสร้าง an event, มันถูกเคลียร์โดยอัตโนมัติโดย hardware.
     0: No action
     1: A capture/compare event ถูกสร้างบน channel 1:
ถ้า channel CC1 ถูกกำหนดค่าเป็น output:
CC1IF flag ถูก set, interrupt or DMA request ที่ตรงกันถูกส่งถ้าถูกเปิดการใช้งาน..
ถ้า channel CC1 ถูกกำหนดค่าเป็น input:
The current value ของ the counter ถูก captured ใน TIMx_CCR1 register. The CC1IF flag ถูก set, the corresponding interrupt or DMA request ถูกส่งถ้าถูกเปิดการใช้งาน. The CC1OF flag ถูกเซ็ตถ้า the CC1IF flag เป็น high อยู่แล้ว.

Bit 0 UG: Update generation
บิตนี้ถูกเซ็ตโดย software เพื่อสร้าง an event, มันถูกเคลียร์โดยอัตโนมัติโดย hardware.
     0: No action
     1: เริ่มต้นใหม่ the counter และสร้าง an update ของ the registers. บันทึกไว้ว่า the prescaler counter ถูกเคลียร์ด้วย
         (อย่างไรก็ตาม the prescaler ratio ไม่ได้รับผลกระทบ). The counter ถูกเคลียร์ถ้า the center-aligned mode ถูกเลือกหรือ
        ถ้า DIR=0 (upcounting), มิฉะนั้นจะเอา the auto-reload value (TIMx_ARR) ถ้า DIR=1 (downcounting).
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 14, 2020, 05:18:05 PM
14.4.7 TIM1 and TIM8 capture/compare mode register 1 (TIMx_CCMR1)
Address offset: 0x18
Reset value: 0x0000

(https://i.imgur.com/e9PeJuE.png)

Output compare mode

Bit 15 OC2CE: Output compare 2 clear enable

Bits 14:12 OC2M[2:0]: Output compare 2 mode

Bit 11 OC2PE: Output compare 2 preload enable

Bit 10 OC2FE: Output compare 2 fast enable

Bits 9:8 CC2S[1:0]: Capture/Compare 2 selection
bit-field นี้กำหนด the direction ของ the channel (input/output) เช่นเดียวกับ the used input.
     00: CC2 channel ถูกกำหนดเป็น output
     01: CC2 channel ถูกกำหนดเป็น input, IC2 ถูกแม็ปบน TI2
     10: CC2 channel ถูกกำหนดเป็น input, IC2 ถูกแม็ปบน TI1
     11: CC2 channel ถูกกำหนดเป็น input, IC2 ถูกแม็ปบน TRC. โหมดนี้กำลังทำงานเพียงถ้า an internal trigger input ถูกเลือก
           ผ่านทาง the TS bit (TIMx_SMCR register)
Note: CC2S bits เขียนได้เพียงเมื่อ the channel ถูก OFF (CC2E = 0 ใน TIMx_CCER).

Bit 7 OC1CE: Output compare 1 clear enable
OC1CE: Output Compare 1 Clear Enable
     0: OC1Ref ไม่ถูกได้รับผลกระทบ the ETRF input
     1: OC1Ref ถูกเคลียร์ทันทีที่ a High level ถูกตรวจจับได้บน ETRF input

Bits 6:4 OC1M: Output compare 1 mode
บิตเหล่านี้กำหนด the behavior ของ the output reference signal OC1REF จากที่ OC1 และ OC1N สืบสายมา. OC1REF จะ active high ขณะที่ OC1 และ OC1N active level ขึ้นอยู่กับ CC1P และ CC1NP bits.
     000: Frozen - The comparison ระหว่าง the output compare register TIMx_CCR1 และ the counter TIMx_CNT ไม่มี
             ผลบน the outputs.(โหมดนี้ถูกใช้สร้าง a timing base).
     001: เซ็ต channel 1 เป็น active level เมื่อเท่ากัน. OC1REF signal ถูกบังคับเป็น high เมื่อ the counter TIMx_CNT เท่ากัน
             กับ the capture/compare register 1 (TIMx_CCR1).
     010: เซ็ต channel 1 เป็น inactive level เมื่อเท่ากัน. OC1REF signal ถูกบังคับเป็น low เมื่อ the counter TIMx_CNT เท่ากัน
             กับ the capture/compare register 1 (TIMx_CCR1).
     011: Toggle - OC1REF สลับเมื่อ TIMx_CNT=TIMx_CCR1.
     100: Force inactive level - OC1REF ถูกบังคับเป็น low.
     101: Force active level - OC1REF ถูกบังคับเป็น high.
     110: PWM mode 1 - ในการนับขึ้น, channel 1 จะ active ตราบเท่าที่ TIMx_CNT<TIMx_CCR1 เป็นอย่างอื่นจะ inactive. ใน
             การนับลง, channel 1 จะ inactive (OC1REF='0) ตราบเท่าที่ TIMx_CNT>TIMx_CCR1 เป็นอย่างอื่นจะ active
             (OC1REF=1).
     111: PWM mode 2 - ในการนับขึ้น, channel 1 จะ inactive ตราบเท่าที่ TIMx_CNT<TIMx_CCR1 เป็นอย่างอื่นจะ active. ใน
             การนับลง, channel 1 จะ active  ตราบเท่าที่ TIMx_CNT>TIMx_CCR1 เป็นอย่างอื่นจะ inactive.       
Note: ใน PWM mode 1 หรือ 2, the OCREF level เปลี่ยนเพียงเมื่อ the result ของ the comparison เปลี่ยนหรือเมื่อ the
           output compare mode สวิทช์จาก "frozen" mode ไปยัง "PWM" mode.

Bit 3 OC1PE: Output compare 1 preload enable
     0: Preload register บน TIMx_CCR1 ถูกปิดการใช้งาน. TIMx_CCR1 สามารถถูกเขียนได้ตลอดเวลา, ค่าใหม่จะถูกนำเข้าโดยทันที.
     1: Preload register บน TIMx_CCR1 ถูกเปิดการใช้งาน. Read/Write operations เข้าถึง the preload register.
         TIMx_CCR1 preload value ถูกโหลดใน the active register ที่แต่ละ update event.
Note: 1: บิตเหล่านี้ไม่สามารถถูกแก้ไขตราบเท่าที่ LOCK level 3 ถูกโปรแกรมแล้ว (LOCK bits ใน TIMx_BDTR register)
         และ CC1S=00 (the channel ถูกกำหนดใน output).
         2: The PWM mode สามารถถูกใช้โดยไม่ต้องตรวจสอบความถูกต้อง the preload register เพียงใน onepulse mode (OPM
         bit เซ็ตใน TIMx_CR1 register). เป็นอย่างอื่น the behavior ไม่ถูกรับประกัน.

Bit 2 OC1FE: Output compare 1 fast enable
บิตนี้ถูกใช้เร่งผลของ an event บน the trigger ใน input บน the CC output.
     0: CC1 ทำงานตามปกติโดยขึ้นอยู่กับ counter และ CCR1 values แม้เมื่อ the trigger จะ ON. The minimum delay ที่ทำให้
         CC1 output ทำงานเมื่อ an edge เกิดขึ้นบน the trigger input คือ 5 clock cycles.
     1: An active edge บน the trigger input ทำงานเหมือน a compare match บน CC1 output. จากนั้น, OC ถูกเซ็ตถึง the
         compare level เป็นอิสระจาก the result ของ the comparison. Delay เพื่อสุ่ม the trigger input และเพื่อทำให้ CC1
         output ทำงานถูกลดลงเป็น 3 clock cycles. OCFE ทำงานเพียงถ้า the channel ถูกกำหนดค่าใน PWM1 หรือ PWM2 mode.

Bits 1:0 CC1S: Capture/Compare 1 selection
bit-field นี้กำหนด the direction ของ the channel (input/output) เช่นเดียวกันกับ the used input.
     00: CC1 channel ถูกกำหนดค่าเป็น output.
     01: CC1 channel ถูกกำหนดค่าเป็น input, IC1 ถูกแม็ปบน TI1.
     10: CC1 channel is configured as input, IC1 is mapped on TI2.
     11: CC1 channel is configured as input, IC1 is mapped on TRC. โหมดนี้กำลังทำงานเพียงถ้า an internal trigger input
           ถูกเลือกผ่านทาง the TS bit (TIMx_SMCR register)
Note: CC1S bits เขียนได้เพียงเมื่อ the channel ถูก OFF (CC1E = 0 in TIMx_CCER).
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 14, 2020, 05:20:57 PM
Input capture mode

Bits 15:12 IC2F: Input capture 2 filter

Bits 11:10 IC2PSC[1:0]: Input capture 2 prescaler

Bits 9:8 CC2S: Capture/compare 2 selection
bit-field นี้กำหนด the direction ของ the channel (input/output) เช่นเดียวกันกับ the used input.
     00: CC2 channel ถูกกำหนดค่าเป็น output.
     01: CC2 channel ถูกกำหนดค่าเป็น input, IC2 ถูกแม็ปบน TI2.
     10: CC2 channel is configured as input, IC2 is mapped on TI1.
     11: CC2 channel is configured as input, IC2 is mapped on TRC. โหมดนี้กำลังทำงานเพียงถ้า an internal trigger input
            ถูกเลือกผ่านทาง the TS bit (TIMx_SMCR register)
Note: CC2S bits เขียนได้เพียงเมื่อ the channel ถูก OFF (CC2E = 0 in TIMx_CCER).

Bits 7:4 IC1F: Input capture 1 filter
bit-field นี้จะกำหนด the frequency ที่ใช้เพื่อสุ่ม TI1 input และความยาวของ digital filter ที่ใช้กับ TI1. The digital filter ถูกทำจาก an event counter ที่ซึ่ง N consecutive events ถูกต้องการเพื่อทำให้ถูกต้อง a transition บน the output:
     0000: No filter, การสุ่มถูกทำที่ fDTS
     0001: fSAMPLING=fCK_INT, N=2
     0010: fSAMPLING=fCK_INT, N=4
     0011: fSAMPLING=fCK_INT, N=8
     0100: fSAMPLING=fDTS/2, N=6
     0101: fSAMPLING=fDTS/2, N=8
     0110: fSAMPLING=fDTS/4, N=6
     0111: fSAMPLING=fDTS/4, N=8
     1000: fSAMPLING=fDTS/8, N=6
     1001: fSAMPLING=fDTS/8, N=8
     1010: fSAMPLING=fDTS/16, N=5
     1011: fSAMPLING=fDTS/16, N=6
     1100: fSAMPLING=fDTS/16, N=8
     1101: fSAMPLING=fDTS/32, N=5
     1110: fSAMPLING=fDTS/32, N=6
     1111: fSAMPLING=fDTS/32, N=8

Bits 3:2 IC1PSC: Input capture 1 prescaler
bit-field นี้กำหนด the ratio ของ the prescaler ทำงานบน CC1 input (IC1).
The prescaler ถูก reset ทันทีที่ CC1E=0 (TIMx_CCER register).
     00: no prescaler, capture ถูกทำแต่ละเวลาที่ an edge ถูกตรวจจับได้บน the capture input
     01: capture ถูกทำหนึ่งครั้งทุกๆ 2 events
     10: capture is done once every 4 events
     11: capture is done once every 8 events

Bits 1:0 CC1S: Capture/Compare 1 selection
bit-field นี้กำหนด the direction ของ the channel (input/output) เช่นเดียวกันกับ the used input.
     00: CC1 channel ถูกกำหนดค่าเป็น output
     01: CC1 channel ถูกกำหนดค่าเป็น input, IC1 ถูกแม็ปบน TI1
     10: CC1 channel is configured as input, IC1 is mapped on TI2
     11: CC1 channel is configured as input, IC1 is mapped on TRC. โหมดนี้กำลังทำงานเพียงถ้า an internal trigger input
           ถูกเลือกผ่านทาง the TS bit (TIMx_SMCR register)
Note: CC1S bits เขียนได้เพียงเมื่อ the channel ถูก OFF (CC1E = 0 in TIMx_CCER).
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 15, 2020, 07:13:17 AM
14.4.8 TIM1 and TIM8 capture/compare mode register 2 (TIMx_CCMR2)
                       Address offset: 0x1C
(https://i.imgur.com/zCaUolk.png)
(https://i.imgur.com/ywZd4Qk.png)
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 15, 2020, 07:38:06 AM
(https://i.imgur.com/6MqbZl3.png)

Bits 15:14 Reserved, must be kept at reset value.

Bit 13 CC4P: Capture/Compare 4 output polarity
refer to CC1P description

Bit 12 CC4E: Capture/Compare 4 output enable
refer to CC1E description

Bit 11 CC3NP: Capture/Compare 3 complementary output polarity
refer to CC1NP description

Bit 10 CC3NE: Capture/Compare 3 complementary output enable
refer to CC1NE description

Bit 9 CC3P: Capture/Compare 3 output polarity
refer to CC1P description

Bit 8 CC3E: Capture/Compare 3 output enable
refer to CC1E description

Bit 7 CC2NP: Capture/Compare 2 complementary output polarity
refer to CC1NP description

Bit 6 CC2NE: Capture/Compare 2 complementary output enable
refer to CC1NE description

Bit 5 CC2P: Capture/Compare 2 output polarity
refer to CC1P description

Bit 4 CC2E: Capture/Compare 2 output enable
refer to CC1E description

Bit 3 CC1NP: Capture/Compare 1 complementary output polarity
     0: OC1N active high.
     1: OC1N active low.
Note: บิตนี้ไม่สามารถเขียนได้ทันทีที่ LOCK level 2 or 3 ถูกโปรแกรมแล้ว (LOCK bits ใน TIMx_BDTR register) และ CC1S="00" (the channel ถูกกำหนดค่าใน output).

Bit 2 CC1NE: Capture/Compare 1 complementary output enable
     0: Off - OC1N ไม่ active. OC1N level เป็นฟังก์ชันของ MOE, OSSI, OSSR, OIS1, OIS1N and CC1E bits.
     1: On - OC1N signal ถูก output บน the corresponding output pin ขึ้นอยู่กับ MOE, OSSI, OSSR, OIS1, OIS1N and
         CC1E bits.

Bit 1 CC1P: Capture/Compare 1 output polarity
CC1 channel configured as output:
     0: OC1 active high.
     1: OC1 active low.
CC1 channel configured as input:
บิตนี้เลือกว่า IC1 หรือ IC1 ถูกใช้สำหรับ trigger หรือ capture operations.
     0: non-inverted: capture ถูกทำบน a rising edge ของ IC1. เมื่อถูกใช้เป็น external trigger, IC1 ถูก non-inverted.
     1: inverted: capture ถูกทำบน a falling edge ของ IC1. เมื่อถูกใช้เป็น external trigger, IC1 ถูก inverted.
Note: บิตนี้ไม่สามารถเขียนได้ทันทีที่ LOCK level 2 or 3 ถูกโปรแกรมแล้ว (LOCK bits ใน TIMx_BDTR register)

Bit 0 CC1E: Capture/Compare 1 output enable
CC1 channel configured as output:
     0: Off - OC1 ไม่ active.
     1: On - OC1 signal จะส่งออกบน the corresponding output pin.
CC1 channel configured as input:
บิตนี้พิจารณาว่าถ้า a capture ของ the counter value สามารถถูกทำได้จริงลงใน the input capture/compare register 1 (TIMx_CCR1) หรือไม่.
     0: Capture ถูกปิดการใช้งาน.
     1: Capture ถูกเปิดการใช้งาน.

(https://i.imgur.com/RdYQ3ho.png)
1. เมื่อทั้งสอง outputs ของ a channel ไม่ถูกใช้ (CCxE = CCxNE = 0), the OISx, OISxN, CCxP and CCxNP bits ต้องถูกเคลียร์.

Note: สถานะของ the external I/O pins ที่ต่อถึง the complementary OCx and OCxN channels ขึ้นอยู่กับ the OCx and
         OCxN channel state and the GPIOand AFIO registers.
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 15, 2020, 08:02:47 AM
(https://i.imgur.com/RNiNEsH.png)
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 15, 2020, 08:07:12 AM
(https://i.imgur.com/QWdSy9F.png)

Bits 15:0 PSC[15:0]: Prescaler value
The counter clock frequency CK_CNT เท่ากันกับ fCK_PSC / (PSC[15:0] + 1).
PSC บรรจุค่าที่ถูกโหลดใน the active prescaler register ที่แต่ละ update event (รวมถึงเมื่อ the counter ถูกเคลียร์ผ่านทาง UG bit ของ TIMx_EGR register หรือผ่านทาง trigger controller เมื่อกำหนดค่าใน "reset mode").
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 15, 2020, 08:11:11 AM
(https://i.imgur.com/wR6U57w.png)

Bits 15:0 ARR[15:0]: Prescaler value
ARR คือค่าที่ถูกโหลดใน the actual auto-reload register.
อ้าองอิงถึง the Section 14.3.1: Time-base unit สำหรับรายละเอียดที่มากขึ้นเกี่ยวกับ ARR update และ behavior.
The counter ถูก blocked ในขณะที่ the auto-reload value เป็นศูนย์.
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 15, 2020, 09:02:08 AM
(https://i.imgur.com/S8vB5NY.png)

Bits 15:8 Reserved, must be kept at reset value.

Bits 7:0 REP[7:0]: Repetition counter value
บิตเหล่านี้ยอมให้ผู้ใช้ตั้งค่า the update rate ของ the compare registers (ตัวอย่างเช่น การส่งถ่ายเป็นระยะจาก preload ถึง active registers) เมื่อ preload registers ถูกเปิดกาใช้งาน, เช่นเดียวกันกับ the update interrupt generation rate, ถ้า interrupt นี้ถูกเปิดกาใช้งาน.
แต่ละเวลาที่ the REP_CNT related downcounter ลงถึงศูนย์, an update event ถูกสร้างและมันจะสตาร์ทการนับจาก REP value. ตามที่ REP_CNT ถูกโหลดซ้ำใหม่ด้วย REP value ได้เพียงที่ the repetition update event U_RC, การเขียนใดๆไปยัง the TIMx_RCR register จะไม่ถูกคำนึงถึงจนกระทั่ง the next repetition update event.
หมายความว่าใน PWM mode (REP+1) ตรงกันกับ :
   – จำนวนของ PWM periods ใน edge-aligned mode
   – จำนวนของ half PWM period ใน center-aligned mode
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 15, 2020, 09:08:10 AM
(https://i.imgur.com/MXAJNbm.png)

Bits 15:0 CCR1[15:0]: Capture/Compare 1 value
ถ้า channel CC1 ถูกกำหนดค่าเป็น output:
CCR1 คือค่าที่ถูกโหลดใน the actual capture/compare 1 register (preload value). มันถูกโหลดอย่างถาวรถ้า the preload feature ไม่ได้ถูกเลือกใน the TIMx_CCMR1 register (bit OC1PE). ถ้าเป็นอย่างอื่น the preload value ถูกคัดลอกลงใน the active capture/compare 1 register เมื่อ an update event เกิดขึ้น.
The active capture/compare register บรรจุค่าที่ถูกเปรียบเทียบกับ the counter TIMx_CNT และให้สัญญาณบน OC1 output.

ถ้า channel CC1 ถูกกำหนดค่าเป็น input:
CCR1 คือ the counter value ที่ส่งถ่ายโดย the last input capture 1 event (IC1). The TIMx_CCR1 register อ่านได้เพียงอย่างเดียวและไม่สามารถถูกโปรแกรม.
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 15, 2020, 09:12:18 AM
(https://i.imgur.com/1Q0sWbL.png)
(https://i.imgur.com/5MrsogE.png)
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 15, 2020, 09:30:44 AM
(https://i.imgur.com/Y8H35Go.png)

Note: ตามที่ the bits AOE, BKP, BKE, OSSI, OSSR and DTG[7:0] สามารถถูก write-locked ขึ้นอยู่กับ the LOCK
         configuration, มันสามารถถูกจำเป็นที่ต้องกำหนดค่าทั้งหมดของมันระหว่างการเขียนครั้งแรกที่เข้าถึง the TIMx_BDTR register.

Bit 15 MOE: Main output enable
บิตนี้ถูกเคลียร์แบบ asynchronous โดย hardware ทันทีที่ the break input ทำงาน. มันถูกเซ็ตโดย software หรือโดยอัตโนมัติขึ้นอยู่กับ the AOE bit. มันทำงานเพียงบน the channels ซึ่งถูกกำหนดค่าใน output.
     0: OC and OCN outputs ถูกปิดการใช้งานหรือบังคับเป็น idle state.
     1: OC and OCN outputs ถูกเปิดการใช้งาน ถ้า respective enable bits ของมันถูกเซ็ต (CCxE, CCxNE ใน TIMx_CCER
         register).
ดู OC/OCN enable description สำหรับรายละเอียดที่มากขึ้น (Section 14.4.9: TIM1 and TIM8 capture/compare enable register (TIMx_CCER)).

Bit 14 AOE: Automatic output enable
     0: MOE สามารถถูกเซ็ตได้เพียงโดย software
     1: MOE สามารถถูกเซ็ตได้เพียงโดย software หรือโดยอัตโนมัติที่ the next update event (ถ้า the break input ไม่ถูกทำให้
         ทำงาน)
Note: บิตนี้ไม่สามารถถูกแก้ไขตราบเท่าที่ LOCK level 1 ถูกโปรแกรมแล้ว (LOCK bits ใน TIMx_BDTR register).

Bit 13 BKP: Break polarity
     0: Break input BRK ทำงานเป็น low
     1: Break input BRK ทำงานเป็น high
Note: บิตนี้ไม่สามารถถูกแก้ไขตราบเท่าที่ LOCK level 1 ถูกโปรแกรมแล้ว (LOCK bits ใน TIMx_BDTR register).
Note: write operation ใดๆไปยังบิตนี้จะหน่วงเวลา 1 APB clock cycle ถึงจะกลายเป็นผล.

Bit 12 BKE: Break enable
     0: Break inputs (BRK and CSS clock failure event) ถูกปิดการใช้งาน
     1; Break inputs (BRK and CSS clock failure event) ถูกเปิดการใช้งาน
Note: บิตนี้ไม่สามารถถูกแก้ไขตราบเท่าที่ LOCK level 1 ถูกโปรแกรมแล้ว (LOCK bits ใน TIMx_BDTR register).
Note: write operation ใดๆไปยังบิตนี้จะหน่วงเวลา 1 APB clock cycle ถึงจะกลายเป็นผล.

Bit 11 OSSR: Off-state selection for Run mode
บิตนี้ถูกใช้เมื่อ MOE=1 บน channels ที่มี a complementary output ซึ่งถูกกำหนดค่าเป็น outputs. OSSR ไม่ถูกนำมาใช้ถ้าไม่มี complementary output นำมาใช้ใน the timer.
ดู OC/OCN enable description สำหรับรายละเอียดที่มากขึ้น (Section 14.4.9: TIM1 and TIM8 capture/compare enable register (TIMx_CCER)).
     0: เมื่อ inactive, OC/OCN outputs ถูกปิดการใช้งาน (OC/OCN enable output signal=0).
     1: เมื่อ inactive, OC/OCN outputs ถูกเปิดการใช้งานด้วย inactive level ของมันทันทีที่ CCxE=1 หรือ CCxNE=1. จากนั้น,
         OC/OCN enable output signal=1
Note: บิตนี้ไม่สามารถถูกแก้ไขทันทีที่ LOCK level 2 ถูกโปรแกรมแล้ว (LOCK bits ใน TIMx_BDTR register).

Bit 10 OSSI: Off-state selection for Idle mode
บิตนี้ถูกใช้เมื่อ MOE=0 บน channels ที่กำหนดค่าเป็น outputs.
ดู OC/OCN enable description สำหรับรายละเอียดที่มากขึ้น (Section 14.4.9: TIM1 and TIM8 capture/compare enable register (TIMx_CCER)).
     0: เมื่อ inactive, OC/OCN outputs ถูกปิดการใช้งาน (OC/OCN enable output signal=0).
     1: เมื่อ inactive, OC/OCN outputs ถูก forced เป็นอันดับแรกด้วย idle level ของมันทันทีที่ CCxE=1 หรือ CCxNE=1.
         OC/OCN enable output signal=1)
Note: บิตนี้ไม่สามารถถูกแก้ไขทันทีที่ LOCK level 2 ถูกโปรแกรมแล้ว (LOCK bits ใน TIMx_BDTR register).

Bits 9:8 LOCK[1:0]: Lock configuration
บิตเหล่านี้เสนอให้มี a write protection ต้าน software errors.
     00: LOCK OFF - ไม่มีบิตที่ถูกป้องกันการเขียน.
     01: LOCK Level 1 = DTG bits ใน TIMx_BDTR register, OISx และ OISxN bits ใน TIMx_CR2 register และ
           BKE/BKP/AOE bits ใน TIMx_BDTR register ไม่สามารถถูกเขียนได้อีกต่อไป.
     10: LOCK Level 2 = LOCK Level 1 + CC Polarity bits (CCxP/CCxNP bits ใน TIMx_CCER register, ตราบเท่าที่ the
           related channel ถูกกำหนดค่าใน output ผ่านทาง the CCxS bits) เช่นเดียวกันกับ OSSR and OSSI bits ไม่สามารถถูก
           เขียนได้อีกต่อไป.
     11: LOCK Level 3 = LOCK Level 2 + CC Control bits (OCxM และ OCxPE bits ใน TIMx_CCMRx registers, ตราบเท่า
           ที่ the related channel ถูกกำหนดค่าใน output ผ่านทาง the CCxS bits) ไม่สามารถถูกเขียนได้อีกต่อไป.
Note: The LOCK bits สามารถถูกเขียนเพียงครั้งเดียวหลังจาก the reset. เมื่อ the TIMx_BDTR register ถูกเขียนแล้ว, สิ่งที่อยู่ภายในของมันจะถูกแช่แข็งจนกระทั่ง the next reset.

Bits 7:0 DTG[7:0]: Dead-time generator setup
bit-field นี้กำหนด the duration ของ the dead-time ที่แทรกระหว่าง the complementary outputs. DT ตรงกันกับระยะเวลานี้.
     DTG[7:5]=0xx => DT=DTG[7:0]x tdtg with tdtg=tDTS.
     DTG[7:5]=10x => DT=(64+DTG[5:0])xtdtg with Tdtg=2xtDTS.
     DTG[7:5]=110 => DT=(32+DTG[4:0])xtdtg with Tdtg=8xtDTS.
     DTG[7:5]=111 => DT=(32+DTG[4:0])xtdtg with Tdtg=16xtDTS.
ตัวอย่างถ้า TDTS=125ns (8MHz), dead-time possible values คือ:
0 to 15875 ns by 125 ns steps,
16 us to 31750 ns by 250 ns steps,
32 us to 63us by 1 us steps,
64 us to 126 us by 2 us steps
Note: bit-field นี้ไม่สามารถถูกแก้ไขตราบเท่าที่ LOCK level 1, 2 or 3 ถูกโปรแกรมแล้ว (LOCK bits in TIMx_BDTR register).
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 15, 2020, 09:37:19 AM
(https://i.imgur.com/Z6isoGH.png)

Bits 15:13 Reserved, must be kept at reset value.

Bits 12:8 DBL[4:0]: DMA burst length
5-bit vector นี้กำหนดจำนวนของ DMA transfers (the timer จะจดจำ a burst transfer เมื่อ a read หรือ a write access ถูกทำกับ the TIMx_DMAR address ถูกกระทำ).
     00000: 1 transfer,
     00001: 2 transfers,
     00010: 3 transfers,
     ...
     10001: 18 transfers.

Bits 7:5 Reserved, must be kept at reset value.

Bits 4:0 DBA[4:0]: DMA base address
5-bit vector นี้กำหนด the base-address สำหรับ DMA transfers (เมื่อ read/write access ถูกทำผ่านทาง the TIMx_DMAR address). DBA ถูกกำหนดเป็น an offset(ค่าที่เพิ่มขึ้น) เริ่มจาก the address ของ the TIMx_CR1 register.
     Example:
     00000: TIMx_CR1,
     00001: TIMx_CR2,
     00010: TIMx_SMCR,
     ...

Example: ให้เราพิจารณาการส่งถ่ายต่อไปนี้: DBL = 7 transfers & DBA = TIMx_CR1. ในกรณีนี้ the transfer ถูกทำจาก
               ไปยัง/จาก 7 registers เริ่มจาก the TIMx_CR1 address.
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 15, 2020, 09:41:52 AM
(https://i.imgur.com/F5bDLxM.png)

Bits 31:0 DMAB[31:0]: DMA register for burst accesses
A read หรือ write operation ถึง the DMAR register เข้าถึง the register ในตำแหน่งที่ the address
            (TIMx_CR1 address) + (DBA + DMA index) x 4
ที่ TIMx_CR1 address คือ the address ของ the control register 1, DBA คือ the DMA base address ถูกกำหนดค่าใน TIMx_DCR register, DMA index ถูกควบคุมโดยอัตโนมัติโดย the DMA transfer, และมีย่านจาก 0 ถึง DBL (DBL ถูกกำหนดค่าใน TIMx_DCR).
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 15, 2020, 09:46:55 AM
ตัวอย่างของวิธีการใช้ the DMA burst feature
ในตัวอย่างนี้ the timer DMA burst feature ถูกใช้เพื่ออัปเดตสิ่งที่อยู่ภายในของ the CCRx registers (x = 2, 3, 4) ด้วย the DMA transferring half words ลงใน the CCRx registers นี้ถูกทำในขั้นตอนต่อไปนี้:
1. กำหนดค่า the corresponding DMA channel ดังต่อไปนี้:
   – DMA channel peripheral address คือ the DMAR register address
   – DMA channel memory address คือ the address ของ the buffer ใน the RAM บรรจุ the data ที่จะถูกส่งถ่ายโดย DMA
      ลงใน CCRx registers.
   – จำนวนของ data ที่จะ transfer = 3 (ดู note ข้างล่าง).
   – Circular mode ถูกปิดการใช้งาน.
2. กำหนดค่า the DCR register โดยการกำหนดค่า the DBA และ DBL bit fields ดังต่อไปนี้: DBL = 3 transfers, DBA = 0xE.
3. เปิดการใช้งาน the TIMx update DMA request (เซ็ต the UDE bit ใน the DIER register).
4. เปิดการใช้งาน TIMx
5. เปิดการใช้งาน the DMA channel

Note: ตัวอย่างนี้สำหรับกรณีที่ทุก CCRx register ถูกอัปเดตหนึ่งครั้ง. ถ้าทุก CCRx register ต้องถูกอัปเดตสองครั้ง ตัวอย่างเช่น,
         จำนวนของ data ที่จะ transfer ควรเป็น 6. ลองยกตัวอย่างของ a buffer ใน the RAM บรรจุ data1, data2, data3, data4,
         data5 และ data6. The data ถูกส่งถ่ายถึง the CCRx registers ดังต่อไปนี้: บน the first update DMA request, data1
         ถูกส่งถ่ายถึง CCR2, data2 ถูกส่งถ่ายถึง CCR3, data3 ถูกส่งถ่ายถึง CCR4 และบน the second update DMA request,
         data4 ถูกส่งถ่ายถึง CCR2, data5 ถูกส่งถ่ายถึง CCR3 และ data6 ถูกส่งถ่ายถึง CCR4.
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 15, 2020, 09:51:18 AM
(https://i.imgur.com/MDMgQef.png)
(https://i.imgur.com/NDn9l74.png)
Title: Re: STM32F1 TIM1 & TIM8
Post by: tha on December 15, 2020, 09:52:07 AM
-