Discussion in "AVR Discussion Forum" started by    Muki    Jun 11, 2008.
Wed Jun 11 2008, 04:06 am
#1
Hello All,
Greetings!
I am trying to do communication between two Atmega32 using SPI. I am using 16 Mhz Crystal. I initialized Master as:

//~~~~~~~~~~~~~~~~SPI master initialisation in polling mode~~~~~~~~~~~~~~~~
void initSpiMaster (void)
{
// set PB4(/SS), PB5(MOSI), PB7(SCK) as output

sbi(DDRB, 7); // set SCK as output
sbi(PORTB, 7); // set SCK hi
cbi(DDRB, 6); // set MISO as input
sbi(DDRB, 5); // set MOSI as output
sbi(DDRB, 4); // SS must be output for Master mode
// to work

// enable SPI in Master Mode with SCK = CK/4
SPCR = (1<<SPE)|(1<<MSTR);

SPSR=0x00; // clear SPIF bit in

}

and Slave as

void spiInit()
{
cbi(DDRB, 6); // set MISO as input
cbi(DDRB, 5); // set MOSI as output
// enable SPI in slave Mode with SCK = CK/4
SPCR=0xC0;
SPSR=0x00;
}

I checked the Clock at master i found that 15-16 Mhz not changing with changes the prescaler also.

I am getting few junk byte at slave side some side.

* Please suggest me am i doing initialization in write way or not
* why Clock frequency is not changes according to prescaler.

Regards
Mukesh
Wed Jun 11 2008, 06:10 am
#2
I really don't see any problem with your code as such.

Well there is one more guy in forum who tried the same thing between two PIC controllers. He was also getting junk bytes. We couldnt find any solution of why its happening like that.

are you using interrupts in Slave controller?
Try to play with clock polarity maybe you can get right data being sent. Keep master as rising and slave as falling and try again.
Tue Jun 24 2008, 10:09 am
#3
Hello.

Try this one out. I was having the same problem but have been solved.

#include "spi_lib.h"

//*****************************
//
//  SPI module initialization  atmega32
//
//*****************************

#define DD_MOSI		5
#define DD_MISO     6
#define DD_SCK      7
#define DDR_SPI     DDRB


void SPI_MasterInit(void)
{
	// Set MOSI and SCK output, all others input
	DDR_SPI = (1<<DD_MOSI)|(1<<DD_SCK);
	// Enable SPI, Master, set clock rate fck/16
	SPCR = (1<<SPE)|(1<<MSTR)|(0<<SPR1)|(0<<SPR0)|(0<<CPOL)|(1<<CPHA);
}

void SPI_sendString(unsigned char * s)
{ 
	int i = 0; 
	while (i < 100) // don't get stuck if it is a bad string
	{
		if( s[i] == '\0' ) break; // quit on string terminator
		SPI_sendChar( s [i++] );
	}
}

void SPI_sendChar(char spiData)
{
	/* Start transmission */
	SPDR = spiData;
	/* Wait for transmission complete */
	while(!(SPSR & (1<<SPIF)));
	_delay_ms(1);

}

void SPI_SlaveInit(void)
{
	/* Set MISO output, all others input */
	DDR_SPI = (1<<DD_MISO);
	/* Enable SPI */
	SPCR = (1<<SPE)|(0<<SPR1)|(0<<SPR0)|(0<<CPOL)|(1<<CPHA);
	SPDR = 0xFE;
}


//**********************
//
// File Name	: 'spi_lib.h'
//
//**********************
#ifndef spi_LIB
#define spi_LIB

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/delay.h>
void SPI_MasterInit(void);
void SPI_sendString(unsigned char * s);
void SPI_sendChar(char spiData);
void SPI_SlaveInit(void);

#endif

good luck :bye


[ Edited Tue Jun 24 2008, 08:51 pm ]
Tue Jun 24 2008, 10:17 am
#4
Write in master controller main program.
SPI_MasterInit();

SPI_sendChar('A');
SPI_sendChar('B');
SPI_sendChar('\r');
SPI_sendString("SPI Master Initialized\r");

Write in slave controller main program, You can use your own usart commands
It receives the character and increment and sends back to master.
SPI_SlaveInit();
while(1) //loop demos
{
while(!(SPSR & (1<<SPIF)));
if (SPDR != 0xfe)
{
Tempdata = SPDR;
USART_sendChar(Tempdata);
SPDR = Tempdata + 1;
}
}

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

NaKrutkADamb
Wed May 15 2024, 12:03 am
MichaelGot
Tue May 14 2024, 04:08 pm
FrankTrelm
Tue May 14 2024, 10:39 am
BillyTum
Tue May 14 2024, 09:08 am
Loganbag
Tue May 14 2024, 04:05 am
MichaelMog
Tue May 14 2024, 03:58 am
ThomasGaxaW
Mon May 13 2024, 05:33 pm
RobertInfup
Mon May 13 2024, 04:28 pm