|
发表于 2017-3-6 11:25:33
|
显示全部楼层
我用时序模拟的收发,但是收到的数据总是0,
// 通过SPI发送数据
void data_sent(unsigned char data)
{
unsigned char i=8;
GPIO_ResetBits(GPIOA,GPIO_Pin_4);//CS低电平有效
while(i)
{
if((data&0x80)>>7) GPIO_SetBits(GPIOA,GPIO_Pin_6);//SO
else GPIO_ResetBits(GPIOA,GPIO_Pin_6);//SO
data<<=1;
Delay_Us_SPI(2);//1M
GPIO_SetBits(GPIOA,GPIO_Pin_5);//SCK
Delay_Us_SPI(2);//1M
GPIO_ResetBits(GPIOA,GPIO_Pin_5);//SCK
i--;
}
}
// 通过SPI接收数据
unsigned char data_receive(void)
{
unsigned char i=8;
unsigned char data=0;
while(i)
{
Delay_Us_SPI(2);//1M
GPIO_SetBits(GPIOA,GPIO_Pin_5);//SCK
data<<=1;
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7)) data|0x01;
Delay_Us_SPI(2);//1M
GPIO_ResetBits(GPIOA,GPIO_Pin_5);//SCK
i--;
}
return(data);
}
//写入操作
void write(void)
{
unsigned int x;
//使能片外EEPROM
GPIO_ResetBits(GPIOA,GPIO_Pin_4);//CS低电平有效
data_sent(0x06);
delay();
GPIO_SetBits(GPIOA,GPIO_Pin_4);//CS低电平有效
delay();
//向片外EEPROM写入数据
GPIO_ResetBits(GPIOA,GPIO_Pin_4);//CS低电平有效
data_sent(0x02);
data_sent(0x00);
for(x=0;x<5;x++)
data_sent(datacun[x]); //连续写入5个字节
delay();
GPIO_SetBits(GPIOA,GPIO_Pin_4);//CS低电平有效
}
// 读取操作
void read(void)
{
unsigned int x;
//由片外EEPROM读取数据
GPIO_ResetBits(GPIOA,GPIO_Pin_4);//CS低电平有效
data_sent(0x03);
data_sent(0x00);
for(x=0;x<5;x++)
{
dataout[x] = data_receive();
USART_Byte_Send(USART1,dataout[x]);
}
delay();
GPIO_SetBits(GPIOA,GPIO_Pin_4);//CS低电平有效
} |
|