__weak void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
/* NOTE : This function Should not be modified, when the callback is needed,
the __HAL_TIM_IC_CaptureCallback could be implemented in the user file
*/
uint16_t tmpccer;
uint32_t dval;
tmpccer = htim->Instance->CCER;
if(tmpccer&(uint16_t)0x0002==0x00)
{
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM4_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/** System Clock Configuration
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInit;
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = 16;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL8;
RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_TIM34;
PeriphClkInit.Tim34ClockSelection = RCC_TIM34CLK_HCLK;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
__SYSCFG_CLK_ENABLE();
}
/* TIM4 init function */
void MX_TIM4_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;
TIM_IC_InitTypeDef sConfigIC;
HAL_NVIC_SetPriority(TIM4_IRQn, 0x0D, 0x00);
HAL_NVIC_EnableIRQ(TIM4_IRQn);
htim4.Instance = TIM4;
htim4.Init.Prescaler = 64-1;
htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
htim4.Init.Period = 10000;
htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
HAL_TIM_Base_Init(&htim4);
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig);
HAL_TIM_IC_Init(&htim4);
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig);
sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
sConfigIC.ICFilter = 0;
HAL_TIM_IC_ConfigChannel(&htim4, &sConfigIC, TIM_CHANNEL_1);
HAL_TIM_Base_Start_IT(&htim4);
HAL_TIM_IC_Start_IT(&htim4, TIM_CHANNEL_1);
}
/** Pinout Configuration
*/
void MX_GPIO_Init(void)
{
/* GPIO Ports Clock Enable */
__GPIOB_CLK_ENABLE();
}
/********************************************************************************************************/
__weak void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
/* NOTE : This function Should not be modified, when the callback is needed,
the __HAL_TIM_IC_CaptureCallback could be implemented in the user file
*/
uint16_t tmpccer;
uint32_t dval;
tmpccer = htim->Instance->CCER;
if(tmpccer&(uint16_t)0x0002==0x00)
{
tmpccer &= (uint16_t)~((uint16_t)0x0002);
tmpccer |= (uint16_t)0x0002;
htim->Instance->CCER = tmpccer;
__HAL_TIM_SetCounter(htim,0);
}
else
{
dval = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
tmpccer &= (uint16_t)~((uint16_t)TIM_CCER_CC1P);
tmpccer |= (uint16_t)(TIM_ICPOLARITY_RISING);
htim->Instance->CCER = tmpccer;
}
}
红外协议有很多种:
1、ITT 协议,如IRT1250 和IRT1260发送芯片。
2、Nokia NRC17 协议,生产厂家Nokia CE。
3、Sharp 协议,主要应用于Sharp 的VCR 中。4、NEC 协议,如HS6221/6222、HT6221/6222 是采用NEC 协议的通用红外遥控发射芯片,亚洲主要用这个。
5、Philips RC-5 协议,发送芯片有:SAA3006、SAA3010、SAA3027
6、还有Philips RC-6 协议,Philips RECS-80 协议,以及Sony SIRC 协议等
单片机解码要看你遥控器发出的码遵循哪种协议(把遥控器拆开看看用的发送芯片是什么型号,查数据手册)。祝你好运!
http://www.xiaovdiy.cn/?post=138这篇文章能帮助你
试试单次触发看波形吧
我是用的示波器的捕获模式采集的,这样不会采集到红外的起始码吗
跟你用什么方式关系不太大,正常采集就可以
非常感谢,我开始就是不知道我设置的定时器对不对