deveshsamaiya
well,i cannot be sure about SPI,however as far as i know about serial communication through UART,internal oscillator poses problems and is not considered as reliable as external stable crystal oscillator for serial communication...
may be internal oscillator could also pose such issues of stability and reliability for spi to spi communication...
Any ways,i have got the code for spi to spi communication between two ATmega32 microcontrollers and it has been compiled in ICC AVR compiler
Posted on: 16-11-2009, 10:26:47 - Automerged
here is the code for data transmission from master spi :
//ICC-AVR application builder : 4/16/2009 6:56:37 PM
// Target : M32
// Crystal: 11.059Mhz
#include <iom32v.h>
#include <macros.h>
#define cs 0x10
unsigned int timer0_counter=0,j=0;
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x03;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00;
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//TIMER0 initialize - prescale:1024
// WGM: Normal
// desired value: 100Hz
// actual value: 100.933Hz (0.9%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0x95; //set count
OCR0 = 0x6B; //set compare
TCCR0 = 0x05; //start timer
}
#pragma interrupt_handler timer0_ovf_isr:iv_TIM0_OVF
void timer0_ovf_isr(void)
{
TCNT0 = 0x95; //reload counter value
timer0_counter++;
if(timer0_counter==2)
{
PORTA^=0x03;
timer0_counter=0;
}
}
//SPI initialize
// clock rate: 2764749hz
void spi_init(void)
{
SPCR = 0x50; //setup SPI - SPE,MSTR,
SPSR = 0x00; //setup SPI
//PORTB=(1<<PORTB4)|(1<<PORTB5)|(1<<PORTB7);
PORTB=(1<<PB4)|(1<<PB5)|(1<<PB7);
DDRB=(1<<DDB4)|(1<<DDB5)|(1<<DDB7);
}
void SPI_MasterTransmit(char cData)
{
/* Start transmission */
SPDR = cData;
/* Wait for transmission complete */
while(!(SPSR & (1<<SPIF)))
;
}
//UART0 initialize
// desired baud rate: 57600
// actual: baud rate:57599 (0.0%)
void uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x00;
UCSRC = BIT(URSEL) | 0x06;
UBRRL = 0x0B; //set baud rate lo
UBRRH = 0x00; //set baud rate hi
UCSRB = 0x18;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer0_init();
spi_init();
uart0_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x01; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//
void main(void)
{
init_devices();
//insert your functional code here...
while(1)
{
PORTB &=~cs;
SPI_MasterTransmit( 'a' );
PORTB |=cs;
}
}
And this is the code SPI slave:
//ICC-AVR application builder : 3/31/2009 1:09:34 PM
// Target : M32
// Crystal: 11.059Mhz
#include <iom32v.h>
#include <macros.h>
unsigned char spiData;
unsigned int k=0;
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00;
DDRC = 0x03;
PORTD = 0x00;
DDRD = 0x00;
}
//TIMER0 initialize - prescale:1024
// WGM: Normal
// desired value: 100Hz
// actual value: 100.933Hz (0.9%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0x95; //set count
OCR0 = 0x6B; //set compare
TCCR0 = 0x05; //start timer
}
#pragma interrupt_handler timer0_ovf_isr:iv_TIM0_OVF
void timer0_ovf_isr(void)
{
TCNT0 = 0x95; //reload counter value
}
//SPI initialize
// clock rate: 2764749hz
void SPI_SlaveInit(void)
{
PORTB=(1<<PB6);
SPCR = 0x40; //setup SPI
SPSR = 0x00; //setup SPI
}
char SPI_SlaveReceive(void)
{
/* Wait for reception complete */
while(!(SPSR & (1<<SPIF)))
;
/* Return data register */
return SPDR;
}
//UART0 initialize
// desired baud rate: 57600
// actual: baud rate:57599 (0.0%)
void uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x00;
UCSRC = BIT(URSEL) | 0x06;
UBRRL = 0x0B; //set baud rate lo
UBRRH = 0x00; //set baud rate hi
UCSRB = 0x18;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer0_init();
SPI_SlaveInit();
uart0_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x01; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void USART_Transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSRA & (1<<UDRE)) )
;
/* Put data into buffer, sends the data */
UDR = data;
}
//
void main(void)
{
init_devices();
//insert your functional code here...
while(1)
{
spiData=SPI_SlaveReceive();
if( (k%32)==0 )
{
USART_Transmit( '\r' );USART_Transmit( '\n' );
}
k++;
USART_Transmit( spiData );
//USART_Transmit( 70 );
}
}
This is quite simple code set,one microcontroller (master) sends a character 'a' continuously to the other microcontroller (slave) through SPI.The microcontroller which receives data from master displays it on virtual terminal through serial port
This is just tom give an idea how you can do spi-to-spi communication in master-slave mode
Posted on: 16-11-2009, 10:38:37 - Automerged
Here is the proteus simulation file named 'SPI32SPI32', for the above codes,attached with this post