CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Reading a EEPROM Memory with a PIC16f877 via SPI

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Spiffy



Joined: 21 Jan 2004
Posts: 12

View user's profile Send private message Send e-mail MSN Messenger

Reading a EEPROM Memory with a PIC16f877 via SPI
PostPosted: Wed Jan 28, 2004 11:00 am     Reply with quote

I'm trying to write and read in a EEprom mem. The program, in C, is sent in a pic 16F877 by using a Qikstart card. I ran the program an got the results by Hyperterminal. I've also checked the writing sequence with an logic Analyser. The writting sequence is fine but when i'm trying to read the data inside, the data on the sdo line isn't clocked and gives me FF (which is a default setting for a memory when there is nothing inside).

here's my program!!!


#include "16f877.h"
#include "stdlib.h"
#use delay (clock=4000000)
#use rs232 (baud=9600,xmit=pin_c6,rcv=pin_c7)
#bit CKE = 0x94.6
#bit SMP = 0x94.7
#bit CKP = 0x14.4
#byte SSPSTAT=0x94
#byte SSPCON=0x14


//Déclaration des variables


char choix;
int data=0,yx;
long int i=0;


void ecriture (void);
void lecture (void);
void main (void)

{

set_tris_C(0x90); //configure les ports pour sortie et entree.
set_tris_A(0x00);
set_tris_E(0x04);

setup_spi(spi_master|spi_l_to_h|spi_clk_div_16);

CKE = 0; // donnée VALIDE de front descendant à front descendant
CKP = 0; // La mémoire échantillone sur front montant
SMP = 0; // Échantillonage au centre d'un créneau
while(1)
{


Printf("\n\r1 pour activer l'écriture ou 2 pour la lecture");
choix=getch();


if (choix=='1')

{
ecriture();
}

if (choix=='2')
{
lecture();
}
else
{
puts("\n\r Erreur");
}
}


}

void ecriture (void)
{

printf("\n\r SSPSTAT:%x",SSPSTAT);
printf("\n\r SSPSTAT:%x",SSPCON);
printf("\n\r appuyer sur UN chiffre:");
yx=getch();
printf("\n\rlecture:%d",yx);
getch();
delay_ms(45);

output_low(pin_a4); //On choisi la memoire
spi_write(0x06); //Write enable

spi_write(0x02); //on veut ecrire sur la memoire
spi_write(0x00);
spi_write(0x00); //On choisi l'adress
spi_write(yx); //On designe le data a envoyer
output_high(pin_a4); //On enleve la selection de memoire
delay_ms(200);
printf("\n\r ok!");

}

void lecture (void)

{
printf("\n\r SSPSTAT:%x",SSPSTAT);
printf("\n\r SSPSTAT:%x",SSPCON);



printf("\n\rReading...");
output_low(pin_a4);
spi_write(0x03);
spi_write(0x00);
spi_write(0x00);
delay_ms(23);

While(i<10)
{
data=spi_read();
printf("\n\rlecture: %lu %x",i,data);
i++;
}
output_high(pin_a4);

}
neil



Joined: 08 Sep 2003
Posts: 128

View user's profile Send private message

This could be the problem
PostPosted: Wed Jan 28, 2004 11:18 am     Reply with quote

Hello Spiffy!

I have noticed in your code you are using "data=spi_read();" to read the data out. The CCS function is a bit strange here and when your SPI device is supplying the clock, you need to write data at the same time! Very strange, but try "data=spi_read(0);" This may be the only problem. I have not looked very closely at the rest of your code though.

Regards,
Neil.
Snoopy
Guest







PostPosted: Wed Jan 28, 2004 4:59 pm     Reply with quote

Salut Spiffy

Il manque des details au niveau de la lecture.
Regarde: ceci vient avec le compilateur. va voir l'exemple

SPI_READ()

Syntax: value = spi_read (data)
Parameters: data is optional and if included is an 8 bit int.
Returns: An 8 bit int


Function:
Return a value read by the SPI. If a value is passed to SPI_READ the data will be clocked out and the data received will be returned. If no data is ready, SPI_READ will wait for the data.

If this device supplies the clock then either do a SPI_WRITE(data) followed by a SPI_READ() or do a SPI_READ(data). These both do the same thing and will generate a clock. If there is no data to send just do a SPI_READ(0) to get the clock.

If the other device supplies the clock then either call SPI_READ() to wait for the clock and data or use SPI_DATA_IS_IN() to determine if data is ready.

Availability:
This function is only available on devices with SPI hardware.

Examples:
in_data = spi_read(out_data);

Eample Files:
ex_spi.c
Guest








PostPosted: Wed Jan 28, 2004 5:46 pm     Reply with quote

J'ai omis ce qui?

J'ai pensé que j'ai expliqué cela!
neil



Joined: 08 Sep 2003
Posts: 128

View user's profile Send private message

PostPosted: Wed Jan 28, 2004 6:00 pm     Reply with quote

sorry, the last message was mine. Should have logged on!
snoopy
Guest







PostPosted: Thu Jan 29, 2004 3:40 pm     Reply with quote

BYTE read_ext_eeprom(9356_ADDRESS address)
{
BYTE data;

rotate_left(&address,1);
output_high(MAX_CS);
spi_write(0x18|(address&1));
spi_write(address);
data=spi_read(0);
output_low(MAX_CS);

return(data);
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group