STM32F1 TIM1 & TIM8

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

Previous topic - Next topic

tha

14.3.18 Interfacing with Hall sensors
นี้ทำได้โดยใช้ the advanced-control timers (TIM1 or TIM8) สร้าง PWM signals เพื่อขับ the motor และ timer TIMx อื่นๆ (TIM2, TIM3, TIM4 หรือ TIM5) เรียกว่า "interfacing timer" ใน Figure 95. The "interfacing timer" จับ the 3 timer input pins (TIMx_CH1, TIMx_CH2, และ TIMx_CH3) ที่ต่อผ่านทาง a XOR ไปยัง the TI1 input channel (ที่เลือกโดยการเซ็ต the TI1S bit ใน the TIMx_CR2 register).

The slave mode controller ถูกกำหนดใน reset mode; the slave input คือ TI1F_ED. ดังนั้น, แต่ละเวลาที่หนึ่งของ the 3 inputs สลับ, the counter สตาร์ทการนับใหม่จาก 0. นี้สร้าง a time base ทริกเกอร์โดยการเปลี่ยนใดๆบน the Hall inputs.

บน the "interfacing timer", capture/compare channel 1 ถูกกำหนดใน capture mode, capture signal คือ TRC (ดู Figure 78). The captured value, ซึ่งตรงกันกับเวลาที่ผ่านไประหว่าง 2 การเปลี่ยนบน the inputs, ให้ข้อมูลเกี่ยวกับ motor speed.

The "interfacing timer" สามารถถูกใช้ใน output mode เพื่อสร้าง a pulse ซึ่งเปลี่ยนการกำหนดค่าของ the channels ของ the advanced-control timer (TIM1 or TIM8) (โดยการทริกเกอร์ a COM event). The TIM1 timer ถูกใช้เพื่อสร้าง PWM signals เพื่อขับ the motor. เพื่อทำการนี้, the interfacing timer channel ต้องถูกโปรแกรมเพื่อว่า a positive pulse ถูกสร้างหลังจาก a programmed delay (ใน output compare หรือ PWM mode). pulse นี้ถูกส่งไปยัง the advanced-control timer (TIM1 or TIM8) ผ่านทาง the TRGO output.

ตัวอย่าง: ผู้ใช้ต้องการเปลี่ยน the PWM configuration ของ the advanced-control timer TIM1 หลังจาก a programmed delay แต่ละเวลาการเปลี่ยนเกิดขึ้นบน the Hall inputs ที่ต่อถึงหนึ่งของ the TIMx timers.
•    กำหนด 3 timer inputs ORed ไปยัง the TI1 input channel โดยการเขียน the TI1S bit ใน the TIMx_CR2 register เป็น
      '1',
