Skip to main content

PIC18f4580 Timer0 calculation using miKroC , CSS and proteus

PIC18f4580 TIMER PROGRAMMING

The PIC18f4580 has four timers. they're named as Timers zero, one, two, and three. they will be used either as timers to come up with a time delay or as counters to count events happening outside the microcontroller.

First, we see however Timers zero is employed to come up with time delays.


Every timer wants a clock pulse to tick. The clock supply will be internal or
external. If we have a tendency to use the inner clock supply, then 1/ fourth of the frequency of the
crystal oscillator on the OSC1 and OSC2 pins (Fosc/4) is fed into the timer.
Therefore, it's used for time delay generation and for that reason is termed a timer.
By selecting the external clock choice, we have a tendency to feed pulses through one among the PIC18's pins: this is often known as a counter.

pic timer
GIF taken from https://exploreembedded.com

Basic registers of the timer

Majority of t timers in 18F are 16 bits wide. Because the PIC 18 has an 8-
bit architecture, each 16-bit timer is accessed as two separate registers of low byte(TMRxL) and high byte (TMRxH). Each timer also has the TCON (timer control) register for setting modes of operation. 

PIC Timer Module
Timer  Size Count Register
Timer0 16 bit TMR0H and TMR0L
Timer1 16 bit TMR1H and TMR1L
Timer2 8 bit TMR2
Timer3 16 bit TMR3H and TMR3L

T0CON (Timer0 control) register:


Each timer has a control register, called TCON, to set the various timer operation modes. T0CON is an 8-bit register used for control of Timer0.

bit 7 TMR0ON: Timer0 On/Off Control bit
1 = Enables Timer0
0 = Stops Timer0

bit 6 T08BIT: Timer0 8-Bit/16-Bit Control bit
1 = Timer0 is configured as an 8-bit timer/counter
0 = Timer0 is configured as a 16-bit timer/counter

bit 5 T0CS: Timer0 Clock Source Select bit
1 = Transition on T0CKI pin
0 = Internal instruction cycle clock (CLKO)

bit 4 T0SE: Timer0 Source Edge Select bit
1 = Increment on high-to-low transition on T0CKI pin
0 = Increment on low-to-high transition on T0CKI pin

bit 3 PSA: Timer0 Prescaler Assignment bit
1 = TImer0 prescaler is not assigned. Timer0 clock input bypasses prescaler.
0 = Timer0 prescaler is assigned. Timer0 clock input comes from prescaler output.

bit 2-0 T0PS2:T0PS0: Timer0 Prescaler Select bits
111 = 1:256 Prescale value
110 = 1:128 Prescale value
101 = 1:64 Prescale value
100 = 1:32 Prescale value
011 = 1:16 Prescale value
010 = 1:8 Prescale value
001 = 1:4 Prescale value
000 = 1:2 Prescale value

TOCON (Timer0 Control) Register

TOCS (Timer0 clock source)

This bit in the T0CON register is used to decide whether the clock source is internal (Fosc/4) or external. If T0CS = 0, then the Fosc/4 is used as clock source. In this case, the timers are often used for time delay generation. See

 If T0CS = 1, the clock source is external and comes from the RA4/T0CKI, which is pin 6 on the DIP package of PIC18F4580. When the clock source comes from an external source, the timer is used as an event counter.  

  

Additional Reading:Concept of Prescaler in Microcontrollers:

A circuit that shows the rate of clocking source to a counter/timer.

Prescaler is an electronic circuit used to reduce a high frequency electrical signal to a low frequency by integer division.

Let’s find the value for T0CON if we want to program Timer0 in 16-bit mode, no prescaler.Use PIC18's Fosc/4 crystal oscillator for the clock source, increment on positive-edge.   

      T0CON = 0000 1000

By loading the above value in TCON0 register indicating:

bit 6 T08BIT:0 = Timer0 is configured as a 16-bit timer/counter
bit 5 T0CS:0    = Internal instruction cycle clock which is Fosc/4 clock source.
bit 3 PSA:1     = Timer0 prescaler is not assigned.Timer0 clock input bypasses                             prescaler        
bit 7 TMR0ON: 0 = Timer0 is off

One more important thing that we can see for understanding is the timer's clock frequency and its period for PIC18-based system, with the 4 MHZ crystal frequency with Assumption that no prescaler is used.
                        
                1/4 * 4 MHZ = 1 MHz and T = 1/1 MHZ = 1 microsecond

 
What happen when timer overflows?


TMR0IF flag bit


Notice that the TMR0IF bit (Timer0 interrupt flag) is part of the INTCON (interrupt control) register. The other options of the INTCON register are discussed in Chapter II. As we will see, when the timer reaches its maximum value of FFFFH, it rolls over to 0000, and TMRoIF is set to 1 (see Figure 9-4). Chapter 11 shows how we can use TMR0IF to cause an interrupt. Next, we describe the 16-bit mode operation for Timer0.


