STM32---SPI通信的总结(库函数操作)
参考代码:
1 void SPI_GPIO_Init(void) 2 { 3 GPIO_InitTypeDef GPIO_InitStructure; 4 SPI_InitTypeDef SPI_InitStructure; 5 6 NVIC_InitTypeDef NVIC_InitStructure; 7 8 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE );//PORTB时钟使能 9 RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE );//SPI2时钟使能 10 11 /* DMA1 clock enable */ 12 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, DISABLE); 13 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); 14 15 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; 16 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //PB13/14/15复用推挽输出 17 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 18 GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOB 19 20 GPIO_SetBits(GPIOB,GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15); //PB13/14/15上拉 21 22 SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //设置SPI单向或者双向的数据模式:SPI设置为双线双向全双工 23 SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //设置SPI工作模式:设置为主SPI 24 SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //设置SPI的数据大小:SPI发送接收8位帧结构 25 SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //串行同步时钟的空闲状态为高电平 26 SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //串行同步时钟的第二个跳变沿(上升或下降)数据被采样 27 SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS信号由硬件(NSS管脚)还是软件(使用SSI位)管理:内部NSS信号有SSI位控制 28 SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; //定义波特率预分频的值:波特率预分频值为256 29 SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //指定数据传输从MSB位还是LSB位开始:数据传输从MSB位开始 30 SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC值计算的多项式 31 SPI_Init(SPI2, &SPI_InitStructure); //根据SPI_InitStruct中指定的参数初始化外设SPIx寄存器 32 33 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); 34 NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel5_IRQn; 35 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; 36 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; 37 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 38 NVIC_Init(&NVIC_InitStructure); 39 40 //SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx, DISABLE); 41 SPI_Cmd(SPI2, ENABLE); 42 }
1 void DMA_LCD_Send( const UINT8 *ptdate,UINT16 len) 2 { 3 DMA_InitTypeDef DMA_InitStructure; 4 5 OLED_DC_Set();//写数据 6 OLED_CS_Clr(); 7 DMA_DeInit(DMA1_Channel5); 8 DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&SPI2->DR; 9 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ptdate;//SPI2_Tx_Buf; 10 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; 11 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; 12 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; 13 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; 14 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; 15 DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; 16 DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; 17 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; 18 DMA_Init(DMA1_Channel5, &DMA_InitStructure); 19 DMA_Cmd(DMA1_Channel5, DISABLE ); //关闭DMA1 所指示的通道 20 21 DMA_SetCurrDataCounter(DMA1_Channel5,len);//DMA通道的DMA缓存的大小 22 //DMA_ClearFlag(DMA1_FLAG_TC5);//清除通道传输完成标志 23 DMA_ClearITPendingBit(DMA1_IT_TC5); 24 25 SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx, ENABLE); 26 DMA_ITConfig(DMA1_Channel5, DMA_IT_TC, ENABLE); 27 DMA_Cmd(DMA1_Channel5, ENABLE); //使能USART1 TX DMA1 所指示的通道 28 29 //while(RESET == DMA_GetFlagStatus(DMA1_FLAG_TC5)); //判断通道传输完成 30 //DMA_ClearFlag(DMA1_FLAG_TC5);//清除通道传输完成标志 31 }
1 void DMA1_Channel5_IRQHandler(void) 2 { 3 if (DMA_GetITStatus(DMA1_IT_TC5)) { 4 DMA_ClearITPendingBit(DMA1_IT_TC5); 5 //DMA_Cmd(DMA1_Channel5, DISABLE); 6 OLED_CS_Set(); 7 } 8 }
LCD刷一张图:
image_100是图片数组:
1 void LCD_ShowPicture_50(UINT16 x,UINT16 y) 2 { 3 //UINT8 *pt=&image_100[0]; 4 LCD_Address_Set(x,y,x+99,y+99); 5 6 DMA_LCD_Send(&image_100[0],20000); 7 }