Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] How to use Timer0 of PIC18F452 as counter

Status
Not open for further replies.

wasif_khan

Newbie level 2
Joined
Dec 13, 2014
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
14
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
            }
    }

 
Last edited by a moderator:

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.

Hope that helps.
 

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
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top