hi
I am testing SPI interface for use with the Rf 24g. I am now testing SPI comunication between a atmega8(slave) and a atmega 16 (master) the problem i am having is sending data from slave to master. I am geting data from a ADC, and when I turn the mcu’s on it recives the data once, and no more. I want it to send/recive every 0.5 seconds.
I have no problem with the master to slave comunication.
The principle is that the slave reads the ADC and puts the result in SPDR register, it is then sendt, and when the entire byte is clocked into the Master the SPI innterupt occurs and puts the recived byte into “DATA” and prints it for me to see.
here is the code:
MASTER:
#include <mega16.h>
#include <stdio.h>
#include <delay.h>
// SPI isr
interrupt [SPI_STC] void spi_isr(void)
{
unsigned char data;
data = SPDR ;
printf ("\n\r%d", data ) ;
}
void main(void)
{
// Port B initialization
PORTB=0x00;
DDRB=0xB0;
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;
// SPI initialization
// SPI Type: Master
// SPI Clock Rate: 2000,000 kHz
// SPI Clock Phase: Cycle Half
// SPI Clock Polarity: Low
// SPI Data Order: MSB First
SPCR=0xD1;
SPSR=0x00;
// Clear the SPI interrupt flag
#asm
in r30,spsr
in r30,spdr
#endasm
// Global enable interrupts
#asm("sei")
SPDR = 0x00 ;
while (1)
{
};
}
SLAVE:
#include <mega8.h>
#include <stdio.h>
#include <delay.h>
#define ADC_VREF_TYPE 0x60
unsigned char read_adc(unsigned char adc_input)
{
ADMUX=adc_input|ADC_VREF_TYPE;
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCH;
}
// SPI isr
interrupt [SPI_STC] void spi_isr(void)
{
}
void main(void)
{
// Input/Output Ports initialization
// Port B initialization
PORTB=0x00;
DDRB=0x10;
// USART initialization
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;
// SPI initialization
// SPI Type: Slave
// SPI Clock Rate: 2000,000 kHz
// SPI Clock Phase: Cycle Half
// SPI Clock Polarity: Low
// SPI Data Order: MSB First
SPCR=0xC0;
SPSR=0x00;
// Clear the SPI interrupt flag
#asm
in r30,spsr
in r30,spdr
#endasm
// Global enable interrupts
#asm("sei")
// ADC Clock frequency: 62,500 kHz
// ADC Voltage Reference: AVCC pin
// 8-bit
ADMUX=ADC_VREF_TYPE;
ADCSRA=0x87;
while (1)
{
read_adc(0x01);
printf ("\n\r%d", ADCH ) ; //printing for debug
SPDR = ADCH ;
delay_ms (500) ;
};
}