AtomSoft
Well-Known Member
Hey im still trying to write some code for a 16megabit (2MB) SPI Atmel chip using a PIC. Here is my C source so far. The issue i suppose it im not getting data that im supposed to get back. Below is the code and a link to the datasheet of the Memory and the PIC im using. I think it has to do with setup of the SPI. Can anyone help me. Sorry for asking so much. But think about it.. i try for days before i ask here.
PIC DATASHEET (18F2525)
AT45DB161D
https://www.electro-tech-online.com/custompdfs/2008/09/AT45DB161D.pdf
Short Description of IC :
https://www.sparkfun.com/commerce/product_info.php?products_id=301
Code:
#include <p18f2525.h>
#include <stdio.h>
#include <delays.h>
#include <string.h>
#pragma config WDT = OFF, XINST = OFF, OSC = INTIO67
#define CS LATCbits.LATC2
#define readSPI() writeSPI(0xFF)
#pragma udata bigdata
unsigned char buffer[512];
unsigned char data[512];
#pragma udata
void main(void);
void Init(void);
void MemRead(unsigned char *addr,char *buff, unsigned int len);
void MemWrite(unsigned char *addr,char *buff, unsigned int len);
unsigned char writeSPI(unsigned char data);
char RdyBusy(void);
void SetSizeFT(void);
void main(void){
unsigned char address = {0x00};
char data2[] = {"Jason Rules and if you dont think so kiss my A*S lol"};
OSCCON = 0x72; //8MHz clock
while(!OSCCONbits.IOFS); //wait for osc stable
strcpy(data,data2);
Init();
while(RdyBusy());
address=address<<9;
MemWrite(address,data,512);
while(RdyBusy());
address=address<<23;
MemRead(address,buffer,512);
while(1){
while(RdyBusy());
}
}
void Init(void){
ADCON1 = 0x0F;
TRISC = 0x10; //SDO(RC5),SCL/SCK(RC3),CS(RC2) = Output, SDA/SDI(RC4) = Input
CS = 1; // Chip Select = OFF (Logic Low = Selected)
SSPSTAT = 0b00000010; //SMP(7)=0, CKE(6)=0 (clock edge idle to active)
SSPCON1 = 0b00000000; //CKP(4)=0 clock polarity (idle low) (Fastes FOSC/4)
//SSPM3:SSPM0(3:0)=010 spi clock FOSC/64 (<400kHz)
SSPCON1bits.SSPEN=1; //SSPEN(5)=1 enable SPI
while(RdyBusy());
SetSizeFT();
}
void MemRead( unsigned char *addr,char *buff, unsigned int len){
unsigned int x;
CS = 0; //Select the chip
writeSPI(0x0B); //Continuous Array Read
for( x=0; x<3; x++) //Send the Address (Page & Byte Start Address)
writeSPI(*addr++);
for( x=0; x<4; x++) //Send the 4 Dont Care Bytes
writeSPI(0x01);
for( x=0; x<len; x++) //Read the Data In (Clock it)
*buff++ = readSPI();
CS = 1; //DeSelect the Chip
}
void MemWrite( unsigned char *addr,char *buff, unsigned int len){
int x;
CS = 0; //Select the chip
writeSPI(0x84); //Buffer Write
for( x=0; x<3; x++) //Send the Address (Page & Byte Start Address)
writeSPI(0x00);
for( x=0; x<len; x++) //Send the 4 Dont Care Bytes
writeSPI(*buff);
CS = 1; //DeSelect the Chip
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
CS = 0; //Select the chip
writeSPI(0x83); //Buffer Write
for( x=0; x<3; x++) //Send the Address (Page & Byte Start Address)
writeSPI(*addr++);
for( x=0; x<len; x++) //Send the 4 Dont Care Bytes
writeSPI(*buff++);
CS = 1; //DeSelect the Chip
}
char RdyBusy(void){
unsigned char stat;
CS = 0; //Select the chip
writeSPI(0xD7); //Status Register
stat = writeSPI(0xD7);
CS = 1; //DeSelect the Chip
if((stat & 0x80) != 0)
return 0;
else
return 1;
}
void SetSizeFT(void){ //Five Twelve (512)
unsigned char stat;
CS = 0; //Select the chip
writeSPI(0x3D);
writeSPI(0x2A);
writeSPI(0x80);
writeSPI(0xA6);
CS = 1; //DeSelect the Chip
}
unsigned char writeSPI(unsigned char data){
SSPBUF=data;
while(!SSPSTATbits.BF);
return SSPBUF;
}
PIC DATASHEET (18F2525)
AT45DB161D
https://www.electro-tech-online.com/custompdfs/2008/09/AT45DB161D.pdf
Short Description of IC :
https://www.sparkfun.com/commerce/product_info.php?products_id=301