Continue to Site

# How does counter count

Status
Not open for further replies.

#### Djsarkar

##### Member
Suppose a hypothetical situation in which 8 bit counter starts at 0 and ends at 255 .Counter takes 1 microsecond time to count one pulses So 5 milliseconds will require 360 pulses

But counter can only count 255 pulses, so now how does counter count 360 pulses in micro?

You don't use an 8 bit counter, you use one sufficiently large enough to cover the range you require.

You can do it by using a larger hardware counter (PIC's commonly have 8 and 16 bit counters available, with some new enhanced ones having 24 bit as well), or by using the overflow from an 8 bit counter to increment a suitably sized variable.

Last edited:
First I don't understand your calculation. A millisecond is 1000 microseconds so 5 mS will be 5000 microseconds so you need to count to 5000. I have no idea where the 360 comes from. Counters in a microcontroller normally set a flag when they overflow. This flag can be configured to cause an interrupt. All you have to do is write an interrupt service routine that increments a software counter. By doing that that you can count up to any number. Many 8 bit microcontrollers have 16 bit counters and I have seen information in data sheets that some more recent microcontrollers have the option of combining two 16 bit counters to directly count up to 32 bits. I have not yet tried using this option.

Les.

First I don't understand your calculation.
Les.
I am sorry My calculations are wrong If we only have to count 356 pulse, then how does the counter count because 8 bit counter will start at 0 and end at 255 and again it will run from 0 to 255. will it count remaining 101 pulses in second round and then it will stop

I am sorry My calculations are wrong If we only have to count 356 pulse, then how does the counter count because 8 bit counter will start at 0 and end at 255 and again it will run from 0 to 255. will it count remaining 101 pulses in second round and then it will stop

Already explained - the actual numbers make no difference - if the counter is only 8 bit it will overflow and carry on counting.

Already explained - the actual numbers make no difference - if the counter is only 8 bit it will overflow and carry on counting.
->The purpose of the prescaler is to allow the timer to be clocked at the rate a user desires.

How does prescaler work?

The prescaler is just another counter that can be configured to divide the input pulses by a number that is configurable. This number would be a binary value such as 2 4, 8, 16.. So if the prescaler was set to divide by 4 then the counter itself would only increment by 1 for every 4 input pulses. (So the counter itself would then overflow after 1024 input pulses. The value in the prescaler cannot be read so you would not know the value of the two least significant bits of the 10 bit value. If you tell us which microcontroller you are using and EXACTLY what you are trying to do we can probably give you an example of the code to do the counting.

Les.

Last edited:
But counter can only count 255 pulses, so now how does counter count 360 pulses in micro?
Counters in a micro; when they overflow, set a overflow bit. This signal the computer and may cause a interrupt. With out using interrupts, you can read the overflow as a 9th bit. You do not know how many times there has been a over flow.

Clear counter, clear OF bit.
Enable counter,
Every time there is a OF, clear OF bit and increment a software counter.
disable counter

I am still confused.

Consider first case, When we need to count 255 pulses. We will load the counter register with 1 value and each time counter will increment and when it will reaches at 255 it will roll over and generated interrupt.

Second case , when we need to count 257 pulses. What happens in this case?

What the value need to load counter register to count 257 pulses?

If I load the value 1 then it will only count 255 pulses and when counter will roll over it will generate interrupt

Consider how you count to 10 .... Starting at 0, when you get to 9 and rollover back to 0, you carry a 1 or add 1 to the next digit. This is Base 10 numbering system .... the micro just uses a base 256 numbering system.

If you want to count to less than 512 (356 mentioned above) then you can still use an 8 bit counter and use the overflow flag as a ninth bit.

Mike.

If you want to count to less than 512 (356 mentioned above) then you can still use an 8 bit counter and use the overflow flag as a ninth bit.

Mike.
My question is what value has to be loaded in the counter register to count 257 pulse

To count to any number less then 512 load the counter with zero and clear the overflow bit. At a count of 257 the counter will contain 1 and the overflow flag will be set. for 356 the counter will contain 100 and the overflow bit will be set. To get the value add 256 to the counter value if the overflow bit is set.

Mike.

Adding to the explanation in post #10. We normally use hexadecimal (Base 16) when representing numbers on computers. So an 8 bit number is represented as two hexadecimal digits. The least significant by bits 0 to 3 and the most significant by bits 4 to 7. So when all bits 0 to 3 increment from all ones to all zeroes (From 0x0F to 0x00) you increment the next group of 4 bits (Bits 4 to 7) from 0x00 to 0x01. If you understand this then you should be able to see if you go past a 8 bit value you just continue the idea. so when bits 4 to 7 increment from all ones to all zeroes you increment the next group of 4 bits (Bits 8 to 11)
Going back to your problem you are using a hardware counter for bits 0 to 7 and any higher bits in software. (The value is stored in a memory location)
So when you see the overflow bit set (You would normally do this in an interrupt routine.) you increment the value stored in a memory location and also clear the overflow bit. One very common use would be for keeping track of time. You might arrange for the hardware counter to overflow every second and you would use that to increment a value that you called "Seconds" The software instructions would check the value of "seconds" every time it was incremented. If it reached 60 it would change it to zero and also increment another value that you called "minutes". You could just extend this idea to go to hours, days etc.
Answering your question in post #13 To start with you would set the counter to zero. It would then count from 0 to 255. then next input pulse (pulse 256) would set it back to zero and increment the ninth bit. (The ninth bit has a value of 256.) You could keep checking the value of the counter but if you also loaded it with a value of 255 at the same time as incrementing the ninth bit it would overflow again on the next input pulse (Pulse 257) This would save having to to do a compare in software. Some microcontrollers have a compare register that can set a bit to indicate that the counter value matches the value in the compare register. This is just an alternative way to achieve detecting the count value. There are normally many different ways to solve a problem.

Les.

Last edited:
Status
Not open for further replies.

Replies
6
Views
2K
Replies
8
Views
1K
Replies
23
Views
4K
Replies
8
Views
3K
Replies
10
Views
2K