我想使用 stm32F746G 获得 PWM 的 DutyCycle。我做了类似 hal PWMINPUT 示例的代码。现在程序无法进入 [HAL_TIM_IC_CaptureCallback] 函数。
#include "main.h"
#define max(a,b) ( ((a)>(b)) ? (a):(b) )
#define min(a,b) ( ((a)>(b)) ? (b):(a) )
#define MASK_R 0xFF
#define MASK_G 0xFF00
#define MASK_B 0xFF0000
/////////////////////////////////////////////////
/* Timer handler declaration */
TIM_HandleTypeDef TimHandle;
/* Timer Input Capture Configuration Structure declaration */
TIM_IC_InitTypeDef sConfig;
/* Slave configuration structure */
TIM_SlaveConfigTypeDef sSlaveConfig;
/* Captured Value */
__IO uint32_t uwIC2Value = 0;
/* Duty Cycle Value */
__IO uint32_t uwDutyCycle = 0;
/* Frequency Value */
__IO uint32_t uwFrequency = 0;
__IO uint32_t counter1 = 0;
DMA2D_HandleTypeDef hdma2d_eval;
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void LCD_LL_ConvertLineToARGB8888(void *pSrc, void *pDst);
static void CPU_CACHE_Enable(void);
int GetRGB(uint32_t color);
int GetRGBHSI(uint32_t color);
//static void GPIO_Configuration(void);
static void Error_Handler(void);
int main(void)
{
uint32_t i;
uint32_t *ptrLcd;
/* Enable the CPU Cache */
CPU_CACHE_Enable();
HAL_Init();
/* Configure the system clock to 216 MHz */
SystemClock_Config();
//GPIO_Configuration();
BSP_LED_Init(LED1);
/*##-1- Initialize the LCD #################################################*/
BSP_LCD_Init();
/* Init LCD screen buffer */
ptrLcd = (uint32_t*)(LCD_FRAME_BUFFER);
for (i=0; i<(BSP_LCD_GetXSize()*BSP_LCD_GetYSize()); i++)
{
ptrLcd[i]=0;
}
BSP_LCD_LayerDefaultInit(1, LCD_FRAME_BUFFER);
/* Enable the LCD */
BSP_LCD_DisplayOn();
/* Select the LCD Foreground layer */
BSP_LCD_SelectLayer(1);
/* Text Introduction Display Begins*/
uint16_t display_duration=0;
BSP_LCD_SetTextColor(LCD_COLOR_ORANGE);
BSP_LCD_SetBackColor(LCD_COLOR_BLACK);
display_duration=0;
while(display_duration<1000)
{
BSP_LCD_DisplayStringAt(20, 130, (uint8_t*)"CSIR-CEERI", CENTER_MODE);
display_duration=display_duration+1;
}
/* Set active window */
BSP_LCD_SetLayerWindow(1, xoffset, yoffset, xsize, ysize);
//HAL_TIM_IC_Start_IT(&htim1,TIM_CHANNEL_2);
HAL_GPIO_WritePin(GPIOI,GPIO_PIN_1,GPIO_PIN_SET);
////////////////////////////////////////////////////////////////
/* Set TIMx instance */
TimHandle.Instance = TIMx;
TimHandle.Init.Period = 0xFFFF;
TimHandle.Init.Prescaler = 0;
TimHandle.Init.ClockDivision = 0;
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
TimHandle.Init.RepetitionCounter = 0;
//TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_IC_Init(&TimHandle) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
/* Common configuration */
sConfig.ICPrescaler = TIM_ICPSC_DIV1;
sConfig.ICFilter = 0;
/* Configure the Input Capture of channel 1 */
sConfig.ICPolarity = TIM_ICPOLARITY_FALLING;
sConfig.ICSelection = TIM_ICSELECTION_INDIRECTTI;
if (HAL_TIM_IC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/* Configure the Input Capture of channel 2 */
sConfig.ICPolarity = TIM_ICPOLARITY_RISING;
sConfig.ICSelection = TIM_ICSELECTION_DIRECTTI;
if (HAL_TIM_IC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/*##-3- Configure the slave mode ###########################################*/
/* Select the slave Mode: Reset Mode */
sSlaveConfig.SlaveMode = TIM_SLAVEMODE_RESET;
sSlaveConfig.InputTrigger = TIM_TS_TI2FP2;
sSlaveConfig.TriggerPolarity = TIM_TRIGGERPOLARITY_NONINVERTED;
sSlaveConfig.TriggerPrescaler = TIM_TRIGGERPRESCALER_DIV1;
sSlaveConfig.TriggerFilter = 0;
if (HAL_TIM_SlaveConfigSynchronization(&TimHandle, &sSlaveConfig) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/*##-4- Start the Input Capture in interrupt mode ##########################*/
if (HAL_TIM_IC_Start_IT(&TimHandle, TIM_CHANNEL_2) != HAL_OK)
{
/* Starting Error */
Error_Handler();
}
//BSP_LCD_DisplayStringAt(20, 20, (uint8_t*)"OK1", LEFT_MODE);
/*##-5- Start the Input Capture in interrupt mode ##########################*/
if (HAL_TIM_IC_Start(&TimHandle, TIM_CHANNEL_1) != HAL_OK)
{
/* Starting Error */
Error_Handler();
}
///////////////////////////////////////////////////////////////
while(1)
{
}
}
static void Error_Handler(void)
{
/* Turn LED3 on */
BSP_LCD_DisplayStringAt(0, 180, (uint8_t*)"fl!", LEFT_MODE);
while (1)
{
}
}
/** * @brief 非阻塞模式下的输入捕获回调 * @param htim : TIM IC 句柄 * @retval None */
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
BSP_LCD_DisplayStringAt(20, 20, (uint8_t*)"OK!", RIGHT_MODE);
if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
{
/* Get the Input Capture value */
uwIC2Value = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);
if (uwIC2Value != 0)
{
/* Duty cycle computation */
//uwDutyCycle = ((HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1)) * 100) / uwIC2Value;
uwDutyCycle = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
/* uwFrequency computation
TIM3 counter clock = (RCC_Clocks.HCLK_Frequency) */
uwFrequency = (HAL_RCC_GetHCLKFreq()/ 2) / uwIC2Value;
char s;
itoa(uwIC2Value,s,10);
BSP_LCD_DisplayStringAt(0, 200, (uint8_t*)s, LEFT_MODE);
char f;
itoa(uwDutyCycle,f,10);
BSP_LCD_DisplayStringAt(0, 272-Font24.Height, (uint8_t*)f, LEFT_MODE);
//TIM_RESET_CAPTUREPOLARITY(htim, TIM_CHANNEL_1);
//TIM_RESET_CAPTUREPOLARITY(htim, TIM_CHANNEL_2);
}
else
{
uwDutyCycle = 0;
uwFrequency = 0;
}
} }