Microcontroller › 8051 › Understanding of Timer and Counter
- This topic has 2 replies, 3 voices, and was last updated 8 years ago by
ismail alper köylü.
-
AuthorPosts
-
April 2, 2017 at 12:19 pm #4634
Allen
ParticipantI know this is repeated question,I referred many post, but i didn't get an clear answer. My understanding is most of the micro-controller will have a 16/32 bit timer, which increments its ticks based on oscillator and clock provided(main/peripheral clock, divider, prescaler) to it.
When i started to work on simple 8051 micro-controller, I thought timer and counter are same. The reason of my thought after the simple below test i did in 8051.
"8051 has a bit C/T in TMOD Register. If timer is started after a reset i see TH/TL register are incremented irrespective of C/T bit (If bit set to 0 or 1 result is same)."
Later i worked on PIC microcontroller, I noticed that the timer and counter has different concept in this controller. If it is configured as Timer Mode, the register TMRnH/TMRnL(where n is 1 or 0) was incremented for every instruction cycle. But if it is configured as Counter, the register was not incremented for every instruction, Then i connected one switch in pin TnCkn. When i pressed that switch, the counter was incremented. With this i changed my perspective that the timer and counter are not same. Timer will increment for every instruction cycle(internal clock/signal) but counter will increment for every time either a raising edge or falling edge with external clock/signal.
Now i am working on Freescale MPC controller, Here i noticed the term GPT(General purpose timer). There are 5 timer units(GPT) in this controller, each timer unit has a register called "Counter Register(TCNTn)". When i started a timer, this register has incremented for every instruction cycle. So i thought this controller has only Timer not a counter. But i perturb with two terms below
1. Counter Register – If it is timer, why it is called Counter register in user manual of MPC.
2. In User manual, there is a section called Timer/Counter under this section, there is a subtopic called GPT and there is no subtopic for counters separately. which means timer and counter are same?
My question:
1. Is really Timer and counter are same?
2. If it is not same, why 8051 and MPC are working as same(act as timer and counter).April 3, 2017 at 9:27 am #14551Hari Prasaath K
ParticipantHi,
Counter – If Input clock to the timing system is given on the external pin it is used as counter. If you want to calculate i.e to count the the input signal changes occuring to the microcontroller.
eg. connecting the pushbutton to the pin and defining it as a counter you can keep counting how many times the button is pressed.
Timer- If the microcontroller own clock soucre is used as a clock input it can be used as a timer. That if we want to measure the duration of the event, or to determine the delay in the event happening.
eg: As explained to the counter, connecting the pushbutton to the microcontroller pin and defining it as timer and calculating the delay between the button pressed.
For register information refering to datasheet of particular controller will provide the clear information.
April 4, 2017 at 8:47 pm #14555ismail alper köylü
ParticipantHello i wanted to write answer your questions but i was not sure about the some things but now i am also with some code
Timers and counters uses the same registers. They operate different. Using keil with c if you do not stop timers/counters they will reaload themselves with the first values loaded on a overflow (Also applies for cleaing flags). Best way to understand is to try some simple codes with LED inputs outputs. Explaning them here will only give you superficial information therefore i am leaving you a timer0/timer, timer0/counter to try out
timer0/timer ***********************
Once you load the registers and start it will repeatedly reload itself whenever the interrupt ends. You can end it with TR0=0; somewhere in your code, this is the basic principle of how timers works.
#include <REGX51.H>void delay(){int i;for(i=0;i<20000;i++);}void timer0() interrupt 1{P2++;delay();}void main(){P2=1;TMOD=0x00;TH0=0xE0;TL0=0x18;ET0=1;EA=1;TR0=1;while(1){}}timer0/counter********************
As counter you are using T0 pin as input and it count high to low edges of transmissions it does not count both high to low and low to high so one of your answer to your questions is here. You can implement the input with pullup resistor and with button enabled ground. This code will keep incrementing P2 port whenever it detects a high to low transmission on P0 port.
#include <REGX51.H>void delay(){int i;for(i=0;i<1250;i++);}void intter() interrupt 1{P2++;delay();}void main(){P2=0;TMOD=0x06;TH0=-1;TL0=-1;ET0=1;ET1=1;EA=1;TR0=1;while(1);}**************************
These are from 89s52. 89s52 also has extended timer2 but i have never used it/needed it before. It is a little bit different from other timers in 89s52. It is best to master timer0 and timer1 before moving on to extimer2 or you will have confussions. 89s52 is failry basic microcontroller to start on C51 architecture it has very few sfr and easy to use with enough ram and flash.
-
AuthorPosts
- You must be logged in to reply to this topic.