mình đang làm đo khoảng cách bằng cảm biến siêu âm srf05 nhưng bị tràn timer nên chỉ đếm đc tới 65535 thôi... ae nào rành giúp mình đi... hjxhjx
main.c
stm32f4xx_it.c
main.c
PHP Code:
#include "stm32f4_discovery.h"
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
void TIM_Config(void);
void Delay(__IO uint32_t time)
{
/*Delay is in milliseconds*/
time=time*80000;
while(time--)
{
}
}
int main(void)
{
/* TIM Configuration */
TIM_Config();
/* TIM4 configuration: PWM Input mode ------------------------
The external signal is connected to TIM4 CH2 pin (PB.07),
The Rising edge is used as active edge,
The TIM4 CCR2 is used to compute the frequency value
The TIM4 CCR1 is used to compute the duty cycle value
------------------------------------------------------------ */
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
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_PWMIConfig(TIM4, &TIM_ICInitStructure);
/* Select the TIM4 Input Trigger: TI2FP2 */
TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2);
/* Select the slave Mode: Reset Mode */
TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset);
TIM_SelectMasterSlaveMode(TIM4,TIM_MasterSlaveMode_Enable);
/* Enable the CC2 Interrupt Request */
TIM_ITConfig(TIM4, TIM_IT_CC2, ENABLE);
STM_EVAL_LEDInit(LED3);
STM_EVAL_LEDInit(LED4);
}
void TIM_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* TIM4 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
/* GPIOB clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
/* TIM4 chennel2 configuration : PB.07 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = 4200 - 1;
/* Connect TIM pin to AF2 */
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_TIM4);
/* Enable the TIM4 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_TimeBaseStructure.TIM_Period = 4;
TIM_TimeBaseStructure.TIM_Prescaler = 360;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_Cmd(TIM4, DISABLE);
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t* file, uint32_t line)
{
while (1)
{}
}
#endif
PHP Code:
#include "stm32f4xx_it.h"
#include "stm32f4_discovery.h"
__IO uint16_t IC2Value = 0;
__IO uint16_t DutyCycle = 0;
__IO uint32_t Frequency = 0;
__IO uint16_t range;
uint16_t cm;
uint16_t time=0;
void NMI_Handler(void)
{
}
void HardFault_Handler(void)
{
while (1)
{
}
}
void MemManage_Handler(void)
{
while (1)
{
}
}
void BusFault_Handler(void)
{
while (1)
{
}
}
void UsageFault_Handler(void)
{
while (1)
{}
}
void DebugMon_Handler(void)
{
}
void SVC_Handler(void)
{
}
void PendSV_Handler(void)
{
}
void SysTick_Handler(void)
{}
uint16_t TIMERANGE1=100;
uint16_t TIMERANGE=100;
void TIM4_IRQHandler(void)
{
RCC_ClocksTypeDef RCC_Clocks;
RCC_GetClocksFreq(&RCC_Clocks);
/* Clear TIM4 Capture compare interrupt pending bit */
TIM_ClearITPendingBit(TIM4, TIM_IT_CC2);
STM_EVAL_LEDOn(LED3);
while (1)
{
STM_EVAL_LEDOn(LED4);
Delay(5);
STM_EVAL_LEDOff(LED4);
while (!GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7));
TIM_Cmd(TIM4, ENABLE);
while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7))
{
time++;
}
TIM_Cmd(TIM4, DISABLE);
range = TIM_GetCounter(TIM4);
time = TIM4->CNT;
range;
cm = range/47;
TIM_SetCounter(TIM4, 0);
Delay(50);
time = 0;
}
}
Comment