Built-in Routines

The mikroC PRO for dsPIC30/33 and PIC24 compiler provides a set of useful built-in utility functions.

The Lo, Hi, Higher, Highest, LoWord, HiWord routines are implemented as macros. If you want to use these functions you must include built_in.h header file (located in the inlclude folder of the compiler) into your project.

The Delay_us and Delay_ms routines are implemented as “inline”; i.e. code is generated in the place of a call, so the call doesn’t count against the nested call limit.

The Vdelay_ms, Vdelay_advanced_ms, Delay_Cyc, Delay_Cyc_Long, Get_Fosc_kHz and Get_Fosc_Per_Cyc are actual C routines. Their sources can be found in Delays.c file located in the uses folder of the compiler.

Besides these, there are also Digital Signal Processing (DSP) routines. Please follow DSP Built-in Routines link for more information.

Lo

Prototype

#define Lo(param) ((char *)&param)[0]

Description

The function returns low byte of number. The function does not interpret bit patterns of number – it merely returns 8 bits as found in register.

This is an “inline” routine; code is generated in the place of the call, so the call doesn’t count against the nested call limit.

Parameters
  • number: input number
Returns

Low byte of number, bits 7..0.

Requires

Nothing.

Example
d = 0x12345678; 	
tmp = Lo(d);  // Equals 0x78

Lo(d) = 0xAA; // d equals 0x123456AA
Notes

None.

Hi

Prototype

#define Hi(param) ((char *)&param)[1]

Description

The function returns high byte of number. The function does not interpret bit patterns of number – it merely returns 8 bits as found in register.

This is an “inline” routine; code is generated in the place of the call, so the call doesn’t count against the nested call limit.

Parameters
  • number: input number
Returns

High byte of number, bits 15..8.

Requires

Nothing.

Example
d = 0x12345678; 	
tmp = Hi(d);  // Equals 0x56

Hi(d) = 0xAA; // d equals 0x1234AA78
Notes

None.

Higher

Prototype

#define Higher(param) ((char *)&param)[2]

Description

The function returns higher byte of number. The function does not interpret bit patterns of number – it merely returns 8 bits as found in register.

This is an “inline” routine; code is generated in the place of the call, so the call doesn’t count against the nested call limit.

Parameters
  • number: input number
Returns

Higher byte of number, bits 23..16.

Requires

Nothing.

Example
d = 0x12345678; 	
tmp = Higher(d);  // Equals 0x34

Higher(d) = 0xAA; // d equals 0x12AA5678
Notes

None.

Highest

Prototype

#define Highest(param) ((char *)&param)[3]

Description

The function returns highest byte of number. The function does not interpret bit patterns of number – it merely returns 8 bits as found in register.

This is an “inline” routine; code is generated in the place of the call, so the call doesn’t count against the nested call limit.

Parameters
  • number: input number
Returns

Highest byte of number, bits 31..24.

Requires

Nothing.

Example
d = 0x12345678; 	
tmp = Highest(d);  // Equals 0x12

Highest(d) = 0xAA; // d equals 0xAA345678
Notes

None.

LoWord

Prototype

unsigned int LoWord(unsigned long number);

Description

The function returns low word of number. The function does not interpret bit patterns of number – it merely returns 16 bits as found in register.

This is an “inline” routine; code is generated in the place of the call, so the call doesn’t count against the nested call limit.

Parameters
  • number: input number
Returns

Low word of number, bits 15..0.

Requires

Nothing.

Example
d = 0x12345678; 	
tmp = LoWord(d);  // Equals 0x5678

LoWord(d) = 0xAAAA; // d equals 0x1234AAAA
Notes

None.

HiWord

Prototype

unsigned int HiWord(unsigned long number);

Description

The function returns high word of number. The function does not interpret bit patterns of number – it merely returns 16 bits as found in register.

This is an “inline” routine; code is generated in the place of the call, so the call doesn’t count against the nested call limit.

