Built-in Routines
mikroBasic PRO for 8051 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
, Delay_Cyc
and Get_Fosc_kHz
are actual Basic routines. Their sources can be found in Delays.mpas
file located in the Uses
folder of the compiler.
Lo
Prototype |
sub function Lo(dim number as longint) as byte |
---|---|
Returns |
Lowest 8 bits (byte) of |
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. |
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 |
Hi
Prototype |
sub function Hi(dim number as longint) as byte |
---|---|
Returns |
Returns next to the lowest byte of |
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. |
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 |
Higher
Prototype |
sub function Higher(dim number as longint) as byte |
---|---|
Returns |
Returns next to the highest byte of |
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. |
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 |
Highest
Prototype |
sub function Highest(dim number as longint) as byte |
---|---|
Returns |
Returns the highest byte of |
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. |
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 |
LoWord
Prototype |
function LoWord(val : longint) : word; |
---|---|
Description |
The function returns low word of Parameters :
|
Parameters |
number
|
Returns |
Low word of |
Requires |
Nothing. |
Example |
d = 0x12345678 tmp = LoWord(d) ' Equals 0x5678 LoWord(d) = 0xAAAA ' d equals 0x1234AAAA |
Notes |
None. |
HiWord
Prototype |
function HiWord(val : longint) : word; |
---|---|
Description |
The function returns high word of Parameters :
|
Parameters |
number
|
Returns |
High word of |
Requires |
Nothing. |
Example |
d = 0x12345678 tmp = HiWord(d) ' Equals 0x1234 HiWord(d) = 0xAAAA ' d equals 0xAAAA5678 |
Notes |
None. |
Inc
Prototype |
sub procedure Inc(dim byref par as longint) |
---|---|
Returns |
Nothing. |
Description |
Increases parameter |
Requires |
Nothing. |
Example |
p = 4 Inc(p) ' p is now 5 |
Dec
Prototype |
sub procedure Dec(dim byref par as longint) |
---|---|
Returns |
Nothing. |
Description |
Decreases parameter |
Requires |
Nothing. |
Example |
p = 4 Dec(p) ' p is now 3 |
Chr
Prototype |
sub function Chr(dim code as byte) as char |
---|---|
Returns |
Returns a character associated with the specified character |
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. |
Requires |
Nothing. |
Example |
c = Chr(10) ' returns the linefeed character |
Ord
Prototype |
sub function Ord(dim character as char) as byte |
---|---|
Returns |
ASCII code of the |
Description |
Function returns ASCII code of the This is an “inline” routine; the code is generated in the place of the call. |
Requires |
Nothing. |
Example |
c = Ord("A") ' returns 65 |
SetBit
Prototype |
sub procedure SetBit(dim variable as byte, dim rbit as word) |
---|---|
Returns |
Nothing. |
Description |
Function sets 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. |
Requires |
Nothing. |
Example |
SetBit(P0, 2) ' Set P0.2 |
ClearBit
Prototype |
sub procedure ClearBit(dim variable as byte, dim rbit as byte) |
---|---|
Returns |
Nothing. |
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. |
Requires |
Nothing. |
Example |
ClearBit(P0, 7) ' Clear P0.7 |
TestBit
Prototype |
sub function TestBit(dim variable, rbit as byte) as word |
---|---|
Returns |
If the bit is set, returns 1, otherwise returns 0. |
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. |
Requires |
Nothing. |
Example |
flag = TestBit(P0, 2) ' 1 if P0.2 is set, otherwise 0 |
Delay_us
Prototype |
sub procedure Delay_us(const time_in_us as longword) |
---|---|
Returns |
Nothing. |
Description |
Creates a software delay in duration 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. |
Requires |
Nothing. |
Example |
Delay_us(1000) ' One millisecond pause |
Delay_ms
Prototype |
sub procedure Delay_ms(const time_in_ms as longword) |
---|---|
Returns |
Nothing. |
Description |
Creates a software delay in duration 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. |
Requires |
Nothing. |
Example |
Delay_ms(1000) ' One second pause |
Vdelay_ms
Prototype |
sub procedure Vdelay_ms(dim time_in_ms as word) |
---|---|
Returns |
Nothing. |
Description |
Creates a software delay in duration of Note that |
Requires |
Nothing. |
Example |
pause = Vdelay_ms(pause) ' ~ one second pause |
Delay_Cyc
Prototype |
sub procedure Delay_Cyc(dim Cycles_div_by_10 as byte) |
---|---|
Returns |
Nothing. |
Description |
Creates a delay based on MCU clock. Delay lasts for 10 times the input parameter in MCU cycles. Note that |
Requires |
Nothing. |
Example |
Delay_Cyc(10) ' Hundred MCU cycles pause |
Clock_kHz
Prototype |
sub function Clock_kHz() as word |
---|---|
Returns |
Device clock in kHz, rounded to the nearest integer. |
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. |
Requires |
Nothing. |
Example |
clk = Clock_kHz() |
Clock_MHz
Prototype |
sub function Clock_MHz() as byte |
---|---|
Returns |
Device clock in MHz, rounded to the nearest integer. |
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. |
Requires |
Nothing. |
Example |
clk = Clock_MHz() |
Get_Fosc_kHz
Prototype |
sub function Get_Fosc_kHz() as longint |
---|---|
Returns |
Device clock in kHz. |
Description |
Function returns device clock in kHz, rounded to the nearest integer.
|
Requires |
Nothing. |
Example |
clk = Get_Fosc_kHz() |
DisableContextSaving
Prototype |
sub procedure DisableContextSaving() |
---|---|
Returns |
Nothing. |
Description |
Use the |
Requires |
This routine must be called from main. |
Example |
DisableContextSaving() 'instruct the compiler not to automatically perform context-switching |
SetFuncCall
Prototype |
sub procedure SetFuncCall(dim FuncName as string) |
---|---|
Returns |
Nothing. |
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 |
Requires |
Nothing. |
Example: |
sub procedure first(dim p, q as byte) ... SetFuncCall(second) ' let linker know that we will call the routine 'second' ... end sub |
SetOrg
Prototype |
sub procedure SetOrg(dim RoutineName as string, dim address as longint) |
---|---|
Returns |
Nothing. |
Description |
Use the |
Requires |
This routine must be called from main. |
Example |
SetOrg(UART1_Write, 0x1234) |
GetDateTime
Prototype |
sub function GetDateTime() as string |
---|---|
Returns |
String with date and time when this routine is compiled. |
Description |
Use the |
Requires |
Nothing. |
Example |
str = GetDateTime() |
DoGetDateTime
Prototype |
sub function DoGetDateTime() as string |
---|---|
Returns |
String with date and time when this routine is compiled. |
Description |
Use the |
Requires |
Nothing. |
Example |
str = DoGetDateTime() |
GetVersion
Prototype |
sub function GetVersion() as string |
---|---|
Returns |
String with current compiler version. |
Description |
Use the |
Requires |
Nothing. |
Example |
str = GetVersion() ' for example, str will take the value of '8.2.1.6' |
DoGetVersion
Prototype |
sub function DoGetVersion() as string |
---|---|
Returns |
String with current compiler version. |
Description |
Use the |
Requires |
Nothing. |
Example |
str = DoGetVersion() ' for example, str will take the value of '8.2.1.6' |
What do you think about this topic ? Send us feedback!