- •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
Compare1a = W
W = Compare1a
'And the TIMER can be used in PWM mode
'You have the choice between 8,9 or 10 bit PWM mode
'Also you can specify if the counter must count UP or down after a match 'to the compare registers
'Note that there are two compare registers A and B
Config Timer1 = Pwm , Pwm = 8 , Compare A Pwm = Clear Up , Compare B Pwm = Clear Down , Prescale = 1
'to set the PWM registers, just assign a value to the compare A and B registers
Compare1a = 100
Compare1b = 200
'Or for better reading :
Pwm1a = 100
Pwm1b = 200
End
CONFIG TIMER2
Action
Configure TIMER2.
Syntax for the 8535
CONFIG TIMER2 = TIMER | PWM , ASYNC=ON |OFF,
PRESCALE = 1 | 8 | 32 | 64 | 128 | 256 | 1024 ,
COMPARE = CLEAR | SET | TOGGLE I DISCONNECT ,
PWM = ON | OFF ,
COMPARE PWM = CLEAR UP| CLEAR DOWN | DISCONNECT ,
CLEAR TIMER = 1|0
Syntax for the M103
CONFIG TIMER2 = COUNTER| TIMER | PWM ,
EDGE= FALLING |RISING,
PRESCALE = 1 | 8 | 64 | 256 | 1024 ,
COMPARE = CLEAR | SET | TOGGLE I DISCONNECT ,
PWM = ON | OFF ,
COMPARE PWM = CLEAR UP| CLEAR DOWN | DISCONNECT ,
CLEAR TIMER = 1|0
Remarks
The TIMER2 is an 8 bit counter.
It depends on the chip if it can work as a counter or not.
The syntax shown above must be on one line. Not all the options need to be selected.
Here is the effect of the various options.
EDGE |
You can select whether the TIMER will count on the falling or rising edge. |
|
Only for COUNTER mode. |
page -393-
© MCS Electronics, 1995-2007
PRESCALE |
The TIMER is connected to the system clock in this case. You can select |
|
the division of the system clock with this parameter. |
|
Valid values are 1 , 8, 64, 256 or 1024 |
|
or |
|
1 , 8, 32 , 64 , 256 or 1024 for the M103 |
|
|
The TIMER2 also has a compare registers
When the timer value matches a compare register, an action can be performed
COMPARE |
The action can be: |
|
SET will set the OC2 pin |
|
CLEAR will clear the OC2 pin |
|
TOGGLE will toggle the OC2 pin |
|
DISCONNECT will disconnect the TIMER from output pin OC2 |
|
|
And the TIMER can be used in 8 bit PWM mode
You can specify if the counter must count UP or down after a match to the compare registers
COMPARE PWM PWM compare mode.
Can be CLEAR UP or CLEAR DOWN
Example
Dim W As Byte
Config Timer2 = Timer , ASYNC = 1 , Prescale = 128
On TIMER2 Myisr
ENABLE INTERRUPTS
ENABLE TIMER2
DO
LOOP
MYISR:
'get here every second with a 32768 KHz xtal RETURN
'You can read or write to the timer with the COUNTER2 or TIMER2 variable W = Timer2
Timer2 = W
CONFIG TWI
Action
Configure the TWI (two wire serial interface).
page -394-
© MCS Electronics, 1995-2007
Syntax
CONFIG TWI = clockspeed
Remarks
clockspeed The desired clock frequency for SCL
CONFIG TWI will set TWSR prescaler bits 0 and 1, and TWBR depending on the used $CRYSTAL frequency and the desired SCL clockspeed.
Typical you need a speed of 400 KHz. Some devices will work on 100 KHz as well.
When TWI is used in SLAVE mode, you need to have a faster clock speed as the master.
It is important that you specify the proper crystal frequency. Otherwise it will result in a wrong TWI clock frequency.
See also
$CRYSTAL
Example
'-----------------------------------------------------------------------
' (c) 2004 MCS Electronics
' This demo shows an example of the TWI
' Not all AVR chips have TWI (hardware I2C) '------------------------------------------------------------------------
'The chip will work in TWI/I2C master mode 'Connected is a PCF8574A 8-bits port extender
$regfile="M8def.dat"' the used chip $crystal= 4000000 ' frequency used $baud= 19200 ' baud rate
$lib"i2c_twi.lbx"' we do not use software emulated I2C but the TWI
Config Scl =Portc.5 ' we need to provide the SCL pin name
Config Sda =Portc.4 ' we need to provide the SDA pin name
'On the Mega8, On the PCF8574A 'scl=PC5 , pin 28 pin 14 'sda=PC4 , pin 27 pin 15
I2cinit' we need to set the pins in the proper state
Config Twi = 100000 ' wanted clock frequency 'will set TWBR and TWSR
page -395-
© MCS Electronics, 1995-2007
'Twbr = 12 'bit rate register 'Twsr = 0 'pre scaler bits
Dim B AsByte, X AsByte
Print"TWI master"
Do
Incr B ' increase value
I2csend&B01110000 , B ' send the value
Print"Error : ";Err' show error status
I2creceive&B01110000 , X ' get a byte
Print X ;" ";Err' show error
Waitms 500 'wait a bit
Loop
End
CONFIG TWISLAVE
Action
Configure the TWI Slave address and bitrate
Syntax
CONFIG TWISLAVE = address , BTR = value , BITRATE = value SAVE=option
Remarks
Address |
The slave address that is assigned to the slave chip. This must be an Even |
|
number. The address 0 is the general call address. |
BTR |
Bytes to receive. With this constant you specify how many bytes will be |
|
expected when the slave receives bytes. |
Bitrate |
This is the I2C/TWI clock frequency. Most chips support 400 KHz (400000) |
|
but all I2C chips support 100000. |
SAVE
SAVE = NOSAVE : this can be used when you do not change a lot of registers in the interrupt.
SAVE : SAVE : this is best to be used when you do not use ASM in the TWI interrupt. See the explanation below.
The variables Twi , Twi_btr and Twi_btw are created by the compiler. These are all bytes The TWI interrupt is enabled but you need to enabled the global interrupt
The TWI Slave code is running as an interrupt process. Each time there is a TWI interrupt some slave code is executed. Your BASIC code is called from the low level slave code under a number of events. You must include all these labels in your Slave application. You do not need to write code in all these sub routines.
Label |
|
Event |
|
|
|
Twi_stop_rstart_received |
|
The Master sent a stop(i2CSTOP) or repeated start. |
|
|
Typical you do not need to do anything here. |
Twi_addressed_goread
The master has addressed the slave and will now continue to send data to the slave. You do not need to take action here.
page -396-
|
|
|
© MCS Electronics, 1995-2007 |
||
|
|
|
|
|
|
|
Twi_addressed_gowrite |
|
The master has addressed the slave and will now continue |
|
|
|
|
|
to receive data from the slave. You do not need to take |
|
|
|
|
|
action here. |
|
|
|
|
|
|
|
|
|
Twi_gotdata |
|
The master has sent data. The variable TWI holds the |
|
|
|
|
|
received value. The byte TWI_BTW is an index that holds |
|
|
|
|
|
the value of the number of received bytes. The first |
|
|
|
|
|
received byte will have an index value of 1. |
|
|
|
|
|
|
|
|
|
Twi_master_needs_byte |
|
The master reads from the slave and needs a value. The |
|
|
|
|
|
variable TWI_BTR can be inspected to see which index |
|
|
|
|
|
byte was needed. With the CONFIG BTR, you specify how |
|
|
|
|
|
many bytes the master will read. |
|
|
|
|
|
|
|
|
The TWI Slave code will save all used registers. But as it will call your BASIC application as the TWI interrupt occurs, your BASIC code could be in the middle of a PRINT statements. When you then execute another PRINT statement , you will destroy registers.
So keep the code in the sub routines to a minimum, and use SAVE option to save all registers.
While two printing commands will give odd results (print 12345 and 456 in the middle of the first print will give 1234545) at least no register is destroyed.
See also
CONFIG TWI
ASM
NONE
Example
'-------------------------------------------------------------------------------
' (c) 1995-2006 MCS Electronics
'This demo shows an example of the TWI in SLAVE mode
' |
Not all AVR chips have TWI (hardware I2C) |
'This demo is a Mega8 I2C A/D converter slave chip
'NOTICE that this demo will only work with the TWI slave library which is avaialble as an add on
'SDA pin is PORTC.4 (pin 27)
'SCL pin is PORTC.5 (pin 28)
'------------------------------------------------------------------------------- |
' the chip we use |
$regfile= "M8def.dat" |
|
$crystal= 8000000 |
' crystal oscillator value |
$baud = 19200 |
' baud rate |
$lib"i2c_twi-slave.lbx" |
|
Print"MCS Electronics M8 TWI-slave demo"
Print"Use with M8-TWI master demo"
Config Adc = Single ,Prescaler = Auto 'Now give power to the chip
StartAdc
Dim W As Word
Config Portb= Output
Dim Status As Byte |
'only for debug |
'Print Hex(status) |
|
page -397-
© MCS Electronics, 1995-2007
Config Twislave = &H70 ,Btr= 2,Bitrate= 100000
'^---slaveaddress
' |
^---------- |
2bytestoreceive |
' |
|
^---bitrateis100KHz |
'The variables |
Twi , Twi_btr and Twi_btw are created by the compiler. These are all bytes |
'The TWI interrupt is enabled but you need to enabled the global interrupt
EnableInterrupts
'this is just an empty loop but you could perform other tasks there
Do
'Print Getadc(0) 'Waitms 500 nop
Loop
End
'The following labels are called from the library. You need to insert code in these subroutines 'Notice that the PRINT commands are remarked.
'You can unmark them and see what happens, but it will result in occasional errors in the transmission 'The idea is that you write your code in the called labels. And this code must execute in as little time 'as possible. So when you slave must read the A/D converter, you can best do it in the main program
'then the data is available when the master needs it, and you do not need to do the conversion which cost time.
'A master can send or receive bytes.
'A master protocol can also send some bytes, then receive some bytes 'The master and slave must match.
'the following labels are called from the library when master send stop or start Twi_stop_rstart_received:
' Print "Master sent stop or repeated start"
Return
'master sent our slave address and will not send data Twi_addressed_goread:
' Print "We were addressed and master will send data"
Return
Twi_addressed_gowrite:
' Print "We were addressed and master will read data"
Return
'this label is called when the master sends data and the slave has received the byte 'the variable TWI holds the received value
Twi_gotdata:
' Print "received : " ; Twi ; " byte no : " ; Twi_btw
Select Case Twi_btw |
'firstbyte |
Case 1:Portb= Twi |
|
Case 2: |
'you can set another port here for example |
End Select |
|
Return |
|
page -398-