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