Parameters
  • number: input number
Returns

High word of number, bits 31..16.

Requires

Nothing.

Example
d = 0x12345678; 	
tmp = HiWord(d);  // Equals 0x1234

HiWord(d) = 0xAAAA; // d equals 0xAAAA5678
Notes

None.

Delay_us

Prototype

void Delay_us(const unsigned long time_in_us);

Description

Creates a software delay in duration of time_in_us microseconds.

This is an “inline” routine; code is generated in the place of the call, so the call doesn’t count against the nested call limit.

Parameters
  • time_in_us: delay time in microseconds. Valid values: constant values, range of applicable constants depends on the oscillator frequency
Returns

Nothing.

Requires

Nothing.

Example
Delay_us(10);  /* Ten microseconds pause */
Notes

None.

Delay_ms

Prototype

void Delay_ms(const unsigned int time_in_ms);

Description

Creates a software delay in duration of time_in_ms milliseconds.

This is an “inline” routine; code is generated in the place of the call, so the call doesn’t count against the nested call limit.

Parameters
  • time_in_ms: delay time in milliseconds. Valid values: constant values, range of applicable constants depends on the oscillator frequency
Returns

Nothing.

Requires

Nothing.

Example
Delay_ms(1000);  /* One second pause */
Notes

For generating delays with variable as input parameter use the Vdelay_ms routine.

Vdelay_ms

Prototype

void Vdelay_ms(unsigned Time_ms);

Description

Creates a software delay in duration of Time_ms milliseconds. Generated delay is not as precise as the delay created by Delay_ms.

Parameters
  • Time_ms: delay time in milliseconds
Returns

Nothing.

Requires

Nothing.

Example
unsignedpause = 1000;
...
Vdelay_ms(pause);  // ~ one second pause
Notes

Vdelay_ms is a library function rather than a built-in routine; it is presented in this topic for the sake of convenience.

VDelay_Advanced_ms

Prototype

void VDelay_Advanced_ms(unsigned time_in_ms, unsigned Current_Fosc_kHz);

Description

Creates a software delay in duration of time_in_ms milliseconds (a variable), for a given oscillator frequency. Generated delay is not as precise as the delay created by Delay_ms.

Parameters
  • Time_ms: delay time in milliseconds
  • Current_Fosc_kHz: desiredoscillator frequency
Returns

Nothing.

Requires

Nothing.

Example
pause = 1000;
fosc = 10000;

VDelay_Advanced_ms(pause, fosc);  // Generates approximately one second pause, for a oscillator frequency of 10 MHz
Notes

Note that VDelay_Advanced_ms is library function rather than a built-in routine; it is presented in this topic for the sake of convenience.

Delay_Cyc

Prototype

void Delay_Cyc(unsigned int x, unsigned int y);

Description

Creates a delay based on MCU clock. Delay lasts for x*16384 + y MCU clock cycles.

Parameters
  • x: NumberOfCycles divided by 16384
  • y: remainder of the NumberOfCycles/16384 division
Returns

Nothing.

Requires

Nothing.

Example
Delay_Cyc(1, 10);  /* 1x16384 + 10 = 16394 cycles pause */
Notes

Delay_Cyc is a library function rather than a built-in routine; it is presented in this topic for the sake of convenience.

Delay_Cyc_Long

Prototype

void Delay_Cyc_Long(unsigned long CycNo);

Description

Creates a delay based on MCU clock. Delay lasts for CycNo MCU clock cycles.

Parameters
  • CycNo: number of cycles
Returns

Nothing.

Requires

Nothing.

Example
Delay_Cyc_Long(16394);  // 16394 cycles pause
Notes

Delay_Cyc_Long is a library function rather than a built-in routine; it is presented in this topic for the sake of convenience.

Clock_kHz

Prototype

unsigned long Clock_kHz();

Description

Function returns device clock in kHz, rounded to the nearest integer.

