先上图:STM32F303芯片,72M的主频
可以看到GPIO的达到了14.4M的翻转速率,
再来上代码:
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
GPIOC->MODER |= 0x55555555;
GPIOC->OSPEEDR |= 0xFFFFFFF;
GPIOC->
UPDR |= 0x55555555;
u32 Gpio_data[4]= {0x0000FFFF,0xFFFF0000,0x0000FFFF,0xFFFF0000};
DMA_InitTypeDef DMA_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* TIMx clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
/* DMAx clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
DMA_DeInit(DMA1_Channel7);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&(GPIOC->BSRR));
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Gpio_data;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = 4;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Enable;
DMA_Init(DMA1_Channel7, &DMA_InitStructure);
DMA1_Channel7->CCR |= DMA_CCR_EN;
用DMA内存到内存的模式,直接把Gpio_data的数据循环的搬到GPIOC的BSRR寄存器上来控制GPIOC上电平的翻转,这样使得GPIO的速度达到了最快,输出70ns的脉宽,这已经是达到了DMA总线带宽的极限,要想再提高速度的话,就得提高STM32芯片的主频。
再来看用代码来实现的话,GPIO能达到多快的速度。
先是用库函数来操作,在Main函数的While循环来翻转电平
while(1)
{
GPIO_Write(GPIOC,0xffff);
GPIO_Write(GPIOC,0x0000);
}
可以看到最快只能输出320ns的脉宽电平,而且高低电平脉宽还不一样,那是因为处理while(1)占用了CPU的时间导致的。
再来看用寄存器直接操作GPIO
while(1)
{
GPIOC->ODR = 0xFFFF;
GPIOC->ODR = 0x0000;
}
可以看到,最小脉宽电平可以达到40ns,直接操作寄存器的速度明显要到库函数操作要快好多,低电平的时间要比高电平的时间长很多,同样也是因为处理while的原因。
用CPU操作寄存器可以达到最快的电平翻转,但是这样CPU的资源全用在这上面,不能再做其实的操作。如果用DMA的话,完全可以不占用CPU资源来达到更高速率的GPIO翻转速度。假如我们要根据自己的需求来产生我们所需要的时序,那要怎样做。其实也很简单,我们只要用定时器触发DMA搬一次数据到BSRR寄存器,然后再通过调整Gpio_data数组里的数据,就可以来实现我们所要的时序。
下面的代码就是通过这种方式来实现让GPIOC产生1us脉宽的时钟,
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
GPIOC->MODER |= 0x55555555;
GPIOC->OSPEEDR |= 0xFFFFFFF;
GPIOC->
UPDR |= 0x55555555;
u32 Gpio_data[4]= {0x0000FFFF,0xFFFF0000,0x0000FFFF,0xFFFF0000};
DMA_InitTypeDef DMA_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* TIMx clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
/* DMAx clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
DMA_DeInit(DMA1_Channel7);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&(GPIOC->BSRR));
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Gpio_data;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = 4;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel7, &DMA_InitStructure);
TIM_TimeBaseStructure.TIM_Period = 72;
TIM_TimeBaseStructure.TIM_Prescaler = 0x0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_DMACmd(TIM4, TIM_DMA_Update, ENABLE);
TIM_SelectOutputTrigger(TIM4,TIM_TRGOSource_Update);
TIM_Cmd(TIM4, ENABLE);
DMA1_Channel7->CCR |= DMA_CCR_EN;
但GPIOE->ODR或GPIOE->BSRR却不行,用的STM32F407,下面是我的代码:
#define TIM4_CCR1_ADDRESS ((uint32)0x40000834) /* TIM4 CCR1 address */
#define GPIOE_ODR_ADDRESS ((uint32)0x40021014) /* address */
uint16 a_SRC_Buffer[4] = {0x0023, 0x0045, 0x00d3, 0x004a};
/*
**********************************************************************************************
函 数 名: TIM_GPIO_Init TIM4
功 能:定时器输出通道GPIO初始化
参 数: 无
返 回: 无
**********************************************************************************************
*/
void TIM_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
/* TIM4 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1PERIPH_TIM4, ENABLE);
/* GPIOA clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1PERIPH_GPIOD | RCC_AHB1PERIPH_GPIOE, ENABLE);
/* DMA1 clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1PERIPH_DMA1, ENABLE);
/* GPIOA Configuration: PD12(TIM4 CH1) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_12;
GPIO_InitStructure.e_GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.e_GPIO_Speed = GPIO_SPEED_100MHZ;
GPIO_InitStructure.e_GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.e_GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* Connect TIM pins to AF1 */
GPIO_PinAFConfig(GPIOD, GPIO_PIN_SOURCE12, GPIO_AF_TIM4);
/* DeInitialize the DMA1 Stream6 */
DMA_DeInit(DMA1_Stream6);
DMA_InitStructure.DMA_Channel = DMA_Channel_2;
// DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32)(&(GPIOE->ODR)); //&(GPIOE->BSRRL) 值没有变化
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32)(TIM4_CCR1_ADDRESS); /* 可以正常修改它的值 */
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32)a_SRC_Buffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_BufferSize = 4;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream6, &DMA_InitStructure);
}
/*
**********************************************************************************************
函 数 名: TIM_DMA_Output_Init
功 能:定时器DMA方式输出通道初始化
参 数: 无
返 回: 无
**********************************************************************************************
*/
void TIM_DMA_Output_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseStructure.TIM_Period = 1000;
TIM_TimeBaseStructure.TIM_Prescaler = 42 - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_COUNTERMODE_UP;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
/* TIM Configuration in PWM Mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = a_SRC_Buffer[0];//0xFFF;
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
/* TIM4 DMA Update enable */
TIM_DMACmd(TIM4, TIM_DMA_Update, ENABLE);
TIM_SelectOutputTrigger(TIM4, TIM_TRGOSource_Update);
/* TIM4 enable */
TIM_Cmd(TIM4, ENABLE);
/* TIM4 PWM Outputs Enable */
// TIM_CtrlPWMOutputs(TIM4, ENABLE);
/* Enable DMA2 Stream5 */
DMA_Cmd(DMA1_Stream6, ENABLE);
// /* Wait until DMA1 Stream6 end of Transfer */
// while (!DMA_GetFlagStatus(DMA1_Stream6, DMA_FLAG_TCIF6))
// {
// }
}
void TLC7524_GPIO_Init(void)
{
GPIO_InitTypeDef s_GPIO_InitStructure;
/* Enable GPIOs clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1PERIPH_GPIOD | RCC_AHB1PERIPH_GPIOE, ENABLE);
/* GPIOI configuration */
s_GPIO_InitStructure.GPIO_Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
s_GPIO_InitStructure.e_GPIO_Mode = GPIO_Mode_OUT;
s_GPIO_InitStructure.e_GPIO_OType = GPIO_OType_PP;
s_GPIO_InitStructure.e_GPIO_Speed = GPIO_SPEED_100MHZ;
s_GPIO_InitStructure.e_GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOE, &s_GPIO_InitStructure);
}
一周热门 更多>