
- •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
Action
Configures the delay time for the DEBOUNCE statement.
Syntax
CONFIG DEBOUNCE = time
Remarks
Time |
A numeric constant which specifies the delay time in mS. |
|
|
When debounce time is not configured, 25 mS will be used as a default.
See also
DEBOUNCE
Example
'----------------------------------------------------------------------------- |
|
------------ |
: deboun.bas |
'name |
|
'copyright |
: (c) 1995-2005, MCS Electronics |
'purpose |
: demonstrates DEBOUNCE |
'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 |
|
Config Debounce = 30 |
'when the config |
statement is not used a default of 25mS will be used |
|
'Debounce Pind.0 , 1 , Pr 'try this for branching when high(1)
Debounce Pind.0 , |
0 |
, Pr , Sub |
|||
Debounce Pind.0 , |
0 |
, Pr , Sub |
|||
' |
|
^ |
^----- |
label to branch to |
|
' |
^ |
|
Branch when P1.0 goes low(0) |
||
' |
|
|
|
Examine P1.0 |
'When Pind.0 goes low jump to subroutine Pr 'Pind.0 must go high again before it jumps again 'to the label Pr when Pind.0 is low
Debounce |
Pind.0 |
, |
1 |
, |
Pr |
'no branch |
Debounce |
Pind.0 |
, |
1 |
, |
Pr |
'will result in a |
return without gosub
End
page -338-

© MCS Electronics, 1995-2007
Pr:
Print "PIND.0 was/is low"
Return
CONFIG I2CDELAY
Action
Compiler directive that overrides the internal I2C delay routine.
Syntax
CONFIG I2CDELAY = value
Remarks
value |
A numeric value in the range from 1 to 255. |
|
A higher value means a slower I2C clock. |
|
|
For the I2C routines the clock rate is calculated depending on the used crystal. In order to make it work for all I2C devices the slow mode is used. When you have faster I2C devices you can specify a low value.
By default a value of 5 is used. This will give a 200 kHZ clock.
When you specify 10, 10 uS will be used resulting in a 100 KHz clock.
When you use a very low crystal frequency, it is not possible to work with high clock frequencies.
ASM
The I2C routines are located in the i2c.lib/i2c.lbx files.
For chips that have hardware TWI, you can use the MasterTWI lib.
See also
CONFIG SCL , CONFIG SDA
Example
'----------------------------------------------------------------------------- |
|
------------ |
: i2c.bas |
'name |
|
'copyright |
: (c) 1995-2005, MCS Electronics |
'purpose |
: demo: I2CSEND and I2CRECEIVE |
'micro |
: Mega48 |
'suited for demo |
: yes |
'commercial addon needed |
: no |
'-----------------------------------------------------------------------------
------------
$regfile = "m48def.dat" |
' specify the used |
micro |
' used crystal |
$crystal = 4000000 |
|
frequency |
|
page -339-
|
© MCS Electronics, 1995-2007 |
$baud = 19200 |
' use baud rate |
$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 |
|
Config Scl = Portb.4 |
|
Config Sda = Portb.5 |
|
Declare Sub Write_eeprom(byval Adres As Byte , Byval Value As Byte) Declare Sub Read_eeprom(byval Adres As Byte , Value As Byte)
Const Addressw = 174 |
'slave write |
address |
'slave read |
Const Addressr = 175 |
|
address |
|
Dim B1 As Byte , Adres As Byte , Value As Byte |
'dim byte |
Call Write_eeprom(1 , 3) |
'write value of |
three to address 1 of EEPROM |
|
Call Read_eeprom(1 , Value) : Print Value |
'read it back |
Call Read_eeprom(5 , Value) : Print Value |
'again for address |
5 |
|
'-------- now write to a PCF8474 I/O expander ------- |
'all outputs high |
I2csend &H40 , 255 |
|
I2creceive &H40 , B1 |
'retrieve input |
Print "Received data " ; B1 |
'print it |
End |
|
Rem Note That The Slaveaddress Is Adjusted Automaticly With I2csend & I2creceive
Rem This Means You Can Specify The Baseaddress Of The Chip.
'sample of writing a byte to EEPROM AT2404 |
|
Sub Write_eeprom(byval Adres As Byte , Byval Value As Byte) |
'start condition |
I2cstart |
|
I2cwbyte Addressw |
'slave address |
I2cwbyte Adres |
'asdress of EEPROM |
I2cwbyte Value |
'value to write |
I2cstop |
'stop condition |
Waitms 10 |
'wait for 10 |
milliseconds |
|
End Sub |
|
'sample of reading a byte from EEPROM AT2404 |
|
Sub Read_eeprom(byval Adres As Byte , Value As Byte) |
'generate start |
I2cstart |
|
I2cwbyte Addressw |
'slave adsress |
I2cwbyte Adres |
'address of EEPROM |
I2cstart |
'repeated start |
I2cwbyte Addressr |
'slave address |
(read) |
'read byte |
I2crbyte Value , Nack |
|
I2cstop |
'generate stop |
page -340-

