© 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 = 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. |
The master has addressed the slave and will now continue to send data to the slave. You do not need to take action here.
|
|
|
© 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) |
|
© 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 |
|