Forum: Mikrocontroller und Digitale Elektronik SPI EEPROM 25AA256


von Teplotaxl X. (t3plot4x1)


Lesenswert?

Um einen Spi-EEPROM (25AA265) mit einem Atmega32 anzusteuern verwende 
ich folgenden Code
1
#include "base.h"
2
#include "uart.h"
3
void SPI_MasterInit(void) {
4
5
    DDRB = (1<<PB4)|(1<<PB5)|(1<<PB7);      // set PB4(SS), PB5 (MOSI) and PB7 (SCK) output, all others input
6
7
    SPCR = (1<<SPE)|(1<<MSTR);           // enable SPI, Master, set clock rate fck/4  
8
  
9
    PORTB |= (1 << PB7)|(1 << PB4);
10
}
11
12
unsigned char spi(unsigned char data)
13
{
14
    SPDR = data;
15
    loop_until_bit_is_set(SPSR, SPIF);
16
 
17
    return SPDR;
18
}
19
20
int main(void) {
21
  uart_init();
22
  SPI_MasterInit();
23
  spi(0b00000010);
24
  spi(0x00);
25
  spi(0x00);
26
  spi(0x83);
27
  while(1) {
28
  spi(0b00000011);
29
  spi(0x00);
30
  spi(0x00);
31
  spi(0xff);
32
  }
33
}

Gesendt wird auch, nur die SO Leitung des EEPROMS bleibt ständig low :(

/CS: Wird vor dem Programmieren manuell (Draht umstöpseln) auf high 
gelegt, danach wieder auf low.
SO: An MISO
/WP: Auf high
Vss:GND
VCC: 5V
/HOLD: Auf high
SCK: An SCK
SI: An MOSI

von holger (Gast)


Lesenswert?

>/CS: Wird vor dem Programmieren manuell (Draht umstöpseln) auf high
>gelegt, danach wieder auf low.

A Write Instruction requires the following sequence. After
the CS line is pulled low to select the device, the WRITE
op-code is transmitted via the SI line followed by the byte
address and the data (D7 - D0) to be programmed (Refer to
Table 6). Programming will start after the CS pin is brought
high. (The LOW-to-High transition of the CS pin must occur
during the SCK low time immediately after clocking in the
D0 (LSB) data bit.

von Teplotaxl X. (t3plot4x1)


Lesenswert?

Der Code schaut jetzt so aus und es will immer noch nicht so recht
1
#include "base.h"
2
#include "uart.h"
3
void SPI_MasterInit(void) {
4
5
    DDRB = (1<<PB4)|(1<<PB5)|(1<<PB7);      // set PB4(SS), PB5 (MOSI) and PB7 (SCK) output, all others input
6
7
    SPCR = (1<<SPE)|(1<<MSTR);           // enable SPI, Master, set clock rate fck/4  
8
  
9
    PORTB |= (1 << PB7)|(1 << PB4);
10
}
11
12
unsigned char spi(unsigned char data)
13
{
14
    SPDR = data;
15
    loop_until_bit_is_set(SPSR, SPIF);
16
 
17
    return SPDR;
18
}
19
20
void select(void) {
21
  PORTB &= ~(1<<PB4);
22
}
23
24
void unselect(void) {
25
  PORTB |= (1<<PB4);
26
}  
27
28
int main(void) {
29
  uart_init();
30
  SPI_MasterInit();
31
  select();
32
  spi(0b00000010);
33
  spi(0x01);
34
  spi(0x00);
35
  spi(0b00000010);
36
  unselect();
37
  while(1) {
38
  select();
39
  spi(0b00000011);
40
  spi(0x01);
41
  spi(0x00);
42
  spi(0xff);
43
  unselect();
44
  }
45
}
Mache ich immer noch was falsch oder ist etwas anderes faul?
PS: bei Adresse 0 bekomme ich 01111100

von holger (Gast)


Lesenswert?

WRITE SEQUENCE (WRITE): In order to program the
AT25128/256, two separate instructions must be executed.
First, the device must be write enabled via the Write
Enable (WREN) Instruction. Then a Write (WRITE) Instruction
may be executed.

von Teplotaxl X. (t3plot4x1)


Lesenswert?

Es geht =]
Der Quelltext
1
void spieep_write(unsigned char a0, unsigned char a1, unsigned char byte) {
2
  select();
3
  spi(0b00000100); //WRDI
4
  unselect();
5
  select();
6
  spi(0b00000110); //WREN
7
  unselect();
8
  select();
9
  spi(0b00000010); //WRITE
10
  spi(a0);
11
  spi(a1);
12
  spi(byte);
13
  unselect();
14
}
15
16
unsigned char spieep_read(unsigned char a0, unsigned char a1) {
17
  unsigned char byte;
18
  select();
19
  spi(0b00000100); //WRDI
20
  unselect();
21
  select();
22
  spi(0b00000011); //READ
23
  spi(a0);
24
  spi(a1);
25
  byte = spi(0xff);
26
  unselect();
27
  return byte;
28
}

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.