STM32F1 TIM2 to TIM5

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

Previous topic - Next topic

tha


tha


tha

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


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



tha

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.

tha

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.



tha

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.



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.



คำแนะนำในการใช้ 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 ขณะที่มันกำลังรันอยู่.

ปล. อากาศกำลังเย็นสบาย เลยได้เยอะหน่อย

tha

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.



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

tha

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