So we set the counting rate by prescalar which impacts how quickly TCNTn will count and that is then compared to OCRnX. This will effective make the counter move at 4MHz which will cause the counter to max out at ~16MHz (((1/4000000)*65536)*1000, multiplied by a 1000 to get to ms). Let’s take an example when the prescalar is 2. What if we counted every 2 cycles as one or every 4 cycles as one we could spread out the time it takes to count. What if you want to wait for longer? The solution is prescalar. So the clock will be counting at 8MHz and will go back to 0 as soon as TCNTn will hit 65536. This means if you have a 16bit counter (can count up to 2^16=65535), your maximum time available before an interrupt is generated is ~8ms. Let’s take for example the internal clock which runs at 8MHz. This concept simply allows you to choose longer units of time (output clock time is defined by clkTn). On top that there is a concept of prescalar. We can select the clock source (Tn) we want, we can have an external clock source or an internal clock source (the clock provided by your MCU). So then how is TCNT1 is counting? The following is shown in the image below: Just a side note I don’t know why there are multiple compare registers, but my best guess is that they can be used for different things and compare at different times. So when you try to write TCNTnH you will always write to a temp register first and when you write to TCNTnL directly will TCNTnH be written to from the temp register. The other major take away is that the OCRnx is divided into high and low bytes and so are TCNTn. TCNT1 is the timer/counter that is counting and as soon as TCNT1 hits the value specified in OCR1A OCF1A goes high until the interrupt corresponding to the flag is executed (this can result in setting another pin high or in our case printing out to the serial monitor.) Don’t worry about the Waveform Generator and OCnx for this explanation. This is set up by the TIFRn register, also known as Timer Interrupt Flag Register. OCF1A sets up a flag that lets the MCU know that TCNT1 will be compared with OCR1A.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |