STM32F7 22 Advanced-control timers (TIM1/TIM8)

Started by tha, December 26, 2022, 03:05:39 PM

Previous topic - Next topic

tha

External clock source mode 2

โหมดนี้ถูกเลือกโดยการเขียน ECE=1 ใน the TIMx_SMCR register.

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

Figure 174 แสดงภาพรวมของ 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

22.3.6 Capture/compare channels

แต่ละ Capture/Compare channel ถูกสร้างขึ้นจาก a capture/compare register (รวมถึง a shadow register), an input stage สำหรับ capture (พร้อมด้วย digital filter, multiplexing และ prescaler, ยกเว้นสำหรับ channels 5 and 6) และ an output stage (พร้อมด้วย comparator and output control).

Figure 176 ถึง Figure 179 แสดงภาพรวมของ one Capture/Compare channel.

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. มันถูกปรับสเกลก่อน 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.

tha

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


tha

ตัวอย่างต่อไปนี้แสดงวิธีการจับ the counter value ใน TIMx_CCR1 เมื่อ TI1 input พุ่งขึ้น. เพื่อทำสิ่งนี้, ใช้ขั้นตอนดังต่อไปนี้
1.   เลือก 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 จะอ่านได้เพียงอย่างเดียว.
2.   โปรแกรม the input filter duration ที่คุณต้องการโดยคำนึงถึง the signal ที่ต่อกับ the timer (เมื่อ the input คือหนึ่งใน the
      TIx (ICxF bits ใน the TIMx_CCMRx register). ลองนึกภาพว่า, เมื่อมีการสลับ, the input signal ไม่มีความเสถียรในระหว่างที่
      ต้อง 5 internal clock cycles. เราต้องโปรแกรม a filter duration ให้ยาวกว่า 5 clock cycles นี้. เราสามารถตรวจสอบความถูกต้อง
      ของการเปลี่ยนแปลงบน TI1 เมื่อ 8 การสุ่มต่อเนื่องด้วย the new level ถูกตรวจจับแล้ว (สุ่มที่ fDTS frequency). ดังนั้นเขียน IC1F
      bits เป็น 0011 in the TIMx_CCMR1 register
3.   เลือก the edge ของ the active transition บน the TI1 channel โดยการเขียน the CC1P bit เป็น 0 ใน the TIMx_CCER
      register (rising edge ในกรณีนี้).
4.   โปรแกรม the input prescaler. ในตัวอย่างของเรา, เราอยากให้ the capture ถูกดำเนินการที่แต่ละ valid transition, ดังนั้น the
      prescaler ถูกปิดการใช้งาน (เขียน IC1PS bits เป็น 00 ใน the TIMx_CCMR1 register).
5.   เปิดการใช้งาน capture จาก the counter ลงใน the capture register โดยการเซ็ต the CC1E bit ใน the TIMx_CCER
      register.
6.   ถ้าต้องการ, เปิดการใช้งาน the related interrupt request โดยการเซ็ต the CC1IE bit ใน the TIMx_DIER register, และ/
      หรือ the DMA request โดยการเซ็ต the CC1DE bit ใน the TIMx_DIER register.

tha

เมื่อ 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

22.3.8 PWM input mode

โหมดนี้เป็นกรณีพิเศษของ input capture mode. ขั้นตอนเหมือนกันยกเว้น:
•    Two ICx signals ถูกแม็ปบน the same TIx input.
•    2 ICx signals นี้ทำงานบน edges ที่มีขั้วตรงกันข้ามกัน.
•    หนึ่งใน the two TIxFP signals นี้ถูกเลือกเป็น trigger input และ the slave mode controller ถูกกำหนดค่าใน reset mode.

tha

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


tha

22.3.9 Forced output mode

ใน output mode (CCxS bits = 00 ใน the TIMx_CCMRx register), แต่ละ output compare signal (OCxREF และจากนั้น OCx/OCxN) สามารถถูกบังคับให้ 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.

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