哪位大侠指点一下,我用2块板子进行SPI通信,主机发送数据给从机,而从机好像并没有收到数据,程序如下:
从机:
void Slaver_Init()
{
DDRB |= ((~(1<<PB0))|(~(1<<PB1))|(~(1<<PB2))|(1<<PB3)); //PB2:MOSI输入 PB3:MISO输出 SS输入
SPCR = (1<<SPIE)|(1<<SPE)|(~(1<<MSTR))|(1<<SPR0);
SPSR = 0x0;
}
char SPI_SlaveReceive(void)
{
SPDR = 0x0;
/* 等待接收结束 */
while(!(SPSR & (1<<SPIF)));
/* 返回数据 */
return SPDR;
}
SIGNAL(SIG_SPI) //SPI中断函数 SPI_STC_vect SIG_SPI
{
buzzering(200, 4000);
rdata = SPI_SlaveReceive();
}
volatile static char rdata = 0;
int main()
{
Slaver_Init();
while(1)
{
if (rdata != 0)
{
//重新恢复主机SPI模式,进行LCD显示数据
cli();
lcd_init();
sei();
lcd_show_a_num8x16(2, 2, 7);
lcd_show_a_num8x16(4, 2, rdata);
rdata = 0;
_delay_ms(1000);
cli();
Slaver_Init();
sei();
} else{
continue;
}
}
}
主机:
void Master_Init()
{
DDRB |= ((1<<PB0)|(1<<PB1)|~((1<<PB3))|(1<<PB2)|(1<<PB4)|(1<<PB5)); //PB2 MOSI 输出
/* 使能SPI 主机模式,设置时钟速率为fck/16 */
SPCR = (1<<SPIE)|(1<<SPE)|(1<<MSTR)|(1<<SPR0);
SPSR = 0x0;
}
void SPI_MasterTransmit(char cData)
{
/* 启动数据传输 */
SPDR = cData;
/* 等待传输结束 */
while(!(SPSR & (1<<SPIF)));
}
void Transmit(char cData)
{
Load_Low;
SPI_MasterTransmit(cData);
Load_High;
}
int main()
{
Master_Init();
keyFlag = 0;
while(1)
{
keyFlag = key_scan();//扫描按键
if(keyFlag)
{
keyFlag = 0;
Transmit(2);
} else {
continue;
}
}
return 0;
}
主要SPI程序如上,希望大虾帮忙看下 |