delay_cycles(volatile unsigned int cycles) is the same as the delay loop from the original $50 tutorial in that it just pauses for a given delay.


int interpolate(int value, int minVal, int maxVal, int minRtn, int maxRtn) is a useful routine. This takes a 'value' that is known to be between 'minVal' and 'maxVal' and then returns a number in the range 'minRtn' and 'maxRtn'. So if you wanted to convert a number which is in the range 0 to 100 into a matching number in the range -255 to 255 you could just do:

int answer = interpolate(value, 0, 100, -255, 255);


int clamp(int value, int minVal. int maxVal); Will limit a value to the range minVal to maxVal. This is the same as writing:

if(value < minVal) value = minVal;

if(value > maxVal) value = maxVal;


The macros 'CRITICAL_SECTION_START' and 'CRITICAL_SECTION_END' can be used to surround a piece of time critical code. Since it disables interrupts then it shouldn't be used if the surrounded code takes a long time to run as you may loose some other critical interrupts.