Em chỉ mới biết dùng Bascom AVR thôi, mấy bữa nay em thử làm giao tiếp ATMEGA32 với con AT24C64 mà nó không chạy, có bác nào biết bascom chỉ giúp em. Đoạn code chương trình em lấy từ phần help, thay đổi lại cái địa chỉ của slave thôi mà không chạy :
$regfile = "m32def.dat"
$crystal = 1000000
Config Scl = Portc.0
Config Sda = Portc.1
Config Porta = Output
Porta = 0
I2cinit
Dim Value As Byte
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 = 416 'slave write address
Const Addressr = 417
Value = 2
Porta = Value 'slave read address
Wait 1
Call Write_eeprom(1 , Value)
Value = 0
Porta = 0
Wait 1
Call Read_eeprom(1 , Value)
Porta = Value
Waitms 150 'read it back
End
Sub Write_eeprom(byval Adres As Byte , Byval Value As Byte)
I2cstart 'start condition
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
Sub Read_eeprom(byval Adres As Byte , Value As Byte)
I2cstart 'generate start
I2cwbyte Addressw 'slave adsress
I2cwbyte Adres 'address of EEPROM
I2cstart 'repeated start
I2cwbyte Addressr 'slave address (read)
I2crbyte Value , Nack 'read byte
I2cstop 'generate stop
End Sub
$regfile = "m32def.dat"
$crystal = 1000000
Config Scl = Portc.0
Config Sda = Portc.1
Config Porta = Output
Porta = 0
I2cinit
Dim Value As Byte
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 = 416 'slave write address
Const Addressr = 417
Value = 2
Porta = Value 'slave read address
Wait 1
Call Write_eeprom(1 , Value)
Value = 0
Porta = 0
Wait 1
Call Read_eeprom(1 , Value)
Porta = Value
Waitms 150 'read it back
End
Sub Write_eeprom(byval Adres As Byte , Byval Value As Byte)
I2cstart 'start condition
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
Sub Read_eeprom(byval Adres As Byte , Value As Byte)
I2cstart 'generate start
I2cwbyte Addressw 'slave adsress
I2cwbyte Adres 'address of EEPROM
I2cstart 'repeated start
I2cwbyte Addressr 'slave address (read)
I2crbyte Value , Nack 'read byte
I2cstop 'generate stop
End Sub
Comment