STM32F1 TIM1 & TIM8

Started by tha, November 21, 2020, 08:18:38 AM

Previous topic - Next topic

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

Note: The capture prescaler ไม่ได้ถูกใช้สำหรับการทริกเกอร์, ดังนั้นไม่มีความจำเป็นที่จะกำหนดค่ามัน.

เมื่อ 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 76 แสดงภาพรวมของ 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

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

Figure 78 ถึง Figure 81 แสดงภาพรวมของ 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. มันถูก 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

14.3.6 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


tha

14.3.7 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


1. The PWM input mode สามารถถูกใช้เพียงกับ the TIMx_CH1/TIMx_CH2 signals เนื่องจากความจริงแล้วมีเพียง TI1FP1 และ TI2FP2 ที่ถูกต่อกับ the slave mode controller.

tha

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

tha

14.3.9 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 เพื่อสลับ OCx output pin เมื่อ CNT เท่ากันกับ CCRx
    – เขียน OCxPE = 0 เพื่อปิดการใช้งาน preload register
    – เขียน CCxP = 0 เพื่อเลือก active high polarity
    – เขียน CCxE = 1 เพื่อเปิดการใช้งาน the output
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 83.