|
COMP_E
Site Admin
Joined: Sat Jan 24, 2009 3:56 pm Posts: 32
|
 Using SPI EEPROM 25AA512 On An HCS08 Microcontroller
Most things that use SPI are rather straight forward, a simple MISO, MOSI, and Slave Select and you’re good to go. That was not the case in my pursuit to get this EEPROM to work. It is incredibly finicky and took about a month hooked up to a logic analyzer and multiple revisions to the way instructions interact, before I was finally able to get it up in running. I hope this code works for you. The particular chip came from Microchip and you can find the data sheet easily enough by searching 25AA512. I am including the .c and .h files here. You will be able to view the entire project along with the .mcp file for which I used the memory end such. I’m not ready to post that project yet but it will have ‘SWIM’ somewhere in the topic title.
As a side note, make sure that Vcc on the EEPROM is tied to the Vcc of the microcontroller otherwise you will find as I did, that odd bits and data come back on reads instead of what you want!!
You will notice that slave select pin is not used. Because I only used this one chip, I tied the SS, WP, and HOLD pins to hi/low as per my needs.
- COMP-E~ XOR would be pleasedThe following code was used in SPI1.c  |  |  |  | Code: ** Settings : ** Bean name : SPI1 ** Device : SPI ** Settings ** Clock settings ** Value of Preselection : 5 ** Value of Selection : 64 ** Frequency : 12.5 kHz ** Mode Select : Master ** Clock Polarity : active-high ** Clock Phase : First edge ** Data shift order : MSB first ** Bidirectional mode : Disabled ** Output enable in Bidirect. : no ** Stop in Wait Mode : Disabled ** Pins ** SCK pin : PTB2_KBIP6_SPSCK_ADP6 ** SCK pin signal : ** MISO pin allocation : Enabled ** MISO pin : PTB4_MISO ** MISO pin signal : ** MOSI pin allocation : Enabled ** MOSI pin : PTB3_KBIP7_MOSI_ADP7 ** MOSI pin signal : ** SS pin allocation : Disabled ** Interrupts ** Interrupt : Vspi ** Receive and fault interrupt : Disabled ** Transmit Interrupt : Disabled ** ISR name : ** Initialization ** Call Init in CPU init. code : yes ** Enable SPI system : yes ** Contents : ** Init - void SPI1_Init(void); **
/* MODULE SPI1. */
#include "SPI1.h"
void SPI1_Init(void) { /* SPIC1: SPIE=0,SPE=1,SPTIE=0,MSTR=1,CPOL=0,CPHA=0,SSOE=0,LSBFE=0 */ setReg8(SPIC1, 0x50); /* SPIC2: ??=0,??=0,??=0,MODFEN=0,BIDIROE=0,??=0,SPISWAI=0,SPC0=0 */ setReg8(SPIC2, 0x00); /* SPIBR: ??=0,SPPR2=1,SPPR1=0,SPPR0=0,??=0,SPR2=1,SPR1=0,SPR0=1 */ setReg8(SPIBR, 0x45); // note that specific baud rates were not functional } // I believe I had success with 0x45 and 0x00
|  |  |  |  |
This EEPROM specific initialization was key to getting things working. It was a function in my main file, but probably belongs in the SPI1.c file.  |  |  |  | Code: void powerUpEEprom(void) {
CS_OFF; delay(2); SPI1_Init(); delay(2);
delay(2); CS_OFF; delay(1); CS_ON; delay(1);
SPI_WREN(); SPI_WRSR(0x00);
/* A small bit of test code */ SPI_WRITE(0x00, 0x00, 0x11); delay(10); SPI_WRITE(0x00, 0x01, 0x22); delay(10); SPI_WRITE(0x00, 0x02, 0x33); delay(10); SPI_WRITE(0x00, 0x03, 0x44); delay(10); SPI_WRITE(0x00, 0x04, 0x55); delay(10); testArray[1] = SPI_READ(0x00,0x00); delay(10); testArray[2] = SPI_READ(0x00,0x01); delay(10); testArray[3] = SPI_READ(0x00,0x02); delay(10); testArray[4] = SPI_READ(0x00,0x03); delay(10); testArray[5] = SPI_READ(0x00,0x04); delay(10); }
|  |  |  |  |
EEPROM.h ~ Computer Engineering is going to the laundromat and saying, "yeah, i can so pipeline this" EEPROM.c
|