博士
最后登录1970-1-1
在线时间 小时
注册时间2016-4-7
|
100火花
我最近在写定时器使能SPI-DMA传输的实验,一开始测PWM信号频率和脉宽都没有错误,但是SPI数据总是会在中间出现那么几个错误的,经过调试发现是定时器有时进中断不及时导致后面传输的数据错位了,下面贴上定时器的配置代码,用的是100K的PWM频率,请大神帮忙看看
//配置定时器中断,在中断中使能SPI1-TX-DMA 触发SPI1SCLK时钟信号
void TIM_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
//设置中断组为0
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
//设置中断源
NVIC_InitStructure.NVIC_IRQChannel = TIM8_UP_TIM13_IRQn;
//设置抢占优先级
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
//设置子优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/**
* period :设置30以1MHz采样,
* 设置300以100KHz采样,
* 设置3000以10KHz采样,
* 设置30000以1KHz采样
* 默认采用100KHz采样
**/
void TIM1_Init(u16 period)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOC,GPIO_PinSource6,GPIO_AF_TIM8);
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //时钟分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //计数模式向上计数
TIM_TimeBaseStructure.TIM_Prescaler = 6-1; //Timer Clock = 180 / (TIM_Prescaler + 1) = 30M
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; //重复计数器,计数器产生TIM_RetitionCounter+1次置位时产生一次中断,这里一次PWM进一次中断
TIM_TimeBaseStructure.TIM_Period = period - 1; //定时器周期,即设定自动重载值
TIM_TimeBaseInit(TIM8,&TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //PWM2为反相占空比模式
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //TIM8_CH1输出使能
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable; //TIM8_CH1N不使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //高电平有效
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //输出反相TIM_OCNPolarity_Low;//输出同相
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; //空闲状态是通道输出电平
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
TIM_OCInitStructure.TIM_Pulse = period*2/3; //PC6作为AD的convst信号
TIM_OC1Init(TIM8,&TIM_OCInitStructure);
// 清除定时器更新中断标志位
TIM_ClearFlag(TIM8, TIM_FLAG_Update);
// 开启定时器更新中断
TIM_ITConfig(TIM8,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM8,ENABLE);
TIM_CtrlPWMOutputs(TIM8,ENABLE);
}
void TIM8_UP_TIM13_IRQHandler (void)
{
static uint32_t time;
SPI_AD7608_CS_HIGH();
if ( TIM_GetITStatus( TIM8, TIM_IT_Update) != RESET )
{
DMA_ClearITPendingBit(DMA2_Stream3,DMA_IT_TCIF3); //清除DMA2_Stream3中断标记位
DMA_Cmd(DMA2_Stream3,ENABLE); //使能DMA2_Stream3
SPI_DMACmd(SPI1,SPI_DMAReq_Tx,ENABLE); //使能SPI_TX DMA
TIM_ClearITPendingBit(TIM8 , TIM_IT_Update); //清除定时器中断标记位
SPI_AD7608_CS_LOW();
}
}
下面是测得的PWM信号波形
下面是测得的CS脚波形
|
|