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

Problem in working with SPI

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



Joined: 24 Feb 2016
Posts: 5

View user's profile Send private message

Problem in working with SPI
PostPosted: Fri Feb 26, 2016 2:57 am     Reply with quote

My target is to interface MIFARE reader to perform Read/Write by using SPI protocol.
For that, I desired to interface DS1307 RTC first. In that, i have some minor problem to deal with it.
So, I attached my code below. Please notify me if any mistake in this code.

Code:

#include<16f877a.h>
#fuses HS,NOWDT
#use delay (clock=8mhz)
#use spi(DI=PIN_B3, DO=PIN_B2, CLK=PIN_B1, ENABLE=PIN_B0, BITS=16)
#use rs232(baud=9600,xmit=PIN_b7,rcv=PIN_b6)

byte i = 0x80;
byte out;

void main() {
   while(TRUE)
   {
      spi_xfer(i);
      out = spi_xfer(i);
      delay_ms(100);
   }
}


I got the output from Proteus oscilloscope window.
CS pin going to low. At that time clock is generated. MOSI data is something generated and send it to slave.
But the MISO line did not send back any data to master.
(I am sending 0x80 command to slave for read the seconds from the RTC chip).

CPU--->DS3234

B0----->CS
B1----->Clock
B2----->DI
B3<-----DO

Please someone help me.....
What i am doing right now?
What i need to change the above code?
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Fri Feb 26, 2016 4:04 am     Reply with quote

The DS1307 is an I2C device, not SPI!

I beleive a driver/example code is almost certainly available in CCS for the DS1307. This should save you a lot of work.
Ttelmah



Joined: 11 Mar 2010
Posts: 19246

View user's profile Send private message

PostPosted: Fri Feb 26, 2016 4:06 am     Reply with quote

Get rid of the enable in the #USE SPI.

It is better to handle this yourself.

The way a 'read' has to happen, is that you clock an _8bit_ control/address into the chip, and then clock the reply back.
So:
Code:

#include<16f877a.h>
#fuses HS,NOWDT
#use delay (clock=8mhz)
#use spi(DI=PIN_B3, DO=PIN_B2, CLK=PIN_B1, MODE=1)
//chip supports mode 1 & mode 3
#use rs232(baud=9600,xmit=PIN_b7,rcv=PIN_b6)
#define SECONDS_READ 0x00
#define SECONDS_WRITE 0x80
#define CS PIN_B0

void main()
{
   int8 rval;
   output_high(CS); //ensure CS starts high
   delay_ms(10);

   while(TRUE)
   {
      output_low(CS); //drop the select
      rval=spi_xfer(SECONDS_READ,8); //clock out the read command
      rval= spi_xfer(0,8); //clock out eight dummy bits and receive reply
      output_high(CS); //finish the transaction
      delay_ms(100);
   }
}

The "W/R" bit needs to be _low_ for a read. 0x80, is the write address for the seconds register.

By operating CS yourself, you can decide (for instance), to send a ten byte command as one transaction, to configure the chip.

Are you using a DS1307, or a DS3234?. The former is I2C, not SPI.
geetha rangasamy



Joined: 24 Feb 2016
Posts: 5

View user's profile Send private message

PostPosted: Fri Feb 26, 2016 4:48 am     Reply with quote

Ttelmah wrote:
Get rid of the enable in the #USE SPI.

It is better to handle this yourself.

The way a 'read' has to happen, is that you clock an _8bit_ control/address into the chip, and then clock the reply back.
So:
Code:

#include<16f877a.h>
#fuses HS,NOWDT
#use delay (clock=8mhz)
#use spi(DI=PIN_B3, DO=PIN_B2, CLK=PIN_B1, MODE=1)
//chip supports mode 1 & mode 3
#use rs232(baud=9600,xmit=PIN_b7,rcv=PIN_b6)
#define SECONDS_READ 0x00
#define SECONDS_WRITE 0x80
#define CS PIN_B0

void main()
{
   int8 rval;
   output_high(CS); //ensure CS starts high
   delay_ms(10);

   while(TRUE)
   {
      output_low(CS); //drop the select
      rval=spi_xfer(SECONDS_READ,8); //clock out the read command
      rval= spi_xfer(0,8); //clock out eight dummy bits and receive reply
      output_high(CS); //finish the transaction
      delay_ms(100);
   }
}

The "R/W" bit needs to be _low_ for a read. 0x80, is the write address for the seconds register.

By operating CS yourself, you can decide (for instance), to send a ten byte command as one transaction, to configure the chip.

