foxbrain
Full Member level 2
- Joined
- Feb 1, 2010
- Messages
- 142
- Helped
- 5
- Reputation
- 10
- Reaction score
- 4
- Trophy points
- 1,298
- Location
- Damas
- Activity points
- 2,322
Hi
i'm working on spi communication between atmega168(master) and atmega16(slave)
the problem is that the slave receives what the master send but the master can't read what the slave sends.here r the codes:
Atmega 16:
Atmega 168:
Please Help!!!!
Thanks
i'm working on spi communication between atmega168(master) and atmega16(slave)
the problem is that the slave receives what the master send but the master can't read what the slave sends.here r the codes:
Atmega 16:
Code:
#include <avr/io.h>
#include <util/delay.h>
#define SETBIT(ADDRESS,BIT) (ADDRESS |= (1<<BIT))
#define CLEARBIT(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT))
#define CHECKBIT(ADDRESS,BIT) (ADDRESS & (1<<BIT))
#define RF_PORT PORTB // SPI Port
#define RF_DDR DDRB
#define RF_PIN PINB
#define SDI 5 // MOSI Pinnummer
#define SCK 7 // SCK Pinnummer
#define CS 4 // CS Pinnummer
#define SDO 6 // MISO Pinnummer
//unsigned int d=0;
// Initialize SPI Slave Device
void del (unsigned int dela)
{
for (int i=0;i<dela;i++)
{
asm("NOP");
}
}
void spi_init_slave (void)
{
RF_DDR&=~((1<<SDO));
RF_DDR&=~((1<<SCK));
RF_DDR&=~((1<<CS));
RF_DDR|=(1<<SDI);
SPCR = (1<<SPE)|(1<<CPOL)|(1<<CPHA)|(1<<DORD); //Enable SPI
}
unsigned char spi_tranceiver (unsigned char data)
{
asm("NOP");
/////////
SPDR = data;
while(!(SPSR & (1<<SPIF)));
asm("NOP");
return(SPDR); //Return received data
}
int main(void)
{
unsigned char a=6;
unsigned char b;
asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
DDRD=0xFF;
spi_init_slave();
while(1)
{
SETBIT(PORTD,0);
_delay_ms(500);
CLEARBIT(PORTD,0);
_delay_ms(500);
b=spi_tranceiver(a);
if (b==5)
{
PORTD=0xFF;
_delay_ms(500);
PORTD=0x00;_delay_ms(500);
}
}
}
Code:
#include <avr/io.h>
#include <util/delay.h>
#define SETBIT(ADDRESS,BIT) (ADDRESS |= (1<<BIT))
#define CLEARBIT(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT))
#define CHECKBIT(ADDRESS,BIT) (ADDRESS & (1<<BIT))
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
#define RF_PORT PORTB // SPI Port
#define RF_DDR DDRB
#define RF_PIN PINB
#define SDI 3 // MOSI Pinnummer
#define SCK 5 // SCK Pinnummer
#define CS 2 // CS Pinnummer
#define SDO 4 // MISO Pinnummer
void spi_init_master (void)
{
//RF_PORT=(1<<CS);
RF_DDR&=~((1<<SDO));
RF_DDR|=(1<<SDI)|(1<<SCK)|(1<<CS);
SPCR=(1<<SPE)|(1<<MSTR)|(1<<CPOL)|(1<<CPHA)|(1<<DORD);
// SPSR |= (1<<SPI2X);
}
unsigned char spi_tranceiver (unsigned char data)
{
cbi(RF_PORT, CS);
SPDR = data; //Load data into the buffer
while(!(SPSR & (1<<SPIF)));
sbi(RF_PORT, CS);
return(SPDR); //Return received data
}
int main(void)
{
unsigned char a=5;
unsigned char b;
CLKPR=(1<<CLKPCE);
CLKPR=(0<<CLKPS0);
CLKPR=(0<<CLKPS1);
CLKPR=(1<<CLKPS2);
CLKPR=(0<<CLKPS3);
DDRD=0xFF;
DDRC=0xFF;
spi_init_master();
asm("NOP");asm("NOP");
while(1)
{
SETBIT(PORTC,0);
_delay_ms(500);
CLEARBIT(PORTC,0);
_delay_ms(500);
b=spi_tranceiver(a);
if (b==6)
{
PORTD=0xFF;
_delay_ms(500);
PORTD=0x00;_delay_ms(500);
}
}
}
Thanks