free 8051 Microcontroller Projects AVR PIC Microcontroller Projects Tutorials Ebooks Libraries, interfacing tutorials, lcd tutorial, stepper motor, dc motor 8051 assembly language programming electronics and communication ECE CSE pdf ebooks library BE final year project ideas Embedded systems

 
8051 microcontroller 8051 microcontroller
Forums

Moderators: Ajay, Junied , abbas1707, Arun Kumar V, pdi33, Shailesh NAYAK, ۞ TPS ۞, shyam, sashijoseph
Author Post
Muki
Tue Jun 10 2008, 03:36PM
 User Offline
Registered Member #8370
Joined: Tue Jun 10 2008, 03:33PM

Posts: 1
Thanked 0 times in 0 post
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

Back to top


Ajay
Tue Jun 10 2008, 05:40PM
Rickey's World Admin

 User Online

Registered Member #1
Joined: Fri Feb 24 2006, 04:56AM

Posts: 3753
Thanked 696 times in 655 posts
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.

www.rickeyworld.info
If you feel satisfied with the user's forum reply please click on the thank button.

Obey forum rules!
Respect others!
Back to top


Amin Sharif
Mon Jun 23 2008, 09:39PM
 User Offline
Registered Member #6421
Joined: Mon Mar 03 2008, 11:44PM

Posts: 31
Thanked 0 times in 0 posts
Hello.

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

CODE:
#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

[ Edited Tue Jun 24 2008, 08:21AM ]
Back to top


Amin Sharif
Mon Jun 23 2008, 09:47PM
 User Offline
Registered Member #6421
Joined: Mon Mar 03 2008, 11:44PM

Posts: 31
Thanked 0 times in 0 posts
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;
}
}
Back to top


 

Jump:     Back to top

Syndicate this thread: rss 0.92 Syndicate this thread: rss 2.0 Syndicate this thread: RDF
Powered by e107 Forum System

8051 Microcontroller Projects 8051 AVR tutorials PIC microcontroller, 8051 assembly language programming electronics and communication ECE CSE pdf ebooks library BE final year project ideas Embedded systems