2

In a 555 Timer the resistors and capacitors determine the frequency and the duration of the timer pulses. How is this actualized in case of a micro-controller timer?

user16307
  • 12,049
  • 52
  • 184
  • 320

3 Answers3

3

Microcontrollers generally use crystals or internal oscillators to generate a reference clock. That clock frequency can (on higher-end chips) be multiplied up using a PLL. The resulting clock is used to run the system.

At its simplest, a timer is a counter plus a comparison value (the period). In software, the user sets the period with a register and turns on the clock to the counter. When the counter value reaches the period value, the comparison logic generates a pulse. This can trigger a CPU interrupt or (on some MCUs) go out through a pin.

Since the timer logic is purely digital, the period (in cycles) has to be calculated based on the clock frequency.

Edit: It seems like you're asking more about low-level implementation. A counter can be implemented using edge-triggered flip-flops. Set up a flip-flop such that its output inverts on the falling edge of every clock cycle. (In a D flip-flop, you can connect the output to the input through an inverter.) Take two these, and connect the output of the first to the clock input of the second. Then, supply a clock to the first flop. They'll toggle like this:

RefClk -> Q1 -> Q2
0         0     0
1         0     0
0         1     0  <-- falling clock edge inverts Q1
1         1     0
0         0     1  <-- falling clock edge inverts Q1, falling Q1 edge inverts Q2
1         0     1
0         1     1  <-- falling clock edge inverts Q1
1         1     1
0         0     0  <-- falling clock edge inverts Q1, falling Q1 edge inverts Q2

By connecting more flip-flops together in this way, you get more bits in your counter. Each additional flop toggles at half the rate of the previous flop.

Adam Haun
  • 21,959
  • 4
  • 51
  • 91
2

As you mentioned, a 555 IC uses a RC circuit to control the timing of the 555 output. Many modern microprocessors have a internal RC circuit that can also be used to generate a train of pulses that the processor uses to coordinate its internal activities. The internal RC circuit makes it easy to get a process up and running, but it is not very accurate or stable over temperature. For applications where the accuracy of the RC circuit is not good enough, the process can disable its internal RC clock and use a digital clock pulse train coming from outside the chip. Typically this would be a circuit based on a crystal which is much more accurate and stable than an RC. There is no reason however that you could not use a 555 circuit to generate the reference clock for a processor, but it will have the same stability issues that the internal RC clock has because it is based on resistors and capacitors.

Knowing the the processor has a clock source to work from, it performs timing operations by simply counting its reference clock pulses. The faster the clock pulses are relative to the desired timing operation, the more accurate the results will be. If you are trying to generate a 1.1 msec pulse but your reference clock is 250 usec, a count of 4 will get a 1.0 msec output and counting to 5 will get you a 1.25 msec pulse. If your reference clock is 1 usec, you can get a very accurate 1 msec pulse. The AVR processor on an Arduino typically runs at 16Mhz.

So how does a process actually count clock pulses? The Timer/Counter function within a processor has hardware to do just that. The actual circuit is called a binary counter, which is simply several flip-flops connected together so that the first flip-flop switches state every clock, the 2nd flip-flop switches state every other clock, the 3rd flip-flop changes state every 4th clock, etc. A small processor will typically have 8-bit counters and/or 16-bit counters, which have 8 or 16 flip-flops respectively. Thus the timing timing/counting function is not done with an RC but digitally. A search for 'digital counters' will turn up a bunch of article on how they actually work.

tomontee
  • 96
  • 6
0

By writing to the registers that correspond to the timer/counter peripherals. Each MCU manufacturer has their own way to manipulate the timer/counters. As is typical, the datasheet is the best place for device-specific information on how to do this.

I'll give you a quick example. After reading the datasheet section on timers of some popular 8-bit MCU, you'll see that most of the timer peripherals have a clock divider. If your MCU's system clock is running at 1MHz, and you set the timer's clock divider to 1:4, you'll get a timer count rate of 250kHz. What that means is a specific register in the MCU's memory will increment once every 4uS. It does it all by yourself. Now you have a ticking time reference within the MCU that you can base timing events off of.

All of this is done without needing to select external components to determine frequencies and duty cycles, as is done with a 555 (with the exception of an external crystal, which may serve as the chip's oscillator).

Dan Laks
  • 8,584
  • 4
  • 27
  • 43
  • thnx but my question is how does it happen "physically"? how does it actualize. in case of 555 resistor is "physically" changes. you say registers do this does that. that doesnt make a picture in my mind. im wondering what happens in there and pulse frequency change. – user16307 Oct 21 '14 at 21:16
  • I'm not sure how deep down the rabbit hole you're looking to go. Are you asking what physically happens at the silicon level? Basically, millions of tiny transistors are arranged in such a way as to read, write, and store individual bits. For timer peripherals, there are no square waves being generated like a 555. It's literally transistors changing their charge state based on a prescribed sequence of instructions. – Dan Laks Oct 21 '14 at 21:28
  • You set a value in a counter register to determine the time, and the register is decremented each clock cycle. Assuming the 4 uS clock decribed in the answer above, if you want a 400 uS pulse, you would load the counter with 100 and start it, simultaneously setting an output bit high. After 100 cycles of the clock, the counter will reach 0, and set the output bit low. – Peter Bennett Oct 21 '14 at 21:31
  • yes this is what im trying to understand. for example when a current passes through a thinner resistor the resistance will be higher right? it is we can picture just like a water and pipe example. but when it comes to microcontrollers sort of components there are a lot of hidden language. i wonder what happens in silicon level. dont need the deep math but at least some understanding a pictorious understanding. otherwise it makes no sense for me to program any microcontroller just like being a robot. – user16307 Oct 21 '14 at 21:33
  • I would suggest, then, posting that as a separate question. It's a bit out of scope of this question. However, I will worn you that a thorough understanding of how MCUs work at the silicon level is a PhD level endeavor. You may not find a satisfactory answer in the simple Q/A format of this website. Instead, you'll find most people will give you simplifications and generalizations, because that's all you need to know to use a microcontroller. – Dan Laks Oct 21 '14 at 21:38
  • @user16307 there are no resistors and capacitors for the timing in a microcontroller, which is sort of "analogue". The conversion from oscillator (crystal, or internal RC - just like the 555 timer!) happens immediately and then it's all digital counters and logic circuits from then on. The timers are adjusted using logic, such as comparisons and counter functional blocks. – KyranF Oct 21 '14 at 22:54
  • ok logic is in our brain right? just like the language. those components doesn't have logic. "they simply physically act" right? my question what happens to them suddenly that they start to produce more frequent pulses? in hardware level if we look with a microscope what kind of picture do you see? – user16307 Oct 21 '14 at 22:58
  • You should look for information on digital logic circuits. You should be able to find transistor-level schematics and descriptions of a simple digital inverter and logic gates. These can be used to build flip-flops (storage elements) that can then be used to build registers and counters. The internal operation of a microcontroller or computer is all digital logic. – Peter Bennett Oct 21 '14 at 23:52
  • Okay, that's clearer. My comment is irrelevant, so I have removed it. – gbulmer Oct 23 '14 at 11:04