STM32F1 TIM2 to TIM5

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

Previous topic - Next topic

tha

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 ไม่สวิทช์ในเวลาเดียวกัน.



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 ถูกเปิดการใช้งาน)



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



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.

tha

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.

tha

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.



tha

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.


tha

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.



tha

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.




tha

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 อื่น



ตัวอย่างเช่น, ผู้ใช้สามารถกำหนดค่า 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.

tha

การใช้ 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.



ในตัวอย่างใน 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).