Built-in Routines
The mikroBasic PRO for ARM compiler provides a set of useful built-in utility functions. Built-in functions do not have any special requirements. You can use them in any part of 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 Basic routines. Their sources can be found in the __Lib_Delays.mbas
file located in the Uses
folder of the compiler.
Lo
Prototype |
sub function Lo(dim Argument as byte..float) as byte |
---|---|
Description |
Function returns the lowest byte of 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 |
|
Returns |
Lowest 8 bits (byte) of |
Requires |
Arguments must be variable of scalar type (i.e. Arithmetic Types and Pointers). |
Example |
d = 0x12345678 tmp = Lo(d) ' Equals 0x78 Lo(d) = 0xAA ' d equals 0x123456AA |
Notes |
None. |
Hi
Prototype |
sub function Hi(dim Argument as as word..float) as byte |
---|---|
Description |
Function returns next to the lowest byte of 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 |
|
Returns |
Returns next to the lowest byte of |
Requires |
Arguments must be variable of scalar type (i.e. Arithmetic Types and Pointers). |
Example |
d = 0x12345678 tmp = Hi(d) ' Equals 0x56 Hi(d) = 0xAA ' d equals 0x1234AA78 |
Notes |
None. |
Higher
Prototype |
sub function Higher(dim Argument as as longword..float) as byte |
---|---|
Description |
Function returns next to the highest byte of 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 |
|
Returns |
Returns next to the highest byte of |
Requires |
Arguments must be variable of scalar type (i.e. Arithmetic Types and Pointers). |
Example |
d = 0x12345678 tmp = Higher(d) ' Equals 0x34 Higher(d) = 0xAA ' d equals 0x12AA5678 |
Notes |
None. |
Highest
Prototype |
sub function Highest(dim Argument as longword..float) as byte |
---|---|
Description |
Function returns the highest byte of 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 |
|
Returns |
Returns the highest byte of |
Requires |
Arguments must be variable of scalar type (i.e. Arithmetic Types and Pointers). |
Example |
d = 0x12345678 tmp = Highest(d) ' Equals 0x12 Highest(d) = 0xAA ' d equals 0xAA345678 |
Notes |
None. |
LoWord
Prototype |
sub function LoWord(dim Argument as word..float) as word |
---|---|
Description |
The function returns low word of |
Parameters |
|
Returns |
Low word of |
Requires |
Nothing. |
Example |
d = 0x12345678 tmp = LoWord(d) ' Equals 0x5678 LoWord(d) = 0xAAAA ' d equals 0x1234AAAA |
Notes |
None. |
HiWord
Prototype |
sub function HiWord(dim Argument as longword..float) as word |
---|---|
Description |
The function returns high word of |
Parameters |
|
Returns |
High word of |
Requires |
Nothing. |
Example |
d = 0x12345678 tmp = HiWord(d) ' Equals 0x1234 HiWord(d) = 0xAAAA ' d equals 0xAAAA5678 |
Notes |
None. |
HigherWord
Prototype |
sub function HigherWord(dim Argument as uint64..extended) as word |
---|---|
Description |
The function returns higher word of |
Parameters |
|
Returns |
Higher word of |
Requires |
Nothing. |
Example |
d = 0x1122334455667788 tmp = HigherWord(d) ' Equals 0x33344 HigherWord(d) = 0xAAAA ' d equals 0x1122AAAA55667788 |
Notes |
None. |
HighestWord
Prototype |
sub function HighestWord(dim Argument as uint64..extended) as word |
---|---|
Description |
The function returns highest word of |
Parameters |
|
Returns |
Highest word of |
Requires |
Nothing. |
Example |
d = 0x1122334455667788 tmp = HighestWord(d) ' Equals 0x1122 HighestWord(d) = 0xAAAA ' d equals 0xAAAA334455667788 |
Notes |
None. |
LoDword
Prototype |
sub function LoDword(dim Argument as longword..extended) as dword |
---|---|
Description |
The function returns low longword of |
Parameters |
|
Returns |
Low longword of |
Requires |
Nothing. |
Example |
d = 0x1122334455667788 tmp = LoDword(d) ' Equals 0x55667788 LoDword(d) = 0xAAAAAAAA ' d equals 0x11223344AAAAAAAA |
Notes |
None. |
HiDword
Prototype |
sub function HiDword(dim Argument as uint64..extended) as dword |
---|---|
Description |
The function returns high longword of |
Parameters |
|
Returns |
High longword of |
Requires |
Nothing. |
Example |
d = 0x1122334455667788 tmp = HiDword(d) ' Equals 0x11223344 HiDword(d) = 0xAAAAAAAA; ' d equals 0xAAAAAAAA55667788 |
Notes |
None. |
Inc
Prototype |
sub procedure Inc(dim byref par as longint) |
---|---|
Description |
Increases parameter |
Parameters |
|
Returns |
Nothing. |
Requires |
Nothing. |
Example |
p = 4 Inc(p) ' p is now 5 |
Notes |
None. |
Dec
Prototype |
sub procedure Dec(dim byref par as longint) |
---|---|
Description |
Decreases parameter |
Parameters |
|
Returns |
Nothing. |
Requires |
Nothing. |
Example |
p = 4 Dec(p) ' p is now 3 |
Notes |
None. |
Chr
Prototype |
sub function Chr(dim code_ as byte) as char |
---|---|
Description |
Function returns a character associated with the specified character This is an “inline” routine; the code is generated in the place of the call. |
Parameters |
|
Returns |
Returns a character associated with the specified character |
Requires |
Nothing. |
Example |
c = Chr(10) ' returns the linefeed character |
Notes |
None. |
Ord
Prototype |
sub function Ord(dim character as char) as byte |
---|---|
Description |
Function returns ASCII code of the This is an “inline” routine; the code is generated in the place of the call. |
Parameters |
|
Returns |
ASCII code of the |
Requires |
Nothing. |
Example |
c = Ord("A") ' returns 65 |
Notes |
None. |
SetBit
Prototype |
sub procedure SetBit(dim byref register_ as word, dim rbit as byte) |
---|---|
Description |
Function sets the bit This is an “inline” routine; the code is generated in the place of the call. |
Parameters |
|
Returns |
Nothing. |
Requires |
Nothing. |
Example |
SetBit(GPIO_PORTB, 2) ' Set GPIO_PORTB.2 |
Notes |
None. |
ClearBit
Prototype |
sub procedure ClearBit(dim byref register_ as word, dim rbit as byte) |
---|---|
Description |
Function clears the bit 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 |
|
Returns |
Nothing. |
Requires |
Nothing. |
Example |
ClearBit(GPIO_PORTC, 7) ' Clear GPIO_PORTC.7 |
Notes |
None. |
TestBit
Prototype |
sub function TestBit(dim register_, rbit as byte) as byte |
---|---|
Description |
Function tests if the bit 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 |
|
Returns |
If the bit is set, returns 1, otherwise returns 0. |
Requires |
Nothing. |
Example |
flag = TestBit(GPIO_PORTE, 2) ' 1 if GPIO_PORTE.2 is set, otherwise 0 |
Notes |
None. |
Delay_us
Prototype |
sub procedure Delay_us(const time_in_us as longword) |
---|---|
Description |
Creates a software delay in duration of This is an “inline” routine; the code is generated in the place of the call, so the call doesn’t count against the nested call limit. |
Parameters |
|
Returns |
Nothing. |
Requires |
Nothing. |
Example |
Delay_us(1000) ' One millisecond pause |
Notes |
None. |
Delay_ms
Prototype |
sub procedure Delay_ms(const time_in_ms as longword) |
---|---|
Description |
Creates a software delay in duration of This is an “inline” routine; the code is generated in the place of the call, so the call doesn’t count against the nested call limit. |
Parameters |
|
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 |
sub procedure Vdelay_ms(dim time_in_ms as word) |
---|---|
Description |
Creates a software delay in duration of |
Parameters |
|
Returns |
Nothing. |
Requires |
Nothing. |
Example |
pause = 1000 ' ... Vdelay_ms(pause) ' ~ one second pause |
Notes |
None. |
VDelay_advanced_ms
Prototype |
sub procedure VDelay_advanced_ms(dim time_ms, Current_Fosc_kHz as word) |
---|---|
Description |
Creates a software delay in duration of Note that |
Parameters |
|
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 |
None. |
Delay_Cyc
Prototype |
sub procedure Delay_Cyc(dim x, y as word) |
---|---|
Description |
Creates a delay based on MCU clock. Delay lasts for |
Parameters |
|
Returns |
Nothing. |
Requires |
Nothing. |
Example |
Delay_Cyc(1, 10) ' 1x16384 + 10 = 16394 cycles pause |
Notes |
|
Delay_Cyc_Long
Prototype |
sub procedure Delay_Cyc_Long(dim CycNo as word) |
---|---|
Description |
Creates a delay based on MCU clock. Delay lasts for |
Parameters |
|
Returns |
Nothing. |
Requires |
Nothing. |
Example |
Delay_Cyc_Long(16384) ' 16384 cycles pause |
Notes |
|
Clock_kHz
Prototype |
sub function Clock_kHz() as longint |
---|---|
Description |
Returns device clock in kHz, rounded to the nearest integer. This is an “inline” routine; the code is generated in the place of the call. |
Parameters |
None. |
Returns |
Device clock in kHz, rounded to the nearest integer. |
Requires |
Nothing. |
Example |
clk = Clock_kHz() |
Notes |
None. |
Clock_MHz
Prototype |
sub function Clock_MHz() as word |
---|---|
Description |
Returns device clock in MHz, rounded to the nearest integer. This is an “inline” routine; the code is generated in the place of the call. |
Parameters |
None. |
Returns |
Device clock in MHz, rounded to the nearest integer. |
Requires |
Nothing. |
Example |
clk = Clock_MHz() |
Notes |
None. |
Get_Fosc_kHz
Prototype |
sub function Get_Fosc_kHz() as longint |
---|---|
Description |
Function returns device clock in kHz, rounded to the nearest integer. |
Parameters |
None. |
Returns |
Device clock in kHz. |
Requires |
Nothing. |
Example |
clk = Get_Fosc_kHz() |
Notes |
|
Get_Fosc_Per_Cyc
Prototype |
sub function Get_Fosc_Per_Cyc() as word |
---|---|
Description |
Function returns device's clock per cycle, rounded to the nearest integer. Note that |
Parameters |
None. |
Returns |
Device's clock per cycle, rounded to the nearest integer. |
Requires |
Nothing. |
Example |
dim clk_per_cyc as word ... clk_per_cyc = Get_Fosc_Per_Cyc() |
Notes |
None. |
SystemReset
Prototype |
sub procedure SystemReset() |
---|---|
Description |
This function will perform a software reset of the entire device. The processor and all peripherals are reset and all device registers will return to their default values
|
Parameters |
None. |
Returns |
Nothing. |
Requires |
Nothing. |
Example |
SystemReset() |
Notes |
None. |
DisableContextSaving
Prototype |
sub procedure DisableContextSaving() |
---|---|
Description |
Use the |
Parameters |
None. |
Returns |
Nothing. |
Requires |
This routine must be called from main. |
Example |
DisableContextSaving() ' instruct the compiler not to automatically perform context-switching |
Notes |
None. |
SetFuncCall
Prototype |
sub procedure SetFuncCall(dim FuncName as string) |
---|---|
Description |
If the linker encounters an indirect function call (by a pointer to function), it assumes that any routine whose address was taken anywhere in the program can be called at that point if it's prototype matches the pointer declaration. Use the SetFuncCall directive within routine body to instruct the linker which routines can be called indirectly from that routine :
Routines specified in the Thus, placing |
Parameters |
|
Returns |
Nothing. |
Requires |
Nothing. |
Example: |
sub procedure first(p, q as byte) ... SetFuncCall(second) ' let linker know that we will call the routine 'second' ... end sub |
Notes |
The |
SetOrg
Prototype |
sub procedure SetOrg(dim RoutineName as string, dim address as longint) |
---|---|
Description |
Use the |
Parameters |
|
Returns |
Nothing. |
Requires |
This routine must be called from main. |
Example |
SetOrg(UART1_Write, 0x1234) |
Notes |
None. |
GetDateTime
Prototype |
sub function GetDateTime() as string |
---|---|
Description |
Use the |
Parameters |
None. |
Returns |
String with date and time when this routine is compiled. |
Requires |
Nothing. |
Example |
str = GetDateTime() |
Notes |
None. |
DoGetDateTime
Prototype |
sub function DoGetDateTime() as string |
---|---|
Description |
Use the |
Parameters |
None. |
Returns |
String with date and time when this routine is compiled. |
Requires |
Nothing. |
Example |
str = DoGetDateTime() |
Notes |
None. |
GetVersion
Prototype |
sub function GetVersion() as string |
---|---|
Description |
Use the |
Parameters |
None. |
Returns |
String with current compiler version. |
Requires |
Nothing. |
Example |
str = GetVersion() ' for example, str will take the value of '8.2.1.6' |
Notes |
None. |
DoGetVersion
Prototype |
sub function DoGetVersion() as string |
---|---|
Description |
Use the |
Parameters |
None. |
Returns |
String with current compiler version. |
Requires |
Nothing. |
Example |
str = DoGetVersion() ' for example, str will take the value of '8.2.1.6' |
Notes |
None. |
CPU_REG_GET
Prototype |
sub function CPU_REG_GET(const reg as TCPUREG) as longword |
---|---|
Description |
Function returns the value of the core register, based upon the argument entered. |
Parameters |
Parameter must be a constant from the enumerated built-in constants list, which can be found at the bottom of this page. |
Returns |
Value of the core register. |
Requires |
Nothing. |
Example |
dim register_value as longword register_value = CPU_REG_GET(CPU_PRIMASK) |
Notes |
None. |
CPU_REG_SET
Prototype |
sub procedure CPU_REG_SET(const register as TCPUREG, dim value as longword) |
---|---|
Description |
Function sets the value of the core register, based upon the |
Parameters |
|
Returns |
Nothing. |
Requires |
Nothing. |
Example |
CPU_REG_SET(CPU_PRIMASK) |
Notes |
None. |
RestoreInterrupts
Prototype |
sub procedure RestoreInterrupts(dim primask as longword) |
---|---|
Description |
Restores (enables or disables) core interrupts based on the last PRIMASK register value given as a routine parametar. |
Parameters |
None. |
Returns |
Returns the value stored in the PRIMASK register before the interrupts were enabled/disabled. |
Requires |
Nothing. |
Example |
RestoreInterrupts(CPU_PRIMASK) |
Notes |
None. |
EnableInterrupts
Prototype |
sub function EnableInterrupts() as longword |
---|---|
Description |
This function enables the processor interrupt and allows the processor to respond to interrupts.
|
Parameters |
None. |
Returns |
Returns the value stored in the PRIMASK register. |
Requires |
Nothing. |
Example |
status_value = EnableInterrupts() |
Notes |
None. |
DisableInterrupts
Prototype |
sub function DisableInterrupts() as longword |
---|---|
Description |
This function disables the processor interrupt and prevents the processor from receiving interrupts.
|
Parameters |
Returns the value stored in the PRIMASK register. |
Returns |
Nothing. |
Requires |
Nothing. |
Example |
status_value = DisableInterrupts() |
Notes |
None. |
NVIC_IntEnable
Prototype |
sub procedure NVIC_IntEnable(const ivt as longword) |
---|---|
Description |
This procedure enables an interrupt vector. The specified interrupt vector is enabled in the interrupt controller.
|
Parameters |
|
Returns |
Nothing. |
Requires |
Nothing. |
Example |
NVIC_IntEnable(IVT_INT_TIMER0A) |
Notes |
The list of the available interrupt vectors be be seen in Code Assistant, by typing IVT and pressing Ctrl + Space,
|
NVIC_IntDisable
Prototype |
sub procedure NVIC_IntDisable(const ivt as longword) |
---|---|
Description |
This procedure disables an interrupt vector. The specified interrupt vector is disabled in the interrupt controller.
|
Parameters |
|
Returns |
Nothing. |
Requires |
Nothing. |
Example |
NVIC_IntDisable(IVT_INT_TIMER0A) |
Notes |
The list of the available interrupt vectors be be seen in Code Assistant, by typing IVT and pressing Ctrl + Space,
|
New
Prototype |
sub procedure New(dim byref Ptr as pointer); |
---|---|
Description |
Allocates a block of memory from the memory Heap, taking care of the alignment. It is recommended to use this routine instead of GetMem. |
Parameters |
|
Returns |
Returns a pointer to the memory block allocated by the function; Otherwise 0 (no free blocks of memory are large enough). |
Requires |
Nothing. |
Example |
|
Notes |
None. |
Dispose
Prototype |
sub procedure Dispose(dim byref Ptr as pointer) |
---|---|
Description |
Disposes a block of memory from the memory Heap, referenced by Ptr and returns its memory to the Heap. |
Parameters |
|
Returns | |
Requires |
Nothing. |
Example |
|
Notes |
After calling this routine, the value of Ptr is nil (0) (not assigned pointer). |
Core Registers | ||||||
---|---|---|---|---|---|---|
CPU_APSR | CPU_IAPSR | CPU_EAPSR | CPU_XPSR | CPU_IPSR | CPU_EPSR | CPU_IEPSR |
CPU_MSP | CPU_PSP | CPU_PRIMASK | CPU_BASEPRI | CPU_BASEPRI | CPU_BASEPRI_MAX | CPU_CONTROL |
What do you think about this topic ? Send us feedback!