Skip to main content

Counter programming of pic18f using CSS,miKroC and proteus



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 the
T0CON 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

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.