Counting without “counter” in microcontroller is Possible?

和自甴很熟 提交于 2019-12-13 04:35:40

问题


Hello friends i am new in microcontroller domain wants to know is this not possible to counts an events without using counter i mean by monitoring the pin which is connected to the the event generator.i am asking this question because presently i want to calculate the frequency of sensor i have already checked it frequency by using DSO(Digital storage oscilloscope) and it(DSO) says its frequency is 98khz but as we all now that the maximum value of an 8 bit counter is 255 then how it is possible......The sensor is connected to PB0(TCNT0/Counter 0 of Atmega16) .........please help


回答1:


You can do it 2 different ways. The 1st is using the prescaler. You can set it to 1024 for example so every 1024th time PB0 is triggered(or what ever the correct term is, it has been a while since I dealt with MCUs) the counter is incremented by 1. So essentially the counter would be counting every KHz (approximately).

The 2nd way is using no prescaler but to set the MCU so that every time the counter overflows you increment some other register by 1, and every time that register overflows you increment another register by 1. Using the 8 bit timer and those 2 additional 8 bit registers you can count up to 16,777,215. I actually prefer this 2nd method but I am probably alone in this : )

Also Greybeard & Andy Brown are right about the stm32 and microprocessor tags, u should remove them.




回答2:


You can assign the sensor pin to an external interrupt pin (INT0\INT1) , then create a global variable that increments in the ISR at every external signal from the sensor , and get sample every fixed amount of time.

This time is configured by the timer interrupt but independent to the number of counts (timer works as a clock only), and so inside the ISR of the timer you should divide the number of counts by this fixed amount of time (counter/Ts).

Then you should set the variable that represents the counter to zero , the counter variable here could be an unsigned long, to make sure that it works with high frequencies.



来源:https://stackoverflow.com/questions/31730103/counting-without-counter-in-microcontroller-is-possible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!