© MCS Electronics, 1995-2007
End Sub
'when you want to control a chip with a larger memory like the 24c64 it requires an additional byte
'to be sent (consult the datasheet):
'Wires from the I2C address that are not connected will default to 0 in most cases!
' |
I2cstart |
'start condition |
' |
I2cwbyte &B1010_0000 |
'slave address |
' |
I2cwbyte H |
'high address |
' |
I2cwbyte L |
'low address |
' |
I2cwbyte Value |
'value to write |
' |
I2cstop |
'stop condition |
'Waitms 10
CONFIG I2CSLAVE
Action
Configures the I2C slave mode.
Syntax
CONFIG I2CSLAVE = address , INT = interrupt , TIMER = tmr
Remarks
Address |
The slave address you want to assign to the I2C slave chip. This is |
|
an address that must be even like 60. So 61 cannot be used. |
Interrupt |
The interrupt that must be used. This is INT0 by default. |
Tmr |
The timer that must be used. This is TIMER0 by default. |
|
|
While the interrupt can be specified, you need to change the library code when you use a non-default interrupt. For example when you like to use INT1 instead of the default INT0.
The same applies to the TIMER. You need to change the library when you like to use another timer.
See Also
CONFIG TWI
Example
'----------------------------------------------------------------------------- |
|
------------ |
: i2c_pcf8574.bas |
'name |
|
'copyright |
: (c) 1995-2005, MCS Electronics |
'purpose |
: shows how you could use the I2C slave library to |
create a PCF8574 |
: AT90S2313 |
'micro |
|
'suited for demo |
: NO, ADDON NEEDED |
'commercial addon needed |
: yes |
'-----------------------------------------------------------------------------
------------
$regfile = "2313def.dat" |
' specify the used |
|
page -341- |
|
© MCS Electronics, 1995-2007 |
micro |
' used crystal |
$crystal = 3684000 |
|
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 |
|
'This program shows how you could use the I2C slave library to create a PCF8574
'The PCF8574 is an IO extender chip that has 8 pins.
'The pins can be set to a logic level by writing the address followed by a value
'In order to read from the pins you need to make them '1' first
'This program uses a AT90S2313, PORTB is used as the PCF8574 PORT 'The slave library needs INT0 and TIMER0 in order to work.
'SCL is PORTD.4 (T0) 'SDA is PORTD.2 (INT0)
'Use 10K pull up resistors for both SCL and SDA
'The Slave library will only work for chips that have T0 and INT0 connected to the same PORT.
'These chips are : 2313,2323, 2333,2343,4433,tiny22, tiny12,tiny15, M8 'The other chips have build in hardware I2C(slave) support.
'specify the slave address. This is &H40 for the PCF8574
'You always need to specify the address used for write. In this case &H40 ,
'The config i2cslave command will enable the global interrupt enable flag ! Config I2cslave = &B01000000 ' same as &H40 'Config I2cslave = &H40 , Int = Int0 , Timer = Timer0
'A byte named _i2c_slave_address_received is generated by the compiler. 'This byte will hold the received address.
'A byte named _i2c_slave_address is generated by the compiler. 'This byte must be assigned with the slave address of your choice
'the following constants will be created that are used by the slave library:
'_i2c_pinmask = &H14
'_i2c_slave_port = Portd
'_i2c_slave_pin = Pind
'_i2c_slave_ddr = Ddrd
'_i2c_slave_scl = 4
'_i2c_slave_sda = 2
'These values are adjusted automatic depending on the selected chip. 'You do not need to worry about it, only provided as additional info
'by default the PCF8574 port is set to input |
|
Config Portb = Input |
'all pins high by |
Portb = 255 |
|
default |
|
'DIM a byte that is not needed but shows how you can store/write the I2C DATA
Dim Bfake As Byte
'empty loop
Do
' you could put your other program code here
page -342-

© MCS Electronics, 1995-2007
'In any case, do not use END since it will disable interrupts
Loop
'here you can write your other program code
'But do not forget, do not use END. Use STOP when needed
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
' The following labels are called from the slave library '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
'When the master wants to read a byte, the following label is allways called 'You must put the data you want to send to the master in variable _a1 which is register R16
I2c_master_needs_data:
'when your code is short, you need to put in a waitms statement
'Take in mind that during this routine, a wait state is active and the
master will wait |
|
'After the return, the waitstate is ended |
' make it an input |
Config Portb = Input |
|
_a1 = Pinb |
' Get input from |
portB and assign it |
|
Return |
|
'When the master writes a byte, the following label is always called 'It is your task to retrieve variable _A1 and do something with it
'_A1 is register R16 that could be destroyed/altered by BASIC statements 'For that reason it is important that you first save this variable
I2c_master_has_data:
'when your code is short, you need to put in a waitms statement
'Take in mind that during this routine, a wait state is active and the master will wait
'After the return, the waitstate is ended
Bfake = _a1 |
' this is not |
needed but it shows how you can store _A1 in a byte |
|
'after you have stored the received data into bFake, you can alter R16 |
|
Config Portb = Output |
' make it an |
output since it could be an input |
'assign _A1 (R16) |
Portb = _a1 |
|
Return |
|
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
'You could simply extend this sample so it will use 3 pins of PORT D for the address selection
'For example portD.1 , portd.2 and portD.3 could be used for the address selection
'Then after the CONFIG I2CSLAVE = &H40 statement, you can put code like:
'Dim switches as Byte |
' |
dim |
byte |
'switches = PIND |
' |
get |
dip switch value |
'switches = switches and &H1110 ' we only need the lower nibble without the LS bit
'_i2c_slave_address = &H40 + switches ' set the proper address
page -343-