- •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
for the frame space |
|
Dim V As Byte , B1 As Byte |
|
Dim C As Integer , D As Byte |
|
Dim S As String * 15 |
|
Input "Use this to ask a question " , V |
'leave out for no |
Input B1 |
|
question |
|
Input "Enter integer " , C |
|
Print C |
|
Inputhex "Enter hex number (4 bytes) " , C |
|
Print C |
|
Inputhex "Enter hex byte (2 bytes) " , D |
|
Print D |
|
Input "More variables " , C , D |
|
Print C ; " " ; D |
|
Input C Noecho |
'supress echo |
Input "Enter your name " , S |
|
Print "Hello " ; S |
|
Input S Noecho |
'without echo |
Print S |
|
End |
|
ELSE
Action
Executed if the IF-THEN expression is false.
Syntax
ELSE
Remarks
You don't have to use the ELSE statement in an IF THEN .. END IF structure. You can use the ELSEIF statement to test for another condition.
IF a = 1 THEN
...
ELSEIF a = 2 THEN
..
ELSEIF b1 > a THEN
...
ELSE
...
END IF
See also
page -476-
© MCS Electronics, 1995-2007
IF , END IF , SELECT-CASE
Example
'----------------------------------------------------------------------------- |
|
------------ |
: if_then.bas |
'name |
|
'copyright |
: (c) 1995-2005, MCS Electronics |
'purpose |
: demo: IF, THEN, ELSE |
'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 |
|
Dim A As Byte , B1 As Byte |
|
Input "Number " , A |
'ask for number |
If A = 1 Then |
'test number |
Print "You got it!" |
|
End If |
|
If A = 0 Then |
'test again |
Print "Wrong" |
'thats wrong |
Else |
'print this if a |
is not 0 |
|
Print "Almost?" |
|
End If |
|
Rem You Can Nest If Then Statements Like This |
|
B1 = 0 |
|
If A = 1 Then |
|
If B1 = 0 Then |
|
Print "B1=0" |
|
End If |
|
Else |
|
Print "A is not 0" |
|
End If |
|
Input "Number " , A |
' |
If A = 1 Then |
|
Print "Ok" |
'use elseif for |
Elseif A = 2 Then |
|
more tests |
|
Print "2" : A = 3 |
|
Elseif A = 3 Then |
|
Print "3" |
|
End If |
|
If A.1 = 1 Then Print "Bit 1 set" |
'test for a bit |
End |
|
page -477-
© MCS Electronics, 1995-2007
ENABLE
Action
Enable specified interrupt.
Syntax
ENABLE interrupt
Remarks
Interrupt |
Description |
INT0 |
External Interrupt 0 |
INT1 |
External Interrupt 1 |
OVF0,TIMER0, |
TIMER0 overflow interrupt |
COUNTER0 |
|
OVF1,TIMER1, |
TIMER1 overflow interrupt |
COUNTER1 |
|
CAPTURE1, ICP1 |
INPUT CAPTURE TIMER1 interrupt |
COMPARE1A,OC1A or |
TIMER1 OUTPUT COMPARE A interrupt |
COMPARE1, OC1 |
In case of only one compare interrupt |
COMPARE1B,OC1B |
TIMER1 OUTPUT COMPARE B interrupt |
SPI |
SPI interrupt |
URXC |
Serial RX complete interrupt |
UDRE |
Serial data register empty interrupt |
UTXC |
Serial TX complete interrupt |
SERIAL |
Disables URXC, UDRE and UTXC |
ACI |
Analog comparator interrupt |
ADC |
A/D converter interrupt |
|
|
By default all interrupts are disabled.
To enable the enabling and disabling of interrupts use ENABLE INTERRUPTS.
Other chips might have additional interrupt sources such as INT2, INT3 etc.
See also
DISABLE
Partial Example
Enable Interrupts |
'allow interrupts to be set |
Enable Timer1 |
'enables the TIMER1 interrupt |
ENCODER
page -478-
© MCS Electronics, 1995-2007
Action
Reads pulses from a rotary encoder.
Syntax
Var = ENCODER( pin1, pin2, LeftLabel, RightLabel , wait)
Remarks
Var |
The target variable that is assigned with the result |
Pin1 and pin2 |
These are the names of the PIN registers to which the output of the |
|
encoder is connected. Both pins must be on the same PIN register. So |
|
Pinb.0 and Pinb.7 is valid while PinB.0 and PinA.0 is not. |
LeftLabel |
The name of the label that will be called/executed when a transition |
|
to the left is encoded. |
RightLabel |
The name of the label that will be called/executed when a transition |
|
to the right is encountered. |
wait |
A value of 0 will only check for a rotation/pulse. While a value of 1 |
|
will wait until a user actual turns the encoder. A value of 1 will thus |
|
halt your program. |
|
|
There are some conditions you need to fulfill :
The label that is called by the encoder must be terminated by a RETURN statement.
The pin must work in the input mode. By default all pins work in input mode.
The pull up resistors must be activated by writing a logic 1 to the port registers as the examples shows.
Rotary encoders come in many flavors. Some encoders also have a build in switch.
A sample of an encoder
page -479-
© MCS Electronics, 1995-2007
Since the microprocessor has internal pull up resistors, you do not need externalpull up resistors for most encoders.
Example
'----------------------------------------------------------------------------- |
|
------------ |
: encoder.bas |
'name |
|
'copyright |
: (c) 1995-2005, MCS Electronics |
'purpose |
: demonstration of encoder function |
'micro |
: Mega128 |
'suited for demo |
: yes |
'commercial addon needed |
: no |
'An encoder has 2 outputs |
and a ground |
'We connect the outputs to pinb.0 and pinb.1
'You may choose different pins as long as they are at the same PORT 'The pins must be configured to work as input pins
'This function works for all PIN registers '-----------------------------------------------------------------------------
------------
$regfile = "m128def.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 |
|
Print "Encoder test"
Dim B As Byte
'we have dimmed a byte because we need to maintain the state of the encoder
Portb = &B11 |
|
|
|
' activate pull up |
registers |
|
|
|
|
Do |
|
|
|
|
B = Encoder(pinb.0 , Pinb.1 , Links , Rechts , 1) |
1 means wait for |
|||
' |
|
|
^--- |
|
change which blocks programflow |
^ |
labels which are |
||
' |
|
^-------- |
||
called |
^ |
^ |
|
port PINs |
' |
|
|||
Print B |
|
|
|
|
Waitms 10 |
|
|
|
|
Loop |
|
|
|
|
End |
|
|
|
|
'so while you can choose PINB0 and PINB7,they must be both member of PINB 'this works on all PIN registers
Links:
Print "left rotation"
Return
Rechts:
Print "right rotation"
Return
page -480-
© MCS Electronics, 1995-2007
End
END
Action
Terminate program execution.
Syntax
END
Remarks
STOP can also be used to terminate a program.
When an END statement is encountered, all interrupts are disabled and a never-ending loop is generated.
When a STOP is encountered the interrupts will not be disabled. Only a never ending loop will be created.
In an embedded application you probably do not want to end the application. But there are cases where you do want to end the application. For example when you controlsome motors, and you determine a failure, you do not want to use a Watchdog reset because then the failure will occur again. In that case you want to display an error, and wait for service personal to fix the failure.
It is important to notice that without the END statement, your programcan behave strange in certain cases. For example :
Print "Hello"
Note that there is no END statement. So what will happen? The program will print "Hello". But as the compiler places the library code behind the program code, the micro will execute the library code ! But without being called. As most library code are assembler sub routines that end with a RET, your program will most likely crash, or reset and repeat for ever.
See also
STOP
Example
Print "Hello" |
'print this |
End |
'end program execution and disable all interrupts |
EOF
Action
Returns the End of File Status.
Syntax
page -481-
