Sei sulla pagina 1di 3

EXPERIMENT 5: TMR0 Application as Counter

Objective:
To learn how to configure Timer 0 as counter.
To utilize PROTEUS to construct a simple basic counter circuit.

Configuring Timer 0 as Counter

Timer 0 can be configured as an 8-bit or 16-bit timer or counter. The T0CON register controls
the operation of Timer0. When the clock source is the instruction cycle clock, it operates as a
timer. When the clock source is the T0CKI pin, it operates as a counter.

7 6 5 4 3 2 1 0

value after TMR0ON T08BIT T0CS T0SE PSA T0PS2 T0PS1 T0PS0
reset 1 1 1 1 1 1 1 1
TMR0ON: Timer0 on/off control bit
0 = stops Timer0
1 = Enables Timer0
T08BIT: Timer0 8-bit/16-bit control bit
0 = Timer0 is configured as a 16-bit timer
1 = Timer0 is configured as an 8-bit timer
T0CS: Timer0 clock source select
0 = Instruction cycle clock
1 = Transition on T0CKI pin
T0SE: Timer0 source edge select bit
0 = Increment on falling edge transition on T0CKI pin
1 = Increment on rising edge transition on T0CKI pin
PSA: Timer0 prescaler assignment bit
0 = Timer0 prescaler is assigned . Timer0 clock input comes from prescaler output.
1 = Timer0 prescaler is not assigned. Timer0 clock input bypasses prescaler.
T0PS2:T0PS0: Timer0 prescaler select bits
000 = 1:2 prescaler value
001 = 1:4 prescaler value
010 = 1:8 prescaler value
011 = 1:16 prescaler value
100 = 1:32 prescaler value
101 = 1:64 prescaler value
110 = 1:128 prescaler value
111 = 1:256 prescaler value

Figure 8.2 T0CON register (reprint with permission of Microchip)

EEEB371 E4-1
To configure Timer 0 as counter, the following configuration need to be made on T0CON
register:

TMR0ON: Timer 0 on/off control bit = 1 ; Enable Timer 0.


T0CS: Timer 0 clock source select = 1 ; Transition on T0CKI pin (RA4) / Timer 0
is configured as counter.
T0SE: Timer 0 source edge select bit = 0 ; Increment on falling edge transition on
T0CKI pin.

Procedure

TMR0 Counter

1. Construct the simple counter circuit below using PROTEUS. Your instructor will verify
your circuit.

EEEB371 E4-2
2. The external impulse supplied through the switch at RA4 will be recorded in TMR0L
register and the value of the current impulse count will be displayed at the LEDs.

3. Write the source code in MikroC Compiler, build it and download the Hex file into the
microcontroller and simulate the PROTEUS program. Print out the source file and write
down your observation.

void Display_Freq(unsigned int value2write)


{
PORTD = value2write; //Display the current count value at PORTD
}

void main()
{
ADCON1 = 0x0F; // Configure all ports with analog function as digital
CMCON = 7; // Disable comparators

TRISD = 0x00; // PORTD O/P


TRISA = 0b00010000; // RA4/T0CKI input
T0CON.TMR0ON = 1; // Turn Timer 0 ON
T0CON.T0CS = 1; //Transition on T0CKI pin (RA4) - configure TMRO as counter
T0CON.T0SE = 0; //Increment on falling edge transition on T0CKI pin (RA4)

TMR0L=0; // Initialize TMR0L reg to 0


PORTD=0; // Turn PORTD off initially

do
{
Display_Freq(TMR0L); //Pass the current value in TMR0L reg to Display_Freq function
} while(1); // Infinite loop

}//End of void main()

4. Task: You are required to construct an electronic counter whereby the current count value
will be displayed on a 7-segment display. Your counter should be able to display the
numbers 0 9. Implement a reset switch so that you may reset the number to 0 anytime
you wish. Use Proteus to build your electronic counter circuit. Build the source code
using C language, download and run the application. Later on, write the same program
using assembly language and verify with Proteus. Write down your observation and
print out the source file with proper comments.

5. Exit from MPLAB, MikroC and PROTEUS. Shutdown your PC and rearrange your
workstation before you leave.

EEEB371 E4-3

Potrebbero piacerti anche