[fpc-pascal] AVR Busy Wait implementation

Simon Ameis SAmeis.fpc at web.de
Thu Jan 14 21:33:36 CET 2016

In general I already know these documents; what I'm looking for is an
implementation of a busy wait, not how to use it.
For C/C++ it's easy to get the code files. But I don't know where to get
compiler built-in procedures for gcc or other language's compilers.

The algorithm itself isn't that complicated:
1. assume you know how fast the cpu is (e.g. 1 MHz, 12 MHz or what ever
fixed value)
2. calculate cpu cycles for given time to wait: cycle count := time to
wait / (1/(cpu speed in MHz))
3. call a loop with known cpu cycle usage

Point 1 a fixed cpu speed is assumed for ease and compile time
optimization (in fact you always need to know the speed of the clock
source as it can't be determined by the MCU).
For point 3 there is a very good explanation in German at

The wicked thing is the calculation of the cpu cycles. The C
implementation uses floating point arithmetics wich is evaluated at
compile time. It doesn't work with wait times which are calculated at

Am 12.01.2016 um 14:54 schrieb Marc Santhoff:
> On Mo, 2016-01-11 at 23:19 +0100, Simon Ameis wrote:
>> Hello,
>> has anyone already written a busy wait procedure for AVR or any other
>> embedded processors?
>> I'm currently facing issues converting Arduino's calculation of time
>> (millisecons/microseconds) to cpu cycles as they rely on compile time
>> elimination of floating point calculation.
> No, but I would search something published freely or having a fitting
> license like BSD and translate it to Pascal.
> Look there for eample:
> https://www.mikrocontroller.net/articles/AVR
> There are lot of projects, the complicated part will be to find the
> license. But if you write soemthing inspired by existing code but do it
> yourself, imho will be OK.
> Marc

More information about the fpc-pascal mailing list