STM32 Timers#8 Make 48 bit Counter by Cascading Timers

Started by tha, February 08, 2023, 08:50:37 AM

Previous topic - Next topic

tha

https://controllerstech.com/stm32-timers-8-make-48-bit-counter-by-cascading-timers/

ขอแปลหน่อยนะครับ ขอขอบคุณเขามา ณ ที่นี้ด้วย ท่านใดพอจะมีทรัพย์ก็ Donate เขาด้วยนะ ฟังภาษาอังกฤษเขาในยูทูบก็ฟังง่ายดี(เปิดคำบรรยายด้วย)ครับ

Make 48 bit Counter by Cascading Timers

นี้คือ the 8th tutorial ใน the STM32 Timer series, และวันนี้เราจะมาดูวิธีการเรียง 16 bit counters 3 ตัวให้เป็น a single 48 bit counter. นี่เป็นบทช่วยสอนอีกบทหนึ่งที่ครอบคลุม the timer synchronization และวันนี้เราจะมาดูวิธีซิงโครไนซ์ the timers โดย the External Clock mode.

สำหรับวัตถุประสงค์ของ the application, ฉันจะรวม the 16bit timers 2 ตัวเพื่อทำ a 32 bit counter, และใช้ counter นี้เพื่อวัดช่วงความถี่ที่กว้างขวาง. มันจะเอาชนะข้อเสียของการใช้ a 16 bit counter, ที่เราสามารถวัดได้ถึงแบนด์วิดท์หนึ่งเท่านั้นเนื่องจากขนาดของ counter.
ในทำนองเดียวกัน คุณสามารถรวม 16bit timers 3 ตัวเพื่อทำ a 48bit counter, หรือ 4 timers เพื่อทำ 64bit counter.

ในบทช่วยสอนนี้ ฉันจะใช้ 2 timers, TIM1 and TIM3. TIM3 เป็น the slave ของ TIM1 และถูกควบคุมโดย the trigger ITR0. นี่เป็นไปตาม the internal trigger connection table ใน the F446RE reference manual.



เมื่อ the timers ถูกใช้ใน the slave mode (External Clock mode), the output ของ the master timer จะถูกใช้เป็น the input clock สำหรับ the slave timer. เมื่อทำเช่นนั้น , the APB clock จะยังคงรันที่ max clock speed และ the master timer's counter ก็เช่นกัน. แต่ the slave timer จะช้าลง, ซึ่งช่วยให้เราวัดความถี่ที่ต่ำกว่าได้.

เราจะเห็นสิ่งนี้ในแอปพลิเคชัน มาดูการตั้งค่ากันก่อน

tha

CubeMX Setup

ก่อนอื่นมาดูที่ the clock setup

ที่นี่ ฉันได้กำหนดค่า the external crystal, ซึ่งคือ 8 MHz, เพื่อจัดให้มี the clock, เพื่อให้ระบบรันที่ 90 MHz. นี้เพื่อให้แน่ใจว่าทั้งสอง the APB timer clocks มีความเร็วเดียวกันและดังนั้นทุก the timers จะมี the same base clock.

tha

The Timer 1 config

ข้างล่างคือ the image ที่แสดงการกำหนดค่าสำหรับ the TIM1.

TIM1 ถูกใช้เป็น the master timer.

     •  ผมได้เปิดใช้งาน the input capture บน the channel 1 เพื่อวัด the input signal.
     •  The timer ถูกให้สัญญานนาฬิกาโดย the APB clock, ดังนั้น the base frequency จะอยู่ที่ 90MHz.
     •  การใช้ the prescalar of 9 จะทำให้ความถี่ลดลงเหลือ 10MHz.
     •  10MHz นี้เป็นความถี่ของ the counter 1 ด้วย. โดยพื้นฐาน the counter 1 จะนับที่อัตรานี้.
     •  The ARR ถูกเซ็ตเป็น 10000. นี้ทำให้คาบเวลาของ the counter 1 เท่ากับo 1ms. (เช็คที่ the image ข้างล่าง)
     •  The trigger event selection ถูกเซ็ตเป็น update event, ดังนั้นเราสามารถเรียงหลั่น timer อีกตัวหนึ่งด้วยสิ่งนี้.
     •  The input capture configuration จะถูกคงเอาไว้ที่ค่าเริ่มต้น, อย่างเช่น capture บน rising edge. the interrupt สำหรับ the
         input capture ถูกเปิดใช้งานด้วย.

The TIM1's output frequency จะเท่ากับ 1KHz. นี้จะถูกใช้เป็น the base clock สำหรับ the slave timer.



