© 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 |
|
© 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!
|
|
© MCS Electronics, 1995-2007 |
' |
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
IDLE
Action
Put the processor into the idle mode.
Syntax
IDLE
Remarks
In the idle mode, the system clock is removed from the CPU but not from the interrupt logic, the serial port or the timers/counters.
The idle mode is terminated either when an interrupt is received(from the watchdog, timers, external level triggered or ADC) or upon system reset through the RESET pin.
Most new chips have many options for Power down/Idle. It is advised to consult the data sheet to see if a better mode is available.
See also
POWERDOWN
Example
IDLE
IF-THEN-ELSE-END IF
Action
Allows conditional execution or branching, based on the evaluation of a Boolean expression.
Syntax
IF expression THEN
[ ELSEIF expression THEN ]
[ ELSE ]
END IF
© MCS Electronics, 1995-2007
Remarks
Expression Any expression that evaluates to true or false.
The one line version of IF can be used :
IF expression THEN statement [ ELSE statement ]
The use of [ELSE] is optional.
Tests like IF THEN can also be used with bits and bit indexes. IF var.bit = 1 THEN
^--- bit is a variable or numeric constant in the range from0-255
Dim Var As Byte, Idx As Byte
Var = 255
Idx = 1
If Var.idx = 1 Then
Print "Bit 1 is 1"
EndIf
See also
ELSE
Example
Dim A As Integer |
|
A = 10 |
'test expression |
If A = 10 Then |
Print "This part is executed." |
'this will be |
printed |
|
Else |
'this not |
Print "This will never be executed." |
End If |
|
If A = 10 Then Print "New in BASCOM" |
|
If A = 10 Then Goto Label1 Elseprint "A<>10" |
|
Label1: |
|
Rem The following example shows enhanced use of IF THEN |
'test for bit |
If A.15 = 1 Then |
Print "BIT 15 IS SET" |
|
EndIf
Rem the following example shows the 1 line use of IF THEN [ELSE]
If A.15 = 0 Then Print "BIT 15 is cleared" Else Print "BIT 15 is set"
INCR
Action
Increments a variable by one.
Syntax
INCR var
Remarks
Var Any numeric variable.
page -529-