Hi oookey ,
Your welcome.
oookey wrote:where went wrong to my version that did not work accordingly, any pointing from you please?
First your configuration settings were not correct.
1) Your microcontroller will be in the permanent reset state if Master CLeaR (MCLR ) is set to external and you have not got a pull-up resistor connected from MCLR pin to +5V (note MCLR can be tied directly to +supply but this is not recommended if programming via ICSP).
You can disable the reset function by changing MCLR in your configuration settings from External to Internal.
2) Watchdog was enabled within configuration settings.
Normally with watchdog enabled, you need place commands to reset WatchDog Timer WDT within a short time period e.g. less than every 18ms or your chip will reset.
This is to prevent lockups.
Your hardware was not running due to 1)
You will need to set Watchdog Timer to off.
Looking at your Flowchart if configuration settings were correct then:
Timer interrupt is set so ISR is accessed every 1/7812.5 = 128uS
all that's happening is while count<1500 (1500*128uS = 0.12sec) the LED is switching on and off for the duty cycle of PWM at the frequency of PWM. 50% duty at 7.8125KHz will mean that LED will look like is on all the time but at about 1/2 its full brightness.
Count2 will increase by 1 every 0.12secs until Count2 is = 5 = 5 * 0.12 = 0.6 seconds.
So after 0.6 seconds both Count and Count2 are reset, PWM is disabled for 3000*128uS = 0.384sec and cycle will start again.
But since settings were not correct then the above will not even take place.
If it did all will be too fast to see.
I believe the best way is set timer interrupt for a much slower interrupt time.
Then within ISR, increment a count that will reset every second, then just increment seconds by 1.
Its not a good idea to keep enabling and disabling PWM.
What I would do is enable PWM before main loop then if LED is to be off set PWM duty to 0.
If LED is to be on at full brightness then set PWM duty for approx 85- 95 %
PWM only has to be faster than persistence of vision frequency (or flicker fusion threshold) .
so it can be any frequency above 60Hz
Personally I use about 1KHz so I'm well above but not too much to cause other issues.
Hope this helped.
Martin