本帖最后由 hunkchen2000 于 2019-1-6 21:44 编辑
各位~~
正點原子的STM32F1的輸入捕獲實驗是用TIM5,我想把這個程式改道F0上面
我用的的是TIM3
請問為什麼不會為什麼不會動~~請強者幫忙看一下程式碼好嗎?
我參考原廠DataSheel Time3的Channel 1中斷輸入是PA6,我在PA6不斷輸入1msec的脈波
但是結果都不能動~~
無法顯示任何資料出來(USART)
include "stm32f0xx.h"
#include "usart.h"
void Delay (uint32_t nCount);
void TIMER_Init(void);
void Delay(__IO uint32_t nTime);
void TimingDelay_Decrement(void);
void GPIO_Config(void);
void EXTI0_Config(void);
static __IO uint32_t TimingDelay;
extern uint32_t Hunk[2];
#define MESSAGE1 "*****Alarm Example"
#define MESSAGE2 " testing on"
#define MESSAGE3 "****** UART-1******"
extern uint8_t TIM3CH1_CAPTURE_STA; //懷眸鳳袨怓
extern uint16_t TIM3CH1_CAPTURE_VAL; //懷眸鳳硉
extern uint32_t count[2];
void TIM3_Cap_Init(uint16_t arr,uint16_t psc);
int main(void)
{
uint32_t temp=0;
SystemCoreClockUpdate();
if (SysTick_Config(SystemCoreClock /1000))
{
while (1);
}
// USART_Configuration();
//GPIO_Config();
//TIMER_Init();
//EXTI0_Config();
TIM3_Cap_Init(0XFFFF,48-1); //
USART_Configuration();
printf("
-----------TestingAAA---------------------");
while(1)
{
Delay(100);
// TIM_SetCompare2(TIM3,TIM_GetCapture2(TIM3)+1);
// if(TIM_GetCapture2(TIM3)==300)
// TIM_SetCompare2(TIM3,0);
if(TIM3CH1_CAPTURE_STA&0X80)//
{
temp=TIM3CH1_CAPTURE_STA&0X3F;
temp*=65536;//祛堤奀潔軞睿
temp+=TIM3CH1_CAPTURE_VAL;//
printf("HIGH:%d us
",temp);
TIM3CH1_CAPTURE_STA=0;//
}
}
}
void Delay(__IO uint32_t nTime)
{
TimingDelay = nTime;
while(TimingDelay != 0);
}
/**
* @brief Decrements the TimingDelay variable.
* @param None
* @retval None
*/
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
}
TIM_ICInitTypeDef TIM3_ICInitStructure;
void TIM3_Cap_Init(uint16_t arr,uint16_t psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//------------------------------------------------ -----------------------
/* TIM3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* GPIOA clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
/* TIM2 chennel2 configuration : PA.01 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6
|GPIO_Pin_7|GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Connect TIM pin to AF2 */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_1);
/* Enable the TIM2 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//------------------------------------------------ -------------------------
//初始化定時器5 TIM3
TIM_TimeBaseStructure.TIM_Period = arr; //
TIM_TimeBaseStructure.TIM_Prescaler =(uint16_t) (((SystemCoreClock / 1000000)) - 1); //
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //設定時鐘分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根據TIM_TimeBaseInitStruct初始化;
//Inpu capture Setting
TIM3_ICInitStructure.TIM_Channel = TIM_Channel_1; //CC1S=01
TIM3_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM3_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //
TIM3_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //
TIM3_ICInitStructure.TIM_ICFilter = 0x00;//
TIM_ICInit(TIM3, &TIM3_ICInitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(TIM3,TIM_IT_Update|TIM_IT_CC1,ENABLE);//
TIM_Cmd(TIM3,ENABLE ); //Timer3 Enable//
}
//------------------------------------------------ ---------------------------------------//
//----------------------------------我的中斷副程式是如下------------------------------//
/******************************************************************************/
/* STM32F0xx Peripherals Interrupt Handlers */
/* Add here the Interrupt Handler for the used peripheral(s) (PPP), for the */
/* available peripheral interrupt handler's name please refer to the startup */
/* file (startup_stm32f0xx.s). */
/******************************************************************************/
uint8_t TIM3CH1_CAPTURE_STA=0; //
uint16_t TIM3CH1_CAPTURE_VAL=0; //
void TIM3_IRQHandler(void)
{
if((TIM3CH1_CAPTURE_STA&0X80)==0)//還未捕獲成功
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
{
if(TIM3CH1_CAPTURE_STA&0X40)//已經捕獲到高電平
{
if((TIM3CH1_CAPTURE_STA&0X3F)==0X3F)//高電平太長了
{
TIM3CH1_CAPTURE_STA|=0X80;//標記成功捕獲一次
TIM3CH1_CAPTURE_VAL=0XFFFF;
}else TIM3CH1_CAPTURE_STA++;
}
}
if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)//捕獲1次發生捕獲事件
{
if(TIM3CH1_CAPTURE_STA&0X40) //
{
TIM3CH1_CAPTURE_STA|=0X80;//
TIM3CH1_CAPTURE_VAL=TIM​​_GetCapture1(TIM3);
TIM_OC1PolarityConfig(TIM3,TIM_ICPolarity_Rising); //CC1P=0 扢離峈奻汔朓眸鳳
}
else
{
// printf("
-----------TestingCCC---------------------");
TIM3CH1_CAPTURE_STA=0;
TIM3CH1_CAPTURE_VAL=0;
TIM_SetCounter(TIM3,0);
TIM3CH1_CAPTURE_STA|=0X40;
TIM_OC1PolarityConfig(TIM3,TIM_ICPolarity_Falling); //CC1P=1
}
}
}
TIM_ClearITPendingBit(TIM3,TIM_IT_CC1|TIM_IT_Update);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>