Are you using a DS1307, or a DS3234?. The former is I2C, not SPI.
Quote:
Executing: "C:\Program files\Picc\CCSC.exe" +FM "spi1.c" #__DEBUG=1 +ICD +DF +LN +T +A +M +Z +Y=9 +EA #__16F877A=TRUE
*** Error 51 "spi1.c" Line 40(35,36): A numeric expression must appear here
*** Error 51 "spi1.c" Line 41(25,26): A numeric expression must appear here
2 Errors, 0 Warnings.
Build Failed.
Halting build on first failure as requested.
BUILD FAILED: Fri Feb 26 16:12:19 2016

I got this error sir.

Thank you for replying.
I already using ds1307.
Now i need to interface RFID Reader That is MFCR522 NXP chip. Before using this chip I need to know the spi. so that is why I am interfacing DS3234 SPI RTC chip.

My questions

1.What is the mode ? I read a lot about spi from websites that has mentioned about CLOCK PHASE and CLOCK POLARITY what is this ?.

2.The compiler help said like this
int i = 34;
spi_xfer(i);
// transfers the number 34 via SPI
int trans = 34, res;
res = spi_xfer(trans);
// transfers the number 34 via SPI
// also reads the number coming in from SPI

But you pass the two values separated by comma in this function like this rval=spi_xfer(SECONDS_READ,8);

this two values
(SECONDS_READ,8)

3. Again you pass the same value but in different method why you make two lines
rval=spi_xfer(SECONDS_READ,8); //clock out the read command
rval= spi_xfer(0,8); //clock out eight dummy bits and receive reply


I am totally confused with two lines.

And I purchased the MFRC522 RFID reader here
http://www.alselectro.com/rfid-13.56mhz--mifair-read-write-with-rc522-chip.html

After finish this DS3234 I will interface this RFID Reader.

Thank you again.
Ttelmah



Joined: 11 Mar 2010
Posts: 19246

View user's profile Send private message

PostPosted: Fri Feb 26, 2016 5:42 am     Reply with quote

Apologies, forgot you cannot use transfer lengths, without using a stream. You need:
Code:

#include<16f877a.h>
#fuses HS,NOWDT
#use delay (clock=8mhz)
#use spi(DI=PIN_B3, DO=PIN_B2, CLK=PIN_B1, MODE=1, STREAM=SPI)
//chip supports mode 1 & mode 3
#use rs232(baud=9600,xmit=PIN_b7,rcv=PIN_b6)
#define SECONDS_READ 0x00
#define SECONDS_WRITE 0x80
#define CS PIN_B0

void main()
{
   int8 rval;
   output_high(CS); //ensure CS starts high
   delay_ms(10);

   while(TRUE)
   {
      output_low(CS); //drop the select
      rval=spi_xfer(SPI,SECONDS_READ,8); //clock out the read command
      rval= spi_xfer(SPI,0,8); //clock out eight dummy bits and receive reply
      output_high(CS); //finish the transaction
      delay_ms(100);
   }
}


For some reason they only allow the number of bits to be specified, when a stream is selected, rather than when a default is selected.
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Fri Feb 26, 2016 7:22 am     Reply with quote

Hi,

Man, what a confusing thread this is! Shocked

This is probably a textbook case on how not to ask for help on the forum! You prominently mention the DS1307 RTC (which is I2C) in a thread about problems with SPI, and in one teeny, tiny comment, also mention the DS3234 (which is SPI). You really need to use more clarity in your help requests!

Who's on first?: https://www.youtube.com/watch?v=kTcRRaXV-fg
_________________
John

If it's worth doing, it's worth doing in real hardware!
temtronic



Joined: 01 Jul 2010
Posts: 9126
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Feb 26, 2016 8:09 am     Reply with quote

guess I'm getting used these 'interesting' posts...

1) please post a 'link' to the MIFARE reader. This information is vital if you want us to help

2) do NOT use PROTEUS. We can't help you with it. It's well known to have major flaws, errors and faulty DRCs. Be aware that just becuse 'it works' in PROTEUS it probably won't in the real world !

3) the 877 is a 5 volt PIC, is the MIFARE a 5 volt device or 3 volt?


Jay
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Feb 26, 2016 3:17 pm     Reply with quote

Quote:
1.What is the mode ? I read a lot about spi from websites that has
mentioned about CLOCK PHASE and CLOCK POLARITY what is this ?.

Read these webpages:
http://dlnware.com/theory/SPI-Transfer-Modes
http://www.totalphase.com/support/articles/200349236-SPI-Background#modes

Quote:
2.The compiler help said like this
int i = 34;
spi_xfer(i);
// transfers the number 34 via SPI
int trans = 34, res;
res = spi_xfer(trans);
// transfers the number 34 via SPI
// also reads the number coming in from SPI

But you pass the two values separated by comma in this function like this rval=spi_xfer(SECONDS_READ,8);

