Created
October 12, 2012 19:09
-
-
Save rafalw/3880907 to your computer and use it in GitHub Desktop.
Obsługa szeregowej pamięci EEPROM 24C04 - przykład wykorzystania magistrali I2C.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' Obsługa pamięci EEPROM 24c04 | |
' - przykład + efekty specjalne | |
$regfile = "attiny2313.dat" | |
$crystal = 4000000 | |
$hwstack = 40 | |
$swstack = 16 | |
$framesize = 32 | |
' Liczby wpisywane do pamięci w banku 0 i 1 (bit P0 adresu urządzenia) | |
Const Val_p0 = 220 | |
Const Val_p1 = 225 | |
' Konfiguracja wyświetlacza | |
Config Lcd = 16 * 1a | |
' Porty mikrokontrolera (dwie linie portu B), które będą emulować wyprowadzenia magistrali I2C (mikrokontroler to MASTER) | |
Config Scl = Portb.1 | |
Config Sda = Portb.0 | |
' Do linii portu D podłączone są diody kontrolne oraz przycisk uruchamiający test zapisu i odczytu | |
Config Portd = &B1110111 | |
Portd = &B0001000 | |
' Pamięć 24c04 (SLAVE): | |
' - wyprowadzenia A2 i A1 podłączone do masy, stąd adresy jak niżej. | |
' Pierwsze 256 bajtów - bank 1 (bit P0 = 0) | |
Const Addrw_p0 = 160 | |
Const Addrr_p0 = 161 | |
' Drugie 256 bajtów - bank 2 (bit P0 = 1) | |
Const Addrw_p1 = 162 | |
Const Addrr_p1 = 163 | |
Dim Adres As Byte , Value As Byte | |
Dim Addrw As Byte , Addrr As Byte | |
Dim Tst_val 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) | |
Declare Sub Error | |
Declare Sub Read_bank_test(byval Bank_nr As Byte) | |
Declare Sub Write_bank_test(byval Bank_nr As Byte) | |
Portd.5 = 0 ' dioda czerwona | |
Portd.4 = 0 ' dioda niebieska | |
Portd.3 = 1 ' "przycisk" uruchamiający proces zapisu/odczytu | |
Cursor Off | |
Cls | |
Lcd "2404 test ready" | |
Do | |
If Pind.3 = 0 Then | |
'Zapis pierwszego banku pamięci (256 bajtów o wartości stałej Val_p0) | |
Write_bank_test 0 | |
Waitms 100 | |
'Zapis drugiego banku pamięci (256 bajtów o wartości stałej Val_p1) | |
Write_bank_test 1 | |
Cls | |
' Przerwa - sygnalizacja zmiany trybu testu na odczyt | |
For Adres = 0 To 2 | |
Waitms 50 | |
Portd.4 = 1 | |
Portd.5 = 1 | |
Waitms 50 | |
Portd.4 = 0 | |
Portd.5 = 0 | |
Next Adres | |
'Odczyt pierwszego banku pamięci | |
Read_bank_test 0 | |
Waitms 100 | |
'Odczyt drugiego banku pamięci | |
Read_bank_test 1 | |
Cls | |
Lcd "2404 test OK!" | |
Waitms 500 | |
Cls | |
Lcd "2404 test ready" | |
End If | |
Loop | |
End | |
' Zapis bajtu do pamięci 24c04 | |
Sub Write_eeprom(byval Adres As Byte , Byval Value As Byte) | |
Portd.4 = 1 | |
I2cstart | |
I2cwbyte Addrw | |
I2cwbyte Adres | |
I2cwbyte Value | |
I2cstop | |
Waitms 5 | |
Portd.4 = 0 | |
End Sub | |
' Odczyt bajtu z pamięci 24c04 | |
Sub Read_eeprom(byval Adres As Byte , Value As Byte) | |
Portd.4 = 1 | |
I2cstart | |
I2cwbyte Addrw | |
I2cwbyte Adres | |
I2cstart | |
I2cwbyte Addrr | |
I2crbyte Value , Nack | |
I2cstop | |
Waitms 5 | |
Portd.4 = 0 | |
End Sub | |
' Sygnalizacja błędu | |
Sub Error | |
Cls | |
Lcd "R/W error" | |
Portd.5 = 1 | |
Waitms 50 | |
Portd.5 = 0 | |
End Sub | |
' Podprogram odczytu i weryfikacji zawartości wskazanego banku pamięci | |
Sub Read_bank_test(byval Bank_nr As Byte) | |
If Bank_nr = 0 Then | |
Addrr = Addrr_p0 | |
Addrw = Addrw_p0 | |
Tst_val = Val_p0 | |
Else | |
Addrr = Addrr_p1 | |
Addrw = Addrw_p1 | |
Tst_val = Val_p1 | |
End If | |
Cls | |
Lcd "P0=" + Bank_nr + ",R,val=" + Tst_val | |
For Adres = 0 To 255 | |
Read_eeprom Adres , Value | |
Waitms 5 | |
If Value <> Tst_val Or Err <> 0 Then | |
Error | |
Cls | |
Lcd "P0=" + Bank_nr + ",R,val=" + Tst_val | |
End If | |
Next Adres | |
End Sub | |
' Podprogram zapisu zawartości wskazanego banku pamięci | |
Sub Write_bank_test(byval Bank_nr As Byte) | |
If Bank_nr = 0 Then | |
Addrw = Addrw_p0 | |
Tst_val = Val_p0 | |
Else | |
Addrw = Addrw_p1 | |
Tst_val = Val_p1 | |
End If | |
Cls | |
Lcd "P0=" + Bank_nr + ",W,val=" + Tst_val | |
For Adres = 0 To 255 | |
Write_eeprom Adres , Tst_val | |
Waitms 5 | |
If Err <> 0 Then | |
Error | |
Cls | |
Lcd "P0=" + Bank_nr + ",W,val=" + Tst_val | |
End If | |
Next Adres | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment