COUNTER PROGRAMMING:
In my previous POST, we used the timers of the PIC18 to generate time delays.These timers can also be used as counters to count events happening outside the PIC 18.
The use of the timer as an event counter is covered in this Post. When the timer is used as a timer, the PIC18's crystal is used as the source of the frequency.
When it is used as a counter, however, it is a pulse outside the PIC 18 that
increments the TH, TL registers. In counter mode, notice that registers such as
TOC0N, TMR0H, and TMR0L are the same as for the timer discussed in the Previous post; they even have the same names.
T0CS bit in T0CON register for Counter Programming:
Recall from the previous posts that the T0CS bit (Timer0 clock source) in theT0CON register decides the source of the clock for the timer. If T0CS = 0, the
timer gets pulses from the crystal oscillator connected to the OSC 1 and OSC2 pins(Fosc/4).
In contrast, when T0CS = 1, the timer is used as a counter and gets its
pulses from outside the PIC 18. Therefore, when T0CS = 1, the counter counts up as pulses are fed from pin RA4 (PORTA.4). The pin is called T0CKI (Timer0 clock input). Notice that the pin belongs to Port A. In the case of Timer0, when T0CS =1, pin RA4 (PORTA.4) provides the clock pulse and the counter counts up for each clock pulse coming from that pin. Similarly, for Timer 1, when TMR1 CS = 1, each clock pulse coming in from pin RC0 (PORTC.0) makes the counter count up. See below
Find the value for T0C0N if we want to program Timer0 as an 16-bit mode
counter, noprescaler. Use an external clock for the clock source and increment
on the positive edge.
Solution:
T0CON = 0X28 16-bit, external clock source, no prescaler.
watch Simulation to get better understanding what we are trying to do.
Proteus Simulation of Counter programming of pic18f4580
miKroC Code for Timer as a Counter of pic18f4580.
void main (void){
ADCON1 = 0xff;
CMCON = 0x7;
TRISC = 0;
TRISA.RA4 = 1; //make
RA4/TOCKI an input
T0CON = 0X28; //Counter 0, 16-bit mode, no prescaler
TMR0H = 0;
TMR0L = 0; //set count to 0
while(1) //repeat forever
{
do
{
T0CON.TMR0ON = 1; //turn on T0
PORTC = TMR0H+TMR0L; //place value on pins
}
while (INTCON.TMR0IF==0); //wait for TF0 to roll over
T0CON.TMR0ON = 0; //turn off T0
INTCON.TMR0IF = 0; //clear TF0
}
}
CSS Code for Timer as a Counter of pic18f4580.
#INT_TIMER0
void tick (void)
{
set_timer0(0);
}
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_EXT_L_TO_H); // Counter 0, 16-bit mode, no prescaler
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
set_tris_a(0xff); // make porta as an input
set_timer0(0); //set count to zero
while(1){ //repeat forever
OUTPUT_C(get_timer0()); //place value on pins
}
}
Friends for further learning about PIC18 checkout
Comments
Post a Comment