need SPI interface coding in C (for 8051)

Status
Not open for further replies.
You have ready-for-use example in atmel web site for hardware SPI.

For software SPI, may consider using discrete IO pins as

Code:
#define uchar	unsigned char

sbit RF_MISO	= P2^3;  //say
sbit RF_MOSI	= P2^2;
sbit RF_SCK	= P2^1;
sbit RF_CSN	= P2^0;
...

void   spiWr(uchar);
uchar spiRd(void);

...

/*
*********************************************************************************************************
* Low level SPI WRITE FUNCTION
*********************************************************************************************************
*/
void spiWr(uchar dat)
{
		/* software SPI, send MSB first */
	static uchar i,c;

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

	RF_SCK = 1;
	c=c<<1;
	RF_SCK = 0;
	}
}

/*
*********************************************************************************************************
* Low level SPI READ FUNCTION
*********************************************************************************************************
*/
uchar spiRd(void)
{
	/* software SPI read, MSB read first */
	static uchar i, dat;

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

John
 
Last edited by a moderator:
i need the SPI coding for the C515 controllers of infenion or siemens the basic structure is similar to the 8051 but with enhance periphirals :?:[/b]
 

TechToys's code is useable on C515
 

i know that the SPI is code is standard in all controllers but i need for C515C controller in which factory test register available by using this register without any slave we can check the SPI communication.
 

hi,

following are the detail of registers



following is the register used for the factory testing

 

Code:
       ORL SSCMODE,#$80  ; set LOOPB to 1
       MOV STB,#$5A      ; send $5A to SPI output
Wait:       
       MOV A,SCF         ; SSC Status Register
       JNB A.0,Wait      ; wait until Transfer completed
       MOV A,SRB         ; receive $5A on Receive Buffer Register
 

hi every one;

i want to interface sd card with 8051 in spi mode...
want help in programming in embedded C...
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…