Electoday 2025

ไมโครคอนโทรลเลอร์ => ARM Processors => Topic started by: tha on September 30, 2020, 12:19:23 PM

Title: STM32F1 TIM2 to TIM5
Post by: tha on September 30, 2020, 12:19:23 PM
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.
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on September 30, 2020, 12:57:46 PM
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
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on September 30, 2020, 01:27:12 PM
(https://i.imgur.com/GRmvgsN.png)
Preload registers ส่งถ่ายไปยัง active registers บน U event สอดคล้องกับ control bit

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

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

Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on September 30, 2020, 07:32:51 PM
ลองเสริจกูเกิ้ลด้วย "stm32 TIM" จะมีเรื่อง Timer ของ stm32 เป็นไฟล์ pdf ของมหาวิทยาลัยเทคโนโลยี่พระจอมเกล้าลาดกระบังอยู่ ลองหามาอ่านเอาครับ
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on November 21, 2020, 03:48:54 PM
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.
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on November 22, 2020, 09:18:25 AM
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 ถูกเปลี่ยนแปลงทันที :

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

Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on November 23, 2020, 08:58:39 AM
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.

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

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

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

ปล. หลายบิตอยู่ งงๆหน่อย จำกันไหวไหม ลองไล่ดูกันก่อน
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on November 23, 2020, 01:13:29 PM
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.

(https://i.imgur.com/4iMRWmP.png)

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

(https://i.imgur.com/XOa83Zt.png)
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on November 24, 2020, 08:23:18 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 autoreloadvalue ลงไปยัง 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 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/o662vVx.png)

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

(https://i.imgur.com/CH8TCYr.png)
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on November 24, 2020, 09:24:14 AM
15.3.3 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 TIMx_SMCR register), ดังนั้น 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 120 แสดงลักษณะการทำงานของ the control circuit และ the upcounter ใน normal mode, โดยไม่มี prescaler.

(https://i.imgur.com/EroZsRF.png)
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on November 24, 2020, 10:21:48 AM
External clock source mode 1
โหมดนี้ถูกเลือกเมื่อ SMS=111 ใน the TIMx_SMCR register. The counter สามารถนับที่แต่ละ rising หรือ falling edge บน a selected input.

(https://i.imgur.com/8iq0V1r.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).
Note: The capture prescaler ไม่ได้ถูกใช้สำหรับการทริกเกอร์, ดังนั้นไม่มีความจำเป็นที่จะกำหนดค่ามัน.
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

เมื่อ 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/SlgBVBQ.png)
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on November 24, 2020, 10:57:20 AM
External clock source mode 2
โหมดนี้ถูกเลือกโดยการเขียน ECE=1 ใน the TIMx_SMCR register.

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

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

(https://i.imgur.com/MYJAy6F.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/kvnjGSu.png)
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on November 25, 2020, 08:16:20 AM
15.3.4 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).

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/MmReqcA.png)

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

(https://i.imgur.com/vS4urgZ.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 TIM2 to TIM5
Post by: tha on November 25, 2020, 08:51:24 AM
---
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on November 25, 2020, 10:36:50 AM
15.3.5 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 TIM2 to TIM5
Post by: tha on November 25, 2020, 02:32:07 PM
---
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on November 25, 2020, 02:52:52 PM
----
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on November 25, 2020, 02:56:59 PM
---
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on November 26, 2020, 08:16:26 AM
15.3.6 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/8t2lGzL.png)
1. The PWM input mode สามารถถูกใช้เพียงกับ the TIMx_CH1/TIMx_CH2 signals เนื่องจากความจริงแล้วมีเพียง TI1FP1 และ TI2FP2 ที่ถูกต่อกับ the slave mode controller.

Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on November 26, 2020, 09:55:29 AM
15.3.7 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 TIM2 to TIM5
Post by: tha on November 27, 2020, 08:32:12 AM
15.3.8 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, OCxPE=0, CCxP=0 และ CCxE=1 เพื่อสลับ OCx output
    pin เมื่อ CNT เท่ากันกับ CCRx, CCRx preload ไม่ถูกใช้, OCx ถูกเปิดการใช้งานและ active high.
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 129.

(https://i.imgur.com/QqTTZ17.png)
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on November 27, 2020, 10:26:26 AM
15.3.9 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 bit ใน the TIMx_CCER register. อ้างอิงถึง the TIMx_CCERx register description สำหรับรายละเอียดที่มากขึ้น.

ใน PWM mode (1 หรือ 2), TIMx_CNT และ TIMx_CCRx เปรียบเทียบกันเสมอเพื่อตัดสินไม่ว่าจะ TIMx_CCRx≤TIMx_CNT or TIMx_CNT≤TIMx_CCRx (ขึ้นอยู่กับทิศทางของ the counter). อย่างไรก็ตาม, เพื่อให้สอดคล้องกับ the ETRF (OCREF สามารถถูกเคลียร์โดย an external event ผ่านทาง the ETR signal จนกระทั่ง the next PWM period), the OCREF signal ถูกถือสิทธิ์ได้เพียง :
•    เมื่อผลของ the comparison เปลี่ยนไป, หรือ
•    เมื่อ the output compare mode (OCxM bits in TIMx_CCMRx register) สวิทช์จาก the "frozen" configuration (ไม่
      comparison, OCxM='000) ไปยังหนึ่งของ the PWM modes(OCxM='110 or '111).

นี้บังคับ the PWM โดย software ขณะที่ the timer กำลังรันอยู่.

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

PWM edge-aligned mode

Upcounting configuration
Upcounting ทำงานเมื่อ the DIR bit ใน the TIMx_CR1 register เป็น low. อ้างอิงถึง Upcounting mode.

ในตัวอย่างดังต่อไปนี้, เราพิจารณา PWM mode 1. The reference PWM signal OCxREF เป็น high ตราบเท่าที่ TIMx_CNT <TIMx_CCRx อย่างอื่นมันจะกลายเป็น low. ถ้า the compare value ใน TIMx_CCRx มากกว่า the auto-reload value (ใน TIMx_ARR) เมื่อนั้น OCxREF จะอยู่ที่ '1. ถ้า the compare value เป็น 0 เมื่อนั้น OCxREF จะอยู่ที่ '0. Figure 130 แสดงบาง edge-aligned PWM waveforms ในตัวอย่างที่ TIMx_ARR=8.

(https://i.imgur.com/NX0ikeZ.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 131 แสดงบาง center-aligned PWM waveforms ในตัวอย่างที่:
•    TIMx_ARR=8,
•    PWM mode เป็น the PWM mode 1,
•    The flag ถูกเซ็ตเมื่อ the counter นับลง ตรงกันกับ the center-aligned mode 1 ที่เลือกสำหรับ CMS=01 ใน TIMx_CR1
      register.

(https://i.imgur.com/MFUmV0U.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 ขณะที่มันกำลังรันอยู่.

ปล. อากาศกำลังเย็นสบาย เลยได้เยอะหน่อย
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on November 28, 2020, 08:23:46 AM
15.3.10 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/Se8tHzw.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 TIM2 to TIM5
Post by: tha on November 28, 2020, 09:58:30 AM
15.3.11 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 133 แสดง the behavior ของ the OCxREF signal เมื่อ the ETRF input กลายเป็น high, สำหรับทั้งสองค่าของ the OCxCE enable bit. ในตัวอย่างนี้, the timer TIMx ถูกโปรแกรมใน PWM mode

(https://i.imgur.com/DIlzMaP.png)
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on November 29, 2020, 06:38:46 AM
15.3.12 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 85. 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 ยังคงทำงานได้ตามปกติ.

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

(https://i.imgur.com/RhzDhn7.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 134 แสดงตัวอย่างของ 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/3VuCLVa.png)

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

(https://i.imgur.com/cLCpm61.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 TIM2 to TIM5
Post by: tha on November 29, 2020, 07:51:15 AM
15.3.13 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_CH3.

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

ตัวอย่างของคุณลักษณะนี้ใช้เพื่ออินเตอร์เฟส Hall sensors ถูกให้ไว้ใน Section 14.3.18.
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on November 29, 2020, 02:59:07 PM
15.3.14 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).

รูปที่ 136 แสดงพฤติกรรมนี้เมื่อ 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/I48IxGT.png)
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on November 29, 2020, 03:48:03 PM
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/7JSOj72.png)
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on November 30, 2020, 06:58:30 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/5AF29G5.png)
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on November 30, 2020, 07:18:41 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/hrbNPXr.png)

Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on November 30, 2020, 09:40:11 AM
15.3.15 Timer synchronization
The TIMx timers ถูกเชื่อมโยงเข้าด้วยกันภายในสำหรับ timer synchronization หรือ chaining. เมื่อ one Timer ถูกกำหนดค่าใน Master Mode, มันสามารถ reset, start, stop หรือให้สัญญาณนาฬิกา the counter ของ Timer อื่นที่กำหนดค่าใน Slave Mode.

Figure 140 แสดงภาพรวมของ the trigger selection และ the master mode selection blocks.

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

การใช้ one timer เป็น prescaler สำหรับ timer อื่น

(https://i.imgur.com/2dsRtOd.png)

ตัวอย่างเช่น, ผู้ใช้สามารถกำหนดค่า Timer 1 เพื่อทำงานเป็น a prescaler สำหรับ Timer 2 (ดู Figure 140). เพื่อทำการนี้
•    กำหนดค่า Timer 1 ใน master mode ดังนั้นมันส่งออก a periodic trigger signal บนแต่ละ update event UEV. ถ้าคุณเขียน
      MMS=010 ใน the TIM1_CR2 register, a rising edge ถูกส่งออกบน TRGO1 แต่ละเวลาที่ an update event ถูกสร้าง
•    เพื่อต่อ the TRGO1 output ของ Timer 1 ไปยัง Timer 2, Timer 2 ต้องถูกกำหนดค่าใน slave mode โดยใช้ ITR0 เป็น
      internal trigger. คุณเลือกนี้ผ่านทาง the TS bits ใน the TIM2_SMCR register (เขียน TS=000).
•    จากนั้นคุณใส่ the slave mode controller ใน external clock mode 1 (เขียน SMS=111 ใน the TIM2_SMCR register). นี้
     เป็นเหตุให้ Timer 2 ถูก clocked โดย the rising edge ของ the periodic Timer 1 trigger signal (ซึ่งตรงกันกับ the timer 1
     counter overflow).
•    ในที่สุดทั้งสอง timers ต้องถูกเปิดการใช้งานโดยการเซ็ต CEN bits (TIMx_CR1 register) ของมันตามลำดับ.

Note: ถ้า OCx ถูกเลือกบน Timer 1 เป็น trigger output (MMS=1xx), rising edge ของมันถูกใช้เพื่อให้สัญญาณนาฬิกา
           the counter ของ timer 2.
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on December 01, 2020, 07:02:26 AM
การใช้ one timer เพื่อเปิดการใช้งาน timer อื่น
ในตัวอย่างนี้, เราควบคุม the enable ของ Timer 2 ด้วย the output compare 1 ของ Timer 1. อ้างอิงถึง Figure 140 สำหรับ connections. Timer 2 นับบน the divided internal clock เท่านั้นเมื่อ OC1REF ของ Timer 1 เป็น high. ทั้งสอง counter clock frequencies ถูกหารโดย 3 โดย the prescaler เปรียบเทียบกับ CK_INT (fCK_CNT = fCK_INT/3).
•    กำหนดค่า Timer 1 master mode เพื่อส่ง Output Compare 1 Reference (OC1REF) signal ของมันเป็น trigger output
      (MMS=100 ใน the TIM1_CR2 register).
•    กำหนดค่า the Timer 1 OC1REF waveform (TIM1_CCMR1 register).
•    กำหนดค่า Timer 2 เพื่อรับ the input trigger จาก Timer 1 (TS=000 ใน the TIM2_SMCR register).
•    กำหนดค่า Timer 2 ใน gated mode (SMS=101 ใน TIM2_SMCR register).
•    เปิดการใช้งาน Timer 2 โดยการเขียน '1 ใน the CEN bit (TIM2_CR1 register).
•    เริ่ม Timer 1 โดยการเขียน '1 ใน the CEN bit (TIM1_CR1 register).

Note: The counter 2 clock ไม่ถูก synchronized ด้วย counter 1, โหมดนี้มีผลเพียง the Timer 2 counter enable
           signal.

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

ในตัวอย่างใน Figure 141, the Timer 2 counter และ prescaler ไม่ได้ถูกกำหนดค่าเริ่มต้นมาก่อนการสตาร์ท. ดังนั้นมันเริ่มการนับจากค่าปัจจุบันของมัน. เป็นไปได้ที่จะเริ่มจาก a given value โดยการรีเซ็ตทั้งสอง timers ก่อนการเริ่ม Timer 1. คุณสามารถจากนั้นเขียนค่าใดๆที่คุณต้องการใน the timer counters. The timers สามารถถูกรีเซ็ตได้ง่ายๆโดย software โดยใช้ the UG bit ใน the TIMx_EGR registers.

ในตัวอย่างถัดไป, เราประสาน Timer 1 และ Timer 2. Timer 1 เป็น the master และเริ่มจาก 0. Timer 2 เป็น the slave และเริ่มจาก 0xE7. The prescaler ratio เป็นเหมือนกันสำหรับทั้งสอง timers. Timer 2 หยุดเมื่อ Timer 1 ถูกปิดการใช้งานโดยการเขียน '0' ไปยัง the CEN bit ใน the TIM1_CR1 register:
•    กำหนดค่าTimer 1 master mode เพื่อส่ง Output Compare 1 Reference (OC1REF) signal ของมันเป็น trigger output
      (MMS=100 ใน the TIM1_CR2 register).
•    กำหนดค่า the Timer 1 OC1REF waveform (TIM1_CCMR1 register).
•    กำหนดค่า Timer 2 เพื่อรับ the input trigger จาก Timer 1 (TS=000 ใน the TIM2_SMCR register).
•    กำหนดค่า Timer 2 ใน gated mode (SMS=101 ใน TIM2_SMCR register).
•    รีเซ็ต Timer 1 โดยการเขียน '1 ใน UG bit (TIM1_EGR register).
•    รีเซ็ต Timer 2 โดยการเขียน '1 ใน UG bit (TIM2_EGR register).
•    เริ่มต้น Timer 2 เป็น 0xE7 โดยการเขียน '0xE7' ใน the timer 2 counter (TIM2_CNTL).
•    เปิดการใช้งาน Timer 2 โดยการเขียน '1 ใน the CEN bit (TIM2_CR1 register).
•    เริ่ม Timer 1 โดยการเขียน '1 ใน the CEN bit (TIM1_CR1 register).
•    หยุด Timer 1 โดยการเขียน '0 ใน the CEN bit (TIM1_CR1 register).

(https://i.imgur.com/2vc6kQT.png)
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on December 01, 2020, 08:25:14 AM
การใช้ one timer เพื่อเริ่ม timer อื่น
ในตัวอย่างนี้, เราเซ็ต the enable ของ Timer 2 ด้วย the update event ของ Timer 1. อ้างอิงถึง Figure 140 สำหรับการต่อ. Timer 2 เริ่มการนับจากค่าปัจจุบันของมัน (ซึ่งสามารถไม่เป็น '0') บน the divided internal clock ทันทีที่ the update event ถูกสร้างโดย Timer 1. เมื่อ Timer 2 รับ the trigger signal,  CEN bit ของมันถูกเซ็ตโดยอัตโนมัติและ the counter นับจนกระทั่งเราเขียน '0 ไปยัง the CEN bit ใน the TIM2_CR1 register. ทั้งสอง counter clock frequencies ถูกหารโดย 3 โดย the prescaler เปรียบเทียบกับ CK_INT (fCK_CNT = fCK_INT/3).
•    กำหนดค่า Timer 1 master mode เพื่อส่ง Update Event (UEV) ของมันเป็น trigger output (MMS=010 ใน the
      TIM1_CR2 register).
•    กำหนดค่า the Timer 1 period (TIM1_ARR registers).
•    กำหนดค่า Timer 2 เพื่อรับ the input trigger จาก Timer 1 (TS=000 ใน the TIM2_SMCR register).
•    กำหนดค่า Timer 2 ใน trigger mode (SMS=110 ใน TIM2_SMCR register).
•    เริ่ม Timer 1 โดยการเขียน '1 ใน the CEN bit (TIM1_CR1 register).

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

ดังตัวอย่างก่อนหน้านี้, ผู้ใช้สามารถเริ่มต้นทั้งสอง counters กอนการเริ่มการนับ. Figure 144 แสดง the behavior ด้วยการกำหนดค่าเดียวกันอย่างใน Figure 143 แต่ใน trigger mode แทน gated mode (SMS=110 ใน the TIM2_SMCR register).

(https://i.imgur.com/PN4IV2M.png)
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on December 01, 2020, 08:58:12 AM
การเริ่ม 2 timers พร้อมกันโดยตอบสนองต่อ an external trigger
ในตัวอย่างนี้, เราเซ็ต the enable ของ timer 1 เมื่อ TI1 input ของมันพุ่งขึ้น, และ the enable ของ Timer 2 ด้วย the enable ของ Timer 1. อ้างอิงถึง Figure 140 สำหรับการต่อ. เพื่อให้แน่ใจว่า the counters อยู่ในแนวเดียวกัน, Timer 1 ต้องถูกกำหนดค่าใน Master/Slave mode (slave ด้วยความเคารพต่อ TI1, master ด้วยความเคารพต่อ Timer 2):
•    กำหนดค่า Timer 1 master mode เพื่อส่ง Enable ของมันเป็น trigger output (MMS=001 ใน the TIM1_CR2 register).
•    กำหนดค่า Timer 1 slave mode เพื่อรับ the input trigger จาก TI1 (TS=100 ใน the TIM1_SMCR register).
•    กำหนดค่า Timer 1 ใน trigger mode (SMS=110 ใน the TIM1_SMCR register).
•    กำหนดค่า the Timer 1 ใน Master/Slave mode โดยการเขียน MSM=1 (TIM1_SMCR register).
•    กำหนดค่า Timer 2 เพื่อรับ the input trigger จาก Timer 1 (TS=000 ใน the TIM2_SMCR register).
•    กำหนดค่า Timer 2 ใน trigger mode (SMS=110 ใน the TIM2_SMCR register).

เมื่อ a rising edge เกิดขึ้นบน TI1 (Timer 1), ทั้งสอง counters เริ่มการนับพร้อมกันบน the internal clock และทั้งสอง TIF flags ถูกเซ็ต.

Note: ในตัวอย่างนี้ทั้งสอง timers ถูกเริ่มต้นก่อนการสตาร์ท (โดยการเซ็ต UG bits ของมันตามลำดับ). ทั้งสอง counters เริ่มจาก
           0, แต่คุณสามารถแทรก an offset ได้โดง่ายระหว่างมันโดยการเขียน ค่าใดๆของ the counter registers (TIMx_CNT). คุณจะ
           เห็นว่า the master/slave mode แทรก a delay ระหว่าง CNT_EN และ CK_PSC บน timer 1.

(https://i.imgur.com/kkAke6i.png)
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on December 01, 2020, 09:04:26 AM
15.3.16 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.
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on December 02, 2020, 06:45:24 AM
15.4 TIMx registers
อ้างอิงถึง Section 2.2 สำหรับรายการของตัวย่อที่ใช้ใน register descriptions.

The 32-bit peripheral registers ต้องถูกเขียนโดย words (32 bits). peripheral registers อื่นทั้งหมดต้องถูกเขียนโดย half-words (16 bits) หรือ words (32 bits). Read accesses สามารถถูกทำโดย bytes (8 bits), half-words (16 bits) หรือ words (32 bits).

(https://i.imgur.com/uk8SSMF.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 และ 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 TIM2 to TIM5
Post by: tha on December 02, 2020, 08:41:52 AM
(https://i.imgur.com/BOMxBNm.png)

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

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 เกิดขึ้น

Bits 2:0 Reserved, must be kept at reset value.

ปล. ตามกันทันหรือเปล่า ถ้าไม่เข้าใจอะไรก็ไปสอบถามทางอีเล็คทูเดย์หรือทางอีทีทีน่ะครับ เป็นทีมๆ
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on December 02, 2020, 10:49:03 AM
(https://i.imgur.com/Nvf2FRf.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.
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on December 02, 2020, 10:54:10 AM
(https://i.imgur.com/9ELn2J9.png)
1. เมื่อ a timer ไม่ถูกแสดงใน the product, the corresponding trigger ITRx ก็ไม่มีให้ใช้งาน
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on December 02, 2020, 11:08:59 AM
(https://i.imgur.com/Jpac9FP.png)
(https://i.imgur.com/BZtlliu.png)
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on December 02, 2020, 12:06:50 PM
(https://i.imgur.com/HzMRWTv.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 ถูกเซ็ตอยู่

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

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 Reserved, must be kept at reset value.

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 (อ้างอิงถึง the synchro control register description), ถ้า URS=0 และ UDIS=0
   ใน the TIMx_CR1 register.
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on December 02, 2020, 12:31:09 PM
(https://i.imgur.com/XnoEyVA.png)

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

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 TIM2 to TIM5
Post by: tha on December 02, 2020, 12:54:00 PM
15.4.7 TIMx capture/compare mode register 1 (TIMx_CCMR1)
Address offset: 0x18
Reset value: 0x0000

The channels สามารถถูกใช้ใน input (capture mode) หรือใน output (compare mode). The direction ของ a channel ถูกกำหนดโดยการกำหนดค่า the corresponding CCxS bits. ทุกบิตอื่นๆของ register นี้มี a different function ใน input และใน output mode. สำหรับ a given bit, OCxx อธิบายหน้าที่ของมันเมื่อ the channel ถูกกำหนดค่าใน output, ICxx อธิบายหน้าที่ของมันเมื่อ the channel ถูกกำหนดค่าใน input. ระวังว่าบิตเดียวกันสามารถมีความหมายแตกต่างกันสำหรับr the input stage และสำหรับ the output stage.

(https://i.imgur.com/yvXtTNb.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 TIM2 to TIM5
Post by: tha on December 03, 2020, 10:44:05 AM
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 TIM2 to TIM5
Post by: tha on December 04, 2020, 08:00:52 AM
(https://i.imgur.com/p9dPWIt.png)
(https://i.imgur.com/rwd9hlf.png)
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on December 04, 2020, 08:42:15 AM
(https://i.imgur.com/zz9PD95.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

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

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

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

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

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

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

Bits 3:2 Reserved, must be kept at reset value.

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.

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/0tsjkdz.png)

Note: The state ของ the external IO pins ที่ต่อถึง the standard OCx channels ขึ้นอยู่กับ the OCx channel state และ the
         GPIO และ AFIO registers
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on December 04, 2020, 08:57:01 AM
(https://i.imgur.com/fopzLax.png)
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on December 04, 2020, 09:07:23 AM
15.4.11 TIMx prescaler (TIMx_PSC)
Address offset: 0x28
Reset value: 0x0000

(https://i.imgur.com/xYV9S6N.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 TIM2 to TIM5
Post by: tha on December 04, 2020, 10:28:26 AM
(https://i.imgur.com/jbJaMeq.png)

Bits 15:0 ARR[15:0]: Prescaler value
ARR คือค่าที่ถูกโหลดใน the actual auto-reload register.
อ้าองอิงถึง the Section 15.3.1: Time-base unit สำหรับรายละเอียดที่มากขึ้นเกี่ยวกับ ARR update และ behavior.
The counter ถูก blocked ในขณะที่ the auto-reload value เป็นศูนย์.
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on December 04, 2020, 10:40:53 AM
(https://i.imgur.com/LnS0cFo.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 TIM2 to TIM5
Post by: tha on December 04, 2020, 10:49:09 AM
(https://i.imgur.com/oDn9aOI.png)
(https://i.imgur.com/krGHgfL.png)
Title: Re: STM32F1 TIM2 to TIM5
Post by: tha on December 04, 2020, 11:29:14 AM
(https://i.imgur.com/uKTfwmE.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 TIM2 to TIM5
Post by: tha on December 04, 2020, 11:45:23 AM
(https://i.imgur.com/I8y3qMT.png)

Bits 15:0 DMAB[15: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 TIM2 to TIM5
Post by: tha on December 04, 2020, 11:51:27 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 TIM2 to TIM5
Post by: tha on December 04, 2020, 12:47:25 PM
(https://i.imgur.com/ZxLe3h6.png)
(https://i.imgur.com/5iW99mO.png)

ปล. ก็จบไปอีกตอน อากาศมันเย็นนะวันนี้ ก็เลยได้มากหน่อย ท่านใดไม่เข้าใจก็สอบถามทางอีทีทีกับอีเล็คทูเดย์เอานะ ทำกันเป็นทีมใช่ไหม