stm32F103RE,中采用TIM1的1通道和4通道进行PWM波输出和停止,其对SPI2的通信有影响。
我用的是STM32F103RE的芯片,TIM1的1通道和4通道进行两路PWM波的输出和停止,但是我发现他会影响SPI2的通信,在SPI2通信前,我让PWM波停止,就会使我的SPI2两个收发数据线上都是高电平,
我的PWM停止函数如下:
void stop_emit_electorde(void)
{
TIM_Cmd(TIM1, DISABLE);
TIM_CtrlPWMOutputs(TIM1, DISABLE);
}
于是我在函数一开始增加了“TIM_DeInit(TIM1);”就不会影响SPI通信了,有人遇到过这种情况吗?知道为什么吗?
我的源PWM程序:
u8 pwm_output = PWM_OUTPUT_A11;//用于两个通道的PWM波切换
void init_timer_gpio(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//¸´Óù¦ÄÜÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
init_emit_electrode();
}
//PWM波的配置,及使能
void init_emit_electrode(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_DeInit(TIM1);
/* Time base configuration */
// TIM_TimeBaseStructure.TIM_Period = 49;
TIM_TimeBaseStructure.TIM_Period = 50;
TIM_TimeBaseStructure.TIM_Prescaler = 5;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse = 25;
if(pwm_output == PWM_OUTPUT_A10)
{
pwm_output = PWM_OUTPUT_A11;
delay_ms(2);
// TIM1->CCER = TIM1->CCER & 0xEFFF;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
}
else
{
pwm_output = PWM_OUTPUT_A10;
// TIM1->CCER = TIM1->CCER & 0xFFFE;
TIM_OC4Init(TIM1, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
}
TIM_ARRPreloadConfig(TIM1, ENABLE);
/* TIM enable counter */
TIM_Cmd(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
/* TIM IT enable */
//TIM_ITConfig(TIM1, TIM_IT_CC1 | TIM_IT_CC2, ENABLE);
}
//停止发射PWM波
void stop_emit_electorde(void)
{
TIM_Cmd(TIM1, DISABLE);
TIM_CtrlPWMOutputs(TIM1, DISABLE);
}
我的SPI2部分的程序,列出用到的部分:
void w25q16_gpio_init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC , ENABLE);
/*!< SPI_FLASH_SPI Periph clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
/*----------SPI2_CS-----------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/*!< Configure SPI_FLASH_SPI pins: SCK PB13*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/*-------------------MISO PB14----------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/*!< Configure SPI_FLASH_SPI pins: MOSI PB15*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
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_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; //ÉèÖÃSPIËÙÂÊ
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStructure);
// SPI_SSOutputCmd(SPI2,ENABLE);
/* Enable SPI2 */
SPI_Cmd(SPI2, ENABLE);
SPI_FLASH_SendByte(0xFF);
/* Deselect the FLASH: Chip Select high */
SPI2_Flash_CS_HIGH();
}
//-----------flash读函数
void Flash_Read_Data(u32 addr, u32 len, u8* dst)
{
u8 adr1,adr2,adr3;
u32 i = 0;
// SPI2_SetSpeed(SPI_BaudRatePrescaler_4);
adr1 = (addr & 0xff0000)>>16;
adr2 = (addr & 0xff00)>>8;
adr3 = addr & 0xff;
SPI2_Flash_CS_LOW();
delay_us(2);
SPI_FLASH_SendByte(READ_DATA);//¶ÁÊý¾ÝÖ¸Áî
SPI_FLASH_SendByte(adr1);
SPI_FLASH_SendByte(adr2);
SPI_FLASH_SendByte(adr3);
for(i=0;i<len;i++)
{
dst[i] = SPI_FLASH_SendByte(0xFF);
}
SPI2_Flash_CS_HIGH();
}
//----Page Program-------------------------------------
void Flash_PagePro(u8 *Pdat,u32 addr,u16 Len)
{
u8 padr1,padr2,padr3;
u8 i;
padr1 = (addr & 0xff0000)>>16;
padr2 = (addr & 0xff00)>>8;
padr3 = addr & 0xff;
Flash_WaitforWriteEnd();
Flash_Write_EnorDis(ENABLE);
SPI2_Flash_CS_LOW();
SPI_FLASH_SendByte(PAGE_PROGRAM);//Ò³±à³Ì0x02
SPI_FLASH_SendByte(padr1);
SPI_FLASH_SendByte(padr2);
SPI_FLASH_SendByte(padr3);
for(i=0;i<Len;i++)
{
SPI_FLASH_SendByte(Pdat[i]);
}
SPI2_Flash_CS_HIGH();
Flash_WaitforWriteEnd();
}
//----------------------------------------------------------------
//----Sector Erase---------------------------------------------
void Flash_Sector_Erase(u32 addr)
{
u8 sadr1,sadr2,sadr3;
sadr1 = (addr & 0xff0000)>>16;
sadr2 = (addr & 0xff00)>>8;
sadr3 = addr & 0xff;
Flash_WaitforWriteEnd();
Flash_Write_EnorDis(ENABLE);//0x06
SPI2_Flash_CS_LOW();
SPI_FLASH_SendByte(SECTOR_ERASE);
SPI_FLASH_SendByte(sadr1);
SPI_FLASH_SendByte(sadr2);
SPI_FLASH_SendByte(sadr3);
SPI2_Flash_CS_HIGH();
delay_ms(50);
Flash_WaitforWriteEnd();
}
我用逻辑分析仪抓SPI通信的时候,发现受TIM1 pwm波配置的影响,我的SPI通信异常:片选信号和时钟信号均正常,但是MOSI和MISO都是高电平,即写入进去指令,也读不出数据?不知道是什么原因,按照文章开都的方法是解决了,但是并没有找到原因