- •Table of Contents
- •Index
- •Keyword Reference
- •Installation
- •Installation of BASCOM
- •BASCOM IDE
- •Running BASCOM-AVR
- •File New
- •File Close
- •File Save
- •File Save As
- •File Print Preview
- •File Print
- •File Exit
- •View PinOut
- •View PDF viewer
- •View Error Panel
- •Edit Undo
- •Edit Redo
- •Edit Cut
- •Edit Copy
- •Edit Paste
- •Edit Find
- •Edit Find Next
- •Edit Replace
- •Edit Goto
- •Edit Toggle Bookmark
- •Edit Goto Bookmark
- •Edit Indent Block
- •Edit Unindent Block
- •Edit Remark Block
- •Program Compile
- •Program Syntax Check
- •Program Show Result
- •Program Simulate
- •Program Send to Chip
- •Tools Terminal Emulator
- •Tools LCD Designer
- •Tools LIB Manager
- •Tools Graphic Converter
- •Tools Stack Analyzer
- •Tools Plugin Manager
- •Tools Batch Compile
- •Options Compiler
- •Options Compiler Chip
- •Options Compiler Output
- •Options Compiler Communication
- •Options Compiler I2C, SPI, 1WIRE
- •Options Compiler LCD
- •Options Communication
- •Options Environment
- •Options Simulator
- •Options Programmer
- •Supported Programmers
- •ISP programmer
- •PG302 programmer
- •Sample Electronics cable programmer
- •KITSRUS Programmer
- •MCS Universal Interface Programmer
- •STK500 Programmer
- •Lawicel BootLoader
- •AVR ISP Programmer
- •USB-ISP Programmer
- •MCS Bootloader
- •Options Monitor
- •Options Printer
- •Window Cascade
- •Window Tile
- •Window Arrange Icons
- •Window Minimize All
- •Help About
- •Help Index
- •Help MCS Forum
- •Help MCS Shop
- •Help Support
- •Help Knowledge Base
- •Help Credits
- •BASCOM Editor Keys
- •Program Development Order
- •PlugIns
- •Font Editor
- •PinOut
- •BASCOM HARDWARE
- •Additional Hardware
- •AVR Internal Hardware
- •AVR Internal Registers
- •AVR Internal Hardware TIMER0
- •AVR Internal Hardware TIMER1
- •AVR Internal Hardware Watchdog timer
- •AVR Internal Hardware Port B
- •AVR Internal Hardware Port D
- •Adding XRAM
- •Attaching an LCD Display
- •Memory usage
- •Using the 1 WIRE protocol
- •Using the SPI protocol
- •Power Up
- •Chips
- •ATtiny22
- •ATtiny13
- •ATtiny15
- •ATtiny25
- •ATtiny45
- •ATtiny85
- •ATtiny26
- •ATtiny2313
- •ATMEGA8
- •ATMEGA16
- •ATMEGA32
- •ATMEGA48
- •ATMEGA88
- •ATMEGA168
- •ATMEGA64
- •ATMEGA103
- •ATMEGA128
- •ATMEGA161
- •ATMEGA162
- •ATMEGA163
- •ATMEGA165
- •ATMEGA169
- •ATMEGA323
- •ATMEGA603
- •ATMEGA8515
- •ATMEGA8535
- •BASCOM Language Fundamentals
- •Changes compared to BASCOM-8051
- •Language Fundamentals
- •Mixing ASM and BASIC
- •Assembler mnemonics
- •Reserved Words
- •Error Codes
- •Newbie problems
- •Tips and tricks
- •ASCII chart
- •BASCOM Language Reference
- •$BAUD
- •$BAUD1
- •$BOOT
- •$CRYSTAL
- •$DATA
- •$DEFAULT
- •$EEPLEAVE
- •$EEPROM
- •$EEPROMHEX
- •$EXTERNAL
- •$FRAMESIZE
- •$HWSTACK
- •$INCLUDE
- •$INITMICRO
- •$LCDPUTCTRL
- •$LCDPUTDATA
- •$LCDRS
- •$LCDVFO
- •$LOADER
- •$LOADERSIZE
- •$NOCOMP
- •$NOINIT
- •$NORAMCLEAR
- •$PROG
- •$PROGRAMMER
- •$REGFILE
- •$ROMSTART
- •$SERIALINPUT
- •$SERIALINPUT1
- •$SERIALINPUT2LCD
- •$SERIALOUTPUT
- •$SERIALOUTPUT1
- •$SWSTACK
- •$TIMEOUT
- •$TINY
- •$WAITSTATE
- •$XRAMSIZE
- •$XRAMSTART
- •1WIRECOUNT
- •1WRESET
- •1WREAD
- •1WSEARCHFIRST
- •1WSEARCHNEXT
- •1WVERIFY
- •1WWRITE
- •ACOS
- •ALIAS
- •ASIN
- •BASE64DEC
- •BASE64ENC
- •BAUD
- •BAUD1
- •BINVAL
- •BIN2GRAY
- •BITWAIT
- •BITS
- •BLOAD
- •BSAVE
- •BUFSPACE
- •BYVAL
- •CALL
- •CHECKSUM
- •CIRCLE
- •CLEAR
- •CLOCKDIVISION
- •CLOSE
- •CLOSESOCKET
- •CONFIG
- •CONFIG 1WIRE
- •CONFIG ACI
- •CONFIG ADC
- •CONFIG ATEMU
- •CONFIG BCCARD
- •CONFIG CLOCK
- •CONFIG CLOCKDIV
- •CONFIG COM1
- •CONFIG COM2
- •CONFIG COMx
- •CONFIG DATE
- •CONFIG DCF77
- •CONFIG DEBOUNCE
- •CONFIG I2CDELAY
- •CONFIG I2CSLAVE
- •CONFIG INPUT
- •CONFIG INTx
- •CONFIG GRAPHLCD
- •CONFIG KBD
- •CONFIG KEYBOARD
- •CONFIG LCD
- •CONFIG LCDBUS
- •CONFIG LCDMODE
- •CONFIG LCDPIN
- •CONFIG PORT
- •CONFIG PRINT
- •CONFIG PRINTBIN
- •CONFIG PS2EMU
- •CONFIG RC5
- •CONFIG SDA
- •CONFIG SCL
- •CONFIG SERIALIN
- •CONFIG SERIALIN1
- •CONFIG SERIALOUT
- •CONFIG SERIALOUT1
- •CONFIG SINGLE
- •CONFIG SPI
- •CONFIG SERVOS
- •CONFIG TCPIP
- •CONFIG TIMER0
- •CONFIG TIMER1
- •CONFIG TIMER2
- •CONFIG TWI
- •CONFIG TWISLAVE
- •CONFIG WAITSUART
- •CONFIG WATCHDOG
- •CONFIG X10
- •CONFIG XRAM
- •CONST
- •COSH
- •COUNTER0 and COUNTER1
- •CPEEK
- •CPEEKH
- •CRYSTAL
- •CURSOR
- •DATA
- •DAYOFWEEK
- •DAYOFYEAR
- •DATE$
- •DATE
- •DEBUG
- •DEBOUNCE
- •DECR
- •DECLARE FUNCTION
- •DECLARE SUB
- •DEFxxx
- •DEFLCDCHAR
- •DELAY
- •DISABLE
- •DISKFREE
- •DISKSIZE
- •DISPLAY
- •DO-LOOP
- •DriveCheck
- •DriveGetIdentity
- •DriveInit
- •DriveReset
- •DriveReadSector
- •DriveWriteSector
- •DTMFOUT
- •ECHO
- •ELSE
- •ENABLE
- •ENCODER
- •EXIT
- •FILEATTR
- •FILEDATE
- •FILEDATETIME
- •FILELEN
- •FILETIME
- •FLUSH
- •FORMAT
- •FOR-NEXT
- •FOURTHLINE
- •FRAC
- •FREEFILE
- •FUSING
- •GETADC
- •GETATKBD
- •GETATKBDRAW
- •GETDSTIP
- •GETDSTPORT
- •GETKBD
- •GETRC
- •GETRC5
- •GETTCPREGS
- •GETSOCKET
- •GLCDCMD
- •GLCDDATA
- •GOSUB
- •GOTO
- •GRAY2BIN
- •HEXVAL
- •HIGH
- •HIGHW
- •HOME
- •I2CINIT
- •I2CRECEIVE
- •I2CSEND
- •I2START,I2CSTOP, I2CRBYTE, I2CWBYTE
- •IDLE
- •IF-THEN-ELSE-END IF
- •INCR
- •INITFILESYSTEM
- •INITLCD
- •INKEY
- •INPUTBIN
- •INPUTHEX
- •INPUT
- •INSTR
- •ISCHARWAITING
- •KILL
- •LCASE
- •LCDAT
- •LCDCONTRAST
- •LEFT
- •LINE
- •LINE INPUT
- •LTRIM
- •LOAD
- •LOADADR
- •LOADLABEL
- •LOADWORDADR
- •LOCAL
- •LOCATE
- •LOOKDOWN
- •LOOKUP
- •LOOKUPSTR
- •LOWERLINE
- •MAKEBCD
- •MAKEINT
- •MAKEDEC
- •MAKETCP
- •MEMCOPY
- •NBITS
- •ON INTERRUPT
- •ON VALUE
- •OPEN
- •PEEK
- •POKE
- •POPALL
- •POWER
- •POWERDOWN
- •POWERSAVE
- •PRINTBIN
- •PSET
- •PS2MOUSEXY
- •PULSEIN
- •PULSEOUT
- •PUSHALL
- •RC5SEND
- •RC5SENDEXT
- •RC6SEND
- •READ
- •READEEPROM
- •READMAGCARD
- •RESET
- •RESTORE
- •RETURN
- •RIGHT
- •ROTATE
- •ROUND
- •RTRIM
- •SECELAPSED
- •SECOFDAY
- •SEEK
- •SELECT-CASE-END SELECT
- •SETFONT
- •SETTCP
- •SETTCPREGS
- •SENDSCAN
- •SENDSCANKBD
- •SERIN
- •SEROUT
- •SETIPPROTOCOL
- •SHIFT
- •SHIFTCURSOR
- •SHIFTIN
- •SHIFTOUT
- •SHIFTLCD
- •SHOWPIC
- •SHOWPICE
- •SINH
- •SOCKETCONNECT
- •SOCKETLISTEN
- •SOCKETSTAT
- •SONYSEND
- •SOUND
- •SPACE
- •SPIIN
- •SPIINIT
- •SPIMOVE
- •SPIOUT
- •SPLIT
- •START
- •STCHECK
- •STOP
- •STRING
- •SYSSEC
- •SYSSECELAPSED
- •SYSDAY
- •SWAP
- •TCPCHECKSUM
- •TCPREAD
- •TCPWRITE
- •TCPWRITESTR
- •TANH
- •THIRDLINE
- •TIME$
- •TIME
- •TOGGLE
- •TRIM
- •UCASE
- •UDPREAD
- •UDPWRITE
- •UDPWRITESTR
- •UPPERLINE
- •VARPTR
- •VERSION
- •WAIT
- •WAITKEY
- •WAITMS
- •WAITUS
- •WHILE-WEND
- •WRITE
- •WRITEEEPROM
- •X10DETECT
- •X10SEND
- •#IF ELSE ENDIF
- •International Resellers
- •International Resellers
- •ASM Libraries
- •EXTENDED I2C
- •MCSBYTE
- •MCSBYTEINT
- •TCPIP
- •LCD4BUSY
- •GLCD
- •GLCDSED
- •LCD-EPSON
- •AVR-DOS File System
- •CF Card
- •Compact FlashCard Driver
- •Elektor CF-Interface
- •XRAM CF-Interface for simulation
- •New CF-Card Drivers
- •Floating Point
- •FP_TRIG
- •DOUBLE
- •I2C SLAVE
- •I2CSLAVE
- •I2C TWI Slave
- •SPISLAVE
- •DATE TIME
- •EUROTIMEDATE
- •DATETIME
- •PS2-AT Mouse and Keyboard Emulation
- •AT_EMULATOR
- •PS2MOUSE_EMULATOR
- •BCCARD
- •BCCARD
- •BCDEF
- •BCCALL
- •BCRESET
- •Tools
- •LCD RGB-8 Converter
© MCS Electronics, 1995-2007
Local X As Byte
X |
= |
5 |
'assign local |
B |
= |
X |
|
I |
= |
-1 |
|
W |
= |
40000 |
|
L |
= |
20000 |
|
S = "test" |
|
||
End Sub |
|
|
|
Sub Testarray(byval A As Byte , B1 As Byte) |
'start sub |
||
Print A ; " " ; B1 |
'print passed |
||
variables |
'change value of |
||
B1 = 3 |
|||
element with index 1 |
'specify the index |
||
B1(1) = 3 |
|||
which does the same as the line above |
'modify other |
||
B1(3) = 3 |
element of array
'You can change A, but since a copy is passed to the SUB, 'the change will not reflect to the calling variable
End Sub
'notice the empty() to indicate that a string array is passed
Sub Teststr(b As Byte , Dl() As String) Dl(b) = Dl(b) + "add"
End Sub
DEFxxx
Action
Declares all variables that are not dimensioned of the DefXXX type.
Syntax
DEFBIT b |
Define BIT |
DEFBYTE c |
Define BYTE |
DEFINT I |
Define INTEGER |
DEFWORD x |
Define WORD |
DEFLNG l |
Define LONG |
DEFSNG s |
Define SINGLE |
DEFDBL z |
Define DOUBLE |
|
|
Remarks
While you can DIM each individual variable you use, you can also let the compiler handle it for you.
All variables that start with a certain letter will then be dimmed as the specified type.
Example
Defbit b : DefInt c 'default type for bit and integers
Set b1 |
'set bit to 1 |
page -452-
© MCS Electronics, 1995-2007
c = 10 |
'let c = 10 |
DEFLCDCHAR
Action
Define a custom LCD character.
Syntax
DEFLCDCHAR char,r1,r2,r3,r4,r5,r6,r7,r8
Remarks
char |
Constant representing the character (0-7). |
r1-r8 |
The row values for the character. |
|
|
You can use the LCD designer to build the characters.
It is important that a CLS follows the DEFLCDCHAR statement(s).
So make sure you use the DEFLCDCHAR before your CLS statement.
Special characters can be printed with the Chr() function.
LCD Text displays have a 64 byte memory that can be used to show your own custom characters. Each character uses 8 bytes as the character is an array from8x8 pixels. You can create a maximum of 8 characters this way. Or better said : you can show a maximum of 8 custom characters at the same time. You can redefine characters in your programbut with the previous mentioned restriction.
A custom character can be used to show characters that are not available in the LCDfont table. For example a Û.
You can also use custom characters to create a bar graph or a music note.
See also
Tools LCD designer
Partial Example
Deflcdchar 1 , 225 , 227 , 226 , 226 , 226 , 242 , 234 , 228 |
' replace ? |
|
with number (0-7) |
|
' replace ? |
Deflcdchar 0 , 240 , 224 , 224 , 255 , 254 , 252 , 248 , 240 |
||
with number (0-7) |
'select data RAM |
|
Cls |
Rem it is important that a CLS is following the deflcdchar statements because
it will set the controller back in datamode |
'print the special |
Lcd Chr(0) ; Chr(1) |
|
character |
|
DEG2RAD
Action
Converts an angle in to radians.
page -453-
© MCS Electronics, 1995-2007
Syntax
var = DEG2RAD( Source )
Remarks
Var |
A numeric variable that is assigned with the degrees of variable |
|
Source. |
Source |
The single or double variable to get the degrees of. |
|
|
All trig functions work with radians. Use deg2rad and rad2deg to convert between radians and angles.
See Also
RAD2DEG
Example
'----------------------------------------------------------------------------- |
|
-- |
: (c) 1995-2005, MCS Electronics |
'copyright |
|
'micro |
: Mega48 |
'suited for demo |
: yes |
'commercial addon needed |
: no |
'purpose |
: demonstrates DEG2RAD function |
'----------------------------------------------------------------------------- |
|
-- |
|
Dim S As Single |
|
S = 90 |
|
S = Deg2Rad(s) |
|
Print S |
|
S = Rad2deg(s) |
|
Print S |
|
End |
|
DELAY
Action
Delay program execution for a short time.
Syntax
DELAY
Remarks
Use DELAY to wait for a short time.
The delay time is ca. 1000 microseconds.
Interrupts that occur frequently and/or take a long time to process, will let the delay
page -454-
© MCS Electronics, 1995-2007
last longer.
When you need a very accurate delay, you need to use a timer.
See also
WAIT , WAITMS
Example
'----------------------------------------------------------------------------- |
|
------------ |
: delay.bas |
'name |
|
'copyright |
: (c) 1995-2005, MCS Electronics |
'purpose |
: demo: DELAY, WAIT, WAITMS |
'micro |
: Mega48 |
'suited for demo |
: yes |
'commercial addon needed |
: no |
'-----------------------------------------------------------------------------
------------
$regfile = "m48def.dat" |
' specify the used |
micro |
' used crystal |
$crystal = 4000000 |
|
frequency |
' use baud rate |
$baud = 19200 |
|
$hwstack = 32 |
' default use 32 |
for the hardware stack |
' default use 10 |
$swstack = 10 |
|
for the SW stack |
' default use 40 |
$framesize = 40 |
|
for the frame space |
|
Ddrb = &HFF |
'port B as output |
Portb = 255 |
|
Print "Starting" |
'lets wait for a |
Delay |
|
very short time |
|
Print "Now wait for 3 seconds" |
|
Portb = 0 |
|
Wait 3 |
|
Print "Ready" |
'wait 10 |
Waitms 10 |
|
milliseconds |
|
Portb = 255 |
|
End |
|
DIM
Action
Dimension a variable.
Syntax
DIM var AS [XRAM/SRAM/ERAM]type [AT location/variable] [OVERLAY]
Remarks
Var |
Any valid variable name such as b1, i or longname. var can also |
|
page -455- |
|
© MCS Electronics, 1995-2007 |
|
|
|
|
|
be an array : ar(10) for example. |
|
|
|
|
Type |
Bit, Byte, Word, Integer, Long, Single, Double or String |
|
XRAM |
Specify XRAM to store variable into external memory |
|
SRAM |
Specify SRAM to store variable into internal memory (default) |
|
ERAM |
Specify ERAM to store the variable into EEPROM |
|
OVERLAY |
Specify that the variable is overlaid in memory. |
|
location |
The address of name of the variable when OVERLAY is used. |
|
|
|
|
A string variable needs an additional length parameter:
Dim s As XRAM String * 10
In this case, the string can have a maximum length of 10 characters. Internally one additional byte is needed to store the end of string marker. Thus in the example above, 11 bytes will be used to store the string.
Note that BITS can only be stored in internal memory.
SCOPE
The scope for DIM is global. So no matter where you use the DIM statements, the variable will end up as a global visible variable that is visible in all modules, procedures and functions.
When you need a LOCAL variable that is local to the procedure or function, you can use LOCAL.
Since LOCAL variables are stored on the frame, it takes more code to dynamic generate and clean up these variables.
AT
The optional AT parameter lets you specify where in memory the variable must be stored. When the memory location already is occupied, the first free memory location will be used. You need to look in the report file to see where the variable is located in memory.
OVERLAY
The OVERLAY option will not use any variable space. It will create a sort of phantom variable:
Dim x as Long at $60 'long uses 60,61,62 and 63 hex of SRAM
Dim b1 as Byte at $60 OVERLAY
Dim b2 as Byte at $61 OVERLAY
B1 and B2 are no real variables! They refer to a place in memory. In this case to &H60 and &H61. By assigning the phantom variable B1, you will write to memory location &H60 that is used by variable X.
So to define it better, OVERLAY does create a normal usable variable, but it wil be stored at the sepcified memory location which could be already be occupied by another OVERLAY variable, or by a normal variable.
Take care with the OVERLAY option. Use it only when you understand it.
You can also read the content of B1: Print B1
This will print the content of memory location &H60.
By using a phantom variable you can manipulate the individual bytes of real variables.
page -456-
© MCS Electronics, 1995-2007
Another example
Dim L as Long at &H60
Dim W as Word at &H62 OVERLAY
W will now point to the upper two bytes of the long.
Using variable name instead of address
As variables can be moved though the program during development it is not always convenient to specify an address. You can also use the name of the variable :
DIM W as WORD
Dim B as BYTE AT W OVERLAY
Now B is located at the same address as variable W.
For XRAM variables, you need additional hardware : an external RAM and address decoder chip.
For ERAM variables, it is important to understand that these are not normal variables. ERAM variables serve as a way to simple read and write the EEPROM memory. You can use READEEPROM and WRITEEEPROM for that purpose too.
ERAM variables only can be assigned to SRAM variables, and ERAM variables can be assigned to SRAM variables. You can not use an ERAM variable as you would use a normal variable.
Dim b as byte, bx as ERAM byte
B= 1
Bx=b ' write to EEPROM
B=bx ' read from EEPROM
See Also
CONST , LOCAL
Example
'----------------------------------------------------------------------------- |
|
------------ |
: dim.bas |
'name |
|
'copyright |
: (c) 1995-2005, MCS Electronics |
'purpose |
: demo: DIM |
'micro |
: Mega48 |
'suited for demo |
: yes |
'commercial addon needed |
: no |
'-----------------------------------------------------------------------------
------------
$regfile = "m48def.dat" |
' specify the used |
micro |
' used crystal |
$crystal = 4000000 |
|
frequency |
' use baud rate |
$baud = 19200 |
|
$hwstack = 32 |
' default use 32 |
for the hardware stack |
' default use 10 |
$swstack = 10 |
|
for the SW stack |
|
|
page -457- |
© MCS Electronics, 1995-2007
$framesize = 40
for the frame space
Dim B1 As Bit
Dim A As Byte
0-255
Dim C As Integer from -32767 - +32768
Dim L As Long
Dim W As Word
Dim S As String * 11 to 11 characters
' default use 40
'bit can be 0 or 1 'byte range from
'integer range
'length can be up
'new feature : you can specify the address of the variable
Dim K As Integer At &H120
'the next dimensioned variable will be placed after variable s
Dim Kk As Integer
'Assign bits |
'or |
B1 = 1 |
|
Set B1 |
'use set |
'Assign bytes |
|
A = 12 |
|
A = A + 1 |
|
'Assign integer |
|
C = -12 |
|
C = C + 100 |
|
Print C |
|
W = 50000 |
|
Print W |
|
'Assign long |
|
L = 12345678 |
|
Print L |
|
'Assign string |
|
S = "Hello world" |
|
Print S |
|
End |
|
DIR
Action
Returns the filename that matches the specified filemask.
Syntax
sFile = DIR(mask)
sFile = DIR()
Remarks
SFile |
A string variable that is assigned with the filename. |
page -458-