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.

SPI protocal configure and working

Status
Not open for further replies.

selva murugesan

Advanced Member level 4
Joined
Mar 30, 2012
Messages
116
Helped
2
Reputation
4
Reaction score
1
Trophy points
1,298
Activity points
2,007
friends,please tell me how to configure and using SPI in atmel?and also working of SPI
 

http://www.atmel.com/Images/doc2486.pdf
have all the answers u ever want about all the things atmega8 does, if u need other than atmega8. write your AVR name in google search window. press search and open 1st *.pdf u see.

if want something what is already writed in that datasheet here is some of it for atmega8;
Code:
  PORTB &=	~(1 << SPI_SS);		//pull slave select low, start communication
  SPI_Transmit(transmitting_BYTE);		
  receivingm_BYTE = SPI_Receive();	
  SPI_Finish();
  PORTB |= (1 << SPI_SS);		//pull slave select high, end communication
*
*
*
/*----------------------------------------------------------------------------
 *   FUNCTION: waits till SPI finish its operation
 *--------------------------------------------------------------------------- 
 *  INPUT: 
 *  --------------------------------------------------------------------- 
 *	byte	- byte value function should insert to register
 *--------------------------------------------------------------------------- 
 *  OUTPUT: 
 *  --------------------------------------------------------------------- 
 *  None 
 *---------------------------------------------------------------------------*/
void SPI_Finish(void) {
  while(!(SPSR & (1<<SPIF)))
    {}
}
/*----------------------------------------------------------------------------
 *   FUNCTION: waits till SPI ready and writes byte to SPI data register
 *--------------------------------------------------------------------------- 
 *  INPUT: 
 *  --------------------------------------------------------------------- 
 *	byte	- byte value function should insert to register
 *--------------------------------------------------------------------------- 
 *  OUTPUT: 
 *  --------------------------------------------------------------------- 
 *  None 
 *---------------------------------------------------------------------------*/
void SPI_Transmit(uint8_t byte) {
  while(!(SPSR & (1<<SPIF)))
    {}
  SPDR = byte;
}
/*----------------------------------------------------------------------------
 *   FUNCTION: waits till SPI ready and reads byte from SPI data register
 *--------------------------------------------------------------------------- 
 *  INPUT: 
 *  --------------------------------------------------------------------- 
 *	
 *--------------------------------------------------------------------------- 
 *  OUTPUT: 
 *  --------------------------------------------------------------------- 
 *  None 
 *---------------------------------------------------------------------------*/
char SPI_Receive() {
  while(!(SPSR & (1<<SPIF)))
    {}
  return SPDR;
}
*
*
*
/*----------------------------------------------------------------------------
 *   FUNCTION: sets up SPI settintgs to control LTC6802
 *--------------------------------------------------------------------------- 
 *  INPUT: 
 *  --------------------------------------------------------------------- 
 *  None 
 *--------------------------------------------------------------------------- 
 *  OUTPUT: 
 *  --------------------------------------------------------------------- 
 *  None 
 *---------------------------------------------------------------------------*/
void SPI_Init(void) {
  DDRB =	(1 << SPI_MOSI)|(1 << SPI_SCK)|(1 << SPI_SS);	// Set MOSI SCK and SS as output 
  SPCR = 	((0 << 7)					//disable SPI interupts
			|(1 << 6)					//enable SPI
			|(0 << 5)					//DORD = 0 to set MSB is 1st to transfer
			|(1 << 4)					//setting device to Master mode							
			|(1 << 3)					//settitng SPI mode
			|(1 << 2)					//^
			|(0 << 1)					//setting SPI clock to Fosc/16	
			|(1 << 0));					//^
  SPSR =	(0 << 0);					//setting SPI clock x2 - disabled
  PORTB |=	(1 << SPI_SS);					//Pull slave select high 
}

P.S. code not tested so might have bugs but scenario should be like that. Also I'm using wait loop before adding byte to registers not after, that is bit different from usualy.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top