14
\$\begingroup\$

PWM obviously require computing resources (and thus cannot be done simultaneously with other processes), but if I were to set a pin as output 5V or 0V, do these states remain constant or are they repeatedly "refreshed" as the microcontroller works on other processes?

This is hard to explain in text so I thought of an analogy to my question. Imagine I have a glass in my hand and am instructed to put it on the table. Then I am instructed to sit down in a chair.

The glass on the table is a state. Do I leave the glass on the table, or do I pick it back up and sit down and the repeat very quickly so you do not realize the glass was ever removed from the table?


Or perhaps more simply, does the microcontroller "forget," if you will, about the states of its pins unless you explicitly program a state change?

Hopefully I made that as confusing as possible.

\$\endgroup\$
7
  • 4
    \$\begingroup\$ +1 Interesting question although your first statement is incorrect and the idea of hardware PWM is so that the processor doesn't have to be involved the whole time. But leave it that way and I'm sure someone will have a good explanation of how it all works. \$\endgroup\$ – PeterJ Aug 15 '13 at 15:12
  • \$\begingroup\$ I thought PWM is such that the processor is changing the HIGH/LOW cycle time. In that way, it seems that the processor is directly involved - at least the time in which the state is changed. \$\endgroup\$ – sherrellbc Aug 15 '13 at 15:16
  • 1
    \$\begingroup\$ To change the the duty cycle from say 10% to 20% the CPU has to be involved but only only for a few cycles to change a register. For hardware PWM say once it's changed to 20% even if the hardware PWM frequency is 1MHz it no longer affects the processor. \$\endgroup\$ – PeterJ Aug 15 '13 at 15:21
  • \$\begingroup\$ Do you have any good links to documentation on how microcontroller work on a lower level? I do not understand your references to the register so cannot fully appreciate these answers. \$\endgroup\$ – sherrellbc Aug 15 '13 at 15:25
  • 1
    \$\begingroup\$ Not very confusing. Try harder! \$\endgroup\$ – radagast Aug 15 '13 at 15:58
16
\$\begingroup\$

The way I interpret the question has nothing to do with PWM, sorry if I am way off base, but it sounds like you used it as just an example.

Pretty much every type of microcontroller and devices with I/O use a latch/FF to drive their output circuitry. What this means is, when you set a state, it stays in that state. It is not like DRAM where the output stats have to be constantly "refreshed" in order to stay at their state.

With your glass example, I have never seen any hardware that would pick up and put down the glass on the table repeatedly. It would only ever put the glass on the table, and leave it there until a state change is requested.

Going back to PWM (just in case you were actually asking about PWM). Whether you bitbang it or your microcontroller has dedicated hardware as other posts outlined, the I/O block is only accessed and modified if a state change is requested by running code or the PWM peripheral.

\$\endgroup\$
2
  • \$\begingroup\$ This is precisely my question, although it appears there is much to microcontrollers that I not realize that I do not understand. \$\endgroup\$ – sherrellbc Aug 15 '13 at 16:03
  • 3
    \$\begingroup\$ Just keep your nose in datasheets, experiment, and ask questions. You will get the hang of it. \$\endgroup\$ – Kris Bahnsen Aug 15 '13 at 16:06
12
\$\begingroup\$

The microcontroller doesn't have to refresh the outputs. Once they're set they keep their state indefinitely (until power is removed). While in older processors the clock was required to maintain the processor's state, today's processors are what is called fully static. That means that the clock can actually be stopped and everything will stay in its current state. That's because all registers (including I/O) are made using flip-flops.

\$\endgroup\$
1
  • \$\begingroup\$ And we are making progress! Thank makes more sense now. Thank you! \$\endgroup\$ – sherrellbc Aug 15 '13 at 16:31
10
\$\begingroup\$

Most modern microcontrollers have a dedicated hardware PWM peripheral which takes care of the PWM, a very rough analogy might be:

The processor core tells the peripheral to: "toggle this pin at 10kHz and 50% duty cycle until I tell you otherwise". Then the core is free to do other stuff. It may set an interrupt, i.e. ask the peripheral to tell it when something of interest happens.
You can maybe think of the core as the "boss" and the peripherals as specialist workers. The core manages the whole program (reads each instruction and acts upon it) and "asks" the peripherals to do various tasks and notify it when they have completed them.

In your analogy, it would be like another person is holding the glass, you instruct them to place it on the table while you are free to sit on the chair.

If the micro didn't have a dedicated peripheral, then it would have to do it "manually" (i.e. itself) and keep track of the pins state and timing between toggles. This would mean a lot of cycles dedicated to pretty menial stuff which is easily handled by a simple peripheral.

Here is a diagram of the layout of a popular 8-bit microcontroller, the PIC16F690. Notice the peripherals arranged at the bottom:

