SPI_eeprom_lezen_KOSTAL.ino


1
#define SLAVESELECT 10 // SS   
2
#define DATAOUT     11 // MOSI 
3
#define DATAIN      12 // MISO 
4
#define SPICLOCK    13 // SCK  
5
6
// How to wire the arduino to the M95160 eeprom:
7
// arduino pin - AVR pin - eeprom pin
8
//  10           SS        1
9
//  11           MOSI      5
10
//  12           MISO      2
11
//  13           SCK       6
12
//  VCC          VCC       3, 7, 8
13
//  GND          GND       4
14
//
15
16
//opcodes
17
#define WREN  6
18
#define WRDI  4
19
#define RDSR  5
20
#define WRSR  1
21
#define READ  3
22
#define WRITE 2
23
24
byte eeprom_output_data;
25
byte eeprom_input_data = 0;
26
byte clr;
27
int address = 0;
28
//data buffer
29
char buffer [128];
30
31
char spi_transfer( volatile char data ) {
32
    SPDR = data;                    // Start the transmission
33
    while( !( SPSR & ( 1 << SPIF ) ) );
34
    return SPDR;                    // return the received byte
35
}
36
37
void setup() {
38
    Serial.begin( 115200 );
39
    Serial.println( "Start" );
40
    pinMode( DATAOUT, OUTPUT );
41
    pinMode( DATAIN, INPUT );
42
    pinMode( SPICLOCK, OUTPUT );
43
    pinMode( SLAVESELECT, OUTPUT );
44
    digitalWrite( SLAVESELECT, HIGH ); //disable device
45
    SPCR = ( 1 << SPE ) | ( 1 << MSTR );
46
    // SPCR = 01010000
47
    //interrupt disabled,spi enabled,msb 1st,master,clk low when idle,
48
    //sample on leading edge of clk,system clock/4 rate (fastest)
49
    SPCR = ( 1 << SPE ) | ( 1 << MSTR );
50
    clr = SPSR;
51
    clr = SPDR;
52
    delay( 10 );
53
}
54
55
byte read_eeprom( int EEPROM_address ) {
56
    //READ EEPROM
57
    int data;
58
    digitalWrite( SLAVESELECT, LOW );
59
    spi_transfer( READ ); //transmit read opcode
60
    spi_transfer( ( char )( EEPROM_address >> 8 ) ); //send MSByte address first
61
    spi_transfer( ( char )( EEPROM_address ) ); //send LSByte address
62
    data = spi_transfer( 0xFF ); //get data byte
63
    digitalWrite( SLAVESELECT, HIGH ); //release chip, signal end transfer
64
    return data;
65
}
66
67
void loop() {
68
    char text[16];
69
    for( int i = 0; i < 2048; i++ ) {
70
        eeprom_output_data = read_eeprom( i );
71
        if( ( i % 16 ) == 0 ) {
72
            sprintf( text, "\r\n%04X 0x%02X,", i, eeprom_output_data );
73
        } else {
74
            sprintf( text, " 0x%02X,", eeprom_output_data );
75
        }
76
        Serial.print( text );
77
    }
78
    while( 1 );
79
}