Ttelmah likes to use an optional method, where the number of bits to
be transferred is specified in the 2nd parameter. You don't have to
do it that way. If you want to use the method shown in the Compiler Help,
then specify the number of bits in the #use spi() statement, as shown
in bold below:
Quote:
#use spi(DI=PIN_B3, DO=PIN_B2, CLK=PIN_B1, MODE=1, BITS=8)


Quote:
Again you pass the same value but in different method why you make two lines
rval=spi_xfer(SECONDS_READ,8); //clock out the read command
rval= spi_xfer(0,8); //clock out eight dummy bits and receive reply

Look in the DS3234 data sheet on page 18, at this diagram:
Quote:
Figure 4. SPI Single-Byte Read

http://datasheets.maximintegrated.com/en/ds/DS3234.pdf
It shows two sequential 8-bit transfers. The first transfer writes the
register address and the 2nd transfer reads the register data.
The comments that Ttelmah put on the two statements show this.

If you add BITS=8 to the #use spi() statement, you can do it like this:
Code:

output_low(CS);
spi_xfer(SECONDS_READ); // Write the register address
rval = spi_xfer(0);  // Read data from the specified register
output_high(CS);

After these lines execute, 'rval' will have the value of the Seconds
register in it.
geetha rangasamy



Joined: 24 Feb 2016
Posts: 5

View user's profile Send private message

MIFARE Reader help
PostPosted: Sat Feb 27, 2016 3:10 am     Reply with quote

temtronic wrote:
guess I'm getting used these 'interesting' posts...

1) please post a 'link' to the MIFARE reader. This information is vital if you want us to help

2) do NOT use PROTEUS. We can't help you with it. It's well known to have major flaws, errors and faulty DRCs. Be aware that just becuse 'it works' in PROTEUS it probably won't in the real world !

3) the 877 is a 5 volt PIC, is the MIFARE a 5 volt device or 3 volt?


Jay

I am using PIC at 5.0v and MIFARE Reader at 3.3v
We designed power supply by using 3.3v regulator(1117).
I am purchasing and using the MIFARE reader by the follwing link:
http://www.alselectro.com/rfid-13.56mhz--mifair-read-write-with-rc522-chip.html
It has the pins below,
1)SDA
2)SCK
3)MOSI
4)MISO
5)IRQ
6)GND
7)RST
8)3.3v
geetha rangasamy



Joined: 24 Feb 2016
Posts: 5

View user's profile Send private message

Thank you for reply
PostPosted: Sat Feb 27, 2016 3:13 am     Reply with quote

Ttelmah wrote:
Apologies, forgot you cannot use transfer lengths, without using a stream. You need:
Code:

#include<16f877a.h>
#fuses HS,NOWDT
#use delay (clock=8mhz)
#use spi(DI=PIN_B3, DO=PIN_B2, CLK=PIN_B1, MODE=1, STREAM=SPI)
//chip supports mode 1 & mode 3
#use rs232(baud=9600,xmit=PIN_b7,rcv=PIN_b6)
#define SECONDS_READ 0x00
#define SECONDS_WRITE 0x80
#define CS PIN_B0

void main()
{
   int8 rval;
   output_high(CS); //ensure CS starts high
   delay_ms(10);

   while(TRUE)
   {
      output_low(CS); //drop the select
      rval=spi_xfer(SPI,SECONDS_READ,8); //clock out the read command
      rval= spi_xfer(SPI,0,8); //clock out eight dummy bits and receive reply
      output_high(CS); //finish the transaction
      delay_ms(100);
   }
}


For some reason they only allow the number of bits to be specified, when a stream is selected, rather than when a default is selected.

I am using PIC at 5.0v and MIFARE Reader at 3.3v
We designed power supply by using 3.3v regulator(1117).
I am purchasing and using the MIFARE reader by the follwing link:
http://www.alselectro.com/rfid-13.56mhz--mifair-read-write-with-rc522-chip.html
It has the pins below,
1)SDA
2)SCK
3)MOSI
4)MISO
5)IRQ
6)GND
7)RST
8)3.3v
geetha rangasamy



Joined: 24 Feb 2016
Posts: 5

View user's profile Send private message

SPI HELP
PostPosted: Sat Feb 27, 2016 3:18 am     Reply with quote

ezflyr wrote:
Hi,

Man, what a confusing thread this is! Shocked

This is probably a textbook case on how not to ask for help on the forum! You prominently mention the DS1307 RTC (which is I2C) in a thread about problems with SPI, and in one teeny, tiny comment, also mention the DS3234 (which is SPI). You really need to use more clarity in your help requests!

Who's on first?: https://www.youtube.com/watch?v=kTcRRaXV-fg

I apologize, we need SPI (DS3234) only...
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