Skip to main content

One second delay with pic microcontroller using miKroC , CSS and proteus


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

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,

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. 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 ar

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.