最近有个项目需要用到AD7606,项目中需要AD7606采用100K速率采样,如此速率下如果采用中断方式显然不太合适,于是决定采样SPI+DMA+TIM1方式。TIM1在输出CONVET信号和CS信号,在输出CS信号时触发SPI DMA启动数据读取,可实践中发现触发一次时只能接收一个数据。于是再加入一个定时器4,定时器4和定时器1组成主从关系,定时器4产生CONVET信号和CS信号,在输出CS信号时启动TIM1由TIM1来启动DMA、SPI。但是TIM1用复位模式时,TIM1的计数就不受TIM4控制,而采用门控方式时TIM1产生的时钟在读取数据时会左右摆动,导致读取数据错位,现在的方式是采用门控方式加 TIM4中断,在中断里复位TIM1,程序如下:
void SPI1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);//使能GPIOA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);//使能SPI1时钟
//PA5,6,7初始化设置
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;//PA5~7复用功能输出
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化
GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);//PA5复用为 SPI1
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);//PA6复用为 SPI1
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);//PA7复用为 SPI1
//这里只针对SPI口初始化
RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,ENABLE);//复位SPI1
RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,DISABLE);//停止复位SPI1
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //为主SPI
SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b; //SPI发送接收16位帧结构
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;//Low; //串行同步时钟的空闲状态为低电平
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //串行同步时钟的第二个跳变沿(上升或下降)数据被采样
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS信号由软件管理
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; //波特率预分频值为4(21MHz)
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //数据从MSB位开始
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC值计算的多项式
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE); //使能SPI外设
}
void DMA_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
/* DMA disable*/
DMA_Cmd(DMA2_Stream2, DISABLE);
DMA_Cmd(DMA2_Stream3, DISABLE);
DMA_DeInit(DMA2_Stream2);
DMA_DeInit(DMA2_Stream3);
// SPI1 RX DMA 配置 Stream2
DMA_InitStructure.DMA_Channel = DMA_Channel_6;//1_6;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&SPI1->DR; //指定DMA的外设基地址为SPI1的数据地址
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)SPI1_Rx_Buff; //指定DMA的内存基地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; //DMA传输方向为读外设 写到内存
DMA_InitStructure.DMA_BufferSize = 4;//DataSize; //传输数量(0-65535,不能为0)
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //失能外设地址增长
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //使能内存地址增长 免去FOR循环
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //PSIZE=16bit
DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_HalfWord; //MSIZE=16bit
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //DMA_Mode_Normal; //DMA模式为非循环模式,非循环模式只进行单次传输。
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //优先权为高
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; //失能FIFO模式
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; //FIFO的阀值为半满
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; //内存突发传输为单一
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; //外设突发传输为单一
DMA_Init(DMA2_Stream2, &DMA_InitStructure); //初始化DMA2_Stream2
//DMA_ITConfig(DMA2_Stream2, DMA_IT_TC, ENABLE);//使能传输完成中断
// SPI1 TX DMA 配置 Stream3
DMA_InitStructure.DMA_Channel = DMA_Channel_6;//1_6;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&SPI1->DR; //指定DMA的外设基地址为SPI1的数据地址
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)SPI1_Tx_Buff; //指定DMA的内存基地址
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; //DMA传输方向为读内存,写外设
DMA_InitStructure.DMA_BufferSize = 4;//DataSize; //传输数量(0-65535,不能为0)
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //失能外设地址增长
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //失能内存地址增长
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //PSIZE=16bit
DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_HalfWord; //MSIZE=16bit
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//DMA_Mode_Normal;// //DMA模式为非循环模式,非循环模式只进行单次传输。
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium ; //优先权为中等
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; //失能FIFO模式
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; //FIFO的阀值为半满
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; //内存突发传输为单一
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; //外设突发传输为单一
DMA_Init(DMA2_Stream3, &DMA_InitStructure); //初始化DMA2_Stream3
//DMA_ITConfig(DMA2_Stream3, DMA_IT_TC, ENABLE); //因为是发送虚拟数据。不需要中断
/*************************************************************************/
/* Enable the SPI Rx DMA request */
//SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Rx, ENABLE);
//SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, ENABLE);
/* 打开DMA通道 */
/**************************************************************************/
/* DMA enable*/
DMA_Cmd(DMA2_Stream2, ENABLE);
DMA_Cmd(DMA2_Stream3, ENABLE);
}
void TIM1_Init(u16 period)//period设置24以1MHz采样,period设置240以100KHz采样,period设置2400以10KHz采样,period设置24000以1KHz采样,
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM1_BDTRInitStruct;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);//使能GPIOE时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8 |GPIO_Pin_9 | GPIO_Pin_10 |GPIO_Pin_11| GPIO_Pin_12 |GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;//模式必须为复用
//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;//频率为快速
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource8, GPIO_AF_TIM1);//PE8 作为 AD的/convst信号
GPIO_PinAFConfig(GPIOE, GPIO_PinSource9, GPIO_AF_TIM1);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource10, GPIO_AF_TIM1);//PE12 作为 AD的/CS信号
GPIO_PinAFConfig(GPIOE, GPIO_PinSource11, GPIO_AF_TIM1);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource12, GPIO_AF_TIM1);//PE12 作为 AD的/CS信号
GPIO_PinAFConfig(GPIOE, GPIO_PinSource13, GPIO_AF_TIM1);
/**************************************************************/
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;//模式必须为复用
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//普通输入模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100M
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource7, GPIO_AF_TIM1);//PE8 作为 AD的/convst信号
/**************************************************************/
//初始化
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //死区控制用。
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
TIM_TimeBaseStructure.TIM_Prescaler = 7 - 1; //Timer clock = 168M /(TIM_Prescaler+1) = 24M
TIM_TimeBaseStructure.TIM_RepetitionCounter = 4;
TIM_TimeBaseStructure.TIM_Period = period - 1;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
/****** 配置BDTR寄存器,配置死区时间****************/
/*
定时器时钟 72M TIM_ClockDivision = TIM_CKD_DIV1时, Tdts = 13.89ns
0 - 1.764us 用算法一
1.778us - 3.505us 用算法二
3.556us - 7.000us 用算法三
7.1117us - 14us 用算法四
需要更长时间,使用TIM_ClockDivision分频
*
TIM1_BDTRInitStruct.TIM_OSSRState = TIM_OSSRState_Disable; //工作模式下死区刹车
TIM1_BDTRInitStruct.TIM_OSSIState = TIM_OSSIState_Disable; //空闲模式下死区刹车
TIM1_BDTRInitStruct.TIM_LOCKLevel = TIM_LOCKLevel_OFF; // 锁定配置
TIM1_BDTRInitStruct.TIM_DeadTime = 205; //死区时间 72:1us 172:3us 205:5us
TIM_BDTRConfig(TIM1,&TIM1_BDTRInitStruct);//*/
//配置输出比较,产生PWM方波
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//PWM1为正常占空比模式,PWM2为反极性模式
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
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 = 10;//130 CS脉冲出现后多久出现时钟信号
TIM_OC1Init(TIM1,&TIM_OCInitStructure);//触发DMA2_Stream3 channel6
TIM_OCInitStructure.TIM_Pulse = 10;//ccr2;
TIM_OC2Init(TIM1,&TIM_OCInitStructure);//触发DMA2_Stream2 channel6
TIM_OCInitStructure.TIM_Pulse = 10;//230ccr3;//PE12 作为 AD的/CS信号
TIM_OC3Init(TIM1,&TIM_OCInitStructure);
//TIM_Cmd(TIM1,ENABLE);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
//TIM_SelectSlaveMode(TIM1,0x06);
//TIM_SelectInputTrigger(TIM1, TIM_TS_ITR3);
//TIM_SelectOnePulseMode(TIM1, TIM_OPMode_Repetitive);//TIM_OPMode_Single);
TIM_SelectSlaveMode(TIM1, TIM_SlaveMode_Gated);//TIM_SlaveMode_Gated);//TIM_SlaveMode_Reset);//TIM_SlaveMode_Gated); //TIM1门控模式
TIM_SelectInputTrigger(TIM1, TIM_TS_ITR3); //控制源TIM4
//TIM1->SMCR = 0x8035;//TIM1受控TIM4(复位模式)
/* TIM1 DMA 请求使能 */
TIM_DMACmd(TIM1, TIM_DMA_CC1 | TIM_DMA_CC2, ENABLE);//
//TIM_DMACmd(TIM1, TIM_DMA_CC2, ENABLE);//
TIM_Cmd(TIM1,DISABLE);//ENABLE);
}
void TIM4_Init(u16 period)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE); //TIM4时钟使能
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); //是能 PORTD时钟
GPIO_PinAFConfig(GPIOD,GPIO_PinSource14,GPIO_AF_TIM4); //GD14复用为TIM4
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14; //GPIO D14
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOD,&GPIO_InitStructure); //初始化PD14
GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM4);//PD14 作为 AD的/CS信号
GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_TIM4);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource14, GPIO_AF_TIM4);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource15, GPIO_AF_TIM4);
//初始化
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //死区控制用。
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;//Up; //向上计数
TIM_TimeBaseStructure.TIM_Prescaler = 7 - 1; //Timer clock = 168M /(TIM_Prescaler+1) = 24M
TIM_TimeBaseStructure.TIM_RepetitionCounter = 4;
TIM_TimeBaseStructure.TIM_Period = period - 1;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);
//配置输出比较,产生PWM方波
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//PWM1为正常占空比模式,PWM2为反极性模式
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;//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 = 202;//ccr1;//PD12 作为 AD_CS脉冲宽度
TIM_OC1Init(TIM4,&TIM_OCInitStructure);//触发DMA2_Stream3 channel6
TIM_OCInitStructure.TIM_Pulse = 5;//ccr2;
TIM_OC2Init(TIM4,&TIM_OCInitStructure);//触发DMA2_Stream2 channel6
TIM_OCInitStructure.TIM_Pulse = 5;//ccr3;//PD14 作为 AD的/CS信号
TIM_OC3Init(TIM4,&TIM_OCInitStructure);
// TIM_Cmd(TIM4,ENABLE);
TIM_CtrlPWMOutputs(TIM4,ENABLE);
TIM_SelectMasterSlaveMode(TIM4, TIM_MasterSlaveMode_Enable);
TIM_SelectOutputTrigger(TIM4, TIM_TRGOSource_OC1Ref);//TIM_TRGOSource_OC1Ref);//TIM_TRGOSource_OC1Ref);
/***********************************************************/
/*TIM2中断设置*/
TIM_ClearFlag(TIM4,TIM_IT_CC1);
TIM_ITConfig(TIM4,TIM_IT_CC1,ENABLE);
NVIC_SetPriorityGrouping(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/***********************************************************/
TIM_Cmd(TIM4,DISABLE);//ENABLE);
/* TIM4 DMA 请求使能 */
//TIM_DMACmd(TIM4, TIM_DMA_CC1 | TIM_DMA_CC2, ENABLE);//
}
现在的问题是1、能否做到在定时器触发时能够正常读取4个数据。2如果不能那就采用2个定时器方案,定时器在采用门控时能否启动从定时器时同时复位从定时器。规格书里是这样说的: |
|