Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[+30 points] for help

Status
Not open for further replies.

tension885

Junior Member level 2
Joined
Feb 16, 2008
Messages
21
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,447
hello,
i want to connect my AT89c51 with one SPI slave (this slave supports SPI mode 0,0). since AT89c51 has no hardware based SPI , so i have to do programming in software for SPI master.
i am showing you SPI routines for SPI master.but i am confused about SPI modes.
i need some guidance....please tell me that
is this code correct?? i.e Can it communicate with SPI slave(supporting mode 0,0)??
please help me its very very urgent :cry::cry::cry::cry
30 points will be rewarded for help

Code:
#include <reg51.h>


sbit miso  = P2^0; 
sbit mosi   = P2^1;
sbit clk  = P2^3;        // clock =0 in start
sbit cs   = P2^2;

void spi_write(unsigned char dat)
{
     
   unsigned char i,c;

   c = dat;
   for(i=0;i<8;i++)
   {
   if((c&0x80)==0x80)
      mosi = 1;
   else
      mosi = 0;

   clk= 1;
   c=c<<1;
   clk= 0;
   }
}
unsigned char spi_read()
{
   
   unsigned char i, dat;

   for(i=0;i<8;i++)
   {
      dat = dat<<1;
      clk = 1;
      if(miso)
         dat = dat+1;
      clk = 0;
   }
   return dat;
}


//please guys review my code and help me
 

Hi,
Your code seems to be ok, except for MISO has to be inited to high. What is your doubt regarding SPI?
Regards,
Laktronics
 

where MISO shud be high???
my doubt is
" will this code work with a slave which supports spi mode 0,0 ???"
 

Hi,
Since MISO is an input pin, you have to set it high in the beginning itself to configure it as input. Even though while sending the data, you are not looking at MISO, the data from slave will be coming to MISO and if the bit is low it may load MISO output of the slave.
The Mode 0 refers to Clock phase = 0 and clock polarity = 0 and that is what your software is using. Also you can make your CS signal low during character receive or Transmit period and it can be connected to SS- signal input of slave.
Have you got specifications of the slave unit you want to connect to?
As per your software, you have to connect MISO to MISO, MOSI to MOSI CLK to CLK and CS- to SS- between Master ans Slave terminals. However, you have to ensure that on the slave side also signals are complementary when connected directly( that is, TX to RX connection is to be ensured).

Regards,
Laktronics
 

Since most SPI communications are full-duplex, you also need to read MISO pin when your are transmitting. The transmit function should be like this
Code:
unsigned char spi_write(unsigned char dat)
{
	unsigned char i,c, retval=0;

	c = dat;
	for(i=0;i<8;i++)
	{
		retval = retval << 1;

		if((c&0x80)==0x80)
		 mosi = 1;
		else
		 mosi = 0;

		clk= 1;

		if(miso)
		 retval = retval+1;

		c=c<<1;
		clk= 0;
	}
	return(retval);
}
 

Hi,

As CMOS has told if you make a combined read/write function, you may not need a separate routine for read and write. The software can always send a dummy code while only reading the data and it can as well neglect the returned value when only writing.
But in the code given by CMOS, it is necessary to add shift of retval and also picking up of the MISO bit has to be done when the clock is high. Your original code is perfect from that point of view. But you need to keep MISO pin high even while sending since the output from slave will be always there whether you want it or not. If you are only interested in writing, it is also not necessary that you have to read the MISO pin.

Regards,
Laktronics
 

laktronics said:
Hi,

As CMOS has told if you make a combined read/write function, you may not need a separate routine for read and write. The software can always send a dummy code while only reading the data and it can as well neglect the returned value when only writing.
But in the code given by CMOS, it is necessary to add shift of retval and also picking up of the MISO bit has to be done when the clock is high. Your original code is perfect from that point of view. But you need to keep MISO pin high even while sending since the output from slave will be always there whether you want it or not. If you are only interested in writing, it is also not necessary that you have to read the MISO pin.

Regards,
Laktronics
Updated the code. Thanks :D
 

Hi,
Yes, the code given by Mr.CMOS can be used as a common master side function for full duplex or half duplex SPI. You have to define SPI pins as well as set MISO high ( to make it an input) before the function is called.

Regards,
Laktronics
 

thanx a lot dear frends.... i learned lot of things from you..thanx again ..
giving +30 to each of u :) :)


[/code]
 

Hi,
That is really nice of you, Thank you too. But if it means you lose so much points from your pocket, please let me know if you are in need of points, I can always donate you back.
Regards,
Laktronics
 

Thanks.. .Let me know if you need points anytime and I'll be glad to give you back.
 

I just want to know whether both the code works and what are the change in the second code is necessary [Please change it and post it].
 

The code given by me should work. Only thing you have to do is make MISO high on power-up.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top