MCU learning, Entry-Level, Part 2, Delay function

Time is the most valuable thing a man can spend. — Theophrastus.

So does MCU world….
Timing could be a vital issue for some application which requires fast resposne or even real time application.

I will show you how MCU achieve to manage TIME.

Source: https://unsplash.com/photos/ZMZHcvIVgbg

From the entry-level Part 1 exercise(LINK), we had got an overview of how we write a piece of code. It isn’t that so hard, right?

However, you may complain about the MCU send the “Hello World” message so fast, causing it too hard to read.

Is there anything we can do to slow it down? YES

There are serval ways to implement it. First is using a delay function.
Most of MCU or Arduino IC will provide delay function. It simply causes MCU to do nothing during the assigned time period. Particle delay() reference

In the official document, it mentioned that it will need an input argument for the time that you plan to wait. For example:

delay(1000); //Wait for 1000 millisecond = 1 second
delay(100); //Wait for 100 millisecond = 0.1 second

Now…where should we put the delay function?

As we mentioned before, “Setup()” only do once after MCU is power-on. In this case, put the delay function after we send the serial message is an intuitive thought. Is there any option? Yes! Put it before we send the serial message will work as well. It just behaves differently for the first time.

So the original code will become:

void loop() {

Compile the code again via press “CTRL+SHIFT+P” and put the command:

Particle: Cloud compile

Once you saw the generated bin file, flash into the particle by the same process. Put the particle in DFU mode and using the terminal to type the command:

Particle flash --usb "Your bin file name"

You should see the “Hello World” message appears once per second(1Hz).

Delay is easy to implement in Particle Photon by using a functional from the library. But there is something you should notice. During the delay period, the MCU is blocked and NOT execute any other instruction. In a white good product such as refrigerator it could be acceptable, but… what about the product that you may design in the future such as Car or Aircraft? Can the main MCU in the ABS module be blocked and cannot execute anything for 10 seconds? NO! It might cause an accident.

What if the MCU has a time concept. In real life, it could be our watch. By having a time reference, we can constantly check the watch and doing things in a certain interval. MCU does have a similar built-in function. The circuit inside of the chip also needs the “clock” to have a reference too. In the CPU spec, you frequently heard that the CPU clock is 3.6 GHz that is the clock speed inside CPU.

Particle Photon also has similar spec: Photon Spec

So, let’s talk about the second function we will use for this exercise:

millis();

You can find the official example and instruction here: LINK

Basically, Particle Photon will start a counter to count how many milliseconds passes after power-on automatically. All you need to do is to call this function and it will tell you the number. For example:

TimeStamp = millis();

In the above code, TimeStamp will record the current value of time pass in millisecond form.

Since you know the TimeStamp of the moment you send the message, you just need to send the message after one-second pass and update the Time Stamp again.

void loop() {

Let’s also check whether it is really 1-second for one message as well:

Just need to add a message about millis() — TimeStamp under Hello World message.

Serial.printlnf("Time Pass: %d", millis() - TimeStamp);

Note: %d is telling using integer format for the number of
“millis() — TimeStamp”

The result:

Except for the first one, the other message is perfectly 1000 millisecond interval which is the 1-second interval. The reason why it takes about 6 seconds for the first message is that Particle Photon will need a certain time to boot up. I will show you how to reduce the boot-up time later.

Now, you have a basic idea about how to know the “time” in MCU. Since this feature is so important, does someone make it as a tool for more convenient to use?

YES! Actually, Particle runs an RTOS in this MCU. You don’t need to know what is an RTOS for this moment. You can imagine it is a super lightweight OS like Windows in MCU. It provides resource management function. One of the resources is the timer which we can use it for time-related application. More accurately, it both manages Software Timer and Hardware Timer.

The last exercise for this part is to show you how we using Software timer to achieve the same effect above.

You have to declare a function prototype for the timer application and also the timer attribute. It will be placed before setup().

For the function prototype declaration, you will tell the compiler that there is a function called “excute_every_second” will be implemented later. It takes no input and no output.

For the timer, you will declare a “Timer” structure called “timer”, it will repeat every 1000 millisecond and every time it will execute the function called “excute_every_second”.

void excute_every_second(void);

Next step is implement the “excute_every_second()”

All you need to do is the same as the exercise above. And put it after the loop().

/* Code that runs in 1Hz or every second */
void excute_every_second()
{
static uint32_t TimeStamp = 0;

The full code will look like this:

Let’s see what will happen!
Compile -> Flash -> Serial monitor

Congratulation! It did the same as the function above!!!

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Let’s conclude what you have learned so far.

First, we used a “delay()” function to force MCU to stop and do nothing for a period which will cause it sent message slower.

Then, we realized it is not ideal. So we learn a new function called “millis()”. We used it as a normal watch to monitor the time pass and decide to send the message or not.

Finally, we found that there is something more powerful which is “Timer”. It will AUTOMATICALLY run the code at the assigned period.

There are still plenty of things that I would like to share with you. Let me know if there is a particular part of MCU that you are interested in as well. Leave the comment below!

Software engineer, Movie lover, Karate beginner.