Hello, I'm Guu.
The other day, I created an LED lighting program using the timer interrupt of the microcomputer in training.
I would like to introduce the "division" that I struggled with.
Introduction
Frequency division is a method of obtaining the desired frequency by dividing the frequency.
I knew the meaning of frequency division, but I didn't know what value should be used to divide the frequency well, so I went to my seniors to ask questions.
Task
Generate10 ms using a clock with afrequency of 20 MHz.
How many divisions are required to obtain the desired frequency?
Guu "I don't know how to divide the frequency..."
Senior: "... Do you know what Hz is 10 ms?"
Guu" 1 / 10 ms = 100, so it's 100 Hz."
Senior: “Then, how many times should you divide 20 MHz to get 100 Hz?”
Guu "20 MHz / 100 Hz = divide by 200000!"
Senior: "Yes. Do you know what to use to divide by 200000?"
` `I found something called a `` count source'' in the microcomputer.
* The count source is the divided frequency of the main clock.
In this case, the count source could be selected from division by 1 (no division), division by 2, division by 8, and division by 32.
Senpai: “Try dividing by using a divide-by-8count source.”
Guu "Yes!"
Challenge dividing by using the count source of dividing by 8!
Try dividing by using a divide-by-8 count source...
20MHz / 8 = 2500000Hz
It never goes to100 Hz!
Guu ``Senpai..., the frequency division is not enough (; O ;) ''
Senior: “Isn’t it? (laughs) Was there anything else that could be used for frequency division?”
If you open the data sheet and search for "dividing"...
Guu "Yes! I found a division ratio of 1 / ( n + 1 ) ( m + 1 )!"
*This is a method of division using the prescaler of the timer.
Prescaler: Using a counter to divide the input frequency by an integer
Challenge division using the division ratio formula! !
Senpai: "Yeah, use this to divide the rest of the frequency. Enter appropriate values for m and n and do the calculation."
Calculating…
Since I divided up to 2500000 Hz with the count source earlier, the rest is divided by 25000.
n = 24999 , m = 0 ?
Senpai: “Oh, by the way, there is a range that can be set for n and m, so be careful.”
Guu"?!"
I checked the datasheet and found the following:
Setting range ofn and m: 00 h to FF h(0 to 255in decimal)
In other words, n = 24999 and m = 0cannot be set...
Recalculate...
Guu: 250 x 100 = 25000, so if you apply this to the division ratio formula...
n = 249and m = 99! ”
Senpai: “OK!”
With this kind of feeling, I was able to divide the frequency safely with the following formula.
(desired frequency) =(original frequency)x 1 /(count source)x 1 / ( n + 1 ) ( m + 1 )
100Hz = 20MHz× 1 / 8× 1 / ( 249 + 1 ) ( 99 + 1 )
be careful
This time, we set the count source and division ratio with the above combination, but these values change depending on the target frequency.
For example, in this case, if the clock source is set to divide by 2, the division ratio will be used to
You have to divide by 100000.
However, n and m can only be set up to 255, and even if each is set to 255
You can only divide up to( 255 + 1 ) ( 255 + 1 ) = 65536.
The count source must be selected to prevent this from happening.
When dividing, please try to generate the exact frequency you want by paying attention to the above points.
Summary
Perform frequency division to generate the target frequency from the original frequency.
In this case, the frequency was divided by the following formula.
(desired frequency) =(original frequency)x 1 /(count source)x 1 / ( n + 1 ) ( m + 1 )
Setting range of n and m: 00 h to FF h (0 to 255 in decimal)
Guu's tweet
I need to be a little careful when setting the division ratio n and m.
Be careful not to accidentally set a value outside the range (>_<)