- •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
Example
Dim I As Integer , Z As Byte
I = &H1001
Z = High(i) ' is 10 hex or 16 dec
End
HIGHW
Action
Retrieves the most significant word of a long variable.
Syntax
var = HIGHW( s )
Remarks
Var |
The variable that is assigned with the MS word of var S. |
S |
The source variable to get the MSB from. |
|
|
There is no LowW() function. This because when you assign a Long to a word or nteger,i only the lower part is assigned. For this reason you do not need a Loww() function. W=L will do the same.
See also
LOW , HIGH
Example
Dim X As Word , L As Long
L = &H12345678
X = Highw(l)
Print Hex(x)
HOME
Action
Place the cursor at the specified line at location 1.
Syntax
HOME UPPER | LOWER | THIRD | FOURTH
Remarks
If only HOME is used than the cursor will be set to the upper line.
You may also specify the first letter of the line like: HOME U
page -522-
|
© MCS Electronics, 1995-2007 |
See also |
|
CLS , LOCATE |
|
For a complete example see LCD |
|
Partial Example |
'set cursor |
Locate 2 , 1 |
|
position |
'display this |
Lcd "*" |
|
Home Upper |
'select line 1 and |
return home |
|
I2CINIT
Action
Initializes the SCL and SDA pins.
Syntax
I2CINIT
Remarks
By default the SCL and SDA pins are in the right state when you reset the chip. Both the PORT and the DDR bits are set to 0 in that case.
When you need to change the DDR and/or PORT bits you can use I2CINIT to bring the pins in the proper state again.
ASM
The I2C routines are located in i2c.lib. _i2c_init is called.
See also
I2CSEND , I2CSTART , I2CSTOP , I2CRBYTE , I2CWBYTE , I2C_TWI Library for using TWI
Example
Config Sda = Portb.5
Config Scl = Portb.7
I2cinit
Dim X As Byte , Slave As Byte |
'reset variable |
X = 0 |
|
Slave = &H40 |
'slave address of |
a PCF 8574 I/O IC |
'get the value |
I2creceive Slave , X |
|
Print X |
'print it |
page -523-
© MCS Electronics, 1995-2007
I2CRECEIVE
Action
Receives data from an I2C serial slave device.
Syntax
I2CRECEIVE slave, var
I2CRECEIVE slave, var , b2W, b2R
Remarks
Slave |
A byte, Word/Integer variable or constant with the slave address |
|
from the I2C-device. |
Var |
A byte or integer/word variable that will receive the information |
|
from the I2C-device. |
b2W |
The number of bytes to write. |
|
Be cautious not to specify too many bytes! |
b2R |
The number of bytes to receive. |
|
Be cautious not to specify too many bytes! |
|
|
You must specify the base address of the slave chip because the read/write bit is set/reset by the software.
When an error occurs, the internal ERR variable will return 1. Otherwise it wil be set to 0.
ASM
The I2C routines are located in the i2c.lib/i2c.lbx files.
See also
I2CSEND, I2CSTART , I2CSTOP, I2CRBYTE , I2CWBYTE
Example
Config Sda = Portb.5 |
|
Config Scl = Portb.7 |
|
Dim X As Byte , Slave As Byte |
'reset variable |
X = 0 |
|
Slave = &H40 |
'slave address of |
a PCF 8574 I/O IC |
'get the value |
I2creceive Slave , X |
|
Print X |
'print it |
Dim Buf(10)as Byte |
|
Buf(1) = 1 : Buf(2) = 2 |
'send two bytes |
I2creceive Slave , Buf(1) , 2 , 1 |
|
and receive one byte |
'print the |
Print Buf(1) |
|
received byte |
|
End |
|
page -524-
© MCS Electronics, 1995-2007
I2CSEND
Action
Send data to an I2C-device.
Syntax
I2CSEND slave, var
I2CSEND slave, var , bytes
Remarks
Slave |
The slave address off the I2C-device. |
Var |
A byte, integer/word or numbers that holds the value, which will be, |
|
send to the I2C-device. |
Bytes |
The number of bytes to send. |
|
|
When an error occurs, the internal ERR variable will return 1. Otherwise it wil be set to 0.
ASM
The I2C routines are located in the i2c.lib/i2c.lbx files.
See also
I2CRECEIVE , I2CSTART, I2CSTOP , I2CRBYTE , I2CWBYTE
Example
Config Sda = Portb.5 |
|
Config Scl = Portb.7 |
|
Dim X As Byte , A As Byte , Bytes As Byte |
'assign variable |
X = 5 |
|
to 5 |
|
Dim Ax(10)as Byte |
'slave address of |
Const Slave = &H40 |
|
a PCF 8574 I/O IC |
'send the value or |
I2csend Slave , X |
|
For A = 1 To 10 |
'Fill dataspace |
Ax(a) = A |
|
Next |
|
Bytes = 10 |
|
I2csend Slave , Ax(1) , Bytes |
|
End |
|
I2START,I2CSTOP, I2CRBYTE, I2CWBYTE
Action
page -525-
© MCS Electronics, 1995-2007
I2CSTART generates an I2C start condition.
I2CSTOP generates an I2C stop condition.
I2CRBYTE receives one byte from an I2C-device.
I2CWBYTE sends one byte to an I2C-device.
Syntax
I2CSTART
I2CSTOP
I2CRBYTE var, ack/nack
I2CWBYTE val
Remarks
Var |
A variable that receives the value from the I2C-device. |
ack/nack |
Specify ACK if there are more bytes to read. |
|
Specify NACK if it is the last byte to read. |
Val |
A variable or constant to write to the I2C-device. |
|
|
These statements are provided as an addition to the I2CSEND and I2CRECEIVE statements. While I2CSEND and I2CRECEIVE are well suited for most tasks, a slave chip might need a special sequence that is not possible with the I2C routines.
When an error occurs, the internal ERR variable will return 1. Otherwise it wil be set to 0.
ASM
The I2C routines are located in the i2c.lib/i2c.lbx files.
See also
I2CSEND , I2CRECEIVE , I2CSTART , I2CSTOP, I2CRBYTE , I2CWBYTE
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 |
' 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 |
|
page -526-
© MCS Electronics, 1995-2007
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 |
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!
page -527-