STM32F103VET6 定时器捕捉跳变沿触发DMA读取端口信息
/******************** (C) COPYRIGHT 2012 WildFire Team **************************by mengyu
IM3_CH1 (PA.6) as capture
**********************************************************************************/
#include "stm32f10x.h"
#include "stm32f10x_conf.h "
void Delay(__IO u32 nCount);
void PA8_INIT(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void RCC_Configuration(void);
void TIM_CAP_init(void);
void GPIOE_IN_Init(void);
void DMA_Port_to_ram_init(void);
uint16_t COUNT = 0;
uint16_t PORT_IN;
int main(void)
{
/* System Clocks Configuration */
RCC_Configuration();
/* NVIC configuration */
NVIC_Configuration();
/* Configure the GPIO ports */
GPIO_Configuration();
PA8_INIT();
GPIOE_IN_Init();
DMA_Port_to_ram_init();
TIM_CAP_init();
while (1)
{
Delay(2000);
GPIO_SetBits(GPIOA, GPIO_Pin_8);
Delay(2000);
GPIO_ResetBits(GPIOA, GPIO_Pin_8);
Delay(2000);
GPIO_SetBits(GPIOA, GPIO_Pin_8);
Delay(2000);
GPIO_ResetBits(GPIOA, GPIO_Pin_8);
}
}
void Delay(__IO u32 nCount) //简单的延时函数
{
for(; nCount != 0; nCount--);
}
void PA8_INIT(void)
{
/*定义一个GPIO_InitTypeDef类型的结构体*/
GPIO_InitTypeDef GPIO_InitStructure;
/*开启GPIOA的外设时钟*/
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE);
/*选择要控制的GPIOA8引脚*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
/*设置引脚模式为通用推挽输出*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
/*设置引脚速率为50MHz */
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
/*调用库函数,初始化GPIOA*/
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* 关闭所有led灯 */
GPIO_SetBits(GPIOA, GPIO_Pin_8);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the TIM3 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* TIM3 channel 1 pin (PA.06) configuration */
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void RCC_Configuration(void)
{
/* TIM3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* GPIOA and GPIOB clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
}
void TIM_CAP_init(void)
{
TIM_ICInitTypeDefTIM_ICInitStructure;
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM3, &TIM_ICInitStructure);
TIM_DMACmd(TIM3, TIM_DMA_CC1, ENABLE); //ENABLE cc1 trig dma
/* TIM enable counter */
TIM_Cmd(TIM3, ENABLE);
/* ENABLE the CC1 Interrupt Request */
TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE);
}
void DMA_Port_to_ram_init(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/* DMA channel1 configuration */
DMA_DeInit(DMA1_Channel6);
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&GPIOE->IDR; //(0x40011400 + 0X08); //portD
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&PORT_IN;//内存地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 1;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设地址固定
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;//内存地址固定
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //16
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;//16
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //循环传输
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel6, &DMA_InitStructure);
/* Enable DMA channel_6*/
DMA_Cmd(DMA1_Channel6, ENABLE);
}
void GPIOE_IN_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOE, ENABLE);
/*选择要控制的GPIOE引脚*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
/*设置引脚模式为上拉或者下拉输入*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
/*设置引脚速率为50MHz */
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
/*调用库函数,初始化GPIOC*/
GPIO_Init(GPIOE, &GPIO_InitStructure);
}
/******************* (C) COPYRIGHT 2012 WildFire Team *****END OF FILE************/
不是所有的定时器都能完成DMA触发 多谢楼主!:)
页:
[1]