STM32F1 TIM2 to TIM5

Started by tha, September 30, 2020, 12:19:23 PM

Previous topic - Next topic

tha

15 General-purpose timers (TIM2 to TIM5)
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.

ส่วนนี้ใช้กับตระกูล STM32F10xxx ทั้งหมดเว้นแต่จะระบุไว้เป็นอย่างอื่น

15.1 TIM2 to TIM5 introduction
The general-purpose timers ประกอบด้วย a 16-bit auto-reload counter ที่ถูกขับโดย a programmable prescaler.

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

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

The timers เป็นอิสระโดยสมบูรณ์, และไม่ได้ใช้ทรัพยากรใดๆร่วมกัน. สามารถถูกซิงโครไนซ์เข้าด้วยกันได้ตามที่อธิบายไว้ใน Section 15.3.15.

tha

15.2 TIMx main features (คุณลักษณะหลักของ TIMx)
General-purpose TIMx 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
   • Synchronization circuit เพื่อควบคุม the timer ด้วย external signals และเชื่อมต่อระหว่าง timers หลายตัว.
   • Interrupt/DMA generation ตามเหตุการณ์ดังต่อไปนี้:
     – Update: counter overflow/underflow, counter initialization (by software or
        internal/external trigger)
     – Trigger event (counter start, stop, initialization or count by internal/external trigger)
     – Input capture
     – Output compare
   • รองรับ incremental (quadrature) encoder และ hall-sensor circuitry สำหรับ positioning purposes
   • Trigger input สำหรับ external clock หรือ cycle-by-cycle current management

tha


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

ก็มีตัวอย่างโปรแกรมให้ลองเอาไปดูกันก่อน ท่านใดเป็นแล้วก็ลองเขียนโปรแกรมได้ สงสัยจะเป็นโปรแกรมที่มากับหนังสือ STM32 ของ INEX ต้องขอเผยแผ่และขอขอบคุณมา ณ ที่นี้ด้วยครับ
http://www.mediafire.com/file/wbptq1f5p6hf0th/TIMER_01.C/file

ปล. มันมีการตั้งอะไรบ้าง ตั้ง clock ตั้ง prescaler(ตัวหาร clock = 72) ก็จะได้ Timer clock = 1MHz ตั้ง period(auto-reload counter) = 1000 ครั้ง ก็จะได้การนับขึ้นทีที่ 1ms ก็จะเกิด interrupt ขึ้นทีหนึ่ง



tha

ลองเสริจกูเกิ้ลด้วย "stm32 TIM" จะมีเรื่อง Timer ของ stm32 เป็นไฟล์ pdf ของมหาวิทยาลัยเทคโนโลยี่พระจอมเกล้าลาดกระบังอยู่ ลองหามาอ่านเอาครับ

tha

15.3 TIMx functional description

15.3.1 Time-base unit
The main block ของ the programmable timer คือ a 16-bit counter พร้อมด้วย auto-reload register ที่เกี่ยวข้องของมัน. The counter สามารถ count up, down หรือทั้ง up and 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)

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 actual counter enable signal CNT_EN ถูกเซ็ต 1 clock cycle หลังจาก CEN.

tha

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 101 and Figure 102 แสดงบางตัวอย่างของ the counter behavior เมื่อ the prescaler ratio ถูกเปลี่ยนแปลงทันที :




tha

15.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.

An Update event สามารถถูกสร้างที่แต่ละ counter overflow หรือโดยการเซ็ต the UG bit ใน the TIMx_EGR register (โดย software หรือโดยการใช้ the slave mode controller).

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 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

An Update event สามารถถูกสร้างที่แต่ละ counter underflow หรือโดยการเซ็ต the UG bit ใน the TIMx_EGR register (โดย software หรือโดยการใช้ the slave mode controller).

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 buffer ของ the prescaler ถูกโหลดซ้ำใหม่ด้วย the preload value (ที่อยู่ภายในของ the TIMx_PSC register)
•    The auto-reload shadow 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.