用两个Atmega16的单片机进行SPI通讯。收到神么就在C管脚显示什么
以下是proteus的截图:虽然数码管接的不对但管脚可以看出数据是正确的
主机程序:
/*
* GccApplication5.c
*
* Created: 2014-9-29 19:46:18
* Author: Administrator
*/
#include <avr/io.h>
#define DD_SS 4 //ss
#define DD_MOSI 5 //MOSI
#define DD_SCK 7 //SCK
void SPI_MasterInit()
{
DDRB = (1<<DD_SS)|(1<<DD_MOSI)|(1<<DD_SCK); //设置ss,mosi,sck引脚下
SPCR = (1<<SPE)|(1<<SPR0)|(1<<MSTR); //enable SPI, mast, fosc/16
}
unsigned char SPI_MasterTransmit(char data)
{
SPDR = data;
while(!(SPSR&(1<<SPIF))); //等行发送完
return SPDR;
}
int main()
{
unsigned char data;
unsigned char i=0x88;
SPI_MasterInit();
while(1)
{
data=SPI_MasterTransmit(i);
}
}
从机程序:
/*
* GccApplication6.c
*
* Created: 2014-9-29 20:02:33
* Author: Administrator
*/
#include <avr/io.h>
#define DD_MISO 6
void SPI_SlaveInit(void)
{
DDRB=(1<<DD_MISO);
SPCR=(1<<SPE);
}
char SPI_SlaveReceive(void)
{
while(!(SPSR&(1<<SPIF)));
return SPDR;
}
int main(void)
{
DDRC = 0xFF;
SPI_SlaveInit();
while(1)
{
PORTC=SPI_SlaveReceive();
}
}
|