Actually I want to count that how many times switch has pressed. For this I made **T0CK1** as input of switch and put TMR0 on external crystal but its not working as I thought it would be
Code I made with my little knowledge about timers
Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
#include<p18f452.h>void main(){
TRISAbits.RA4=1;// COnfigure RA4 as input Port. (External Clock)
T0CON=0xA8;// No Prescale, 16-bit mode, External Clock
TRISB=0x00;while(1){
PORTB=TMR0L;//just for the show that it has incremended}}
Initialization
1. set timer 0 as counter by using T0CKI clock input
2. set the corresponding bit in TRISx register which connects to T0CKI external clock to 1.
3. set an interrupt for timer0 in interrupt routine.
Normal operation
1. when interrupt occurs add one to a static or global variable
2. update it to a LCD display or on UART or reset timer count if required after a periodic interval.
U dont require external crystal for external clock for timer0, the switch can directly be connected to timer clock input with capacitor to ground to remove debounce. Make sure u include a debounce circuit as it may give u false key press count. Using switch as clock will make timer0 increment on every key press.
Interupt Routine will be invoked on every time when switch is pressed??
I am little confused Interupt Routine is called when TMR0L and TMR0H is filled....
Connect 10k pulldown resistor on T0CK1 pin . ISR only invoked when the timer is overflowed from 0xffff.
TMR0IF flag should be cleared in software.
Post your ISR function code for more help