If you always get the case for zero, then it seems like your microcontroller doesn't register when button is pressed. There can be many reasons and without actual circuit, hard to tell exactly what could be wrong. I suggest you double check your circuit and see if pressing a button actually changes voltage (replace MCU with single LED and when you press button, it should turn off)
And when it's working, there will be an issue with button bounce. If you're not debouncing it with hardware, then you can see in image below that there's a small period of time when values jump between logical 0 and 1, so you would need to either use internal timers to delay button registering or register a button press after certain amount of samples (there are a lot of software debounce libraries on the internet)
I've also compiled a bit modified program for 1MHz mega32 with LCD, code can be seen here. Program working as intended, it registers a number of button clicks and prints them. Used a pull-up setup (just like your code suggests). Since there's no hardware debouncing, it sometimes registered a few more clicks than I've pressed. So for now, CHECK your circuit, then DOUBLE CHECK it.
UPDATED
Example code (compiled using atmel studio 6.2)
Used with below circuit. Keep in mind, that this setup has button bouncing and may register more presses than you actually pressed.
If this setup does not work, then it's possible MCU is defective (but unlikely), so again - DOUBLE CHECK your circuit.
See if every connection is correct;
See if your wiring (diodes, switches etc) are correct;
Since you can't upload your circuit for some reason, if you're using external clock source, see if it's connected correctly and fuse bytes are set (then set F_CPU to your external clock frequency)
Avoid using large delay cycles, use for (;;) if you must.
And again, I've given example of circuit with which code works well enough (not counting lack of software debounce) and if there's something different in your setup, you MUST PROVIDE additional information if you want more help.
The timer being used to generate the PWM signal has an interrupt service routine (ISR). It is either triggered every time the output pin changes (Interrupt on match), or the timer is reloaded (Interrupt-on-overflow).
That interrupt service routine can increment a variable to count one more pulse every time it is called.
AFAICT, all of the PIC18F8722 timers support, at least, one of those two types of ISR.
There is no need for another hardware counter.
The pulse count will be stored in a 'global' variable and so can be read by code outside the ISR. If you are using C to program the PIC18F8722, then the pulse count variable will be defined as 'volatile'.
Ideally, the count will fit in one byte, you will only need to count up to 255. However, if the pulse count variable needs to be bigger than a byte, you'll need to be slightly careful how the code outside the ISR uses it.
A variable bigger than one byte will be read in several instructions, so it is possible for the interrupt to happen in the middle of the main code accessing its value. This can result in the main code occasionally getting confused about the actual value of the pulse count.
(Please add a bit more information to your question, so that we can understand if you need help with solving this kind of problem. If it is not an issue, it seems too much complexity to add to an answer.)
Best Answer
Use another timer as a slave to count
TIM2
reloads.SMCR
register in the Reference Manual to see if it can be slaved toTIM2
.TIM5
would be a good choice as it has a 32 bit counter.TIM2
as master, trigger output on Update (MMS
bits inCR2
). It will generate a trigger output whenever the PWM counter is reloaded.TIM5
as slave. First, check the table at the end of theSMCR
register description. FindTIM2_TRGO
(the master) in the row corresponding toTIM5
(the slave). Note theTS=xxx
setting in the column header, this value goes into theTS
bits ofSMCR
. Slave Mode (SMS
) should be External Clock Mode (111
=0x07
). Now,TIM5
will count the reloads ofTIM2
, effectively the PWM cycles.TIM5
) first, the master (TIM2
) afterwards.