This is an “inline” routine; code is generated in the place of the call, so the call doesn’t count against the nested call limit.

Parameters

None.

Returns

Device clock in kHz, rounded to the nearest integer.

Requires

Nothing.

Example
unsigned long clk;
...
clk = Clock_kHz();
Notes

None.

Clock_Mhz

Prototype

unsigned long Clock_MHz();

Description

Function returns device clock in MHz, rounded to the nearest integer.

This is an “inline” routine; code is generated in the place of the call, so the call doesn’t count against the nested call limit.

Parameters

None.

Returns

Device clock in MHz, rounded to the nearest integer.

Requires

Nothing.

Example
unsigned long clk;
...
clk = Clock_Mhz();
Notes

None.

Get_Fosc_kHz

Prototype

unsigned long Get_Fosc_kHz();

Description

Function returns device clock in kHz, rounded to the nearest integer.

Note that Get_Fosc_kHz is library function rather than a built-in routine; it is presented in this topic for the sake of c?nvenience.

Parameters

None.

Returns

Device clock in kHz, rounded to the nearest integer.

Requires

Nothing.

Example
unsigned long clk;
...
clk = Get_Fosc_kHz();
Notes

None.

Get_Fosc_Per_Cyc

Prototype

unsigned int Get_Fosc_Per_Cyc();

Description

Function returns device's clock per cycle, rounded to the nearest integer.

Note that Get_Fosc_Per_Cyc is library function rather than a built-in routine; it is presented in this topic for the sake of convenience.

Parameters

None.

Returns

Device's clock per cycle, rounded to the nearest integer.

Requires

Nothing.

Example
unsigned int clk_per_cyc;
...
clk_per_cyc = Get_Fosc_Per_Cyc();
Notes

None.

__AddToFarPointer

Prototype

void __AddToFarPointer(const void far** ptr, unsigned long increment);

Description

This function adds a given number to a pointer to a constant allocated in the far program memory space.

Parameters
  • ptr: pointer to a constant allocated in the far program memory space.
  • increment: number to increment pointer by.
Returns

Nothing.

Requires

Nothing.

Example
const code far unsigned char *cpointer;  // pointer to a const array in far program memory space
const code far char const_array[10];     // const array allocated in far program memory space
...
cpointer = &const_array[5] ;
__AddToFarPointer(&cpointer, 1);  // increment pointer by 1; cpointer now points to const_array[6]
Notes

None.

__SubFromFarPointer

Prototype

void __SubFromFarPointer(const void far** ptr, unsigned long decrement);

Description

This function subtracts pointer to a constant allocated in the far program memory space by a given number.

Parameters
  • ptr: pointer to a constant allocated in the far program memory space.
  • decrement: number to decrement pointer by.
Returns

Nothing.

Requires

Nothing.

Example
const code far unsigned char *cpointer;  // pointer to a const array in far program memory space
const code far char const_array[10];     // const array allocated in far program memory space
...
cpointer = &const_array[5] ;
__SubFromFarPointer(&cpointer, 1);  // decrement pointer by 1; cpointer now points to const_array[4]
Notes

None.

__FlashAddressToFarPointer

Prototype

const far char *__FlashAddressToFarPointer(unsigned long address);

Description

This function converts a given Flash memory address to a pointer to a constant allocated in the far program memory space.

Parameters
  • address: address in Flash memory to be converted.
Returns

Pointer to a constant allocated in the far program memory space.

Requires

Nothing.

Example


          
Notes

None.

__FarPointerToFlashAddress

Prototype

unsigned long __FarPointerToFlashAddress(const far char * ptr);

Description

This function converts a pointer to a constant (allocated in the far program memory space) to the appropriate Flash memory address.

Parameters
  • ptr: pointer to a constant allocated in the far program memory space.
Returns

Appropriate Flash memory address.

Requires

Nothing.

Example


          
Notes

None.

Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code