tha

TIM3 config (Slave Timer)



TIM3 เป็น the slave ของ the TIM1.

     •  ผมได้เซ็ต the slave mode (External Clock Mode 1), และ the Trigger source ถูกเซ็ตเป็น ITR0.
     •  เมื่อ the Update Event จาก the master timer ถูกรวมกับ the External Clock Mode 1, the output of the master
         timer จะถูกใช้เป็น the clock สำหรับ the slave timer.
     •  เนื่องจาก the prescalar ถูกเซ็ตเป็น 1, the counter 3 จะรันที่ 1KHz, ดังนั้นแต่ละนับจะใช้เวลา 1ms.
     •  The ARR ถูกเซ็ตเป็น 65535. นี้ทำให้ the counter 3 period เท่ากับ 65 Seconds.

ดังนั้นโดยการใช้การรวมของ 2 counters นี้, เราสามารถวัด the Frequencies จาก 10MHz(โดยทฤษฎี) ถึง 0.015Hz.


tha

Some insight into the code

The main function



     •  ใน the main function, เราจะสตาร์ท the TIM1 ใน the input capture interrupt mode.
     •  นอกจากนี้เราจะสตาร์ทง่ายๆ the TIM3 เพื่อว่า counter ของมันสามารถนับได้เช่นกัน.

ทันทีที่ the rising edge of the input signal ถูกตรวจพบ, the Input capture callback จะถูกเรียก. เราจะเขียนส่วนที่เหลือของ the code ภายในมัน.

tha

Input Capture callback



     •  ใน the callback, ก่อนอื่นเราจะเช็คว่า the detected edge เป็นขอบแรกกรือขอบที่สอง.
     •  ถ้ามันเป็น the first edge, จากนั้นเราจะรีเซ็ตทั้งสอง the counters และเซ็ต the is_first_captured เป็น 1 แสดงให้เห็นว่า the
         first edge ถูกตรวจจับแล้ว.
     •  เมื่อ the second edge มาถึง the callback จะถูกเรียกอีกครั้ง.
     •  ที่นี่ก่อนอื่นเราจะอ่าน the counters ของ the timers ที่เกี่ยวข้อง.
          •  ตอนนี้เราจะรวมทุก the counters เพื่อทำ a final counter value.
          •  แต่ละนับใน the counter 3 จะเท่ากับ 10000 นับใน counter 1.
     •  ดังนั้นเพื่อทำพวกเขาให้เท่ากัน, เราจำเป็นต้องคุณ the counter 3 ด้วย 10000.
     •  ตอนนี้เรามี the final counter value ในเทอมของ the counter 1, ดังนั้นเพื่อคำนวณ the frequency, เราจะหาร the counter
         1 clock โดย the counter value.
     •  สุดท้ายรีเซ็ต the is_first_captured, เพื่อว่ากระบวนการทั้งหมดสามารถเริ่มต้นได้อีกครั้ง.

tha

Result

เพื่อวัตถุประสงค์ของการทดสอบ ฉันใช้ the NE555 module ซึ่งสามารถส่งออกช่วงความถี่ที่แตกต่างกันได้.

แม้ว่าเราจะใช้หลาย timers, เฉพาะ TIM1 เท่านั้นที่ถูกใช้เป็น the input capture mode. ดังนั้นจึงมีขาอินพุตเพียงขาเดียวดังแสดงในภาพด้านล่าง



ข้างล่างคือรูปที่แสดง the lowest and highest frequency



ดังที่แสดงในภาพด้านบน, the STM32 สามารถวัดได้ the 0.57 Hertz, เช่นเดียวกับ 222KHz. แม้ว่าเราจะกำหนดค่า the TIM1 ให้รันที่ 10MHz, แต่ความจริงที่ว่ามันสามารถวัดความถี่ได้ต่ำถึง 0.5Hz นั้นสามารถทำได้โดยการเรียงหลั่น the timers.

แม้ว่าฉันจะไม่ได้ใช้ the 48bit counter ใน tutorial นี้, แต่คุณยังคงสามารถใช้ตรรกะเดียวกันในการสร้างได้.
คุณจำเป็นต้องเรียงหลั่น 16bit timers 3 ตัวที่มี timer ตัวที่หนึ่งให้สัญญานนาฬิกาตัวที่สอง, และตัวที่สองให้สัญญานนาฬิกาตัวที่สาม,            TIM1->TIM2->TIM3.

ด้วยการกำหนดค่านี้, สมการเพื่อคำนวณ the frequency ถูกแสดงข้างล่าง