#define SPI SPI1
#define SPI_MOSI_BUSCLK RCC_AHBENR_GPIOA
#define SPI_MOSI_PIN GPIO_Pin_9
#define SPI_MOSI_PORT GPIOA
#define SPI_MOSI_AFSOURCE GPIO_PinSource9
#define SPI_MOSI_AFMODE GPIO_AF_0
#define SPI_NSS_BUSCLK RCC_AHBENR_GPIOA
#define SPI_NSS_PIN GPIO_Pin_0
#define SPI_NSS_PORT GPIOA
#define SPI_NSS_AFSOURCE GPIO_PinSource15
#define SPI_NSS_AFMODE GPIO_AF_0
#define SPI_MISO_BUSCLK RCC_AHBENR_GPIOA
#define SPI_MISO_PIN GPIO_Pin_10
#define SPI_MISO_PORT GPIOA
#define SPI_MISO_AFSOURCE GPIO_PinSource10
#define SPI_MISO_AFMODE GPIO_AF_0
#define SPI_SCK_BUSCLK RCC_AHBENR_GPIOA
#define SPI_SCK_PIN GPIO_Pin_8
#define SPI_SCK_PORT GPIOA
#define SPI_SCK_AFSOURCE GPIO_PinSource8
#define SPI_SCK_AFMODE GPIO_AF_0
static void SPI1_NVIC_Init(void)
{
/* 串口中断初始化 */
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
static void SPI1_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* 串口端口初始化 */
RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOA, ENABLE);
GPIO_StructInit(&GPIO_InitStructure);
//spi_cs
GPIO_InitStructure.GPIO_Pin = SPI_NSS_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(SPI_NSS_PORT, &GPIO_InitStructure);
//spi1_sck
GPIO_InitStructure.GPIO_Pin = SPI_SCK_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_FLOATING;
GPIO_Init(SPI_SCK_PORT, &GPIO_InitStructure);
//spi1_mosi
GPIO_InitStructure.GPIO_Pin = SPI_MOSI_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(SPI_MOSI_PORT, &GPIO_InitStructure);
//spi1_miso
GPIO_InitStructure.GPIO_Pin = SPI_MISO_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(SPI_MISO_PORT, &GPIO_InitStructure);
GPIO_PinAFConfig(SPI_MOSI_PORT, SPI_MOSI_AFSOURCE, SPI_MOSI_AFMODE);
GPIO_PinAFConfig(SPI_NSS_PORT, SPI_NSS_AFSOURCE, SPI_NSS_AFMODE);
GPIO_PinAFConfig(SPI_MISO_PORT, SPI_MISO_AFSOURCE, SPI_MISO_AFMODE);
GPIO_PinAFConfig(SPI_SCK_PORT, SPI_SCK_AFSOURCE, SPI_SCK_AFMODE);
}
static void SPI1_Config(unsigned short spi_baud_div)
{
SPI_InitTypeDef SPI_InitStructure;
SPI_StructInit(&SPI_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1ENR_SPI1,ENABLE);
/* Slave Mode */
SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_DataWidth = 8;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;
SPI_InitStructure.SPI_BaudRatePrescaler = (SPI_BaudRatePrescaler_TypeDef)spi_baud_div;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_Init(SPI_ &SPI_InitStructure);
SPI_SlaveAdjust(SPI, SPI_SlaveAdjust_FAST);
SPI_CSInternalSelected(SPI, ENABLE);
/* Enable SPI2 RXEN interrupt */
SPI_ITConfig(SPI, SPI_IT_RX, ENABLE);
SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Rx);
SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Tx);
SPI_Cmd(SPI, ENABLE);
}
void SPI_Config(unsigned short spi_baud_div)
{
SPI1_GPIO_Config();
SPI1_Config(spi_baud_div);
SPI1_NVIC_Init();
}
void SPI_Send(Use_Typedef *UserTick)
{
uint8 i, sd[5] = {0xAA, 0, 0, 0, 0xEE};
sd[1] = UserTick->AdcValue[UserTick->pNum][0];
sd[2] = UserTick->AdcValue[UserTick->pNum][1] >> 8;
sd[3] = UserTick->AdcValue[UserTick->pNum][1];
for(i = 0; i < 5; i++)
{
SPI_SendData(SPI, sd[ i ]);
}
}
|