Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

My #1 Enemy SPI

Status
Not open for further replies.

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.

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
 
ok i got it for anyone else out there lol I used the internal SPI Routines. With my own CS Here ya go.
FULLY TESTED and proud to say working 100%
Code:
#include <p18f2525.h>
#include <stdio.h>
#include <delays.h>
#include <string.h>
#include <spi.h>

#pragma config WDT = OFF, XINST = OFF, OSC = INTIO67

#define CS LATAbits.LATA5
#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 short long address = 0x00;
    unsigned short long address2 = 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());

    address2=address2<<23;
    MemRead(address,buffer,512);
	while(1){
	    CS = 1;
	}
}

void Init(void){
    ADCON1 = 0x0F;

    TRISC = 0x10;               //SDO(RC5),SCL/SCK(RC3),CS(RC2) = Output, SDA/SDI(RC4) = Input
    TRISAbits.TRISA5 = 0;

    CS = 1;                     // Chip Select = OFF (Logic Low = Selected)

    OpenSPI(SPI_FOSC_4,MODE_00,SMPMID);

//	SSPSTAT = 0b00000001;       //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++);

        WriteSPI(0xFF);
    
    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 wait;

    CS = 0;                         //Select the chip 
    WriteSPI(0xD7);
    wait = WriteSPI(0xD7);          //Status Register
    CS = 1;                         //DeSelect the Chip

    //stat = stat & 0x80;

    if(wait != 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
}
 
Last edited:
Sometimes when you put it out there for others to look at you'll take another look yourself and find the problem(s)...

Congrats'... Looks like a neat memory...

Mike
 
ok i got it for anyone else out there lol I used the internal SPI Routines. With my own CS Here ya go.
FULLY TESTED and proud to say working 100%
Code:
char data2[] = {"Jason Rules and if you dont think so kiss my A*S lol"};
LOL! :D Good work. Nice to know there is a simple SPI chip that can store large amounts of data without the complication of using an SD card and FAT.
 
ok i got it for anyone else out there lol I used the internal SPI Routines. With my own CS Here ya go.
FULLY TESTED and proud to say working 100%
Code:
#include <p18f2525.h>
#include <stdio.h>
#include <delays.h>
#include <string.h>
#include <spi.h>

#pragma config WDT = OFF, XINST = OFF, OSC = INTIO67

#define CS LATAbits.LATA5
#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 short long address = 0x00;
    unsigned short long address2 = 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());

    address2=address2<<23;
    MemRead(address,buffer,512);
	while(1){
	    CS = 1;
	}
}

void Init(void){
    ADCON1 = 0x0F;

    TRISC = 0x10;               //SDO(RC5),SCL/SCK(RC3),CS(RC2) = Output, SDA/SDI(RC4) = Input
    TRISAbits.TRISA5 = 0;

    CS = 1;                     // Chip Select = OFF (Logic Low = Selected)

    OpenSPI(SPI_FOSC_4,MODE_00,SMPMID);

//	SSPSTAT = 0b00000001;       //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++);

        WriteSPI(0xFF);
    
    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 wait;

    CS = 0;                         //Select the chip 
    WriteSPI(0xD7);
    wait = WriteSPI(0xD7);          //Status Register
    CS = 1;                         //DeSelect the Chip

    //stat = stat & 0x80;

    if(wait != 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
}


hello, would you be so kind as to show me what's in spi.h? im trying to convert your program using CCS C. thanks in advance..
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top