STM32F1 TIM2 to TIM5

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

Previous topic - Next topic

tha

Center-aligned mode (up/down counting)
ใน center-aligned mode, the counter นับจาก 0 ไปยัง the auto-reload value (ที่อยู่ภายในของ the TIMx_ARR register) – 1, สร้าง a counter overflow event, จากนั้นนับจาก the 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






tha

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.


tha

External clock source mode 1
โหมดนี้ถูกเลือกเมื่อ SMS=111 ใน the TIMx_SMCR register. The counter สามารถนับที่แต่ละ rising หรือ falling edge บน a selected input.



ตัวอย่างเช่น, ในการกำหนดค่า 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



tha

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.



ตัวอย่างเช่น, ในการกำหนดค่า 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


tha

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



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



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 ออกมา

tha


tha

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.

tha