STM32F1 TIM1 & TIM8

Started by tha, November 21, 2020, 08:18:38 AM

Previous topic - Next topic

tha

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.

tha

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


Preload registers ส่งถ่ายไปยัง active registers บน U event สอดคล้องกับ control bit

tha

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 ถูกเปลี่ยนแปลงทันที :



tha

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.




tha

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.




tha

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




tha

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 ถูกเขียน.


Update event : Preload register ส่งถ่ายถึง active register และ update interrupt ถูกสร้าง
Update event : ถ้า the repetition counter underflow เกิดขึ้นเมื่อ the counter เท่ากันกับ the auto-reload value

tha

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.