' ***************************************************************************** ' * Title : EEPROM programmer.bas ' * Last Updated : 05.03.2006 ' * Target device: At90s2313, 24C32 ' * Author : www.avrprojects.net ' * Program code : BASCOM-AVR ' * Hardware req. : ' * Description : ' * This application reads and writes one page at a time to an 24c32 EEPROM connected to an ' * AT2313 microcontroller. ' ***************************************************************************** Dim D_w As Byte , D_r As Byte , Adress As Byte , E_adr As Byte , E_dat As Byte , I As Byte , Count As Byte Dim Rw As String * 1 , Datastr As String * 32 Dim E_data(16) As Byte Config Scl = Portb.0 'assign the SCl line to PORTD.0 Config Sda = Portb.1 'assign the SDA line to PORTD.1 'Declare Sub Byteread 'Declare Sub Bytewrite Declare Sub Pagewrite Declare Sub Pageread Main: Do Input "(R)ead page / (W)rite page?" , Rw If Rw = "R" Or Rw = "r" Then Goto Read_eeprom If Rw = "W" Or Rw = "w" Then Goto Write_eeprom Loop End 'end program Read_eeprom: Do Input "Read EEPROM page :" , Adress Print Adress ; ":" ; E_adr = Adress * 16 Call Pageread For I = 1 To 16 Print E_data(i) ; ","; Next I Print Loop Write_eeprom: Do Input "Write EEPROM page :" , Adress For I = 1 To 16 Input "Input byte ";i;" :" , E_data(i) Next I Print Adress ; ":" For I = 1 To 16 Print E_data(i) ; ","; Next I E_adr = Adress * 16 Call Pagewrite Print Loop '********** page write to EEPROM *************************************************** Sub Pagewrite I2cstart 'generate start I2cwbyte &B1010_0000 'send device address I2cwbyte &H00 'H adress of EEPROM I2cwbyte &H00 'L adress of EEPROM For I = 1 To 16 D_w = E_data(i) I2cwbyte D_w 'data to EEPROM Next Adress I2cstop 'stop condition Waitms 10 End Sub Pagewrite '********** page read from EEPROM ************************************************** Sub Pageread I2cstart 'generate start I2cwbyte &B1010_0000 'send device adsress I2cwbyte &H00 'H address of EEPROM I2cwbyte E_adr 'L address of EEPROM I2cstart 'repeated start I2cwbyte &B1010_0001 'slave address (read) For I = 1 To 16 I2crbyte D_r , Ack 'data to EEPROM E_data(i) = D_r Next I I2crbyte D_r , Nack 'read byte from EEPROM I2cstop End Sub Pageread 'generate stop '********** byte write to EEPROM *************************************************** 'Sub Bytewrite 'I2cstart 'generate start 'I2cwbyte &B1010_0000 'send device address 'I2cwbyte 0 'H adress of EEPROM 'I2cwbyte 0 'L adress of EEPROM 'I2cwbyte D_w 'data to EEPROM 'I2cstop 'stop condition 'Waitms 10 'End Sub '********** byte read from EEPROM ************************************************** 'Sub Byteread 'I2cstart 'generate start 'I2cwbyte &B1010_0000 'send device adsress 'I2cwbyte &H00 'H address of EEPROM 'I2cwbyte &H00 'L address of EEPROM 'I2cstart 'repeated start 'I2cwbyte &B1010_0001 'slave address (read) 'I2crbyte D_r , Nack 'read byte from EEPROM ' I2cstop 'generate stop 'End Sub Byteread