The TMR0 interrupt is generated when the TMR0 register overflows from FFh to 00h in 8-bit mode, or from FFFFh to 0000h in 16-bit mode. This overflow sets the TMR0IF flag bit. The interrupt can be masked by clearing the TMR0IE bit (INTCON<5>). Before re-enabling the interrupt, the TMR0IF bit must be cleared
in software by the Interrupt Service Routine. Since Timer0 is shut down in Sleep mode, the TMR0 interrupt cannot awaken the processor from Sleep.



Steps to program Timer0 in 16-bit mode


To generate a time delay using the Timer0 mode 16, the following steps are
taken:

1. Load the value into the T0CON register indicating which mode (8-bit or 16-
bit) is to be used and the selected prescaler option.

2. Load register TMR0H followed by register TMR0L with initial count values.

3. Start the timer with the instruction T0CON.TMR0ON = 1;

4. Keep monitoring the timer flag (TMR0IF) to see if it is raised. Get out of the
loop when TMR0IF becomes high.

5. Stop the timer with the instruction T0CON.TMR0ON = 0;

6. Clear the TMR0IF flag for the next round.

7. Go back to Step 2 to load TMR0H and TMR0L again.



To clarify the above steps, see Simulation and related given example.

In this simulation we will calculate the exact time delay and the square wave frequency generated on pin , we need to know the XTAL frequency. See calculation

Calculation:

Assuming that XTAL = 10 MHz, write a program to generate a square wave with a period of 10 ms on pin RA0.

For a square wave with T = 10 millisecond we must have a time delay of 5 millisecond. 

Because  XTAL(Fosc) = 10 MHz, 

                in Timer we know clock source feed to timer will be (Fosc/4)
                  
          10Mhz/4 = 2500000 Hz

          1/  2500000=  0.0000004  
   the counter counts up every 0.4 microsecond.

       This means that we need 5 ms / 0.4 ms = 12,500 clocks.
 (for 16 bit)  65,536- 12,500 = 53,036 = CF2CH.
                            
        Therefore, we have TMR0H = CF and TMR0L = 2C 
                         


 Proteus Simulation:

 Simulation of square wave with a period of 10 ms on pin RA0.


miKroC Programming Code Using Timer 0 generates a square wave with a period of 10 ms on pin RA0.

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();          //calling function
}
}


// function to initialize timer0  
void TODelay (void)
{
 T0CON = 0X08;    //no prescaler
 TMR0H = 0xCF;    //loading value 
 TMR0L = 0x2C;
 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
}



CCS Programming Code Using Timer 0 generates a square wave with a period of 10 ms on pin RA0.

#INT_TIMER0
void tick (void)
{
output_toggle(PIN_A0);


set_timer0(0xCF2C);
}

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_1);
   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(0xCF2C);

while(1){


}

} 


Friends for further learning about PIC18 checkout next. 


Comments

  1. I'm learning the program, please let me know about timer opteration, because we can't buy books in our country.anyone have to share me pdf.helpme

    ReplyDelete
  2. you can follow https://microcontrollerpicavr.blogspot.com/2017/08/pic18-timer-tutorial.htmlfor basic understanding.

    ReplyDelete

Post a Comment

Popular posts from this blog

How to use SPI Debugger

  SPI Debugger Hi Friends, in this post, we will discuss how to use SPI Debugger available in proteus for serial peripheral interface (SPI) which is a built-in feature of PIC microcontroller to communicate and data exchange between PIC and other devices. SPI Debugger Introduction to SPI Guys, SPI can be considered as a programmable shift  register, The SPI module is a synchronous serial I/O port that shifts a serial bit stream of variable length and data rate between the PIC and other peripheral devices. Here “synchronous” means that the data transmission is synchronized to a clock signal. To avoid further delay, let's see some important connections and different configurations to effectively use SPI Debugger. How to use SPI Debugger in MASTER & SLAVE configuration Select the virtual instrument and from the instrument, list selects "SPI Debugger". SPI Debugger Insert two SPI Debugger on the working area, select any one of them,

Frequency Counter using Interrupts PIC18f4580 Project

Frequency Counter  using PIC18f458 0 Proje ct This POST describes the construction of small frequency counter with a cheap PIC18f4580 microcontroller with 16 x 2 LCD.   Prerequisites: PIC18F4580 TIMER Programming. PIC18F4580 COUNTER Programming. PIC18F4580 Interfacing with 16x2 LCD. PIC18F4580 Interrupts Programming  ( we will cover in this POST) Concept: Frequency  is the number of occurrences of a repeating event per unit time. in our case we will measure a number of clocks generated by clock source per unit time. In this project, LCD is used to display the frequency and PIC timer 1 to measure the input signal and Timer0 to generate an indication that one second has gone. System software utilizes Timer-1 in the 16-bit counter mode to count the input signal and overflows of the counter are added to provide the aggregate count in multiples of 65536. Totaling the existing value of the counter at the conclusion provides the full count.