Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
BASCOM AVR, help reference (2007).PDF
Скачиваний:
288
Добавлен:
12.08.2013
Размер:
17.02 Mб
Скачать

© 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-