Sunteți pe pagina 1din 50

End

BASCOM-8051 $ASM - $END ASM


LANGUAGE REFERENCE Action
Start of inline assembly code block.
© 1999 MCS Electronics Syntax
1WRESET,1WREAD,1WWRITE $ASM
Action Remarks
These routines can be used to communicate with Dallas Use $ASM together with $END ASM to insert a block of
Semiconductors 1Wire-devices. assembler code in your BASIC code.
Syntax Example
Dim c as Byte
1WRESET $ASM
1WWRITE var1 Mov r0,#{C} ;address of c
var2 = 1WREAD() Mov a,#1
Mov @r0,a ;store 1 into var c
Remarks $END ASM
1WRESET Reset the 1WIRE bus. The error variable ERR will Print c
return 1 End
if an error occurred.
1WWRITE var1 Sends the value of var1 to the bus. $INCLUDE
var2 = 1WREAD() Reads a byte from the bus and places it into Action
var2. Includes an ASCII file in the program at the current position.
var1 : Byte, Integer, Word, Long, Constant. Syntax
var2 : Byte, Integer, Word, Long. $INCLUDE file
Example Remarks
'-------------------------------------------------- file Name of the ASCII file, which must contain valid BASCOM
' 1WIRE.BAS
' Demonstrates 1wreset, 1wwrite and 1wread()
statements.
' pullup of 4K7 required to VCC from P.1 This option can be used if you make use of the same routines in
' DS2401 serial button connected to P1.1 Many programs. You can write modules and include them into
'-------------------------------------------------- your
Config 1wire = P1.1 'use this pin program.
Dim Ar(8) As Byte , A As Byte , I As Byte
1wreset 'reset the bus If there are changes to make you only have to change the module
Print Err 'print error 1 if error file,
1wwrite &H33 'read ROM command not all your BASCOM programs.
For I = 1 To 8 You can only include ASCII files!
Ar(I) = 1wread() 'read byte
Next Example
For I = 1 To 8 '---------------------------------------------------------
Printhex Ar(I); 'print output --
Next ' (c) 1997,1998 MCS Electronics
Print 'linefeed '---------------------------------------------------------
---
' file: INCLUDE.BAS $CRYSTAL = var
' demo: $INCLUDE
'--------------------------------------------------------- Remarks
--- var Frequency of the crystal.
Print "INCLUDE.BAS" var : Constant.
$include c:\bascom\123.bas 'include file that prints Hello When you want to use an unsupported crystal/baud rate you can
Print "Back in INCLUDE.BAS"
End
use this compiler directive.
When you do, you must also use the corresponding $BAUD
$BAUD directive.
Action These statements always work together.
Instruct the compiler to override the baud rate setting from the See also
options menu. $BAUD
Syntax Example
$BAUD = var $BAUD = 2400
$CRYSTAL = 14000000
Remarks PRINT "Hello"
var The baud rate that you want to use. END
var : Constant.
When you want to use a crystal/baud rate that can't be selected $IRAMSTART
from the options, you can Action
use this compiler directive. Compiler directive to specify starting internal memory location.
You must also use the $CRYSTAL directive. Syntax
These statements always work together. $IRAMSTART = constant
In the generated report, you can view which baud rate is actually Remarks
generated. constant A constant with the starting value (0-255)
However, the baudrate is only shown when RS-232 statements See also
are used like PRINT, INPUT $NOINIT $RAMSTART
etc.
Example
See also $NOINIT
$CRYSTAL $NOSP
Example $IRAMSTART = &H60 'first usable memory location
$BAUD = 2400 SP = 80
$CRYSTAL = 14000000 ' 14 MHz crystal DIM I As Integer
PRINT "Hello"
END
$DEFAULT XRAM
Action
$CRYSTAL Compiler directive to place each dimensioned variable as XRAM.
Action Syntax
Instruct the compiler to override the crystal frequency options $DEFAULT XRAM
setting. Remarks
Syntax
When you are using many XRAM variables it makes sense to set The db0-db7 lines of the LCD must be connected to the data
this option, so you don't lines
have to type XRAM each time. D0-D7.
To dimension a variable to be stored into IRAM, specify IRAM in The RS line of the LCD must be connected to the address line
that case. A0.
Example On systems with external RAM/ROM, it makes more sense to
$DEFAULT XRAM attach
Dim X As Integer 'will go to XRAM the LCD to the data bus. With an address decoder, you can
Dim Z As IRAM Integer 'will be stored in IRAM
select the
$LARGE LCD display.
Action Example
$LCD = &HA000 'writing to this address will make the E-
Instructs the compiler that LCALL statements must be used. line of the LCD high.
Syntax LCD "Hello world"
$LARGE
Remarks
$NOBREAK
Internally when a subroutine is called the ACALL statement is Action
used. Instruct the compiler that BREAK statements must not be
The ACALL instruction needs only two bytes (the LCALL needs compiled.
three bytes) Syntax
The ACALL statement however can only address routines with a $NOBREAK
maximal offset of 2048. Remarks
AT89C2051 chips will have no problems with that. With the BREAK statement, you can generate a reserved opcode
When code is generated for another uP, the subroutine being that is used by the
called can be further away and simulator to pause the simulation.
you will receive an error. With the $LARGE statement you instruct When you want to compile without these opcodes you don't have
the compiler to use the to remove the BREAK
LCALL statement which can address the full 64K address space. statement: you can use the $NOBREAK statement to achieve the
Example same.
$LARGE 'I received an error 148 so I need this option See also
$LCD BREAK
Action Example
$NOBREAK
Instruct the compiler to generate code for 8-bit LCD displays BREAK ' this isn't compiled into code so the simulator
attached to the data bus. will not pause
Syntax End
$LCD = [&H] address $NOINIT
Remarks Action
address The address where must be written to, to enable the Instruct the compiler that no initialisation must be performed.
LCD display.
Syntax $OBJ
$NOINIT
Remarks Action
Includes Intel objectcode.
BASCOM initialises the processor depending on the used
statements. Syntax
When you want to handle this by yourself you can specify this $OBJ obj
with the compiler directive Remarks
$NOINIT. obj is the object code to include.
The only initialisation that is always done is the setting of the Example
stack pointer and the $OBJ D291 'this is equivalent to SET P1.1
initialisation of the LCD display (if LCD statements are used). $RAMSTART
See also Action
$NOSP
Specifies the location of the external RAM memory.
Example Syntax
$NONIT
'your program goes here $RAMSTART = [&H]address
End Remarks
$NOSP address The (hex)-address where the data is stored.
Or the lowest address that enables the RAM chip.
Action You can use this option when you want to run your
Instruct the compiler that the stack pointer must not be set. code in systems with external RAM memory.
Syntax address : Constant.
$NOSP See also
Remarks $RAMSIZE
BASCOM initialises the processor depending on the used Example
statements. $ROMSTART = &H4000
When you want to handle this by yourself you can specify this $RAMSTART = 0
with the compiler directive $RAMSIZE = &H1000
$NOINIT.
The only initialisation that is always done is the setting of the
$RAMSIZE
stack pointer and the Action
initialisation of the LCD display (if LCD statements are used). Specifies the size of the external RAM memory.
With the $NOSP directive the stack will not be initialised either. Syntax
See also $RAMSIZE = [&H] size
$NOINIT Remarks
Example size Size of external RAM memory chip.
$NOSP size : Constant.
$NOINIT See also
End
$RAMSTART
Example With the redirection of the INPUT command, you can use your
$ROMSTART = &H4000 own routines.
$RAMSTART = 0 This way you can use other devices as input devices.
$RAMSIZE = &H1000
Note that the INPUT statement is terminated when a RETURN
DIM x AS XRAM Byte 'specify XRAM to store variable in XRAM
code (13) is received.
$ROMSTART See also
Action $SERIALOUTPUT
Specifies the location of the ROM memory. Example
Syntax $SERIALINPUT = Myinput
'here goes your program
$ROMSTART = [&H] address END
Remarks !myinput:
address The (hex)-address where the code must start. ;perform the needed actions here
mov a, sbuf ;serial input buffer to acc
Default is 0. This value will be used when $ROMSTART is not ret
specified.
You can use this option when you want to test the code in RAM. $SERIALINPUT2LCD
The code must be uploaded and placed into the specified Action
address This compiler directive will redirect all serial input to the LCD
and can be called from a monitor program. display instead of echoing to
The monitor program must relocate the interrupts to the correct the serial port.
address! When $ROMSTART = &H4000 is specified the monitor Syntax
program must perform a LJMP instruction. For address 3 this $SERIALINPUT2LCD
must
be &H4003. Otherwise, interrupts can not be handled correctly.
Remarks
That is up to the monitor program. You can also write your own custom input or output driver with
the $SERIALINPUT and
See also $SERIALOUTPUT statements, but the $SERIALINPUT2LCD is
$RAMSTART handy when you use a LCD
Example display.
$ROMSTART = &H4000 'ROM enabled at 4000 hex
See also
$SERIALINPUT $SERIALINPUT , $SERIALOUTPUT
Action Example
Specifies that serial input must be redirected. $SERIALINPUT2LCD
Dim v as Byte
Syntax CLS
$SERIALINPUT = label INPUT "Number ", v 'this will go to the LCD display
Remarks $SERIALOUTPUT
label The name of the assembler routine that must be called
when a Action
character is needed from the INPUT routine. The character must Specifies that serial output must be redirected.
be returned in ACC. Syntax
$SERIALOUTPUT = label Returns the absolute value of a numeric variable.
Remarks Syntax
label The name of the assembler routine that must be called var = ABS(var2)
when a Remarks
character is send to the serial buffer (SBUF). var Variable that is assigned the absolute value of var2.
The character is placed into ACC. Var2 The source variable to retrieve the absolute value from.
With the redirection of the PRINT and other serial output related var : Byte, Integer, Word, Long.
commands, you can use var2 : Integer, Long.
your own routines. The absolute value of a number is always positive.
This way you can use other devices as output devices. See also
Example -
$SERIALOUTPUT = MyOutput
'here goes your program Difference with QB
END You can not use numeric constants since the absolute value is
!myoutput: obvious for numeric
;perform the needed actions here
mov sbuf, a ;serial output buffer (default)
constants.
ret Does also not work with Singles.
Example
$SIM Dim a as Integer, c as Integer
Action a = -1000
c = Abs(a)
Generates code without waiting loops for the simulator. Print c
Syntax End
$SIM Output
Remarks 1000
When simulating the WAIT statement, you will experience that it
takes a long time to
ALIAS
execute. You can also switch off the updating of variables/source Action
which costs time, but an Indicates that the variable can be referenced with another name.
alternative is the $SIM directive. Syntax
You must remove the $SIM statement when you want to place newvar ALIAS oldvar
your program into a Remarks
chip/EPROM. oldvar Name of the variable such as P1.1
See also newvar New name of the variable such as direction
- Aliasing port pins can give the pin names a more meaningful
Example name.
$SIM 'don't make code for WAIT and WAITMS See also
WAIT 2 'the simulator is faster now
CONST
ABS() Example
Action
direction ALIAS P1.1 'now you can refer to P1.1 with the BCD() will displays values with a trailing zero.
variable direction The BCD() function is intended for the PRINT/LCD statements.
SET direction 'has the same effect as SET P1.1
END Use the MAKEBCD function to convert variables.
See also
ASC() MAKEBCD, MAKEDEC
Action Example
Convert a string into its ASCII value. Dim a as byte
a = 65
Syntax LCD a
var = ASC(string) Lowerline
Remarks LCD BCD(a)
End
var Target variable that is assigned.
String String variable or constant from which to retrieve the BITWAIT
ASCII value.
Action
var : Byte, Integer, Word, Long.
Wait until a bit is set or reset.
string : String, Constant.
Note that only the first character of the string will be used. Syntax
When the string is empty, a zero will be returned. BITWAIT x SET/RESET
See also Remarks
CHR() X Bit variable or internal register like P1.x , where x ranges form
0-7.
Example When using bit variables be sure that they are set/reset by
Dim a as byte, s as String * 10
s = ABC software.
a = Asc(s) When you use internal registers that can be set/reset by
Print a hardware such as P1.0 this doesn't
End
apply.
Output See also
65
-
BCD() Example
Dim a as bit
Action BITWAIT a , SET 'wait until bit a is set
Converts a variable into its BCD value. BITWAIT P1.7, RESET 'wait until bit 7 of Port 1 is 0.
Syntax End
PRINT BCD( var ) ASM
LCD BCD( var ) BITWAIT P1.0 , SET will generate :
Remarks Jnb h'91,*+0
BITWAIT P1.0 , RESET will generate :
var Variable to convert. Jb h'91,*+0
var1 : Byte, Integer, Word, Long, Constant.
When you want to use a I2C clock device which stores its values BREAK
as BCD values you can use Action
this function to print the value correctly. Generates a reserved opcode to pause the simulator.
Syntax See also
BREAK DECLARE, SUB
Remarks Example
You can set a breakpoint in the simulator but you can also set a Dim a as byte, b as byte
Declare Sub Test(b1 as byte)
breakpoint from code using
a = 65
the BREAK statement. Call test (a) 'call test with parameter A
Be sure to remove the BREAK statements when you debugged test a 'alternative call
your program or use the End
$NOBREAK meta command. SUB Test(b1 as byte) 'use the same variable as the
declared one
The reserved opcode used is A5. LCD b 'put it on the LCD
See also Lowerline
$NOBREAK LCD BCD(b1)
End SUB
Example
PRINT "Hello"
BREAK 'the simulator will pause now
CHR()
..... Action
..... Convert a byte, Integer/Word variable or a constant to a
End character.
CALL Syntax
Action PRINT CHR(var)
s = CHR(var)
Call and execute a subroutine.
Syntax Remarks
CALL Test [(var1, var-n)] var Byte, Integer/Word variable or numeric constant.
s A string variable.
Remarks When you want to print a character to the screen or the LCD
Var1 Any BASCOM variable or constant.. display,
Var-n Any BASCOM variable or constant. You must convert it with the CHR() function.
Test Name of the subroutine. In this case Test
With the CALL statement, you can call a procedure or subroutine. See also
As much as, 10 parameters can be passed but you can also call ASC()
a subroutine without Example
Dim a as byte
parameters. a = 65
For example: Call Test2 LCD a
The call statement enables you to implement your own Lowerline
statements. LCDHEX a
You don't have to use the CALL statement: LCD Chr(a)
End
Test2 will also call subroutine test2
When you don't supply the CALL statement, you must leave out CLS
the parenthesis. Action
So Call Routine(x,y,z) must be written as Routine x,y,x
Clear the LCD display and set the cursor home. The config statement configures all kind of hardware related
Syntax statements.
CLS Select one of the following topics to learn more about a specific
Remarks config statement.
Clearing the LCD display does not clear the CG-RAM in which CONFIG TIMER0, TIMER1
the custom characters are CONFIG TIMER2 (for 8052 compatible chips)
stored. CONFIG LCD
CONFIG LCDBUS
See also
CONFIG LCDPIN
$LCD , LCD
CONFIG BAUD
Example CONFIG 1WIRE
Cls
LCD " Hello" CONFIG SDA
End CONFIG SCL
CONFIG DEBOUNCE
CONST CONFIG WATCHDOG
Action CONFIG SPI
Declares a symbolic constant.
Syntax
CONFIG TIMER0, TIMER1
DIM symbol AS CONST value Action
Configure TIMER0 or TIMER1.
Remarks
Symbol The name of the symbol. Syntax
CONFIG TIMERx = COUNTER/TIMER , GATE=INTERNAL/EXTERNAL ,
Value The value to assign to the symbol. MODE=0/3
Assigned constants consume no program memory.
The compiler will replace all occurrences of the symbol with the
Remarks
TIMERx TIMER0 or TIMER1.
assigned value. COUNTER will configure TIMERx as a COUNTER and TIMER will
See also configure TIMERx as a TIMER.
DIM A TIMER has built in clockinput and a COUNTER has external
clockinput.
Example GATE INTERNAL or EXTERNAL. Specify EXTERNAL to enable gate
'---------------------------------------------------- control with the INT input.
' (c) 1997,1998 MCS Electronics MODE Time/counter mode 0-3. See Hardware for more details.
' CONST.BAS
'----------------------------------------------------
So CONFIG TIMER0 = COUNTER, GATE = INTERNAL,
Dim A As Const 5 'declare a as a constant MODE=2 will configure TIMER0 as
Dim B1 As Const &B1001 a COUNTER with not external gatecontrol , in mode 2 (auto
Waitms A 'wait for 5 milliseconds reload)
Print A When the timer/counter is configured, the timer/counter is
Print B1
End stopped so you must start it
afterwards with the START TIMERx statement.
CONFIG See the additional statements for other microprocessors that use
the CONFIG statement.
Example CONFIG BAUD
CONFIG TIMER0=COUNTER, MODE=1, GATE=INTERNAL
COUNTER0 = 0 'reset counter 0 Action
START COUNTER0 'enable the counter to run Configure the uP to select the intern baud rate generator.
DELAY 'wait a while
PRINT COUNTER0 'print it
This baud rate generator is only available in the 80535, 80537
END and compatible chips.
Syntax
CONFIG LCD CONFIG BAUD = baudrate
Action Remarks
Configure the LCD display. Baudrate Baudrate to use : 4800 or 9600
Syntax Example
CONFIG LCD = LCDtype CONFIG BAUD = 9600 'use internal baud generator
Remarks Print "Hello"
LCDtype The type of LCD display used. This can be : End
40 * 4,16 * 1, 16 * 2, 16 * 4, 16 * 4, 20 * 2 or 20 * 4
Default 16 * 2 is assumed.
CONFIG 1WIRE
Example Action
CONFIG LCD = 40 * 4 Configure the pin to use for 1WIRE statements.
LCD "Hello" 'display on LCD Syntax
FOURTHLINE 'select line 4
LCD "4" 'display 4
CONFIG 1WIRE = pin
END Remarks
Pin The port pin to use such as P1.0
CONFIG LCDBUS See also
Action 1WRESET , 1WREAD , 1WWRITE
Configures the LCD databus. Example
Syntax Config 1WIRE = P1.0 'P1.0 is used for the 1-wire bus
CONFIG LCDBUS = constant 1WRESET 'reset the bus
Remarks CONFIG SDA
Constant 4 for 4-bit operation, 8 for 8-bit mode (default)
Use this statement together with the $LCD = address statement.
Action
When you use the LCD display in the bus mode the default is to Overrides the SDA pin assignment from the Option Settings.
connect all the data lines. Syntax
With the 4-bit mode, you only have to connect data lines d7-d4. CONFIG SDA = pin
See also Remarks
CONFIG LCD Pin The port pin to which the I2C-SDA line is connected.
Example When you use different pins in different projects, you can use this
$LCD = &H8000 'address of enable signal statement to override the
Config LCDBUS = 4 '4 bit mode Options Compiler setting for the SDA pin. This way you will
LCD "hello" remember which pin you used
because it is in your code and you do not have to change the
settings from the options.
CONFIG SPI
See also Action
CONFIG SCL Configures the SPI related statements.
Example Syntax
CONFIG SDA = P3.7 'P3.7 is the SDA line CONFIG SPI = SOFT, DIN = PIN, DOUT = PIN , CS = PIN, CLK
= PIN
CONFIG SCL Remarks
Action DIN Data input. Pin is the pin number to use such as p1.0
Overrides the SCL pin assignment from the Option Settings. DOUT Data output. Pin is the pin number to use such as p1.1
Syntax CS Chip select. Pin is the pin number to use such as p1.2
CONFIG SCL = pin CLK Clock. Pin is the pin number to use such as p1.3
Remarks See also
Pin The port pin to which the I2C-SCL line is connected. SPIIN SPIOUT
When you use different pins in different projects, you can use this Example
statement to override the Config SPI = SOFT, DIN = P1.0 , DOUT = P1.1, CS = P1.2,
Options Compiler setting for the SCL pin. This way you will CLK = P1.3
remember which pin you used SPIOUT var , 1 'send 1 byte
because it is in your code and you do not have to change the CONFIG LCDPIN
settings from the options.
Action
See also Override the LCD-options to store the settings in your program.
CONFIG SDA
Syntax
Example CONFIG LCDPIN , DB4=
CONFIG SCL = P3.5 'P3.5 is the SCL line
P1.1,DB5=P1.2,DB6=P1.3,DB7=P1.4,E=P1.5,RS=P1.6
CONFIG DEBOUNCE Remarks
Action P1.1 etc. are just an example in the syntax.
Configures the delaytime for the DEBOUNCE statement. See also
Syntax CONFIG LCD
CONFIG DEBOUNCE = time Example
CONFIG LCDPIN ,DB4=
Remarks P1.1,DB5=P1.2,DB6=P1.3,DB7=P1.4,E=P1.5,RS=P1.6
Time A numeric constant which specifies the delay time in mS.
When debounce time is not configured, 25 mS will be used as a CONFIG WATCHDOG
default. Action
Note that the delay time is based on a 12 MHz clock frequency. Configures the watchdog timer from the AT89C8252
See also Syntax
DEBOUNCE CONFIG WATCHDOG = time
Example Remarks
Config Debounce = 25 mS '25 mS is the default time The interval constant in mS the watchdogtimer will count to.
Possible settings : Use counterX = 0 to reset the counter.
16 , 32, 64 , 128 , 256 , 512 , 1024 and 2048. The counter can count from 0 to 255 in mode 2 (8-bit auto
When the WD is started, a reset will occur after the specified reload).
number of mS. And to 65535 in mode 1(16-bit).
With 2048, a reset will occur after 2 seconds, so you need to The counterx variables are intended to set/retrieve the
reset the WD in your programs TIMER/COUNTER registers from
periodically. BASCOM. COUNTER0 = TL0 and TH0.
See also So the COUNTERx reserved variable is a 16 bit variable.
START WATCHDOG , STOP WATCHDOG , RESET To set TLx or THx, you can use : TL0 = 5 for example.
WATCHDOG Note that the COUNTERx variable operates on both the TIMERS
Example and COUNTER because
'----------------------------------------------------- the TIMERS and COUNTERS is the same thing except for the
' (c) 1998 MCS Electronics mode they are working in. To
' WATCHD.BAS demonstrates the AT89S8252 watchdog timer load a reload value, use the LOAD statement.
' select 89s8252.dat !!!
'-----------------------------------------------------
After you have accessed the COUNTERx variable, you must
Config Watchdog = 2048 'reset after 2048 mSec START the timer with the
Start Watchdog 'start the watchdog timer statement START COUNTERx, because accessing the
Dim I As Word TIMER/COUNTER via the
For I = 1 To 10000 COUNTERx variable will STOP the TIMER.
Print I 'print value
' Reset Watchdog Example
'you will notice that the for next doesnt finish because '---------------------------------------------------------
of the reset -----
'when you unmark the RESET WATCHDOG statement it will ' (c) 1997,1998 MCS Electronics
finish because the '---------------------------------------------------------
'wd-timer is reset before it reaches 2048 msec -----
Next ' file: COUNTER.BAS
End ' demo: COUNTER
'---------------------------------------------------------
COUNTERx -----
' Connect the timer input P3.4 to a frequency generator
Action ' *TIMER/COUNTER 1 is used for RS-232 baudrate generator
Set or retrieve the COUNTER0 or COUNTER1 variable. '---------------------------------------------------------
For 8052 TIMER2 compatible chips, COUNTER2 can be used -----
too. Dim A As Byte , C As Integer
Config Timer0 = Counter , Gate = Internal , Mode = 1
Syntax 'Timer0 = counter : timer0 operates as a counter
COUNTERX = var or 'Gate = Internal : no external gate control
var = COUNTERX 'Mode = 1 : 16-bit counter
Counter0 = 0 'clear counter
Remarks Start Counter0 'enable the counter to count
var A byte, Integer/Word variable or constant that is assigned to Do 'set up a loop
the counter. A = Inkey 'check for input
counterX COUNTER0 , COUNTER1 or COUNTER2. C = Counter0 'get counter value
Start Counter0 'Restart the timer
Print C 'print it Set the LCD Cursor State.
Loop Until A = 27 'until escape is pressed
End Syntax
For the next example the ASM code is shown: CURSOR ON / OFF BLINK / NOBLINK
COUNTER0 = 1000 Remarks
Generated code :
Clr TCON.4
You can use both the ON or OFF and BLINK or NOBLINK
Mov tl0,#232 parameters.
Mov th0,#3 At power up the cursor state is ON and NOBLINK.
CPEEK() See also
DISPLAY
Action Example
Returns a byte stored in code memory. Dim a as byte
Syntax a = 255
var = CPEEK( address ) LCD a
CURSOR OFF 'hide cursor
Remarks Wait 1 'wait 1 second
var Numeric variable that is assigned with the content of the CURSOR BLINK 'blink cursor
program End
memory at address
address Numeric variable or constant with the address location
DATA
There is no CPOKE statement because you can not write into Action
program memory. Specifies values to be read by subsequent READ statements.
See also Syntax
PEEK , POKE , INP , OUT DATA var [, varn]
Example Remarks
'----------------------------------------------------- var Numeric or string constant.
' (c) 1998 MCS Electronics Difference with QB
' PEEK.BAS
' demonstrates PEEk, POKE, CPEEK, INP and OUT
Integer and Word constants must end with the % -sign.
' Long constants must end with the &-sign.
'----------------------------------------------------- Single constants must end with the !-sign.
Dim I As Integer , B1 As Byte See also
'dump internal memory
For I = 0 To 127 'for a 8052 225 could be used READ , RESTORE
' Break Example
B1 = Peek(i) 'get byte from internal memory DIM a AS BYTE, I AS BYTE, L AS Long, S As XRAM STRING * 15
Printhex B1 ; " "; RESTORE DTA 'point to data
'Poke I , 1 'write a value into memory FOR a = 1 TO 3
Next READ a : PRINT a 'read data and print it
Print 'new line NEXT
'be careful when writing into internal memory !! RESTORE DTA2 'point to data
READ I : PRINT I
CURSOR READ I : PRINT I
RESTORE DTA3
Action
READ L : PRINT L hold its state.
RESTORE DTA4 What also should be mentioned is that P2.2-P2.7 and P3 have
READ S : PRINT S
END internal pull up resistors. This
DTA1: can affect the debounce statement. With these portpins,
DATA 5, 10, 100 debounce is best to be used as:
DTA2: Debounce P1.1, 0, Pr [, sub ] , as it will not require an external
DATA -1%, 1000%
Integer and Word constants must end with the %-sign.
pull up reisitor.
(Integer : <0 or >255) See also
DTA3: CONFIG DEBOUNCE
DATA 1235678&
'long constants must end with the &-sign Example
DTA4: '-----------------------------------------------------
DATA "Hello world" ' DEBOUN.BAS
REM You can also mix different constant types on one line ' demonstrates DEBOUNCE
DATA "TEST" , 5 , 1000% , -1& , 1.1! '-----------------------------------------------------
CONFIG DEBOUNCE = 30 'when the config statement is not
DEBOUNCE used a default of 25mS
will be used
Action Do
Debounce a port pin connected to a switch. 'Debounce P1.1 , 1 , Pr 'try this for branching when
high(1)
Syntax Debounce P1.0 , 0 , Pr,SUB
DEBOUNCE Px.y , state , label [ , SUB] ' ^----- label to branch to
Remarks ' ^---------- branch when P1.0 goes low(0)
Px.y A port pin like P1.0 , to examine. ' ^---------------- examine P1.0
'when P1.0 goes low jump to subroutine Pr
state 0 for jumping when Px.y is low , 1 for jumping when Px.y is 'P1.0 must go high again before it jumps again
high 'to the label Pr when P1.0 is low
label The label to GOTO when the specified state is detected Loop
SUB The label to GOSUB when the specified state is detected End
Pr:
When you specify the optional parameter SUB, a GOSUB to label Print "P1.0 was/is low"
is performed instead of a Return
GOTO.
The DEBOUNCE statements wait for a port pin to get high(1) or DECR
low(0). Action
When it does it waits 25 mS and checks again (eliminating Decrements a variable by one.
bounce of a switch) Syntax
When the condition is still true and there was no branch before, it DECR var
branches to the label. Remarks
When DEBOUNCE is executed again, the state of the switch
Var Variable to decrement.
must have gone back in the var : Byte, Integer, Word, Long, Single.
original position before it can perform another branch.
There are often situations where you want a number to be
Each DEBOUNCE statement which use a different port uses 1
decreased by 1.
BIT of the internal memory to
The DECR statement is faster then var = var - 1.
See also
Defint, DefBit, DefByte, DefWord
INCR Action
Declares all variables that are not dimensioned of the DefXXX
Example
'--------------------------------------------------------- type.
----- Syntax
' (c) 1997,1998 MCS Electronics DEFBIT b
'---------------------------------------------------------
-----
DEFBYTE c
' file: DEC.BAS DEFINT I
' demo: DECR DEFWORD x
'--------------------------------------------------------- Difference with QB
----- QB allows you to specify a range like DEFINT A - D. BASCOM
Dim A As Byte
A = 5 'assign value to a doesn't support this.
Decr A 'dec (by one) Example
Print A 'print it Defbit b : DefInt c 'default type for bit and integers
End Set b1 'set bit to 1
c = 10 'let c = 10
DECLARE SUB
Action DEFLCDCHAR
Declares a subroutine. Action
Syntax Define a custom LCD character.
DECLARE SUB TEST[(var as type)] Syntax
Remarks DEFLCDCHAR char,r1,r2,r3,r4,r5,r6,r7,r8
test Name of the procedure. Remarks
Var Name of the variable(s). Maximum 10 allowed. char Variable representing the character (0-7).
Type Type of the variable(s). Bit, Byte,Word/Integer, Long or r1-r8 The row values for the character.
String. char : Byte, Integer, Word, Long, Constant.
You must declare each sub before writing the sub procedure. r1-r8 : Constant.
See also You can use the LCD designer to build the characters.
CALL, SUB It is important that after the DEFLCDCHAR statement(s), a CLS
follows.
Example The special characters can be printed with the Chr() function.
Dim a As Byte, b1 As Byte, c As Byte
Declare Sub Test(a As Byte) See also
a = 1 : b1 = 2: c = 3 Edit LCD designer
Print a ; b1 ; c
Call Test(b1) Example
Print a ;b1 ; c DefLCDchar 0,1,2,3,4,5,6,7,8 'define special character
End Cls 'select LCD DATA RAM
Sub Test(a as byte) LCD Chr(0) 'show the character
Print a ; b1 ; c End
End Sub
DELAY
Action ' (c) 1997-1999 MCS Electronics
'---------------------------------------------------------
Delay program execution for a short time. -----
Syntax ' file: DIM.BAS
DELAY ' demo: DIM
'---------------------------------------------------------
Remarks -----
Use DELAY to wait for a short time. Dim B1 As Bit 'bit can be 0 or 1
The delay time is 100 microseconds based on a system Dim A As Byte 'byte range from 0-255
frequency of 12 MHz. Dim C As Integer 'integer range from -32767 - +32768
Dim A As String * 10 'string with length of 10 characters
See also Dim ar(10) As Byte 'dimension array
WAIT , WAITMS 'assign bits
Example B1 = 1 'or
P1 = 5 'write 5 to port 1 Set B1 'use set
DELAY 'wait for hardware to be ready 'assign bytes
A = 12
DIM A = A + 1
'assign integer
Action C = -12
Dimension a variable. C = C + 100
Print C
Syntax End
DIM var AS [XRAM/IRAM] type
Remarks DISABLE
var Any valid variable name such as b1, i or longname. var can Action
also be an array : ar(10) for example. Disable specified interrupt.
type Bit, Byte, Word, Integer, Long, Single or String Syntax
XRAM Specify XRAM to store variable in external memory DISABLE interrupt
IRAM Specify IRAM to store variable in internal memory (default) Remarks
A string variable needs an additional length parameter: Interrupt : INT0, INT1, SERIAL, TIMER0, TIMER1 or TIMER2.
Dim s As XRAM String * 10 By default all interrupts are disabled.
In this case, the string can have a length of 10 characters. To disable all interrupts specify INTERRUPTS.
Note that BITS can only be stored in internal memory. To enable the enabling and disabling of individual interrupts use
Difference with QB ENABLE INTERRUPTS.
In QB you don't need to dimension each variable before you use Depending on the chip used, there can be more interrupts.
it. In BASCOM you must Look at microprocessor support for more details.
dimension each variable before you use it.
In addition, the XRAM/IRAM options are not available in QB.
See also
ENABLE
See Also
CONST , ERASE
Example
ENABLE INTERRUPTS 'enable the setting of interrupts
Example ENABLE TIMER0 'enable TIMER0
'--------------------------------------------------------- DISABLE SERIAL'disables the serial interrupt.
----- DISABLE INTERRUPTS 'disable all interrupts
DISPLAY Syntax
ELSE
Action Remarks
Turn LCD display on or off.
You don't have to use the ELSE statement in an IF THEN .. END
Syntax IF structure.
DISPLAY ON / OFF You can use the ELSEIF statement to test for another condition.
Remarks IF a = 1 THEN
The display is turned on at power up. ...
See also ELSEIF a = 2 THEN
- ..
Example ELSEIF b1 > a THEN
Dim a as byte ...
a = 255 ELSE
LCD a
...
DISPLAY OFF
Wait 1 END IF
DISPLAY ON See also
End IF , END IF SELECT CASE
DO .. LOOP Example
A = 10 'let a = 10
Action IF A > 10 THEN 'make a decision
Repeat a block of statements until condition is true. PRINT " A >10" 'this will not be printed
Syntax ELSE 'alternative
PRINT " A not greater than 10" 'this will be printed
DO END IF
statements
LOOP [ UNTIL expression ] ENABLE
Remarks Action
You can exit a DO..LOOP with the EXIT DO statement. Enable specified interrupt.
See also Syntax
EXIT , WHILE WEND , FOR , NEXT ENABLE interrupt
Example Remarks
Dim A As Byte Interrupt INT0, INT1, SERIAL, TIMER0, TIMER1 or TIMER2
DO 'start the loop
A = A + 1 'increment A
By default all interrupts are disabled.
PRINT A 'print it To enable the enabling and disabling of interrupts use ENABLE
LOOP UNTIL A = 10 'Repeat loop until A = 10 INTERRUPTS.
Print A 'A is still 10 here Other microprocessors can have more interrupts than the
8051/8052.
ELSE Look at specific microprocessor support for more details.
Action See also
Executed if the IF-THEN expression is false. DISABLE
Example PRINT " Number < 10" 'print this
ENABLE INTERRUPTS 'allow interrupts to be set END IF 'end structure
ENABLE TIMER1 'enables the TIMER1 interrupt END IF 'end structure
END 'end program
END ERASE
Action Action
Terminate program execution.
Erases a variable so memory will be released.
Syntax Syntax
END
ERASE var
Remarks Remarks
STOP can also be used to terminate a program.
var The name of the variable to erase.
When an END or STOP statement is encountered, a never-
The variable must be dimensioned before you can erase it.
ending loop is generated.
When you need temporary variables, you can erase them after
See also you used them. This way
STOP your program uses less memory.
Example You can only ERASE the last dimensioned variables. So when
PRINT " Hello" 'print this you DIM 2 variables for local
END 'end program execution
purposes, you must ERASE these variables. The order in which
END IF you ERASE them doesn't
Action matter.
End an IF .. THEN structure. For example :
Dim a1 as byte , a2 as byte , a3 as byte , a4 as byte
Syntax
'use the vars
END IF or ENDIF
ERASE a3 : ERASE a4 'erase the last 2 vars because they were
Remarks temp vars
You must always end an IF .. THEN structure with an END IF Dim a5 as Byte 'Dim new var
statement. Now you can't erase the vars a1 and a2 anymore !
You can nest IF ..THEN statements. Note that ERASED variables don't show up in the report file nor in
The use of ELSE is optional. the simulator.
The editor converts ENDIF to End If when the reformat option is
Example
switched on. DIM A As Byte 'DIM variable
Example A = 255 'assign value
Dim nmb As Byte Print A 'PRINT variable
AGAIN: 'label ERASE A 'ERASE
INPUT " Number " , nmb 'ask for number DIM A AS INTEGER 'DIM again but now as INT
IF a = 10 THEN 'compare PRINT A 'PRINT again
PRINT " Number is 10" 'yes REM Note that A uses the same space a the previous ERASED
ELSE 'no var A so
IF nmb > 10 THEN 'is it greater REM it still holds the value of the previous assigned
PRINT " Number > 10" 'yes variable
ELSE 'no
NEXT , EXIT FOR
EXIT Example
Action y = 10 'make y 10
Exit a FOR..NEXT, DO..LOOP , WHILE ..WEND or SUB..END FOR a = 1 TO 10 'do this 10 times
SUB. FOR x = y TO 1 'this one also
PRINT x ; a 'print the values
Syntax NEXT 'next x (count down)
EXIT [FOR] [DO] [WHILE] [SUB] NEXT 'next a (count up)
Remarks Dim S as Single
For S = 1 To 2 Step 0.1
With the EXIT ... statement you can exit a structure at any time. Print S
Example Next
IF a >= b1 THEN 'some silly code END
DO 'begin a DO..LOOP
A = A + 1 'inc a FOURTHLINE
IF A = 100 THEN 'test for a = 100
EXIT DO 'exit the DO..LOOP
Action
END IF 'end the IF..THEN Reset LCD cursor to the fourth line.
LOOP 'end the DO Syntax
END IF 'end the IF..THEN FOURTHLINE
FOR Remarks
Action Only valid for LCD displays with 4 lines.
Execute a block of statements a number of times. See also
Syntax HOME , UPPERLINE , LOWERLINE , THIRDLINE , LOCATE
FOR var = start TO/DOWNTO end [STEP value] Example
Dim a as byte
Remarks a = 255
var The variable counter to use LCD a
start The starting value of the variable var Fourthline
end The ending value of the variable var LCD a
Upperline
value The value var is increased/decreased with each time NEXT END
is
encountered. FUSING
var : Byte, Integer, Word, Long, Single. Action
start: Byte, Integer, Word, Long, Single, Constant. Formats a floating-point value.
end : Byte, Integer, Word, Long, Single, Constant. Syntax
step : Byte, Integer, Word, Long, Single, Constant. var = Fusing( source, mask)
For incremental loops, you must use TO.
For decremental loops, you must use DOWNTO.
Remarks
Var The string that is assigned with the result.
You must end a FOR structure with the NEXT statement.
source A variable of the type single that must be formatted.
The use of STEP is optional. By default, a value of 1 is used.
mask The formatting mask . ###.##
See also The # sign is used to indicate the number of digits
before and after the decimal point. Normal rounding is $crystal = 11059200
Dim W As Word 'allocate space for variable
used.
Do 'forever
See also W = Getrc(p1.7) 'get RC value
STR Print W 'print it
Example Wait 1 'wait a moment
$large Loop
Dim X As Single , Y As Single , Result As Single 'return values for cap=10nF .The resistor values where
Dim I As Integer measured with a DVM
Dim Buf As String * 16 ' 250 for 10K9
Input "Enter x " , X 'ask for 2 values ' 198 for 9K02
Input "Enter y " , Y ' 182 for 8K04
Print "X+Y=" ; : Result = X + Y : Print Result 'calculate ' 166 for 7K
Print "X-Y=" ; : Result = X - Y : Print Result ' 154 for 6K02
Print "X/Y=" ; : Result = X / Y : Print Result ' 138 for 5K04
Print "X*Y=" ; : Result = X * Y : Print Result ' 122 for 4K04
Buf = Fusing(result , #.##) 'format a string ' 106 for 3K06
Print Buf 'print it ' 86 for 2K16
' 54 for 1K00
GETRC ' 22 for 198 ohm
' 18 for 150 ohm
Action ' 10 for 104 ohm
Retrieves the value of a resistor or a capacitor. ' 6 for 1 ohm (minimum)
Syntax 'As you can see there is a reasonable linearity
'So you can do some math to get the resistor/capacitor
var = GETRC( pin ) value
Remarks 'But the function is intended to serve as a rough
Var The variable that receives the value. indication for resistor values
'You can also change the capacitor to get larger values.
pin The port pin for the R/C is connection. 'With 10nF, the return value fits into a byte
See also 'Of course the R or the C value must be known in order to
{bmc Getrc.bmp} calculate the other
value.
Example
'---------------------------------------------------------
------
GETRC5
' GETRC.BAS Action
' retrieve resistor value Retrieves a RC5 infrared code and subaddress.
' Connect 10KOhm variable resistor from +5V to P1.7 for
this example Syntax
' Connect 10nF capacitor from P1.7 to ground GETRC5(address , command)
' The GETRC(pin) function measures the time needed to Remarks
charge the capacitor
address The RC5 sub address received.
'---------------------------------------------------------
------- command The RC5 command received.
Config Timer0 = Timer , Gate = Internal , Mode = 1 'the Use a sharp infrared receiver SFH506-36 and connect it to port
GETRC() functions needs pin 3.2 to use this command.
timer 0
$baud = 9600 'just my settings
This statement works together with the INT0 interrupt. See the Example
example below on how to use GOSUB Routine 'branch to routine
it. Print "Hello" 'after being at 'routine' print this
END 'terminate program
{bmc sfh506.bmp}
Routine: 'this is a subroutine
Example x = x + 2 'perform some math
'--------------------------------------------------------- PRINT X'print result
------------------- RETURN 'return
' RC5.BAS (c) 1999 MCS Electronics
' connect SFH506-36 IR-receiver to PORT 3.2 (INT0) GOTO
'---------------------------------------------------------
------------------- Action
Dim New As Bit Jump to the specified label.
Dim Command As Byte , Subaddress As Byte Syntax
clr tcon.0 GOTO label
On Int0 Receiverc5 Nosave
Enable Int0 Remarks
Enable Interrupts Labels can be up to 32 characters long.
Do When you use duplicate labels, the compiler will give you a
If New = 1 Then 'received new code
Print Command ; " " ; Subaddress
warning.
New = 0 'reset new bit See also
End If GOSUB
Loop
Receiverc5: 'interrupt routine Example
Getrc5(Subaddress, command) Start: 'a label must end with a colon
New = 1 A = A + 1 'increment a
Return IF A < 10 THEN'is it less than 10?
GOTO Start 'do it again
GOSUB END IF 'close IF
PRINT " Ready" 'that is it
Action
Branch to and execute subroutine. HEX()
Syntax Action
GOSUB label Returns a string representation of a hexadecimal number.
Remarks Syntax
label The name of the label where to branch to. var = Hex( x )
With GOSUB, your program jumps to the specified label, and Remarks
continues execution at that var A string variable.
label. X A numeric variable such as Byte, Integer or Word.
When it encounters a RETURN statement, program execution will See also
continue after the GOSUB HEXVAL
statement. Example
See also Dim a as Byte, S as String * 10
GOTO CALL a = 123
s = Hex(a) See also
Print s
End LOW
Example
HEXVAL() Dim I As Integer , Z As Byte
Action I = &H1001
Z = High(I) ' is 16
Convert string representing a hexadecimal number into a numeric
variable. HOME
Syntax Action
var = HEXVAL( x ) Place the cursor at the specified line at location 1.
Remarks Syntax
var The numeric variable that must be assigned. HOME UPPER / LOWER /THIRD / FOURTH
X The hexadecimal string that must be converted. Remarks
var : Byte, Integer, Word, Long. If only HOME is used than the cursor will be set to the upperline.
x : String. You can also specify the first letter of the line like: HOME U
The string that must be converted must have a length of 2 bytes
See also
,4 bytes of 8 bytes, for
CLS , LOCATE , LCD
bytes, integers/words and longs respectively.
Difference with QB Example
Lowerline
In QB you can use the VAL() function to convert hexadecimal LCD " Hello"
strings. Home Upper
But since that would require an extra test for the leading &H LCD " Upper"
signs, that are required in QB, a
separate function was designed.
I2CRECEIVE
Action
See also Receives data from an I2C serial device.
HEX , VAL , STR
Syntax
Example I2CRECEIVE slave, var
Dim a as Integer, s as string * 15
s = "000A" I2CRECEIVE slave, var ,b2W, b2R
a = Hexval(s) : Print a Remarks
End slave A byte, Word/Integer variable or constant with the slave
HIGH address from the I2C-device.
Var A byte or integer/word variable that will receive the
Action
information
Retrieves the most significant byte of a variable.
from the I2C-device.
Syntax b2W The number of bytes to write.
var = HIGH ( s ) Be cautious not to specify too many bytes!
Remarks b2R The number of bytes to receive.
var The variable that is assigned with the MSB of var S. Be cautious not to specify too many bytes!
s The source variable to get the MSB from.
In BASCOM LT you could specify DATA for var, but since arrays Next
bytes = 10
are supported now you can
I2CSEND slave,ax(),bytes
specify and array instead of DATA. END
This command works only with some additional hardware. See
appendix D. I2START,I2CSTOP, I2CRBYTE,
See also I2CWBYTE
I2CSEND
Action
Example I2CSTART generates an I2C start condition.
x = 0 'reset variable
slave = &H40 'slave address of a PCF 8574 I/O IC I2CSTOP generates an I2C stop condition.
I2CRECEIVE slave, x 'get the value I2CRBYTE receives one byte from an I2C-device.
PRINT x 'print it I2CWBYTE sends one byte to an I2C-device.
Dim buf(10) as String
buf(1) = 1 : buf(2) = 2 Syntax
I2CRECEIVE slave, buf(), 2, 1'send two bytes and receive I2CSTART
one byte I2CSTOP
Print buf(1) 'print the received byte I2CRBYTE var, 8/9
I2CSEND I2CWBYTE val
Action Remarks
var A variable that receives the value from the I2C-device.
Send data to an I2C-device.
8/9 Specify 8 or ACK if there are more bytes to read. (ACK)
Syntax Specify 9 or NACK if it is the last byte to read. (NACK)
I2CSEND slave, var val A variable or constant to write to the I2C-device.
I2CSEND slave, var , bytes This command works only with additional hardware. See
Remarks appendix D.
slave The slave address off the I2C-device. These functions are provided as an addition to the I2CSEND and
var A byte, integer/word or numbers that holds the value, which I2CRECEIVE functions.
will See also
be, send to the I2C-device. I2CRECEIVE I2CSEND
bytes The number of bytes to send.
This command works only with additional hardware. See
Example
-------- Writing and reading a byte to an EEPROM 2404 ----
appendix D. -------------
See also DIM a As Byte
I2CRECEIVE DIM adresW AS CONST 174 'write of 2404
DIM adresR AS CONST 175 'read adres of 2404
Example I2CSTART 'generate start
x = 5 'assign variable to 5 I2CWBYTE adresW 'send slaveadres
Dim ax(10) As Byte I2CWBYTE 1 'send adres of EEPROM
slave = &H40 'slave address of a PCF 8574 I/O IC I2CWBYTE 3 'send a value
bytes = 1 'send 1 byte I2CSTOP 'generate stop
I2CSEND slave, x 'send the value or WaitMS 10 'wait 10 mS because that is the time that the
For a = 1 to 10 chip
ax(a) = a 'Fill dataspace needs to write the data
----------------now read the value back into the var a --- New is te ability to use the one line version of IF :
----------------
IF expression THEN statement [ ELSE statement ]
I2CSTART 'generate start
I2CWBYTE adresW 'write slaveadres The use of [ELSE] is optional.
I2CWBYTE 1 'write adres of EEPROM to read Also new is the ability to test on bits :
I2CSTART 'generate repeated start IF var.bit = 1 THEN
I2CWBYTE adresR 'write slaveadres of EEPROM
I2CRBYTE a,9 'receive value into a. 9 means last byte to
See also
receive ELSE , END IF
I2CSTOP 'generate stop Example
PRINT a 'print received value DIM A AS INTEGER
END A = 10
IF A = 10 THEN 'test expression
IDLE PRINT " This part is executed." 'this will be printed
Action ELSE
PRINT " This will never be executed." 'this not
Put the processor into the idle mode. END IF
Syntax IF A = 10 THEN PRINT "New in BASCOM"
IDLE IF A = 10 THEN GOTO LABEL1 ELSE PRINT "A<>10"
LABEL1:
Remarks REM The following example shows enhanced use of IF THEN
In the idle mode, the system clock is removed from the CPU but IF A.15 = 1 THEN 'test for bit
not from the interrupt logic, PRINT "BIT 15 IS SET"
the serial port or the timers/counters. END IF
REM the following example shows the 1 line use of IF THEN
The idle mode is terminated either when an interrupt is received [ELSE]
or upon system reset IF A.15 = 0 THEN PRINT "BIT 15 is cleared" ELSE PRINT "BIT
through the RESET pin. 15 is set"
See also INCR
POWERDOWN
Example Action
IDLE Increments a variable by one.
Syntax
IF INCR var
Action Remarks
Allows conditional execution or branching, based on the Var Any numeric variable.
evaluation of a Boolean expression. There are often situations where you want a number to be
Syntax increased by 1.
IF expression THEN The INCR statement is faster then var = var + 1.
[ ELSEIF expression THEN ] See also
[ ELSE ] DECR
END IF Example
Remarks DO 'start loop
expression Any expression that evaluates to true or false. INCR a 'increment a by 1
PRINT a'print a
LOOP UNTIL a > 10 'repeat until a is greater than 10 Example
INKEY Dim a As Byte
a = INP(&H8000) 'read value that is placed on databus(d0-
Action d7) at
Returns the ASCII value of the first character in the serial input 'hex address 8000
PRINT a
buffer. END
Syntax
var = INKEY INPUTBIN
Remarks Action
var Byte, Integer, Word, Long or String variable. Read binary values from the serialport.
If there is no character waiting, a zero will be returned. Syntax
The INKEY routine can be used when you have a RS-232 INPUTBIN var1 [,var2]
interface on your uP. Remarks
See the manual for a design of a RS-232 interface. var1 The variable that is assigned with the characters from the
The RS-232 interface can be connected to a comport of your serial
computer. port.
See also var2 An optional second (or more) variable that is assigned with
WAITKEY the
Example characters from the serial.
DO 'start loop The number of bytes to read is depending from the variable you
A = INKEY 'look for character use.
IF A > 0 THEN 'is variable > 0?
PRINT A 'yes , so print it When you use a byte variable, 1 character is read from the serial
END IF port.
LOOP 'loop forever An integer will wait for 2 characters and an array will wait until the
whole array is filled.
INP() Note that the INPUTBIN statement doesn't wait for a <RETURN>
Action but just for the number of
Returns a byte read from a hardware port or external memory bytes.
location. See also
Syntax PRINTBIN
var = INP(address) Example
Remarks Dim a as Byte, C as Integer
var Numeric variable that receives the value. INPUTBIN a, c 'wait for 3 characters
End
address The address where to read the value from.
The INP statement only works on systems with an uP that can INPUTHEX
address external
Action
Memory.
Allows input from the keyboard during program execution.
See also Syntax
OUT
INPUTHEX [" prompt" ] , var [ , varn ] [ NOECHO ]
Remarks The RS-232 interface can be connected to a serial
prompt An optional string constant printed before the prompt communication port of your computer.
character. This way you can use a terminal emulator and the keyboard as
Var,varn A numeric variable to accept the input value. an input device.
NOECHO Disables input echoed back to the Comport. You can also use the build in terminal emulator.
The INPUTHEX routine can be used when you have a RS-232 Difference with QB
interface on your uP. In QB you can specify &H with INPUT so QB will recognise that a
See the manual for a design of an RS-232 interface. hexadecimal string is used.
The RS-232 interface can be connected to a serial BASCOM implements a new statement : INPUTHEX.
communication port of your computer. See also
This way you can use a terminal emulator and the keyboard as INPUTHEX PRINT
input device. Example
You can also use the build in terminal emulator. '---------------------------------------------------------
If var is a byte then the input must be 2 characters long. -----
If var is an integer/word then the input must be 4 characters long. ' (c) 1997,1998 MCS Electronics
'---------------------------------------------------------
If var is a long then the input must be 8 characters long. -----
Difference with QB ' file: INPUT.BAS
In QB you can specify &H with INPUT so QB will recognise that a ' demo: INPUT, INPUTHEX
hexadecimal string is used. '---------------------------------------------------------
-----
BASCOM implement a new statement: INPUTHEX. 'To use another baudrate and crystalfrequency use the
See also 'metastatements $BAUD = and $CRYSTAL =
INPUT $baud = 1200 'try 1200 baud for example
$crystal = 12000000 '12 MHz
Example Dim V As Byte , B1 As Byte
Dim x As Byte Dim C As Integer , D As Byte
INPUTHEX " Enter a number " , x 'ask for input Dim S As String * 15 'only for uP with XRAM support
INPUT Input "Use this to ask a question " , V
Input B1 'leave out for no question
Action Input "Enter integer " , C
Allows input from the keyboard during program execution. Print C
Inputhex "Enter hex number (4 bytes) " , C
Syntax Print C
INPUT [" prompt" ] , var [ , varn ] [ NOECHO ] Inputhex "Enter hex byte (2 bytes) " , D
Remarks Print D
Input "More variables " , C , D
prompt An optional string constant printed before the prompt Print C ; " " ; D
character. Input C Noecho 'suppress echo
Var,varn A variable to accept the input value or a string. Input "Enter your name " , S
NOECHO Disables input echoed back to the Comport. Print "Hello " ; S
Input S Noecho 'without echo
The INPUT routine can be used when you have an RS-232 Print S
interface on your uP. End
See the manual for a design of an RS-232 interface.
Next
LCD Locate 2 , 1 'set cursor position
Lcd "*" 'display this
Action Wait 1 'wait a moment
Send constant or variable to LCD display. Shiftcursor Right 'shift the cursor
Syntax Lcd "@" 'display this
LCD x Wait 1 'wait a moment
Home Upper 'select line 1 and return home
Remarks Lcd "Replaced." 'replace the text
x Variable or constant to display. Wait 1 'wait a moment
More variables can be displayed separated by the ; -sign Cursor Off Noblink 'hide cursor
LCD a ; b1 ; " constant" Wait 1 'wait a moment
Cursor On Blink 'show cursor
The LCD statement behaves just like the PRINT statement. Wait 1 'wait a moment
See also Display Off 'turn display off
LCDHEX , $LCD CONFIG LCD Wait 1 'wait a moment
Display On 'turn display on
Example '-----------------NEW support for 4-line LCD------
'--------------------------------------------------------- Thirdline
----- Lcd "Line 3"
' (c) 1997,1998 MCS Electronics Fourthline
'--------------------------------------------------------- Lcd "Line 4"
----- Home Third 'goto home on line three
' file: LCD.BAS Home Fourth
' demo: LCD, CLS, LOWERLINE, SHIFTLCD, SHIFTCURSOR, HOME Home F 'first letterer also works
' CURSOR, DISPLAY Locate 4 , 1 : Lcd "Line 4"
'--------------------------------------------------------- Wait 1
----- 'Now lets build a special character
Dim A As Byte 'the first number is the characternumber (0-7)
Config Lcd = 16 * 2 'configure lcd screen 'The other numbers are the rowvalues
'other options are 16 * 4 and 20 * 4, 20 * 2 'Use the LCD tool to insert this line
'When you don't include this option 16 * 2 is assumed Deflcdchar 0 , 31 , 17 , 17 , 17 , 17 , 17 , 31 , 0'
'$LCD = address will turn LCD into 8-bit databus mode replace ? with number (0-7)
' use this with uP with external RAM and/or ROM Deflcdchar 1 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 31'
' because it Ain't need the port pins ! replace ? with number (0-7)
Cls 'clear the LCD display Cls 'cls is needed after deflcdchar
Lcd "Hello world." 'display this at the top line Lcd Chr(0) ; Chr(1) 'print the special character
Wait 1 '----------------- Now use an internal routine -----------
Lowerline 'select the lower line -
Wait 1 Acc = 1 'value into ACC
Lcd "Shift this." 'display this at the lower line Call Write_lcd 'put it on LCD
Wait 1 End
For A = 1 To 10
Shiftlcd Right 'shift the text to the right
Wait 1 'wait a moment
LCDHEX
Next Action
For A = 1 To 10 Send variable in hexadecimal format to the LCD display.
Shiftlcd Left 'shift the text to the left Syntax
Wait 1 'wait a moment
LCDHEX var string The string to calculate the length of.
Remarks Example
var Variable to display. Dim S As String * 12
var1 : Byte, Integer, Word, Long, Single, Constant. Dim A As Byte
S = "test"
The same rules apply as for PRINTHEX. A = Len(s)
See also Print A ' prints 4
LCD
Example
LOAD
Dim a as byte Action
a = 255 Load specified TIMER with a value for autoreload mode.
LCD a Syntax
Lowerline
LCDHEX a LOAD TIMER , value
End Remarks
TIMER TIMER0, TIMER1 or TIMER2.
LEFT() Value The variable or value to load.
Action When you use the ON TIMERx statement with the
Return the specified number of leftmost characters in a string. TIMER/COUNTER in mode 2,
Syntax you can specify on which interval the interrupt must occur.
var = Left(var1 , n ) The value can range from 1 to 255 for TIMER0 and TIMER1.
Remarks For TIMER2 the range is 1-65535.
var The string that is assigned. The LOAD statement calculates the correct reload value out of
Var1 The source string. the parameter.
n The number of characters to get from the source string. The formula : TLx = THx = (256-value)
n : Byte, Integer, Word, Long, Constant. For TIMER2 : RCAP2L = RCAP2H = (65536 - value)
For string operations, all the strings must be of the same type : The load statement is not intended to assign/read a value to/from
internal or external. the timers/counters. Use
Example COUNTERx instead.
Dim s As XRAM String * 15, z As XRAM String * 15 See additional hardware for more details
s = "ABCDEFG" Example
z = Left(s,5) LOAD TIMER0, 100 'load TIMER0 with 100
Print z 'ABCDE Will generate :
End Mov tl0,#h'9C
LEN Mov th0,#h'9C
LOAD TIMER2, 1000
Action Will generate:
Mov RCAP2L,#24
Returns the length of a string. Mov RCAP2H,#252
Syntax
var = LEN( string ) LOCATE
Remarks Action
var A numeric variable that is assigned with the length of string. Moves the LCD cursor to the specified position.
Syntax LOOKUPSTR
LOCATE y , x
Remarks Action
Returns a string from a table.
x Constant or variable with the position. (1-64*)
y Constant or variable with the line (1 - 4*) Syntax
* Depending on the used display var =LOOKUPSTR( value, label )
See also Remarks
CONFIG LCD , LCD , HOME , CLS var The string returned
value A value with the index of the table. The index is zero-
Example
LCD "Hello" based. That
Locate 1,10 is, 0 will return the first element of the table.
LCD "*" Label The label where the data starts
Value : Byte, Integer, Word, Long, Constant. Range(0-255)
LOOKUP See also
Action LOOKUP
Returns a value from a table.
Example
Syntax Dim s as string, idx as Byte
var =LOOKUP( value, label ) idx = 0 : s = LookupStr(idx,Sdata)
Remarks Print s 'will print 'This'
End
Var The returned value Sdata:
Value A value with the index of the table Data "This" , "is" ,"a test"
Label The label where the data starts
var : Byte, Integer, Word, Long, Single. LOW
value : Byte, Integer, Word, Long, Constant. Action
Difference with BASCOM LT Retrieves the least significant byte of a variable.
In BASCOM LT, the lookup function only works with byte tables. Syntax
In BASCOM-8051, you can use it with integer, word, long and var = LOW ( s )
single types too. Remarks
See also Var The variable that is assigned with the LSB of var S.
LOOKUPSTR S The source variable to get the LSB from.
Example See also
DIM b1 As Byte , I as Integer HIGH
b1 = Lookup(1, dta)
Print b1 ' Prints 2 (zero based) Example
I = Lookup(0,DTA2) Dim I As Integer , Z As Byte
End I = &H1001
DTA: Z = Low(I) ' is 1
DATA 1,2,3,4,5
DTA2: 'integer data LOWERLINE
1000% , 2000% Action
Reset the LCD cursor to the lowerline. Syntax
Syntax varn = MAKEINT(LSB , MSB)
LOWERLINE Remarks
Remarks Varn Variable that will be assigned with the converted value.
- LSB Variable or constant with the LS Byte.
See also MSB Variable or constant with the MS Byte.
UPPERLINE , THIRDLINE , FOURTHLINE , HOME The equivalent code is:
varn = (256 * MSB) + LSB
Example
LCD "Test" See also
LOWERLINE MAKEDEC BCD()
LCD "Hello"
End
Example
Dim a As Integer, I As Integer
MakeBCD() a = 2
I = MakeINT(a , 1) 'I = (1 * 256) + 2 = 258
Action End
Convert a variable into its BCD value.
Syntax
MakeDEC()
var1 = MAKEBCD(var2) Action
Convert a BCD byte or Integer/Word variable to its DECIMAL
Remarks
value.
var1 Variable that will be assigned with the converted value.
Var2 Variable that holds the decimal value. Syntax
When you want to use an I2C clock device, which stores its var1 = MAKEDEC(var2)
values as BCD values you can Remarks
use this function to convert variables from decimal to BCD. var1 Variable that will be assigned with the converted value.
For printing the bcd value of a variable, you can use the BCD() var2 Variable that holds the BCD value.
function. When you want to use an I2C clock device, which stores its
See also values as BCD values you can
MAKEDEC BCD() use this function to convert variables from BCD to decimal.
Example See also
Dim a As Byte MAKEBCD
a = 65 Example
LCD a Dim a As Byte
Lowerline a = 65
LCD BCD(a) LCD a
a = MakeBCD(a) Lowerline
LCD " " ; a LCD BCD(a)
End a = MakeDEC(a)
MAKEINT() LCD " " ; a
End
Action
Compact two bytes into a word or integer. MID()
Action Action
The MID function returns part of a string (a sub string). Ends a FOR..NEXT structure.
The MID statement replaces part of a string variable with another Syntax
string. NEXT [var]
Syntax Remarks
var = MID(var1 ,st [, l] ) Var The index variable that is used as a counter when you
MID(var ,st [, l] ) = var1 form the structure with FOR var. Var is optional and
Remarks not needed.
var The string that is assigned. You must end each FOR statement with a NEXT statement.
Var1 The source string. See also
st The starting position. FOR
l The number of characters to get/set. Example
Operations on strings require that all strings are of the same y = 10 'make y 10
type(internal or external) FOR a = 1 TO 10 'do this 10 times
FOR x = y TO 1'this one also
See also PRINT x ; a 'print the values
LEFT , RIGHT NEXT 'next x (count down)
Example NEXT a 'next a (count up) END
Dim s As XRAM String * 15, z As XRAM String * 15
s = "ABCDEFG" ON Interrupt
z = Mid(s,2,3) Action
Print z 'BCD
z="12345"
Execute subroutine when specified interrupt occurs.
Mid(s,2,2) = z Syntax
Print s 'A12DEFG ON interrupt label [NOSAVE]
End
Remarks
MOD interrupt INT0, INT1, SERIAL, TIMER0 ,TIMER1 or TIMER2.
Action Chip specific interrupts can be found under microprocessor
Returns the remainder of a division. support.
Label The label to jump to if the interrupt occurs.
Syntax NOSAVE When you specify NOSAVE, no registers are saved
ret = var1 MOD var2
and
Remarks restored in the interrupt routine. So when you use this option
ret The variable that receives the remainder. be sure to save and restore used registers.
var1 The variable to divide. You must return from the interrupt routine with the RETURN
var2 The divisor. statement.
Example You may have only one RETURN statement in your interrupt
a = 10 MOD 3 'divide 10 through 3 routine because the compiler
PRINT a 'print remainder (1)
restores the registers and generates a RETI instruction when it
NEXT encounters a RETURN
statement in the ISR.
You can't use TIMER1 when you are using SERIAL routines such x = 2 'assign a variable interrupt
ON x GOSUB lbl1, lbl2,lbl3 'jump to label lbl3
as PRINT,
x=0
Because TIMER1 is used as a BAUD RATE generator. ON x GOTO lbl1, lbl2 , lbl3
When you use the INT0 or INT1 interrupt you can specify on END
which condition the interrupt lbl3:
must be triggered. PRINT " lbl3"
RETURN
You can use the Set/Reset statement in combination with the Lbl1:
TCON-register for this Lbl2:
purpose.
SET TCON.0 : trigger INT0 by falling edge. OPEN - CLOSE
RESET TCON.0 : trigger INT0 by low level. Action
SET TCON.2 : trigger INT1 by falling edge. Opens and closes a device.
RESET TCON.2 : trigger INT1 by low level. Syntax
See Hardware for more details OPEN "device" for MODE As #channel
Example CLOSE #channel
ENABLE INTERRUPTS
ENABLE INT0 'enable the interrupt
Remarks
ON INT0 Label2 nosave 'jump to label2 on INT0 device There are 2 hardware devices supported: COM1 and
DO 'endless loop COM2.
LOOP With the software UART, you must specify the portpin and the
END baudrate.
Label2:
PRINT " An hardware interrupt occurred!" 'print message COM3.0:9600 will use PORT 3.0 at 9600 baud.
RETURN MODE You can use BINARY, INPUT or OUTPUT for COM1 and
COM2, but for the
ON Value software UART pins, you must specify INPUT or OUTPUT.
Action channel The number of the channel to open. Must be a positive
Branch to one of several specified labels, depending on the value constant.
of a variable. Since there are uP's such as the 80537 with 2 serial channels on
Syntax board, the compiler must
ON var [GOTO] [GOSUB] label1 [, label2 ] know which serial port you want to use. That is why the OPEN
Remarks statement is implemented.
var The numeric variable to test. With only 1 serial port on board, you don't need this statement.
This can also be a SFR such as P1. The statements that support the device are PRINT , PRINTHEX,
label1, INPUT and INPUTHEX.
label2 Every opened device must be closed using the CLOSE #channel
The labels to jump to depending on the value of var. statement. Of course, you
Note that the value is zero based. So when var = 0, the first must use the same channel number.
specified label is The software UART, only supports the GET and PUT statements
jumped/branched. to retrieve and send data.
Example
COM1: and COM2: are hardware ports, and can be used with Action
PRINT etc. Sends a byte to a hardware port or external memory address.
See also Syntax
Example 1 OUT address, value
'only works with a 80517 or 80537 Remarks
CONFIG BAUD1 = 9600 'serial 1 baudrate
OPEN "COM2:" FOR BINARY AS #1 'open the port address The address where to send the byte to.
PRINT #1, "Hello" 'print to serial 1 value The variable or value to send.
PRINT "Hello" 'print to serial 0 The OUT statement only works on systems with an uP that can
CLOSE #1 'close the channel address external
Example 2 Memory.
'works with every port pin
Dim A As Byte , S As String * 16 , I As Byte , Dum As Byte
See also
'a software comport is named after the pin you use INP
'for example P3.0 will be "COM3.0:" (so there is no P) Example
'for software comports, you must provide the baudrate Dim a as byte
'So for 9600 baud, the devicename is "COM3.0:9600" OUT &H8000,1 'send 1 to the databus(d0-d7) at hex address
'When you want to use the pin for sending, you must open 8000
the device for OUTPUT END
'When you want to use the pin for receiving, you must open Will generate :
the device for INPUT Mov A,#1
'At this time only variables can be send and received with Mov dptr,#h'8000
the PUT and GET Movx @dptr,a
statements.
'In the feature PRINT etc. will support these software P1,P3
comports.
Open "com3.1:9600" For Output As #1 'p3.1 is normally Action
used for tx so testing is easy P1 and P3 are special function registers that are treated as
Open "com3.0:9600" For Input As #2 'p3.0 is normally variables.
used for RX so testing is easy
S = "test this" 'assign string
Syntax
Dum = Len(s) 'get length of string Px = var
For I = 1 To Dum 'for all characters from left to right var = Px
A = Mid(s , I , 1) 'get character Remarks
Put #1 , A 'write it to comport
Next
x The number of the port. (1 or 3). P3.6 can’t be used
Do with an AT89C2051!
Get #2 , A 'get character from comport Var The variable to retrieve or to set.
Put #1 , A 'write it back Note that other processors can have additional ports such as
Print A 'use normal channel P0,P2,P4 etc.
Loop
Close #1 ' finally close When you select the proper .DAT file, you can also use these
device ports as variables.
Close #2 In fact, you can use any SFR as a variable in BASCOM.
End ACC = 0 'will reset the accumulator for example
OUT See hardware for a more detailed description of the ports.
Example See also
Dim a as BYTE, b1 as BIT PEEK , CPEEK , INP , OUT
a = P1 'get value from port 1
a = a OR 2 'manipulate it Example
P1 = a 'set port 1 with new value POKE 127, 1 'write 1 to address 127
P1 = &B10010101 'use binary notation End
P1 = &HAF 'use hex notation
b1 = P1.1 'read pin 1.1
POWERDOWN
P1.1 = 0 'set it to 0 Action
Put processor into powerdown mode.
PEEK() Syntax
Action POWERDOWN
Returns a byte stored in internal memory.
Remarks
Syntax The powerdown mode stops the system clock completely.
var = PEEK( address ) The only way to reactivate the micro controller is by system reset.
Remarks See also
var Numeric variable that is assigned with the content of the IDLE
memory
location address
Example
POWERDOWN
address Numeric variable or constant with the address
location.(0-255) PRINT
See also Action
POKE , CPEEK , INP , OUT Send output to the RS-232 port.
Example Syntax
DIM a As Byte PRINT var ; " constant"
a = Peek( 0 ) 'return the first byte of the internal
memory (r0) Remarks
End var The variable or constant to print.
You can use a semicolon (;) to print more than one variable at
POKE one line.
Action When you end a line with a semicolon, no linefeed will be added.
Write a byte to an internal memory location. The PRINT routine can be used when you have a RS-232
Syntax interface on your uP.
POKE address , value See the manual for a design of a RS-232 interface.
Remarks The RS-232 interface can be connected to a serial
address Numeric variable with the address of the memory communication port of your computer.
location to set. (0-255) This way you can use a terminal emulator as an output device.
value Value to assign. (0-255) You can also use the build in terminal emulator.
Be careful with the POKE statement because you can change See also
variables with it that can PRINTHEX , INPUT , OPEN , CLOSE
cause your program to function incorrect. Example
'---------------------------------------------------------
----- PRINTHEX
' (c) 1997,1998 MCS Electronics
'---------------------------------------------------------
Action
----- Sends a variable in hexadecimal format to the serial port.
' file: PRINT.BAS Syntax
' demo: PRINT, PRINTHEX PRINTHEX var
'---------------------------------------------------------
----- Remarks
Dim A As Byte , B1 As Byte , C As Integer var The variable to print.
A = 1 The same rules apply to PRINTHEX as PRINT.
Print "print variable a " ; A The PRINTHEX routine can be used when you have a RS-232
Print 'new line
Print "Text to print." 'constant to print interface on your uP.
B1 = 10 See the manual for a design of a RS-232 interface.
Printhex B1 'print in hexa notation The RS-232 interface can be connected to a serial
C = &HA000 'assign value to c% communication port of your computer.
Printhex C 'print in hex notation
Print C 'print in decimal notation
This way you can use a terminal emulator as an output device.
C = -32000 You can also use the build in terminal emulator.
Print C See also
Printhex C PRINT , INPUTHEX
Rem Note That Integers Range From -32767 To 32768
End Example
Dim x As Byte
PRINTBIN INPUT x 'ask for var
PRINT x 'print it in decimal format
Action PRINTHEX "Hex " ; x 'print it in hex format
Print binary content of a variable to the serial port.
Syntax PRIORITY
PRINTBIN var [,varn] Action
Remarks Sets the priority level of the interrupts.
var The variable which value is send to the serial port. Syntax
varn Optional variables to send. PRIORITY SET / RESET interrupt
PRINTBIN is equivalent to PRINT CHR(var); but whole arrays Remarks
can be printed this way. SET Bring the priority level of the interrupt to a higher level.
When you use a Long for example, 4 bytes are printed. RESET Bring the priority level of the interrupt to a lower level.
See also Interrupt The interrupt to set or reset.
INPUTBIN The interrupts are: INT0, INT1, SERIAL, TIMER0, TIMER1 and
Example TIMER2.
Dim a(10) as Byte, c as Byte Interrupt INT0 always has the highest priority.
For c = 1 To 10 When more interrupts occur at the same time the following order
a(c) = a 'fill array is used to handle the
Next
PRINTBIN a(1) 'print content interrupts.
Note that other microprocessors can have additional/other READ C : PRINT C
READ C : PRINT C
interrupt setting.
Restore dta3 : Read s : Print s
Read microprocessor support to check the additions. END
Interrupt Priority dta:
INT0 1 (highest) Data 5,10,15
TIMER0 2 dta2:
Data 1000%, -2000%
INT1 3 dta3:
TIMER1 4 Data " hello"
SERIAL 5 (lowest)
Example REM
PRIORITY SET SERIAL 'serial int highest level Action
ENABLE SERIAL 'enable serial int Instruct the compiler that comment will follow.
ENABLE TIMER0 'enable timer0 int
ENABLE INTERRUPTS 'activate interrupt handler Syntax
ON SERIAL label 'branch to label if serial int occur REM or '
DO 'loop for ever Remarks
LOOP
Label: 'start label You can comment your program for clarity.
PRINT " Serial int occurred." 'print message You can use REM or ' followed by your comment.
RETURN 'return from interrupt All statements after REM or ' are treated as comment so you
cannot
READ use statements after a REM statement.
Action New is the possibility to use block comments:
Reads those values and assigns them to variables. '( start block comment
Syntax print "This will not be compiled
READ var ') end block comment
Remarks Note that the starting ' sign will ensure compatibility with QB
var Variable that is assigned data value. Example
It is best to place the DATA lines at the end of your program. REM TEST.BAS version 1.00
PRINT a' " this is comment : PRINT " hello"
Difference with QB ^--- this will not be executed!
It is important that the variable is of the same type as the stored
data. RESET
See also Action
DATA , RESTORE Reset a bit of a PORT (P1.x, P3.x) or an internal
Example bit/byte/integer/word variable.
Dim A As Byte, I As Byte, C As Integer, S As XRAM String * Syntax
10 RESET bit
RESTORE dta
FOR a = 1 TO 3 RESET var.x
READ i : PRINT i Remarks
NEXT bit Can be a P1.x, P3.x or any bit variable where x=0-7.
RESTORE DTA2
var Can be a byte, integer or word variable.
x Constant of variable to reset.(0-7) for bytes and (0-15) for Subroutines must be ended with a related RETURN statement.
Integer/Word Interrupt subroutines must also be terminated with the Return
See also statement.
SET See also
Example GOSUB
Dim b1 as bit, b2 as byte, I as Integer Example
RESET P1.3 'reset bit 3 of port 1 GOSUB Pr 'jump to subroutine
RESET b1 'bitvariable PRINT result 'print result
RESET b2.0 'reset bit 0 of bytevariable b2 END 'program ends
RESET I.15 'reset MS bit from I Pr: 'start subroutine with label
RESTORE result = 5 * y 'do something stupid
result = result + 100 'add something to it
Action RETURN 'return
Allows READ to reread values in specified DATA statements. RIGHT()
Syntax Action
RESTORE label
Return a specified number of rightmost characters in a string.
Remarks Syntax
label The label of a DATA statement.
var = RIGHT(var1 ,st )
See also
DATA , READ
Remarks
var The string that is assigned.
Example Var1 The sourcestring.
DIM a AS BYTE, I AS BYTE
RESTORE dta st The starting position.
FOR a = 1 TO 3 All strings must be of the same datatype, internal or external.
READ a : PRINT a See also
NEXT
RESTORE DTA2
LEFT , MID
READ I : PRINT I Example
READ I : PRINT I Dim s As XRAM String * 15, z As XRAM String * 15
END s = "ABCDEFG"
DTA1: z = Right(s,2)
Data 5, 10, 100 Print z 'FG
DTA2: End
Data -1%, 1000%
Integers must end with the %-sign. (Integer : <0 or >255) ROTATE
RETURN Action
Shifts all bits one place to the left or right.
Action Syntax
Return from a subroutine. ROTATE var , LEFT/RIGHT [ , shifts]
Syntax Remarks
RETURN
var Byte, Integer/Word or Long variable.
Remarks shifts The number of shifts to perform.
Note that the carryflag goes into the LSB or MSB depending on CASE ELSE
END SELECT
the shift direction. This works
END
just like the ASM statements RLC and RRC. When this behaviour
is not wanted, clear the SET
carry bit before a shift with the CLR C statement. Action
See also Set a bit of a PORT(P1.x,P3.x) or a bit/byte/integer/word variable.
SHIFTIN , SHIFTOUT Syntax
Example SET bit
Dim a as Byte SET var.x
a = 128
ROTATE a, LEFT , 2 Remarks
Print a '1 Bit P1.x, P3.x or a Bitvariable.
Generated code : Var A byte, integer, word or long variable.
Mov R7,#2
Mov R0,#h'21
X Bit of variable (0-7) to set. (0-15 for Integer/Word)
Mov a,@r0 See also
Rlc a RESET
Djnz r7,*-1
Mov @r0,a
Example
Dim b1 as Bit, b2 as byte, c as Word, L as Long
SELECT SET
SET
P1.1 'set bit 1 of port 1
b1 'bitvariable
Action SET b2.1 'set bit 1 of var b2
Executes one of several statement blocks depending on the SET C.15 'set highest bit of Word
value of an expression. SET L.31 ‘set MS bit of LONG

Syntax SHIFTCURSOR
SELECT CASE var Action
CASE test1 : statements Shift the cursor of the LCD display left or right by one position.
[CASE test2 : statements ] Syntax
CASE ELSE : statements SHIFTCURSOR LEFT / RIGHT
END SELECT
See also
Remarks SHIFTLCD
var Variable. to test
Test1 Value to test for.
Example
LCD "Hello"
Test2 Value to test for. SHIFTCURSOR LEFT
See also End
- SHIFTIN and SHIFTOUT
Example
Dim b2 as byte Action
SELECT CASE b2 'set bit 1 of port 1 Shifts a bitstream in or out a variable.
CASE 2 : PRINT "2" Syntax
CASE 4 : PRINT "4"
CASE IS >5 : PRINT ">5" 'a test requires the IS keyword
SHIFTIN pin , pclock , var , option
SHIFTOUT pin , pclock , var , option Mov R0,#h'21
Mov r2,#h'01
Remarks __UNQLBL1:
pin The portpin which serves as as input/output. Mov r3,#8
pclock The portpin which generates the clock. __UNQLBL2:
Var The variable that is assigned. Jnb P1.1,*+0
Mov c,P1.0
Option Option can be : Rlc a
0 - MSB shifted in/out first when clock goes low Jb P1.1,*+0
1 - MSB shifted in/out first when clock goes high Djnz r3,__UNQLBL2
2 - LSB shifted in/out first when clock goes low Mov @r0,a
3 - LSB shifted in/out first when clock goes high Dec r0
Djnz r2,__UNQLBL1
For the SHIFTIN statement, you can add 4 to the
parameter to use the external clock signal for shifting. SHIFTLCD
It depends on the type of the variable, how many shifts will occur. Action
When you use a byte, 8 shifts will occur and for an integer, 16 Shift the LCD display left or right by one position.
shifts will occur.
Syntax
See also SHIFTLCD LEFT / RIGHT
Example Remarks
Dim a as byte
SHIFTIN P1.0 , P1.1 , a , 0 -
SHIFTOUT P1.2 , P1.1 , a , 0 See also
For the SHIFTIN example the following code is generated:
Setb P1.1
SHIFTCURSOR
Mov R0,#h'21 Example
Mov r2,#h'01 LCD "Very long text"
__UNQLBL1: SHIFTLCD LEFT
Mov r3,#8 Wait 1
__UNQLBL2: SHIFTLCD RIGHT
Clr P1.1 End
Nop
Nop SOUND
Mov c,P1.0 Action
Rlc a
Setb P1.1 Sends pulses to a port pin.
Nop Syntax
Nop SOUND pin, duration, frequency
Djnz r3,__UNQLBL2
Mov @r0,a Remarks
Dec r0 pin Any I/O pin such as P1.0 etc.
Djnz r2,__UNQLBL1 duration The number of pulses to send. Byte, integer/word or
Of course it depends on the parameter, which code will be constant.
generated.
To shift with an external clock signal:
(1- 32768).
SHIFTIN P1.0, P1.1 , a , 4 'add 4 for external clock Frequency The time the pin is pulled low and high.
Generated code:
When you connect a speaker or a buzzer to a port pin (see
hardware) , you can use the
SPIIN
SOUND statement to generate some tones. Action
The port pin is switched high and low for frequency uS. Reads a value from the SPI-bus.
This loop is executed duration times. Syntax
See also SPIIN var, bytes
- Remarks
Example var The variable that is assigned with the value read from the
SOUND P1.1 , 10000, 10 'BEEP SPI-bus.
End bytes The number of bytes to read.
SPACE() See also
SPIOUT , CONFIG SPI
Action Example
Returns a string that consists of spaces. Dim a(10) as byte
Syntax CONFIG SPI = SOFT, DIN = P1.0, DOUT = P1.1, CS=P1.2, CLK =
var = SPACE(x ) P1.3
SPIIN a(1) , 4 'read 4 bytes
Remarks
x The number of spaces. SPIOUT
Var The string that is assigned. Action
Using 0 for x, will result in a string of 255 bytes because there is Sends a value of a variable to the SPI-bus.
no check for a zero length Syntax
assign. SPIOUT var , bytes
Example Remarks
Dim s as XRAM String * 15, z as XRAM String * 15
s = Space(5) var The variable whose content must be send to the SPI-bus.
Print " {" ;s ; " }" '{ } bytes The number of bytes to send.
Dim A as Byte See also
A = 3
S = Space(a)
SPIIN , CONFIG SPI
Genereated code for last 2 lines : Example
; ---------- library routine ----------- CONFIG SPI = SOFT, DIN = P1.0, DOUT = P1.1, CS=P1.2, CLK =
_sStr_String: P1.3
Mov @r1,a Dim a(10) as Byte , X As Byte
Inc r1 SPIOUT a(1) , 5 'send 5 bytes
Djnz r2,_sStr_String SPIOUT X , 1 'send 1 byte
Clr a
Mov @r1,a START
Ret Action
;---------------------------------------
Mov R1,#h'22 ; location of string Start the specified timer/counter.
Mov R2,h'21 ; number of spaces Syntax
Mov a,#32 START timer
Acall _sStr_String
Remarks START TIMERx
timer TIMER0, TIMER1, TIMER2, COUNTER0 or COUNTER1. Example
You must start a timer/counter in order for an interrupt to occur '---------------------------------------------------------
(when the external gate is -----
' (c) 1997,1998 MCS Electronics
disabled). '---------------------------------------------------------
TIMER0 and COUNTER0 are the same device. -----
See also ' file: TIMER0.BAS
' demo: ON TIMER0
STOP TIMERx ' *TIMER1 is used for RS-232 baudrate generator
Example '---------------------------------------------------------
ON TIMER0 label2 -----
LOAD TIMER0, 100 Dim Count As Byte , Gt As Byte
START TIMER0 Config Timer0 = Timer , Gate = Internal , Mode = 2
DO 'start loop 'Timer0 = counter : timer0 operates as a counter
LOOP 'loop forever 'Gate = Internal : no external gate control
label2: 'perform an action here 'Mode = 2 : 8-bit auto reload (default)
RETURN On Timer0 Timer_0_int
Load Timer0 , 100 'when the timer reaches 100 an interrupt
STOP 'will occur
Action Enable Interrupts 'enable the use of interrupts
Enable Timer0 'enable the timer
Stop program execution. Rem Setting Of Priority
Syntax Priority Set Timer0 'highest priority
STOP Start Timer0 'start the timer
Count = 0 'reset counter
Remarks Do
END can also be used to terminate a program. Input "Number " , Gt
When an END or STOP statement is encountered, a never- Print "You entered : " ; Gt
ending loop is generated. Loop Until Gt = 1 'loop until users presses ESC key
Stop Timer0
Example End
PRINT var 'print something Rem The Interrupt Handler For The Timer0 Interrupt
STOP 'thats it Timer_0_int:
STOP TIMERx Inc Count
If Count = 2 Then
Action Print "Timer0 Interrupt occured"
Stop the specified timer/counter. Count = 0
End If
Syntax Return
STOP timer
Remarks STR()
timer TIMER0, TIMER1, TIMER2, COUNTER0 or COUNTER1. Action
You can stop a timer when you don't want an interrupt to occur. Returns a string representation of a number.
TIMER0 and COUNTER0 are the same. Syntax
See also var = Str( x )
Remarks SUB
var A string variable.
X A numeric variable. Action
x : Byte, Integer, Word, Long, Single. Defines a Sub procedure.
The string must be big enough to store the string. Syntax
See also SUB Name[(var1)]
VAL Remarks
Difference with QB name Name of the sub procedure, can be any non-reserved
In QB STR() returns a string with a leading space. This behaviour word.
is not in BASCOM. var1 The name of the parameter.
You must end each subroutine with the END SUB statement.
Example
Dim a as Byte, S as XRAM String * 10 You must Declare Sub procedures before the SUB statement.
a = 123 The parameter names and types must be the same in both the
s = Str(a) declaration and the Sub
Print s procedure.
End
Parameters are global to the application.
STRING() That is the used parameters must be dimensioned with the DIM
Action statement.
Therefore, the variables can be used by the program and sub
Returns a string consisting of m repetitions of the character with
procedures.
ASCII
The following examples will illustrate this:
Code n. Dim a as byte, b1 as byte, c as byte 'dim used variables
Syntax Declare Sub Test(a as byte) 'declare subroutine
var = STRING(m ,n ) a = 1 : b1 = 2: c = 3 'assign variables
Print a ; b1 ; c 'print them
Remarks Call Test(b1) 'call subroutine
var The string that is assigned. Print a ;b1 ; c 'print variables again
n The ASCII-code that is assigned to the string. End
m The number of characters to assign. Sub Test(a as byte) 'begin procedure/subroutine
print a ; b1 ; c 'print variables
Since a string is terminated by a 0 byte, you can't use 0 for n. End Sub
Using 0 for m will result in a string of 255 bytes, because there is
no check on a length
See also
assign of 0. When you need this let us know. CALL, DECLARE
See also Example
SPACE -
Example SWAP
Dim s as XRAM String * 15
s = String(5,65)
Action
Print s 'AAAAA Exchange two variables of the same type.
End Syntax
SWAP var1, var2
Remarks a = 255
LCD a
var1 A variable of type bit, byte, integer or word. Lowerline
var2 A variable of the same type as var1. LCD a
After the swap, var1 will hold the value of var2 and var2 will hold Upperline
the value of var1. End
Example
Dim a as integer,b1 as integer
VAL()
a = 1 : b1 = 2 'assign two integers Action
SWAP a, b1 'swap them Converts a string representation of a number into a number.
PRINT a ; b1 Syntax
THIRDLINE var = Val( s )
Action Remarks
Reset LCD cursor to the third line. var A numeric variable that is assigned with the value of s.
s Variable of the string type.
Syntax var : Byte, Integer, Word, Long, Single.
THIRDLINE
See also
Remarks STR
- Example
See also Dim a as byte, s As XRAM string * 10
UPPERLINE , LOWERLINE , FOURTHLINE s = "123"
a = Val(s) 'convert string
Example Print a
Dim a as byte
End
a = 255
LCD a
Thirdline
VARPTR()
LCD a Action
Upperline Retrieves the memory-address of a variable.
End
Syntax
UPPERLINE var = VARPTR( var2 )
Action Remarks
Reset LCD cursor to the upperline. var The variable that is assigned with the address of var2.
Syntax var2 A variable to retrieve the address from.
UPPERLINE See also
Remarks PEEK POKE
- Example
Dim I As Integer , B1 As Byte
See also B1 = Varptr(I)
LOWERLINE THIRDLINE FOURTHLINE Generated code:
Mov h'23,#h'21
Example
Dim a as byte WAIT
Action No accurate timing is possible with this command.
Suspends program execution for a given time. In addition, the use of interrupts can slow this routine.
Syntax This statement is provided for the I2C statements.
WAIT seconds When you write to an EEPROM you must wait for 10 mS after the
write instruction.
Remarks
seconds The number of seconds to wait. See also
The delay time is based on a clockfrequency of 12 Mhz. DELAY WAIT
No accurate timing is possible with this command. Example
When you use interrupts, the delay can be extended. WAITMS 10 'wait for 10 mS
Print "*"
See also
DELAY WHILE .. WEND
Example Action
WAIT 3 'wait for three seconds Executes a series of statements in a loop, as long as a given
Print "*" condition is true.
WAITKEY Syntax
Action WHILE condition
Wait until a character is received in the serial buffer. statements
WEND
Syntax
var = WAITKEY Remarks
If the condition is true then any intervening statements are
Remarks
executed until the WEND
var Variable that is assigned with the ASCII value of the
statement is encountered.
serial buffer.
BASCOM then returns to the WHILE statement and checks
var : Byte, Integer, Word, Long, String.
condition.
See also If it is still true, the process is repeated.
INKEY If it is not true, execution resumes with the statement following
Example the WEND statement.
Dim A As Byte
A = Waitkey 'wait for character See also
Print A DO .. LOOP
Example
WAITMS WHILE a <= 10
Action PRINT a
Suspends program execution for a given time in mS. INC a
WEND
Syntax
WAITMS mS Hardware - LCD display
Remarks The LCD display can be connected as follows:
mS The number of milliseconds to wait. (1-255) LCD-DISPLAY PORT PIN
The delay time is based on a clock frequency of 12 Mhz. DB7 P1.7 14
DB6 P1.6 13 BAUDRATE GENERATOR
DB5 P1.5 12 INTERRUPTS and PRIORITY
DB4 P1.4 11 80517,80537
E P1.3 6 GETAD
RS P1.2 4 WATCHDOG
RW Ground 5 BAUDRATE GENERATOR
Vss Ground 1 BAUDRATE GENERATOR1
Vdd +5 Volt 2 INTERRUPTS and PRIORITY
Vo 0-5 Volt 3
This leaves P1.1 and P1.0 and P3 for other purposes.
AT898252 WATCHDOG
You can change the LCD pin layout from the Options LCD menu. The AT89S8252 has a build in watchdog timer.
You can select the display used with the CONFIG LCD A watchdog timer is a timer that will reset the uP when it reaches
statement. a certain value.
The LCD display operates in 4-bit mode. So during program execution this WD-timer must be reset before
See the $LCD statement for operation in 8-bit mode. it exceeds its maximum
BASCOM supports many statements to control the LCD display. value.
For those who want to have more control, the example below This is used to be sure a program is running correct.
shows how to do so. When a program crashes or sits in an endless loop it will not
Acc = 5 'load register A with value reset the WD-timer so an
Call Lcd_control 'it is a control value to control the display automatic reset will occur resulting in a restart.
Acc = 65 'load with new value (letter A) START WATCHDOG ‘will start the watchdog timer.
Call Write_lcd ‘writes it to the LCD display STOP WATCHDOG ‘will stop the watchdog timer.
Note that lcd_control and write_lcd are assembler subroutines RESET WATCHDOG ‘will reset the watchdog timer.
which can be called from See also
BASCOM. CONFIG WATCHDOG
See manufacture details from your LCD display for the correct Example
assignment. '-----------------------------------------------------
' (c) 1998 MCS Electronics
Microprocessor support ' WATCHD.BAS demonstrates the AT89S8252 watchdog timer
' select 89s8252.dat !!!
Some microprocessors have additional features compared to the '-----------------------------------------------------
AT89C2051/8051. Config Watchdog = 2048 'reset after 2048 mSec
8032/8052/AT89S8252 Start Watchdog 'start the watchdog timer
TIMER2 Dim I As Word
AT89S8252 For I = 1 To 10000
Print I 'print value
WATCHDOG ' Reset Watchdog
DATA EEPROM 'you will notice that the for next doesnt finish because
Alternative port-pin functions of the reset
80515,80535,80517,80535 'when you unmark the RESET WATCHDOG statement it will
finish because the
GETAD
'wd-timer is reset before it reaches 2048 msec
WATCHDOG Next
End READEEPROM var [, address ]
WATCHDOG var Any BASCOM variable name.
Address The address of the EEPROM where to read the data
The AT89S8252 has a build in watchdog timer.
from.
A watchdog timer is a timer that will reset the uP when it reaches
Ranges from 0 to 2047.
a certain value.
You can omit the address when you have written a value before
So during program execution this WD-timer must be reset before
with the WRITEEEPROM var statement.
it exceeds its maximum
Because in that case the compiler knows about the address
value. This is used to be sure a program is running correct.
because it is assigned by the compiler.
When a program crashes or sits in an endless loop it will not
Example
reset the WD-timer so an Dim S As String * 15 , S2 As String * 10
automatic reset will occur resulting in a restart. S = "Hello" : S2 = "test"
CONFIG WATCHDOG = value Dim L As Long
value The time in mS it takes the WD will overflow, causing a L = 12345678
Writeeeprom S
reset. Writeeeprom S2 'write strings
Possible values are : Writeeeprom L 'write long
16,32,64,128,256,512,1024 or 2048 S = "" : S2 = "" : L = 0 'clear variables
START WATCHDOG will start the watchdog timer. Readeeprom L : Print L
STOP WATCHDOG will stop the watchdog timer. Readeeprom S : Print S
Readeeprom S2 : Print S2
RESET WATCHDOG will reset the watchdog timer. End
Example
DIM A AS INTEGER Alternative port-pin functions
CONFIG WATCHDOG = 2048 'after 2 seconds a reset will occur
START WATCHDOG 'start the WD
The AT89S8252 ports have alternative functions.
DO The followng table shows the alternative functions.
PRINT a Port pin Alternate function
a = a + 1 'notice the reset P1.0 T2 external count input to timer.counter 2, clock out
REM RESET WATCHDOG 'delete the REM to run properly P1.1 T2EX timer/counter 2 capture/reload trigger and
LOOP
END direction flag
P1.4 /SS Slave port select input
DATA EEPROM P1.5 MOSI Master data output, slave data input pin for SPI
The AT89S8252 has a build in 2Kbytes flash EEPROM. channel
You can use this to store data. P1.6 MISO Master data input, slave data output pin for SPI
Two statements are provided: WRITEEEPROM and channel
READEEPROM. P1.7 SCK Master clock output, slave clock input pin for SPI
WRITEEEPROM var [, address ] channel
var Any BASCOM variable name. P3.0 RxD serial input port
Address The address of the EEPROM where to write the data to. P3.1 TxD serial output port
Ranges from 0 to 2047. P3.2 /INT0 external interrupt 0
When you omit the address the address will be assigned P3.3 /INT1 external interrupt 1
automatic. You can view the assigned address in the report file. P3.4 T0 timer 0 external input
P3.5 T1 timer 1 external input (There is no reload value in this mode. Even when a capture
P3.6 /WR external data memory write strobe event occurs from T2EX the
P3.7 /RD external data memory read strobe counter keeps on counting T2EX pin transitions or osc/12 pulses)
/ Means active low Auto reload mode
In the 16-bit auto reload mode, TIMER2 can be configured as a
TIMER2 in 8032 and compatibles timer or counter, which can
Some microprocessors have an additional timer on board : be programmed to count, up or down. The counting direction is
TIMER2. determined by bit DCEN.
This section describes the 8032 compatible TIMER2 and is not TIMER2 will default to counting up to &HFFFF and sets the TF2
compatible with th TIMER2 overflow flag bit upon
found in the 80C535 and others. overflow. This causes the TIMER2 registers to be reloaded with
TIMER2 is a 16-bit timer/counter which can operate as either an the 16-bit value in RCAP2L
event timer or an event and RCAP2H.
counter. TIMER2 has three main operating modes : capture, The values in RCAP2L and RCAP2H are pre-set by software
auto-reload(up or down means.
counting) , and baud rate generator. Counter mode:
Capture mode CONFIG TIMER2=COUNTER,GATE=INTERNAL,MODE=0
In the capture mode there are two options : Timer mode:
_ 16-bit timer/counter, which upon overflowing sets, bit TF2, the CONFIG TIMER2=COUNTER,GATE=INTERNAL,MODE=0
TIMER2 overflow bit. This If EXEN2=1 then a 16-bit reload can be triggered either by an
bit can be used to generate an interrupt. overflow or by a 1 to 0
Counter mode : transition at input T2EX. This transition also sets the EXF2 bit.
CONFIG TIMER2 = COUNTER, GATE = INTERNAL, MODE = 1 The TIMER2 interrupt, if
Timer mode: enabled, can be generated when either TF2 or EXF2 are 1.
CONFIG TIMER2=TIMER, GATE= INTERNAL,MODE =1 Counter mode:
_ As above but with the added future that a 1 to 0 transition on at CONFIG TIMER2=COUNTER,GATE=EXTERNAL,MODE=0
external input T2EX Timer mode:
causes the current values in the TIMER2 registers TL2 and TH2 CONFIG TIMER2=TIMER,GATE=EXTERNAL,MODE=0
to be captured into the TIMER2 can also count up or down. This mode allows pin T2EX
capture registers RCAP2L and RCAP2H. to control the direction of
Counter mode: count. When logic 1 is applied at pin T2EX TIMER2 will count up.
CONFIG TIMER2 = COUNTER, GATE = EXTERNAL, MODE = 1 TIMER2 will overflow at
Timer mode: &HFFFF and sets the TF2 flag, which can then generate an
CONFIG TIMER2=TIMER,GATE=EXTERNAL,MODE=1 interrupt, if the interrupt is
In addition the transition at T2EX causes bit EXF2 in T2CON to enabled. This timer overflow also causes the 16-bit value in
be set and EXF2 like TF2 RCAP2L en RCAP2H to be
can generate an interrupt. reloaded in to the timer registers TL2 and TH2.
The TIMER2 interrupt routine can interrogate TF2 and EXF2 to Counter mode:
determine which event
caused the interrupt.
CONFIG TIMER1 can be used for the reception with an other baudrate.
TIMER2=COUNTER,GATE=INTERNAL/EXTERNAL,MODE=0,DI CONFIG TIMER2=TIMER,GATE=INTERNAL,MODE=4
RECTION=UP Note that TIMER1 must be set-up from assembler this way.
Timer mode: Clock output
CONFIG Some 8052 deviants have the ability to generate a 50% duty
TIMER2=COUNTER,GATE=INTERNAL/EXTERNAL,MODE=0,DI cycle clock on P1.0.
RECTION=UP CONFIG TIMER2=TIMER,MODE=5
Logic 0 applied at pin T2EX causes TIMER2 to count down. The The output frequency = (fOSC / 4) / (65536-CAPTURE)
timer will under flow when Use CAPTURE = value to set the capture register.
TL2 and TH2 become equal to the value stored in RCAP2L and How to determine what caused the interrupt
RCAP2H. TIMER2 under You can test the bit T2CON.7 to see if a overflow caused the
flows sets the TF2 flag and causes &HFFFF to be reloaded into interrupt.
the timer registers TL2 and You can test bit T2CON.6 whether either a reload or capture is
TH2. caused by a negative
Counter mode: transition on T2EX.
CONFIG Timer2_ISR:
TIMER2=COUNTER,GATE=INTERNAL/EXTERNAL,MODE=0,DI If T2CON.7 = 1 Then
RECTION=DOWN Print “Timer overflowed”
Timer mode: Else
CONFIG If T2CON.6 = 1 Then
TIMER2=COUNTER,GATE=INTERNAL/EXTERNAL,MODE=0,DI Print “External transition”
RECTION=DOWN End if
The external flag TF2 toggles when TIMER2 under flows or End If
overflows. Return
The EXF2 flag does not generate an interrupt in counter
UP/DOWN mode.
INTERRUPTS and PRIORITY 80515
Baud rate generator The 80515 and 80535 have more interrupts and priority is
This mode can be used to generate a baud rate for the serial handled different compared
port. TIMER1 can be used for to the 8051.
an other task this way. Enable interrupts:
CONFIG TIMER2=TIMER,GATE=INTERNAL,MODE=2 ENABLE AD 'AD converter
Receive only ENABLE INT2|INT3|INT4|INT5|INT6 'external interrupt 2-6
This mode can be used to generate the baudrate for the receiver ENABLE TIMER2EX 'timer2 external reload
only. Disable interrupts:
TIMER1 can be used for the transmission with an other baudrate. DISABLE AD 'AD converter
CONFIG TIMER2=TIMER,GATE=INTERNAL,MODE=3 DISABLE INT2|INT3|INT4|INT5|INT6 'external interrupt 2-6
Note that TIMER1 must be setup from assembler this way. DISABLE TIMER2EX 'timer2 external reload
Transmit only Selecting of priority:
This mode can be used to generate the baud rate for transmitter PRIORITY SET|RESET source , level
only. level can be 0,1,2 or 3.(0=lowest,3=highest)
The source can be : ref=0 'range from 0 to 5 Volt
b1=getad(channel,ref) 'place A/D into b1
INT0/ADC
TIMER0/INT2 WATCHDOG 80515
INT0/INT3 The 80515 and 80535 both have a WD-timer.
TIMER1/INT4 This is a 16 bit timer that can't be stopped!
SERIAL/INT5 It will reset the system after 65535 uS at 12MHz.
TIMER2/INT6 START WATCHDOG 'start the WD-timer.
Note that only one of the pairs must be selected. RESET WATCHDOG 'will reset the WD-timer.
PRIORITY SET INT4,3 'will set INT4 to the highest priority.
When two ints occur with the same priority the first source in INTERRUPTS and PRIORITY 80537
the list The 80517 and 80537 have more interrupts and priority is
Will be handled first. So when both TIMER1 and INT4 have handled different compared
the same priority, TIMER1 to the 8051.
will be serviced first. Enable interrupts:
Look at a datasheet for more details. ENABLE AD 'AD converter
ENABLE INT2|INT3|INT4|INT5|INT6 'external interrupt 2-6
GETAD ENABLE TIMER2EX 'timer2 external reload
Action ENABLE CTF 'compare timer interrupt
Retrieves the analog value from channel 0-7. ENABLE SERIAL1 'serial1 interrupt
Channel ranges from 0-11 on a 80517 or 80537. Disable interrupts:
Syntax DISABLE AD 'AD converter
var = GETAD(channel, range) DISABLE INT2|INT3|INT4|INT5|INT6 'external interrupt 2-6
Remarks DISABLE TIMER2EX 'timer2 external reload
var The variable that is assigned with the A/D value DISABLE CTF 'compare timer interrupt
channel The channel to measure DISABLE SERIAL1 'serial1 interrupt
range The internal range selection. Selecting of priority:
0 = 0-5 Volt PRIORITY SET|RESET source , level
192 = 0 - 3.75 Volt level can be 0,1,2 or 3.(0=lowest,3=highest)
128 = 0 - 2.5 Volt source can be :
64 = 0 - 1.25 Volt INT0/ADC/SERIAL1
12 = 3.75 - 5 Volt TIMER0/INT2
200 = 2.5 - 3.75 Volt INT0/INT3
132 = 1.25 - 2.5 Volt TIMER1/CTF/INT4
The GETAD() function is only intended for the 80515, SERIAL/INT5
80535,80517 and 80535. TIMER2/INT6
It is a microprocessor dependend support feature. Note that only one of the TRIPLE-pairs must be selected.
See also PRIORITY SET INT4,3 'will set INT4 to the highest priority.
When two ints occur with the same priority the first source in the
Example list will be handled first. So
Dim b1 as Byte, Channel as byte,ref as byte
channel=0 'input at P6.0
when both TIMER1 and INT4 have the same priority, TIMER1 will
be serviced first.
Look at a datasheet for more details.
CONFIG BAUD1
Action
Configure the uP to select the intern baud rate generator for
serial channel 1.
This baud rate generator is only available in the 80517 and
80537.
Syntax
CONFIG BAUD1 = baudrate
Remarks
baudrate Baudrate to use : 2048 – 37500
The 80517 and 80537 have 2 serial ports on board.
See also
CONFIG BAUD
Example
CONFIG BAUD1 = 9600 'use internal baud generator
Print "Hello"
End

S-ar putea să vă placă și