MSPF430F5328 用p4.1 p4.2 p4.3 SCK SI SO;用查询发送和接收,为什么不行呢?代码如下:
void SPIFlash_Init(void)
{
//P4.3,2,1 option select
P4OUT|=BIT0;//CS=H
P4DIR |=BIT0;//|(1<<BIT1)|(1<<BIT3);//P4.0 P4.1 P4.3 output
//P4DIR &=~(1<<BIT2); //p4.2 input
P4SEL |=(1<<BIT1)|(1<<BIT2)|(1<<BIT3);//04.1 p4.2 p4.3 SCK SI SO
UCB1CTL1 |=UCSWRST;//SPI enable
UCB1CTL0=0X29;//同步
UCB1CTL1 |=SPI_CLOCKSOURCE_SMCLK;
UCB1BR0 =0X02; UCB1BR1 =0; //0分频
//UCB1IE=0X03;
UCB1CTL1 &=~UCSWRST;//SPI enable
__delay_cycles(100);
}
bool SPI_WriteData(unsigned char val)
{
UCB1TXBUF=val; while (!(UCB1IFG&UCTXIFG));
return TRUE;
}
unsigned char SPI_ReadData(void)
{
unsigned char val;
UCB1TXBUF=0x00;
while (!(UCB1IFG&UCTXIFG));
val=UCB1RXBUF;
return val;
}
请教高手,问题出在哪里?谢谢!
Peter_Zheng:
不用中断的可以类似于如下的代码
char TransceiveSPI (char txdata)
{
while (!(UCA0IFG & UCTXIFG)); // wait until last SPI-Transmission is finished
UCA0TXBUF = txdata; // send 8 clocks "txdata" and SPI-clocks
while (!(UCA0IFG & UCRXIFG)); // wait until one byte is received
rxdata = UCA0RXBUF;
}
BingGuo zhang:
在主模式下,需要把SIMO,CLK设置为输出,SOMI设置为输入吗?
Triton Zhang:
有测试管脚的波形没有? 是否管脚波形就没出来。
Peter_Zheng:
是的,clk输出,SIMO输出,SOMI输入。