|
150黑幣
這個是SPI初始化代碼
void SPIx_I2Cx_GPIOs_Init(void* bus_type)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD |
RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, DISABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD |
RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE);
//SPI1_NSS
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;//PA4
GPIO_Init(GPIOA,&GPIO_InitStructure);
//SPI1_CLK
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;//PA5
GPIO_Init(GPIOA, &GPIO_InitStructure);
//SPI_MISO
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;//PA6
GPIO_Init(GPIOA, &GPIO_InitStructure);
//SPI_MOSI
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;//PA7
GPIO_Init(GPIOA, &GPIO_InitStructure);
SPI_I2S_DeInit(bus_type);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;//PD4
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//PD3
GPIO_Init(GPIOD, &GPIO_InitStructure);
// SPIx Enable = 0 for SPI Mode // SPIx Enable = RSN for GP22
GPIO_WriteBit(GPIOD, GPIO_Pin_3, Bit_RESET);
}
void SPIx_I2Cx_Interface_Init(void* bus_type)
{
SPI_InitTypeDef SPI_InitStructure;
//RCC_SYSCLKConfig (RCC_SYSCLKSource_HSI);
//RCC_HCLKConfig (RCC_SYSCLK_Div1);
RCC_APB2PeriphResetCmd (RCC_APB2Periph_SPI1, DISABLE);
RCC_APB2PeriphClockCmd (RCC_APB2Periph_SPI1, ENABLE);
// All are defined in stm32f10x_spi.h
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;//
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
// SPI frequence devider
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
// Apply SPIx configuration
SPI_Init(bus_type, &SPI_InitStructure);
// Enabling the SPIx Interface
SPI_Cmd(bus_type, ENABLE);
// Enabling the NSS Output during transmission
// SPI_SSOutputCmd (bus_type, ENABLE);
GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_SET);
}
這個是應用代碼:
unsigned char Send_24Bit_Opcode(unsigned char command, unsigned short address, unsigned char data)
{
unsigned char Result_read = 0;
// x >> y, mean x is shifted by y bit-positions to the right
unsigned char Byte_0 = data;
unsigned char Byte_1 = address;
unsigned char Byte_2 = address>>8 | command;
// SPIx BUS_TYPE -------------------------------------------------------------
// Deactivating Reset SPIx
GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_RESET);
// GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_RESET);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)==0) {}
SPI_I2S_SendData(SPI1, Byte_2); // send byte 2
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)==0) {}
SPI_I2S_SendData(SPI1, Byte_1); // send byte 1
if (command==0x90) // write command
{
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)==0) {}
SPI_I2S_SendData(SPI1, Byte_0); // send byte 0
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)==0) {}
Simple_delay_43ns((void*)10); // important delay (16) at SPI freq.=750kHz
}
if (command==0x10) // read command
{
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)==RESET) {};
Simple_delay_43ns((void*)10); // important delay (16) at SPI freq.=750kHz
//Compulsory reads to DR and SR to clear OVR,
//so that next incoming data is saved
SPI_I2S_ReceiveData(SPI1); // To clear OVR
SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE); // To clear OVR
//Reading byte1
SPI_I2S_SendData(SPI1, 0xFF); // DUMMY WRITE
// Wait until RX buffer is not empty, then read the received data
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE)==0) {}
Result_read = SPI_I2S_ReceiveData(SPI1); // Read
}
// Reset to device SPIx
GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_SET);
//GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_SET);
return Result_read;
}
Send_24Bit_Opcode(0x90,1,0x55);//0x90 寫
err = Send_24Bit_Opcode(0x10,1,0xff);//err = 0x55 0x10讀
在使用的過程中,怎么弄也讀不出來正確的數值,現在讀出來的都是0xff。請大家看看究竟是哪出現了問題。
情景是:
用STM32控制器件,通訊使用SPI1,器件要求spi: CPOL=0;CPHA=1;MODE=1;DORD為0,MSB最先輸出。
|
最佳答案
查看完整內容
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; 由軟件控制就不需要 SPI_SSOutputCmd (bus_type, ENABLE);
|