Prescaler and generating a large time delay
Project :One Second Delay using PIC18f4580's Timer0
As we have got seen in the examples so far, the duration of the time delay build using
the crystal frequency and the timer's 16-bit register.
Crystal frequency and width of 16-bit register can not be modified using code.
Both of these factors are on the far side the jurisdiction of the PIC 18 coder.
The largest time delay is achieved by making both TMR0H and TMR0L zero.
What if that is not enough?
We can use the prescaler option in theT0CON register to increase the delay by reducing the period. The prescaler option of T0CON allows us to divide the instruction clock by a factor up to 256.As we have seen so far, with no prescaler enabled, the crystal oscillator frequencyis divided by 4 (Fosc/4) and then fed into Timer0. If we enable the prescaler bit in the T0CON register, however, then we can divide the instruction clock (Fosc/4) further before it is fed into Timer0. The lower 3 bits of the T0CON register give the options of the number we can divide by.
This number can be 2, 4, 8, 16, 32, 64, and so on. Notice that the lowest number is 2 and the highest number is 256. Examine Examples to see how the prescaler options are programmed.
Find the value for T0CON if we want to program Timer0 in 16-bit mode
with a prescaler of 64 and use internal clock (Fosc/4) for the clock source,
positive-edge.
Solution:
we have T0CON = 0000 0101; 16-bit mode, XTAL clock source,prescaler of
64.
Find the timer's clock frequency and its period for various PIC18-based
systems, with
the following crystal frequencies. Assume that a prescaler of 1 :64 is
used.
(a) 10 MHZ (b) 16 MHz
Solution:
(a) 1/4 x 10 MHz = 2.5 MHz and
1/64 x 2.5 MHz = 39062.5 Hz due to 1:64 prescaler and T = 1/39062.5Hz = 25.6 microsecond
(b) 1/4 x 16 MHz = 4 MHz and 1/64 x 4 MHz = 62500 Hz due to prescaler
and T =1162500 Hz = 16microseconds
Lets write a program for one second.Assume XTAL = 10 MHz.
Calculation:
Assuming that XTAL = 10 MHz, write a
program to generate a square wave with on time of 1second on pin RA0.
Because XTAL(Fosc) = 10 MHz,
in Timer we know clock source feed to timer will be
(Fosc/4)
and 1/64 x 2.5 MHz = 39062.5 Hz due to 1:64 prescaler and T = 1/39062.5Hz = 25.6 microsecond
the counter counts up every 25.6 microsecond.
This means
that we need 1s / 25.6 microsecond. = 39062.5 clocks.
(for 16 bit) 65,536- 39062.5 = 26473.5 = 6769H.
Therefore,
we have TMR0H = 67 and TMR0L = 69
Proteus Simulation for One second Delay using Timer0 of Pic184580:
CSS Programming Code Using Timer 0 generates a delay of one second .
#INT_TIMER0
void tick (void)
{
output_toggle(PIN_A0);
set_timer0(0x6769);
}
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_INTERNAL|RTCC_DIV_64);
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_timer0(0x6769);
while(1){
}
}
miKroC Programming Code Using Timer 0 generates a delay of one second
void TODelay (void); //function declaration //
#define mybit PORTA.RA0
void main (void){
ADCON1 = 0xff; //Configure Analog Pin as a digital IO
CMCON = 0x7; //off compators
TRISA.trisA0 = 0; //make Pin RA0 as an Output
while(1){
mybit = ~mybit; //toggling RA0
TODelay();
}
}
void TODelay (void)
{
T0CON = 0X5; //prescaler 64
TMR0H = 0x67; //loading value
TMR0L = 0x69;
T0CON.TMR0ON = 1; //start timer
while (INTCON.TMR0IF==0); //wait for TF0 to roll over
T0CON.TMR0ON = 0; //turn off timer
INTCON.TMR0IF = 0; // clear TF0
}
Friends for further learning about PIC18 checkout
Comments
Post a Comment