enter image description here

\$\endgroup\$
5
  • \$\begingroup\$ I did not realize such peripheral components existed. So are these components rather small processors? It makes great sense such things exist. \$\endgroup\$ – sherrellbc Aug 15 '13 at 15:36
  • \$\begingroup\$ Not processors, no - the peripherals are more like dedicated hardware blocks designed specifically to perform a certain task. For instance a typical micro will have on board peripherals such as ADC, PWM, Timers, UART, SPI, etc. \$\endgroup\$ – Oli Glaser Aug 15 '13 at 15:40
  • \$\begingroup\$ I will definitely delve deeper into this subject, although my understanding of these systems if certainly biased. I was under the impression that the uC would be doing all the processing. The "instructions" given to the peripherals must be interpreted. I suppose this is where I am not fully comprehending. \$\endgroup\$ – sherrellbc Aug 15 '13 at 15:42
  • 2
    \$\begingroup\$ I added a diagram of a typical micro - a good place to start reading is the datasheet, which I also linked to. \$\endgroup\$ – Oli Glaser Aug 15 '13 at 15:44
  • \$\begingroup\$ @sherrellbc, that is where a processor and a microprocessor is different. In a µC, you can see that e.g. RAM, ROM, ALU, Serial Port etc are all integrated into the same chip. In a PC, you know that the processor is connected externally to such circuits. \$\endgroup\$ – chwi Aug 21 '13 at 7:38
5
\$\begingroup\$

You are making some assumptions that are not exactly valid. Also, yes, you made the question as confusing as possible. Seriously.

PWM can be done simultaneously with other processes. If done in software, you use timer interrupts to generate the PWM signal on a GPIO pin. Other interrupts can run, and the main process is doing unrelated things. Also, many MCUs can do the PWM directly in the timer peripheral, freeing the MCU to do other things.

As for the I/O Pins, they are multiplexed. But you are in control of how they are multiplexed, so that's not really an issue.

\$\endgroup\$
3
  • \$\begingroup\$ I understand your comment regarding the PWM timer interrupts - that makes sense. In this way the processor is involved only at the time it changes the output state. Although, you say the I/O pins are multiplexed - so that is to say if I program the controller to output a pin HIGH and then perform some other unrelated calculation, then the pin is momentarily set to LOW while such processes are performed? \$\endgroup\$ – sherrellbc Aug 15 '13 at 15:20
  • \$\begingroup\$ @sherrellbc You are in control of how the pins are multiplexed. Normally you configure them at boot time and don't change them after that. So it would not be "momentarily low", unless you specifically tell it to do that. \$\endgroup\$ – user3624 Aug 15 '13 at 15:47
  • \$\begingroup\$ I appologize if by adding the PWM example I confused readers. The intent of this question was to determine whether or not if an instructed I/O pin set to HIGH is multiplexed by the controller (i.e. because it must constantly "refresh" the pin states and only do one thing at at a time). However, now I realize there is much to microcontrollers that I do not understand (peripherals). So it appeasr to me that the answer is no, output pins are set to a state and remain in that state perpetually untill changed. \$\endgroup\$ – sherrellbc Aug 15 '13 at 16:02
1
\$\begingroup\$

Most of the I/O pins on microcontrollers are multi-function, but I wouldn't call them multiplexed.

For example, several pins on an AVR can be used as digital input, digital output, or analog input. You would normally select the desired function as part of the program initialization, and not change it later (although I might see some reason to change an analog input to a digital input to view the same signal.)

For digital outputs, once the pins are set to be outputs they will hold the last value the processor wrote to them - no need to "refresh" them periodically.

\$\endgroup\$
1
  • \$\begingroup\$ They would be multiplexed if the processor set the value, removed that value, performed other instructions and then looped around and set the original value again. This is the essence of my question. However, from the above answers there are various perihperals that beform these functions and the processor has nothing to do with it until instructed to do otherwise; I had no idea about this. Lots to learn. Thanks for the answer. \$\endgroup\$ – sherrellbc Aug 16 '13 at 12:38
1
\$\begingroup\$

In a simple example of a microcontroller hardware PWM peripheral, an 8 bit counter might be connected to an 8 bit digital comparator. The microcontroller would load a number into the comparator and increment the counter with the system clock or some prescale divided version of it. the counter would then free-run, counting from 0 to 255 and back to zero repeatedly. The comparator would have an output indicating whether the counter value is greater or less than the comparator value. This would become the PWM output. The period of the PWM would be how long it takes for the counter to complete a count cycle and the duty cycle would be what fraction of the total count is represented by the comparator value. The microcontroller code would not have anything to do except set the hardware up initially and change the comparator data when a pwm change is desired. The PWM would output a continuous stream of PWM pulses without processor attention.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.