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 code between two ATMega168 controllers

Status
Not open for further replies.

eeuml11

Newbie level 1
Joined
Apr 26, 2011
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,302
I am fairly new to coding and the microcontroller all together.
I'm trying to have an SPI tansfer between two ATMega168s using C programming.

Here is my code:
#include <avr/io.h>
#include <util/delay.h>

#define SPI_PORT PORTB
#define SPI_DDR DDRB
#define SPI_CS PB2

unsigned char SPI_WriteRead(unsigned char dataout)
{
unsigned char datain;

// Start transmission (MOSI)
SPDR = dataout;

// Wait for transmission complete
while(!(SPSR & (1<<SPIF)));

// Get return Value;
datain = SPDR;

// Latch the Output using rising pulse to the RCK Pin
SPI_PORT |= (1<<SPI_CS);

// Disable Latch
SPI_PORT &= ~(1<<SPI_CS);

// Return Serial In Value (MISO)
return datain;
}

int main(void)
{
unsigned char cnt;

// Declare variables
char dataRegisterByte = 0;

//Configure PORTD as an output port
DDRD |= (1 << PD0);
DDRD |= (1 << PD1);
DDRD |= (1 << PD2);
DDRD |= (1 << PD3);
DDRD |= (1 << PD4);
DDRD |= (1 << PD5);
DDRD |= (1 << PD6);
DDRD |= (1 << PD7);

char levelOneCubeOne = 0x05; //0000 0101 LEVEL1
// Transmit a test byte to the slave
SPI_MasterTransmit(levelOneCubeOne);

// Initial the AVR ATMega168 SPI Peripheral

// Set MOSI and SCK as output, others as input
SPI_DDR = (1<<PB3)|(1<<PB5)|(1<<PB2);

// Latch Disable (RCK Low)
SPI_PORT &= ~(1<<SPI_CS);

// Enable SPI, Master, set clock rate fck/2 (maximum)
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SCR0);
SPSR = (1<<SPI2X);



while(1) {

// BEGIN NEW CODE
// Get the data from the data register
dataRegisterByte = SPI_SlaveReceive();

// Check Status of BIT0
if (dataRegisterByte[0] == 1)
{
// Light RED LED
PORTD |= (1 << PD0);
}
else
{
// Light GREEN LED
PORTD |= (1 << PD1);
}

// Check Status of BIT1
if (dataRegisterByte[1] == 1)
{
// Light RED LED
PORTD |= (1 << PD2);
}
else
{
// Light GREEN LED
PORTD |= (1 << PD3);
}

// Check Status of BIT2
if (dataRegisterByte[2] == 1)
{
// Light RED LED
PORTD |= (1 << PD4);
}
else
{
// Light GREEN LED
PORTD |= (1 << PD5);
}

// Check Status of BIT3
if (dataRegisterByte[3] == 1)
{
// Light RED LED
PORTD |= (1 << PD6);
}
else
{
// Light GREEN LED
PORTD |= (1 << PD7);
}
//END NEW CODE

} // END WHILE LOOP
}
return 0;
}

this works for the master but not for the slave. It is obvious that I'm missing something but I can't figure it out. Can someone please help??
 

first you read the SPI control register in that one bit is used for mastre and slave, for slave configuration clock is not necessary because the clock is applied from master....
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top