Atmega16 SPI interface

Status
Not open for further replies.

Saulius Skeirys

Newbie level 4
Joined
Mar 19, 2014
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
43
Hello guys,

I am facing an issue, where I can't manage to work SPI interface connection between two atmega's.
Basically, I am trying to get data from slave avr uart input and send it to master uart pin through SPI.
I guess the problem would be somewhere in code (maybe opening SPI connection...).
If anyone could take a look and comment it, that would be appreciated.

Master code:
Code:
#define F_CPU 2000000UL			//Define the MPU operating frequency

#include <avr/io.h>				//Header file for AVR device specific I/O Definitions.
#include <avr/interrupt.h>		//Header file for incorportaing interrupt handling faclity (not used here).
#include <util/delay.h>			//Header file for incorporating delay routines.
#include "USART.h"				//Defines C functions for accessing USART Module.


#define BIT(x)			(1 << (x))	//Set a particular bit mask
#define CHECKBIT(x,b) 	(x&b)		//Checks bit status
#define SETBIT(x,b) 	x|=b;		//Sets the particular bit
#define CLEARBIT(x,b) 	x&=~b;		//Sets the particular bit
#define TOGGLEBIT(x,b) 	x^=b;		//Toggles the particular bit

void WaitMs(unsigned int ms);
unsigned char ch;
int main()
{
	//	unsigned int ch;
	
	unsigned char ch;

	/* Set MOSI and SCK output, all others input */
	DDRB = (1<<5)|(1<<3)|(1<<2)|(1<<4);
	/* Enable SPI, Master, set clock rate fck/16 */
	SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);

	uart_init(9600);		//Initialise the USART Module with the given Baudrate
	
	WaitMs(200);
	ch=0;
	

	sei();//enable global interr

	while(1)   //Enter into a uncoditional while loop..
	{
		
	}

	return 0;
}


ISR(SPI_STC_vect)
{
	ch=SPDR;
	uart_putc(ch);
}


void WaitMs(unsigned int ms)	//Generate a delay of ms milli second.
{
	unsigned int i;

	for(i=0;i<=ms/10;i++)
	{
		_delay_ms(10);
	}
}
Slave code:
Code:
#define F_CPU 2000000UL			//Define the MPU operating frequency

#include <avr/io.h>				//Header file for AVR device specific I/O Definitions.
#include <avr/interrupt.h>		//Header file for incorportaing interrupt handling faclity (not used here).
#include <util/delay.h>			//Header file for incorporating delay routines.
#include "USART.h"				//Defines C functions for accessing USART Module.


#define BIT(x)			(1 << (x))	//Set a particular bit mask
#define CHECKBIT(x,b) 	(x&b)		//Checks bit status
#define SETBIT(x,b) 	x|=b;		//Sets the particular bit
#define CLEARBIT(x,b) 	x&=~b;		//Sets the particular bit
#define TOGGLEBIT(x,b) 	x^=b;		//Toggles the particular bit

void WaitMs(unsigned int ms);
unsigned char ch;


int main()
{

	// Set MOSI and SCK output, all others input
	DDRB = (1<<5)|(1<<3)|(1<<2);
	// Enable SPI, Master, set clock rate fck/16, SPI MODE 1
	SPCR = (1<<SPE)|(1<<SPIE);

	uart_init(9600);		//Initialise the USART Module with the given Baudrate
	//Frame format.

	

	//sei();//enable global interr
	
	while(1)   //Enter into a uncoditional while loop..
	{
		ch=uart_getc();
		uart_putc(ch);
		SPDR = ch; /* Start transmission by putting data in SPDR */
		while(!(SPSR & (1<<SPIF)));/* Wait for transmission complete */
		WaitMs(50);
	}

	return 0;
}



void WaitMs(unsigned int ms)	//Generate a delay of ms milli second.
{
	unsigned int i;

	for(i=0;i<=ms/10;i++)
	{
		_delay_ms(10);
	}
}

Just in case, proteus simulation attached.
THANKS!
 

Attachments

  • spi project.rar
    16.5 KB · Views: 60

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…