О, нашел в записной книге (только писалось это в 2007 году, часть кода может быть неверным), тут видно как конфигурится МК в Мастера и передает байт по SPI:
/************************************************** ***
This program was produced by the
CodeWizardAVR V1.24.8d Professional
Automatic Program Generator
© Copyright 1998-2006 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project :
Version :
Date : 21.06.2007
Author : F4CG
Company : F4CG
Comments:
Chip type : ATmega8
Program type : Application
Clock frequency : 10,000000 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 256
************************************************** ***/
#include ‹mega8.h›
#include ‹delay.h›
unsigned long i1=0;
// SPI interrupt service routine
interrupt [SPI_STC] void spi_isr(void)
{
unsigned char data;
data=SPDR;
// Place your code here
}
void write_config_data (unsigned int config_word){
PORTB.0=0; // nCS
SPDR=config_word/256;
while (SPSR.7!=1){};
SPDR=config_word%256;
while (SPSR.7!=1){};
PORTB.0=1; // nCS
}
// Declare your global variables here
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=Out Func4=In Func3=Out Func2=Out Func1=In Func0=In
// State7=T State6=T State5=0 State4=T State3=0 State2=0 State1=T State0=T
PORTB=0x00;
DDRB=0x2C;
// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
MCUCR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// SPI initialization
// SPI Type: Master
// SPI Clock Rate: 78,125 kHz
// SPI Clock Phase: Cycle Half
// SPI Clock Polarity: Low
// SPI Data Order: MSB First
SPCR=0x53;
SPSR=0x00;
PORTB=0x00;
DDRB=0x2F; // 0.0.1.0.1.1.1.1
PORTC=0x00;
DDRC=0x03; // 0.0.0.0.0.0.1.1
PORTD=0x00;
DDRD=0xA8; // 1.0.1.0.1.0.0.0
PORTB.1=1; // nFSEL
PORTD.7=1;
PORTD.3=1;
PORTD.5=1;
for(i1=1; i1‹90000; i1++); // delay_ms(500)
PORTD.7=0;
PORTD.3=0;
PORTD.5=0;
write_config_data (0x80D0); // Reg_1_Configuration Register
write_config_data (0xC4E7); // Reg_2_Automatic Frequency Adjust Register
write_config_data (0x9880); // Reg_3_Transmit Configuration Register
write_config_data (0xA620); // Reg_5_Frequency Setting Register
write_config_data (0x9580); // Reg_6_Receiver Control Register
write_config_data (0xC22C); // Reg_7_Baseband Filter Register
write_config_data (0xCA83); // Reg_9_FIFO and RESET Mode Configuration Register
write_config_data (0xC611); // Reg_10_Data Rate Setup Register
write_config_data (0x8259); // Reg_11_Power Management Register
write_config_data (0xC01F); // Reg_14_Battery Detect Threshold and Clock Output Register
// Clear the SPI interrupt flag
#asm
in r30,spsr
in r30,spdr
#endasm
// Global enable interrupts
#asm("sei")
while (1)
{
PORTD.7=1;
for(i1=1; i1‹90000; i1++); // delay_ms(500)
PORTD.7=0;
write_config_data (0x8279); // Reg_11_Power Management Register (TX=on)
for(i1=1; i1‹50000; i1++); // delay_ms(500)
PORTB.0=0; // nCS
SPDR=0xB8;
while (SPSR.7!=1){};
//while (PINB.4!=0){};
SPDR=0x2D;
while (SPSR.7!=1){};
//while (PINB.4!=0){};
SPDR=0xD4;
while (SPSR.7!=1){};
//while (PINB.4!=0){};
SPDR=0xAA;
while (SPSR.7!=1){};
//while (PINB.4!=0){};
PORTB.0=1; // nCS
for(i1=1; i1‹50000; i1++); // delay_ms(500)
write_config_data (0x8259); // Reg_11_Power Management Register (TX=off)
for(i1=1; i1‹90000; i1++); // delay_ms(500)
};
}
================================================== ========
А тут ведется прием, вроде:
/************************************************** ***
This program was produced by the
CodeWizardAVR V1.24.8d Professional
Automatic Program Generator
© Copyright 1998-2006 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project :
Version :
Date : 23.06.2007
Author : F4CG
Company : F4CG
Comments:
Chip type : ATmega8
Program type : Application
Clock frequency : 10,000000 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 256
************************************************** ***/
#include ‹mega8.h›
#include ‹delay.h›
unsigned char count_1,count_3,count_4,Bytes[5];
int i1=0,i=0;
// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{
// Place your code here
}
#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7
#define FRAMING_ERROR (1‹‹FE)
#define PARITY_ERROR (1‹‹UPE)
#define DATA_OVERRUN (1‹‹OVR)
#define DATA_REGISTER_EMPTY (1‹‹UDRE)
#define RX_COMPLETE (1‹‹RXC)
// USART Receiver buffer
#define RX_BUFFER_SIZE 8
char rx_buffer[RX_BUFFER_SIZE];
#if RX_BUFFER_SIZE‹256
unsigned char rx_wr_index,rx_rd_index,rx_counter;
#else
unsigned int rx_wr_index,rx_rd_index,rx_counter;
#endif
// This flag is set on USART Receiver buffer overflow
bit rx_buffer_overflow;
// USART Receiver interrupt service routine
interrupt [USART_RXC] void usart_rx_isr(void)
{
char status,data;
status=UCSRA;
data=UDR;
PORTD.3=1;
for(i1=0; i1‹10000;i1++); // delay_ms(1)
PORTD.3=0;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
{
rx_buffer[rx_wr_index]=data;
if (++rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0;
if (++rx_counter == RX_BUFFER_SIZE)
{
rx_counter=0;
rx_buffer_overflow=1;
};
};
Bytes[count_1]=data;
count_1++;
if (count_1==3)
{
count_1=0;
if (Bytes[0]==129) // Write TX data
{
PORTB.0=0; // nCS
for(i=1; i‹3; i++)
{
SPDR=Bytes[i]; // Bytes send
while(SPSR.7!=1){};
}
PORTB.0=1; // nCS
}
if (Bytes[0]==130) // Write config data
{
PORTB.0=0; // nCS
for(i=1; i‹3; i++)
{
SPDR=Bytes[i]; // Byte_1 send
while(SPSR.7!=1){};
}
PORTB.0=1; // nCS
}
if (Bytes[0]==132) // Read Stat Reg and FIFO
{
count_4=28;
}
}
}
#ifndef _DEBUG_TERMINAL_IO_
// Get a character from the USART Receiver buffer
#define _ALTERNATE_GETCHAR_
#pragma used+
char getchar(void)
{
char data;
while (rx_counter==0);
data=rx_buffer[rx_rd_index];
if (++rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;
#asm("cli")
--rx_counter;
#asm("sei")
return data;
}
#pragma used-
#endif
// Standard Input/Output functions
#include ‹stdio.h›
// SPI interrupt service routine
interrupt [SPI_STC] void spi_isr(void)
{
unsigned char data;
data=SPDR;
/*
// Place your code here
count_3++;
Bytes[count_3]=data;
if (count_3==3)
{
count_3=0;
}
*/
}
// Declare your global variables here
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=Out Func4=In Func3=Out Func2=Out Func1=In Func0=In
// State7=T State6=T State5=0 State4=T State3=0 State2=0 State1=T State0=T
PORTB=0x00;
DDRB=0x2C;
// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x04;
DDRD=0xAA;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Falling Edge
// INT1: Off
//GICR|=0x40;
//MCUCR=0x02;
//GIFR=0x40;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud rate: 9600
UCSRA=0x00;
UCSRB=0x98;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x40;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// SPI initialization
// SPI Type: Master
// SPI Clock Rate: 78,125 kHz
// SPI Clock Phase: Cycle Half
// SPI Clock Polarity: Low
// SPI Data Order: MSB First
SPCR=0x53;
SPSR=0x00;
for(i1=0; i1‹5000000;i1++); // delay_ms(500)
PORTB=0x02;
DDRB=0x2F; // 0.0.1.0.1.1.1.1
PORTC=0x02;
DDRC=0x03; // 0.0.0.0.0.0.1.1
PORTD=0x04;
DDRD=0xAA; // 1.0.1.0.1.0.1.0
PORTD.3=1;
for(i1=0; i1‹10000;i1++); // delay_ms(1)
PORTD.3=0;
PORTD.5=1;
for(i1=0; i1‹10000;i1++); // delay_ms(1)
PORTD.5=0;
PORTD.7=1;
for(i1=0; i1‹10000;i1++); // delay_ms(1)
PORTD.7=0;
// Clear the SPI interrupt flag
#asm
in r30,spsr
in r30,spdr
#endasm
// Global enable interrupts
#asm("sei")
while (1)
{
/*
// Place your code here
count_4++;
if (count_4==29) // Read Stat Reg
{
//GICR=0x00; // Int0 Dis.
SPCR=0xD3; // SPI Int. En.
PORTB.0=0; // nCS
for(i1=0; i1‹10000; i1++); // delay_ms(1)
for(i=1; i‹3; i++)
{
SPDR=0; // Bytes send
for(i1=0; i1‹10000;i1++); // delay_ms(1)
}
delay_ms(1);
SPDR=Byte_1; // Byte_1 read (SReg)
delay_ms(1);
SPDR=Byte_2; // Byte_2 read (SReg)
delay_ms(1);
SPDR=Byte_3; // Byte_3 read (FIFO)
delay_ms(1);
PORTB.0=1; // nCS
SPCR=0x53; // SPI Int. Dis.
delay_ms(1);
UDR=Byte_0; // Return Byte_0
delay_ms(80);
UDR=Byte_1; // Return Byte_1
delay_ms(80);
UDR=Byte_2; // Return Byte_2
delay_ms(80);
UDR=131; // Return 131
delay_ms(80);
UDR=Byte_3; // Return Byte_3
delay_ms(80);
UDR=Byte_3; // Return Byte_3
delay_ms(80);
counter4=0;
//GICR|=0x40; // Falling Edge
//MCUCR=0x02;
//GIFR=0x40;
Byte_3=0;
//Byte_3=SPSR.7;
}
if (count_4==10)
{
counter4=0;
}
*/
};
}
Принятые данные (любые) отправлялись на ПК через RS232. Только спустя столько времени не ручаюсь за правильность всего года и разумность строк и решений.