Skip to content

sw_delay

gicking edited this page Feb 6, 2018 · 4 revisions

back to Command Reference / Time

Description

Pauses the program for the amount of time (in milliseconds) specified as parameter. There are 1000 milliseconds in a second.

Notes:

  • sw_delay() uses loops and NOPs (in contrast to delay()). It therefore does not require the TIM4 interrupt to be active.
  • since sw_delay() does not use the 1ms interrupt, it becomes inaccurate if a high interrupt load is present

Inclusion

  • defined in sw_delay.h
  • auto-loaded in main_general.h
  • no #define required

Syntax

sw_delay(ms)

Parameters

  • input:

    • ms: the number of milliseconds to pause (unsigned long)
  • output:

    • none

Returns

  • Nothing

Example Code

The below code toggles pin PD0 every 500ms.

#include "main_general.h"

void setup() {
  pinMode(&PORT_D, 0, OUTPUT);
}

void loop() {
  pinOutputReg(&PORT_D, pin0) ^= 1;
  sw_delay(500);
}

Relevant Tutorial

  • tbd

Notes and Warnings

While it is easy to create a blinking LED with the sw_delay() function, and many sketches use short delays for such tasks as switch debouncing, the use of sw_delay() in a sketch has significant drawbacks. No other reading of sensors, mathematical calculations, or pin manipulation can go on during the delay function, so in effect, it brings most other activity to a halt. For alternative approaches to controlling timing see the millis() or setTimeout() functions. More knowledgeable programmers usually avoid the use of sw_delay() for timing of events longer than 10’s of milliseconds unless the sketch is very simple.

Certain things do go on while the sw_delay() function is controlling the STM8 however, namely interrupt handling and automatic hardware functions. For example, interrupt-diven serial communication and timer-based PWM output are maintained. However, at high interrupt load, sw_delay() becomes inaccurate.

See also

Clone this wiki locally