•    โปรแกรม the time base: เขียน the TIMx_ARR เป็น the max value (the counter ต้องถูกเคลียร์โดย the TI1 change. เซ็ต
      the prescaler เพื่อให้ได้ a maximum counter period ที่ยาวนานกว่าเวลาระหว่าง 2 การเปลี่ยนบน the sensors,
•    โปรแกรม channel 1 ใน capture mode (TRC ถูกเลือก): เขียน the CC1S bits ใน the TIMx_CCMR1 register เป็น '11'. ผู้
      ใช้สามารถโปรแกรม the digital filter อีกด้วยถ้าต้องการ,
•    โปรแกรม channel 2 ใน PWM 2 mode พร้อมด้วย the desired delay: เขียน the OC2M bits เป็น '111' และ the CC2S bits
      เป็น '00' ใน the TIMx_CCMR1 register,
•    เลือก OC2REF เป็น trigger output บน TRGO: เขียน the MMS bits ใน the TIMx_CR2 register เป็น '101',

ใน the advanced-control timer TIM1, the right ITR input ต้องถูกเลือกเป็น trigger input, the timer ถูกโปรแกรมเพื่อสร้าง PWM signals, the capture/compare control signals ถูกโหลดไว้ล่วงหน้า (CCPC=1 ใน the TIMx_CR2 register) และ the COM event ถูกควบคุมโดย the trigger input (CCUS=1 ใน the TIMx_CR2 register). The PWM control bits (CCxE, OCxM) ถูกเขียนหลังจาก a COM event สำหรับ the next step (นี้สามารถถูกทำใน an interrupt subroutine ที่สร้างโดย the rising edge ของ OC2REF).

Figure 95 อธิบายตัวอย่างนี้.


tha

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

รูปที่ 96 แสดงพฤติกรรมนี้เมื่อ 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

14.3.20 Timer synchronization
The TIMx timers ถูกเชื่อมโยงเข้าด้วยกันภายในสำหรับ timer synchronization หรือ chaining. อ้างอิงถึง
Section 15.3.15: Timer synchronization สำหรับรายละเอียด.

Note: The clock ของ the slave timer ต้องถูกเปิดการใช้งานก่อนเพื่อได้รับ events จาก the master timer, และต้องไม่ถูก
           เปลี่ยนกลางอากาศขณะที่ triggers ถูกได้รับจาก the master timer.

tha

14.3.21 Debug mode
เมื่อ the microcontroller เข้าสู่ debug mode (Cortex®-M3 core - ถูกหยุด), the TIMx counter ยังคงทำงานตามปกติหรือหยุดอย่างใดอย่างหนึ่ง, ขึ้นอยู่กับ DBG_TIMx_STOP configuration bit ใน DBGMCU module. สำหรับรายละเอียดที่มากขึ้น, อ้างอิงถึงo Section 31.16.2: Debug support for timers, watchdog, bxCAN and I2C.

เพื่อความมุ่งหมายด้านปลอดภัย, เมื่อ the counter ถูกหยุด (DBG_TIMx_STOP = 1 ใน DBGMCU_APBx_FZ register), the outputs ถูกปิดการใช้งาน (ราวกับว่า the MOE bit ถูก reset). The outputs สามารถอย่างใดอย่างหนึ่งถูกบังคับเป็น an inactive state (OSSI bit = 1), หรือมีการควบคุมของมันเข้าควบคุมโดย the GPIO controller (OSSI bit = 0) เพื่อ force พวกมันเป็นไฮอิมพิแด๊นท
(Hi-Z).

tha

14.4 TIM1 and TIM8 registers
อ้างอิงถึง Section 2.2 สำหรับรายการของตัวย่อที่ใช้ใน register descriptions.

The peripheral registers สามารถถูกเข้าถึงโดย half-words (16-bit) หรือ words (32-bit).



Bits 15:10 Reserved, must be kept at reset value.

Bits 9:8 CKD: Clock division
bit-field นี้แสดง the division ratio ระหว่าง the timer clock (CK_INT) frequency และ the dead-time และ sampling clock ที่ถูกใช้โดย the digital filters (ETR, TIx),
      00: tDTS = tCK_INT
      01: tDTS = 2 x tCK_INT
      10: tDTS = 4 x tCK_INT
      11: Reserved

Bit 7 ARPE: Auto-reload preload enable
     0: TIMx_ARR register ไม่ถูก buffered
     1: TIMx_ARR register ถูก buffered

Bits 6:5 CMS: Center-aligned mode selection
     00: Edge-aligned mode. The counter นับขึ้นหรือลงขึ้นอยู่กับ the direction bit (DIR).
     01: Center-aligned mode 1. The counter นับขึ้นและลงอีกทางหนึ่ง. Output compare interrupt flags ของ channels ที่
           กำหนดค่าใน output (CCxS=00 ใน TIMx_CCMRx register) ถูกเซ็ตเพียงเมื่อ the counter กำลังนับลง.
     10: Center-aligned mode 2. The counter นับขึ้นและลงอีกทางหนึ่ง. Output compare interrupt flags ของ channels ที่
           กำหนดค่าใน output (CCxS=00 ใน TIMx_CCMRx register) ถูกเซ็ตเพียงเมื่อ the counter กำลังนับขึ้น.
     11: Center-aligned mode 3. The counter นับขึ้นและลงอีกทางหนึ่ง. Output compare interrupt flags ของ channels ที่
           กำหนดค่าใน output (CCxS=00 ใน TIMx_CCMRx register) ถูกเซ็ตทั้งเมื่อ the counter กำลังนับขึ้นหรือลง.
Note: จะไม่ยอมให้สวิทช์จาก edge-aligned mode ไปยัง center-aligned mode ตราบเท่าที่ the counter ถูก enabled
            (CEN=1)

Bit 4 DIR: Direction
     0: Counter ถูกใช้เป็น upcounter
     1: Counter ถูกใช้เป็น downcounter
Note: บิตนี้ถูกอ่านได้เพียงเมื่อ the timer ถูกกำหนดค่าใน Center-aligned mode หรือ Encoder mode.

Bit 3 OPM: One-pulse mode
     0: Counter ไม่ถูกหยุดที่ update event
     1: Counter หยุดการนับที่ the next update event (โดยการเคลียร์ the bit CEN)

Bit 2 URS: Update request source
บิตนี้ถูกเซ็ตและเคลียร์โดย software เพื่อเลือก the UEV event sources.
     0: the following events ใดๆสร้าง an update interrupt หรือ DMA request ถ้า enabled. events เหล่านี้สามารถเป็น:
         – Counter overflow/underflow
         – การเซ็ต the UG bit
         – Update generation ผ่านทาง the slave mode controller
     1: เพียง counter overflow/underflow เท่านั้นที่สร้าง an update interrupt หรือ DMA request ถ้า enabled.

Bit 1 UDIS: Update disable
บิตนี้ถูกเซ็ตและเคลียร์โดย software เพื่อเปิดการใช้งาน/ปิดการใช้งาน UEV event generation.
     0: UEV ถูกเปิดการใช้งาน. The Update (UEV) event ถูกสร้างโดยหนึ่งของ the following events:
         – Counter overflow/underflow
         – การเซ็ต the UG bit
         – Update generation ผ่านทาง the slave mode controller
     Buffered registers จากนั้นถูกโหลดด้วย preload values ของมัน.
     1: UEV ถูกปิดการใช้งาน. The Update event ไม่ถูกสร้าง, shadow registers เก็บค่าของมัน (ARR, PSC, CCRx). อย่างไรก็ตาม
         the counter และ the prescaler จะถูกเริ่มต้นใหม่ถ้า the UG bit ถูกเซ็ตหรือถ้า a hardware reset ถูกได้รับจาก the slave
         mode controller.

Bit 0 CEN: Counter enable
     0: Counter ถูกปิดการใช้งาน
     1: Counter ถูกเปิดการใช้งาน
Note: External clock, gated mode และ encoder mode สามารถทำงานเพียงถ้า the CEN bit ถูกเซ็ตมาก่อนแล้วโดย
         software. อย่างไรก็ตาม trigger mode สามารถเซ็ต the CEN bit โดยอัตโนมัติโดย hardware.
CEN ถูกเคลียร์โดยอัตโนมัติใน one-pulse mode, เมื่อ an update event เกิดขึ้น.