326|1

141

帖子

0

资源

一粒金砂(中级)

【上海航芯 ACM32F070开发板+触控功能评估板】PWM信号频率占空比测量功能实现 [复制链接]

本帖最后由 qinyunti 于 2022-9-27 22:24 编辑

PWM信号采集

 

前言

本次基于定时器的捕获功能和段码显示,实现一个PWM信号采集的功能。

 

原理见TIM1_Init

sSlaveConfig.TriggerPolarity = TIM_SLAVE_CAPTURE_ACTIVE_FALLING; 设置为下降沿开始捕获。

通道1设置下降沿捕获Tim_IC_Init_Para.ICPolarity = TIM_SLAVE_CAPTURE_ACTIVE_FALLING;  

通道2设置上升沿捕获Tim_IC_Init_Para.ICPolarity = TIM_SLAVE_CAPTURE_ACTIVE_RISING;

所以CCR2即周期,CCR1-CCR即高脉宽时间。

image.png  

 

过程

基于LCD_TK进行。

使用上4位数显示频率。

使用下4位数显示占空比,0~100。

 

使用TIMER1,PC8输入。

试用APB时钟System_Get_APBClock可以看到时钟是64M,

16位计数器,最长可以测量2^16*1/64M=1.024ms的周期,也就是大于1k频率的信号。

 

App.c代码如下

/*

  ******************************************************************************

  * @File    APP.c

  * @author  CWT

  * @version V1.0.0

  * @date    2020

  * @brief   LCD demo source code.

  ******************************************************************************

*/

#include "APP.h"

#include "SHT30.h"

#include "lcd_GDC04212.h"

#include "lcd_GDC03828.h"

#include "lcd_YR1618A.h"

#include "buzzer.h"

#include "TKEY.h"

 

LCD_HandleTypeDef         *lcdhandle;  //LCD HandleÖ¸Õë

 

I2C_HandleTypeDef I2C_Handle;   //I2C HandleÖ¸Õë

 

#define BUFFER_LENGTH    (256)

uint8_t gu8_TxBuffer[BUFFER_LENGTH];

uint8_t gu8_RxBuffer[BUFFER_LENGTH];

 

uint8_t gu8_time_count_free = 0;//0-free,1-0.5s,2-1s,

uint32_t gu32_time_1s_cont = 0;

 

extern volatile uint32_t gu32_SystemCount;

 

 

void(*LCD_DisplayNum)(uint32_t);//LCDÏÔʾº¯ÊýÖ¸Õë

 

void(*LCD_Init)(void);//LCD³õʼ»¯º¯ÊýÖ¸Õë

 

extern void (*SysTick_Handler_Callback)(void);

 

extern LCD_HandleTypeDef         lcdhandle_GDC04212;

extern LCD_HandleTypeDef         lcdhandle_GDC03828;

extern LCD_HandleTypeDef         lcdhandle_YR1618A;

 

TIM_HandleTypeDef TIM6_Handler;  

 

DMA_HandleTypeDef  DMA_CH1_Handle;

 

DMA_LLI_InitTypeDef DMA_CH1_Handle_link1;

DMA_LLI_InitTypeDef DMA_CH1_Handle_link2;

DMA_LLI_InitTypeDef DMA_CH1_Handle_link3;

DMA_LLI_InitTypeDef DMA_CH1_Handle_link4;

DMA_LLI_InitTypeDef DMA_CH1_Handle_link5;

DMA_LLI_InitTypeDef DMA_CH1_Handle_link6;

DMA_LLI_InitTypeDef DMA_CH1_Handle_link7;

DMA_LLI_InitTypeDef DMA_CH1_Handle_link8;

DMA_LLI_InitTypeDef DMA_CH1_Handle_link9;

 

 

#if defined LCD_GDC04212 //¶ÔÓ¦GDC04212µÄ9Ö¡Êý¾Ý£¬·Ö±ðÏÔʾΪ¡°1111¡±-"9999"

uint32_t wdata1[16]={0x06000600,0x06000600,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

uint32_t wdata2[16]={0x0B060B06,0x0B060B06,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

uint32_t wdata3[16]={0xF020F02,0xF020F02,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

uint32_t wdata4[16]={0x06030603,0x06030603,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

uint32_t wdata5[16]={0xD030D03,0xD030D03,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

uint32_t wdata6[16]={0x0D070D07,0x0D070D07,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

uint32_t wdata7[16]={0x07000700,0x07000700,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

uint32_t wdata8[16]={0x0F070F07,0x0F070F07,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

uint32_t wdata9[16]={0xF030F03,0xF030F03,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

#elif defined LCD_GDC03828 //¶ÔÓ¦GDC03828µÄ9Ö¡Êý¾Ý£¬·Ö±ðÏÔʾΪ¡°1111¡±-"9999"

uint32_t wdata1[16]={0x06000600,0x06000600,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

uint32_t wdata2[16]={0x0B060B06,0x0B060B06,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

uint32_t wdata3[16]={0xF020F02,0xF020F02,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

uint32_t wdata4[16]={0x06030603,0x06030603,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

uint32_t wdata5[16]={0xD030D03,0xD030D03,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

uint32_t wdata6[16]={0x0D070D07,0x0D070D07,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

uint32_t wdata7[16]={0x07000700,0x07000700,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

uint32_t wdata8[16]={0x0F070F07,0x0F070F07,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

uint32_t wdata9[16]={0xF030F03,0xF030F03,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

#else//¶ÔÓ¦YR1618AµÄ9Ö¡Êý¾Ý£¬·Ö±ðÏÔʾΪ¡°1111¡±-"9999"

uint32_t wdata1[16]={0x06C8E0FF,0x02488055,0x06C8E0FF,0x02488055,0,0,0,0,0,0,0,0,0,0,0,0};

uint32_t wdata2[16]={0x048060AA,0x00000000,0x048060AA,0x00000000,0,0,0,0,0,0,0,0,0,0,0,0};

uint32_t wdata3[16]={0x06C8E0FF,0x048060AA,0x02488055,0x02488055,0,0,0,0,0,0,0,0,0,0,0,0};

uint32_t wdata4[16]={0x06C8E0FF,0x048060AA,0x048060AA,0x02488055,0,0,0,0,0,0,0,0,0,0,0,0};

uint32_t wdata5[16]={0x048060AA,0x06C8E0FF,0x048060AA,0x00000000,0,0,0,0,0,0,0,0,0,0,0,0};

uint32_t wdata6[16]={0x02488055,0x06C8E0FF,0x048060AA,0x02488055,0,0,0,0,0,0,0,0,0,0,0,0};

uint32_t wdata7[16]={0x02488055,0x06C8E0FF,0x06C8E0FF,0x02488055,0,0,0,0,0,0,0,0,0,0,0,0};

uint32_t wdata8[16]={0x06C8E0FF,0x00000000,0x048060AA,0x00000000,0,0,0,0,0,0,0,0,0,0,0,0};

uint32_t wdata9[16]={0x06C8E0FF,0x06C8E0FF,0x06C8E0FF,0x02488055,0,0,0,0,0,0,0,0,0,0,0,0};

#endif

 

EXTI_HandleTypeDef EXTI_Line13_Handle;

 

GPIO_InitTypeDef GPIO_PC13_Handle;

/*********************************************************************************

* Function    : LCD_IRQHandler

* Description : LCD Interrupt handler

* Input       :

* Outpu       :

* Author      : CWT                         Data : 2021Äê

**********************************************************************************/

void LCD_IRQHandler(void)

{

    HAL_LCD_IRQHandler(lcdhandle);

  

}

/*********************************************************************************

* Function    : DMA_IRQHandler

* Description : DMA Interrupt handler

* Input       :

* Outpu       :

* Author      : CWT                         Data : 2021Äê

**********************************************************************************/

void DMA_IRQHandler(void)

{

    HAL_DMA_IRQHandler(&DMA_CH1_Handle);

}

/************************************************************************

 * function   : HAL_I2C_MspInit

 * Description:

 * input      : hi2c : pointer to a I2C_HandleTypeDef structure that contains

 *                     the configuration information for I2C module

 ************************************************************************/

void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)

{

    /*

      NOTE : This function should be modified by the user.

    */

    

    /* For Example */

    GPIO_InitTypeDef GPIO_Handle;

    

    /* I2C1 */

 

    /* I2C2 */

    if (hi2c->Instance == I2C2)

    {

        System_Module_Enable(EN_I2C2);  

        System_Module_Enable(EN_GPIOCD);   

 

        /* I2C2 SDA  PortD Pin7 */

        /* I2C2 SCL  PortD Pin6 */

        GPIO_Handle.Pin            = GPIO_PIN_6 | GPIO_PIN_7;

        GPIO_Handle.Mode           = GPIO_MODE_AF_PP;

        GPIO_Handle.Pull           = GPIO_PULLUP;

        GPIO_Handle.Alternate      = GPIO_FUNCTION_1;

        HAL_GPIO_Init(GPIOD, &GPIO_Handle);

        

        /* Clear Pending Interrupt */

        NVIC_ClearPendingIRQ(I2C2_IRQn);

        

        /* Enable External Interrupt */

        NVIC_EnableIRQ(I2C2_IRQn);

    }

}

/************************************************************************

 * function   : I2C_Init

 * Description: I2C Initiation.

* Input       :

* Outpu       :

* Author      : ZK                         Data : 2022Äê

 ************************************************************************/

void I2C_Init(void)

{

    

    I2C_Handle.Instance         = I2C2;

    I2C_Handle.Init.I2C_Mode    = I2C_MODE_MASTER;

    I2C_Handle.Init.Tx_Auto_En  = TX_AUTO_EN_ENABLE;

    I2C_Handle.Init.Clock_Speed = CLOCK_SPEED_STANDARD;  

    I2C_Handle.Init.No_Stretch_Mode = NO_STRETCH_MODE_NOSTRETCH;   

    

    HAL_I2C_Init(&I2C_Handle);

}

/************************************************************************

 * function   : SHT30_Read_Result

 * Description: get SHT30 result

* Input       :

* Outpu       :

* Author      : ZK                         Data : 2022Äê

 ************************************************************************/

void SHT30_Read_Result(uint8_t *sht30_data,float *getTempHum)

{

uint16_t tem,hum;

float Temperature=0;

float Humidity=0;

    tem = ((sht30_data[0]<<8) | sht30_data[1]);//ζÈÆ´½Ó

    hum = ((sht30_data[3]<<8) | sht30_data[4]);//ʪ¶ÈÆ´½Ó

    

    /*ת»»Êµ¼ÊζÈ*/

    Temperature= (175.0*(float)tem/65535.0-45.0) ;// T = -45 + 175 * tem / (2^16-1)

    Humidity= (100.0*(float)hum/65535.0);// RH = hum*100 / (2^16-1)

    

//    if((Temperature>=-20)&&(Temperature<=125)&&(Humidity>=0)&&(Humidity<=100))//¹ýÂË´íÎóÊý¾Ý

//    {

//        printfS("%6.2f*C %6.2f%%",Temperature,Humidity);//111.01*C 100.01%£¨±£Áô2λСÊý£©

//    }

    getTempHum[0]=Temperature;

    getTempHum[1]=Humidity;

}

/************************************************************************

 * function   : SHT30_Init

 * Description: SHT30 Initiation.

* Input       :

* Outpu       :

* Author      : ZK                         Data : 2022Äê

 ************************************************************************/

void SHT30_Init(void)

{

    I2C_Init();

    SHT30_Reset(I2C_Handle);

}

/*********************************************************************************

* Function    : LCD_Config

* Description : Config LCD

* Input       :

* Outpu       :

* Author      : CWT                         Data : 2021Äê

**********************************************************************************/

void LCD_Config(void)

{   

    

    #if defined LCD_GDC04212

        LCD_DisplayNum=LCD_DisplayNum_GDC04212;

        LCD_Init=LCD_Init_GDC04212;

        lcdhandle=&lcdhandle_GDC04212;

 

    #elif defined LCD_GDC03828

        LCD_DisplayNum=LCD_DisplayNum_GDC003828;

        LCD_Init=LCD_Init_GDC03828;

        lcdhandle=&lcdhandle_GDC03828;

 

    #else

        LCD_DisplayNum=LCD_DisplayNum_YR1618A;

        LCD_Init=LCD_Init_YR1618A;

        lcdhandle=&lcdhandle_YR1618A;

        

 

    #endif  

        LCD_Init();

        LCD_BACK_LED();

}

 

/*********************************************************************************

* Function    : LCD_DisplayTest

* Description : LCD DisplayTest

* Input       :

* Outpu       :

* Author      : ZK                         Data : 2022Äê

**********************************************************************************/

void LCD_DisplayTest()

{

    while(1)

    {

        #ifdef LCD_YR1618A

        LCD_YR1618A_SelfDisplay();

        #else

        for(uint16_t i=0;i<10000;i++)   

        {

            LCD_DisplayNum(i);

            System_Delay_MS(100);

        }  

        #endif

    }

}

void EXTI_IRQHandler(void)

{

    HAL_EXTI_IRQHandler(&EXTI_Line13_Handle);

}

 

/*********************************************************************************

* Function    : LCD_Sleep_DMA_Test

* Description : LCD Display in sleep,use DMA to Reresh data to LCDRAM

* Input       :

* Outpu       :

* Author      : CWT                         Data : 2021Äê

**********************************************************************************/

void LCD_Sleep_DMA_Test()/*SleepģʽÏÂÓÃDMA½øÐÐÑ­»·ÏÔʾ*/

{   

    memset(&DMA_CH1_Handle,0,sizeof(DMA_CH1_Handle));//³õʼ»¯ÇåÁãHandle

    //DMAÅäÖÃ

    DMA_CH1_Handle.Instance              = DMA_Channel0;

    DMA_CH1_Handle.Init.Data_Flow        = DMA_DATA_FLOW_M2P;

    DMA_CH1_Handle.Init.Request_ID       = REQ22_TIM3_CH2_LCDFRAME;

    DMA_CH1_Handle.Init.Source_Inc       = DMA_SOURCE_ADDR_INCREASE_ENABLE;

    DMA_CH1_Handle.Init.Desination_Inc   = DMA_DST_ADDR_INCREASE_ENABLE;

    DMA_CH1_Handle.Init.Source_Width     = DMA_SRC_WIDTH_WORD;

    DMA_CH1_Handle.Init.Desination_Width = DMA_DST_WIDTH_WORD;

    HAL_DMA_Init(&DMA_CH1_Handle);

    

    DMA_CH1_Handle.Instance->CTRL &= ~DMA_CHANNEL_CTRL_ITC;//¹Ø±ÕÁ´±íÍê³ÉÖжϣ¬·ñÔòSleepģʽÏ£¬°áÔËÍæÁ´±í1Ôò²úÉúÖжϻ½ÐÑSleep

    

    //DMAÁ´±íÅäÖÃ1

    DMA_CH1_Handle_link1.SrcAddr = (uint32_t)(wdata1);

    DMA_CH1_Handle_link1.DstAddr = (uint32_t)(&lcdhandle->Instance->LCD_RAM[0]);

    DMA_CH1_Handle_link1.Next = &DMA_CH1_Handle_link2;

    DMA_CH1_Handle_link1.Control = DMA_CH1_Handle.Instance->CTRL | 16;

    

    //DMAÁ´±íÅäÖÃ2

    DMA_CH1_Handle_link2.SrcAddr = (uint32_t)(wdata2);

    DMA_CH1_Handle_link2.DstAddr = (uint32_t)(&lcdhandle->Instance->LCD_RAM[0]);

    DMA_CH1_Handle_link2.Next = &DMA_CH1_Handle_link3;

    DMA_CH1_Handle_link2.Control = DMA_CH1_Handle.Instance->CTRL | 16;

    

    //DMAÁ´±íÅäÖÃ3

    DMA_CH1_Handle_link3.SrcAddr = (uint32_t)(wdata3);

    DMA_CH1_Handle_link3.DstAddr = (uint32_t)(&lcdhandle->Instance->LCD_RAM[0]);

    DMA_CH1_Handle_link3.Next = &DMA_CH1_Handle_link4;

    DMA_CH1_Handle_link3.Control = DMA_CH1_Handle.Instance->CTRL | 16;

 

    //DMAÁ´±íÅäÖÃ4

    DMA_CH1_Handle_link4.SrcAddr = (uint32_t)(wdata4);

    DMA_CH1_Handle_link4.DstAddr = (uint32_t)(&lcdhandle->Instance->LCD_RAM[0]);

    DMA_CH1_Handle_link4.Next = &DMA_CH1_Handle_link5;

    DMA_CH1_Handle_link4.Control = DMA_CH1_Handle.Instance->CTRL | 16;

    //DMAÁ´±íÅäÖÃ5

    DMA_CH1_Handle_link5.SrcAddr = (uint32_t)(wdata5);

    DMA_CH1_Handle_link5.DstAddr = (uint32_t)(&lcdhandle->Instance->LCD_RAM[0]);

    DMA_CH1_Handle_link5.Next = &DMA_CH1_Handle_link6;

    DMA_CH1_Handle_link5.Control = DMA_CH1_Handle.Instance->CTRL | 16;

    

    //DMAÁ´±íÅäÖÃ6

    DMA_CH1_Handle_link6.SrcAddr = (uint32_t)(wdata6);

    DMA_CH1_Handle_link6.DstAddr = (uint32_t)(&lcdhandle->Instance->LCD_RAM[0]);

    DMA_CH1_Handle_link6.Next = &DMA_CH1_Handle_link7;

    DMA_CH1_Handle_link6.Control = DMA_CH1_Handle.Instance->CTRL | 16;

    

    //DMAÁ´±íÅäÖÃ7

    DMA_CH1_Handle_link7.SrcAddr = (uint32_t)(wdata7);

    DMA_CH1_Handle_link7.DstAddr = (uint32_t)(&lcdhandle->Instance->LCD_RAM[0]);

    DMA_CH1_Handle_link7.Next =  &DMA_CH1_Handle_link8;

    DMA_CH1_Handle_link7.Control = DMA_CH1_Handle.Instance->CTRL | 16;

 

    //DMAÁ´±íÅäÖÃ8

    DMA_CH1_Handle_link8.SrcAddr = (uint32_t)(wdata8);

    DMA_CH1_Handle_link8.DstAddr = (uint32_t)(&lcdhandle->Instance->LCD_RAM[0]);

    DMA_CH1_Handle_link8.Next = &DMA_CH1_Handle_link9;

    DMA_CH1_Handle_link8.Control = DMA_CH1_Handle.Instance->CTRL | 16;

    //DMAÁ´±íÅäÖÃ9

    DMA_CH1_Handle_link9.SrcAddr = (uint32_t)(wdata9);

    DMA_CH1_Handle_link9.DstAddr = (uint32_t)(&lcdhandle->Instance->LCD_RAM[0]);

    DMA_CH1_Handle_link9.Next = NULL;

    //×îºóÒ»´Î´«Ê俪ÆôÁ´±íÍê³ÉÖжϣ¬ÓÃÓÚ»½ÐÑLCD£¬Ò²¿É¸ù¾ÝÐèÒª£¬Ö¸ÏòÁ´±í1Ñ­»·ÏÔʾ

    DMA_CH1_Handle_link9.Control = DMA_CH1_Handle.Instance->CTRL | 16|(DMA_CHANNEL_CTRL_ITC);

    

    /*Á´±íÅäÖÃÍê³É£¬ÉèÖÃDMAµÄÁ´±íµØַΪÁ´±í1£¬¿ªÊ¼DMA´«Êä*/

    DMA_CH1_Handle.Instance->LLI =(uint32_t)&DMA_CH1_Handle_link1;  

    __HAL_LINK_DMA((*lcdhandle), DMA_Handle, DMA_CH1_Handle);

    HAL_LCD_Start_DMA(lcdhandle, wdata1, 16);

    

    /*½øÈëSleepÇ°ÐèÒª¹Ø±ÕSystickºÍLCDÖжϣ¬·ñÔòÒ»½øÈëSleep¾Í±»»½ÐÑ*/

    SysTick->CTRL = 0;

    lcdhandle->Instance->CR1 &= ~LCD_CR1_IE;

    System_Enter_Sleep_Mode(SLEEPENTRY_WFI);//½øÈësleep

    

    /*°áÔËÍê³É£¬Í˳öSleep£¬ÏÔʾ1234*/

    LCD_DisplayNum(1234);

}

 

/*********************************************************************************

* Function    : Exit_Config

* Description : Exit_Config

* Input       :

* Outpu       :

* Author      : ZK                         Data : 2022Äê

**********************************************************************************/

void Exit_Config(void)

{    

    /* Initialization GPIO */

 

    /* RTC access enable */

    System_Enable_Disable_RTC_Domain_Access(FUNC_ENABLE);

    

    __HAL_RTC_PC13_SEL(0);  // GPIO function    

    __HAL_RTC_PC13_PULL_UP_ENABLE();

    __HAL_RTC_PC13_DIGIT();

  

    System_Module_Enable(EN_EXTI);

    /* Config EXTI */

    EXTI_Line13_Handle.u32_Line    = EXTI_LINE_13;

    EXTI_Line13_Handle.u32_Mode    = EXTI_MODE_INTERRUPT;

    EXTI_Line13_Handle.u32_Trigger = EXTI_TRIGGER_FALLING;

    EXTI_Line13_Handle.u32_GPIOSel = EXTI_GPIOC;

    HAL_EXTI_SetConfigLine(&EXTI_Line13_Handle);

}

 

 

/*********************************************************************************

* Function    : LCD_StopTest

* Description : LCD Dispaly in Stop

* Input       :

* Outpu       :

* Author      : CWT                         Data : 2021Äê

**********************************************************************************/

void LCD_StopTest()/*StopģʽÏÂÏÔʾ*/

{   

    LCD_DisplayNum(6666);

    

    Exit_Config();

    HAL_EXTI_ClearAllPending();

    System_Delay_MS(2000);

    System_Enter_Stop_Mode(STOPENTRY_WFI);//½øÈëSTOP£¬²»»½ÐÑ£¬²é¿´ÊÇ·ñÕý³£ÏÔʾ

    

    LCD_DisplayNum(8888);/*»½ÐÑ£¬ÈôÍ˳öStop£¬ÔòÏÔʾ8888*/

}

 

 

/*********************************************************************************

* Function    : LCD_Test

* Description : LCD Test

* Input       :

* Outpu       :

* Author      : CWT                         Data : 2021Äê

**********************************************************************************/

void LCD_Test(enum_TEST_MODE_t fe_Mode)

{    

    switch (fe_Mode)

    {

        /* Õý³£ÏÔʾģʽ */

        case TEST_Dispaly_Normal:

        {

            LCD_DisplayTest();

        }

        

        /* StopģʽÏÔʾ */

        case TEST_Dispaly_InStop:

        {

            LCD_StopTest();

            while(1)

            {

 

            }

        }

    

        /* Sleepģʽ+DMA´«Êä+»½ÐÑÏÔʾ*/

        case TEST_Dispaly_Sleep_DMA:

        {

            LCD_Sleep_DMA_Test();

            while(1)

            {

 

            }

        }

        default: break;

    }

}

 

/*********************************************************************************

* Function    : Get_Touch_Key_Value

* Description : get key value

* Input       :

* Output      : key value

* Author      : LWQ                         Data : 2022Äê

**********************************************************************************/

uint8_t Get_Touch_Key_Value(void)

{

    static uint8_t key_flags = NO_KEY_DOWN;            /* ÊÇ·ñÓа´¼ü°´ÏµıêÖ¾±äÁ¿ */

    static uint32_t key_value = 0;

    uint8_t i=0, key_num = 0;

    uint32_t uwTouchBits = 0;

    uint32_t uwTouchBits_ref = 0;

    uint32_t uwkey_value = 0;

    uint8_t  ucKey = 0;

 

    uwTouchBits = TK_GetPressRelaeseFlag();

    uwTouchBits_ref = uwTouchBits;

    uwkey_value = key_value;

 

    for(i = 0; i < MAX_KEY_NUM; i++)

    {

        uwTouchBits &= (~key_value);

        if((uwTouchBits >> i) & 0x0001)

        {

            key_num++;

        }

    }

 

    if(key_flags == NO_KEY_DOWN)

    {

        if(key_num == 1)

        {

            key_flags = KEY_DOWN;

        }

    }

    else if((key_value & uwTouchBits) == 0)

    {

        if(key_value != uwTouchBits_ref)

        {

            key_flags = NO_KEY_DOWN;

            key_value = 0;

        }

 

        if(key_num == 1)

        {

            key_flags = OTHER_KEY_DOWN;

        }

    }

 

    if(key_flags != NO_KEY_DOWN)

    {

        if(key_num == 1 && (key_value & uwTouchBits) == 0)

        {

            for(i = 0; i < MAX_KEY_NUM; i++)

            {

                if((key_num == 1) && ((uwTouchBits >> i) & 0x0001))//Åжϰ´¼üÖµ

                {                                      

                    if((uwkey_value & uwTouchBits_ref) == 0)

                    {

                        key_value = uwTouchBits;

                        ucKey = i;

                        printfS("TK_%d\r\n", i);

                    }

                    else

                    {

                        key_value = uwkey_value;

                    }

 

                }

            }

        }

    }

    if(ucKey != 0)//È·ÈÏÊÇ°´Ï£¨²»ÊÇ̧ÊÖ£©

    {

        Beep_On(5);

    }         

 

    return ucKey;

}

 

void SysTick_Callback(void)

{

    if(gu8_time_count_free==0)

    {

        if(gu32_time_1s_cont==400)

        {

            gu8_time_count_free=2;

            gu32_time_1s_cont=0;

        }

        else if(gu32_time_1s_cont==200)

        {

            gu8_time_count_free=1;

            gu32_time_1s_cont++;

        }

        else

        {

            gu32_time_1s_cont++;

        }

    }

}

 

/***********************************************************************

 * Filename    : app.c

 * Description : app source file

 * Author(s)   : xwl  

 * version     : V1.0

 * Modify date : 2020-04-07  

 ***********************************************************************/

#include  "app.h"       

 

 

TIM_HandleTypeDef TIM_Handler;  

 

volatile uint32_t Capture_Ch1, Capture_Ch2;  

uint32_t Timer_CC1IF, Timer_CC2IF;  

uint32_t freq, duty_cycle;   

 

 

void TIM1_MSP_Pre_Init(TIM_HandleTypeDef * htim)   

{

HAL_TIMER_MSP_Init(htim);       

}  

 

//use PC8 to capture input signal  

void TIM1_MSP_Post_Init(void)    

{

GPIO_InitTypeDef gpio_init;

 

gpio_init.Pin = GPIO_PIN_8;  //TIM1_CH1  

gpio_init.Mode = GPIO_MODE_AF_PP;          

gpio_init.Pull = GPIO_PULLUP;    

gpio_init.Alternate = GPIO_FUNCTION_2;         

HAL_GPIO_Init(GPIOC, &gpio_init);         

 

NVIC_ClearPendingIRQ(TIM1_CC_IRQn);   

NVIC_EnableIRQ(TIM1_CC_IRQn);     

}  

 

void TIM1_Init(void)  

{

TIM_SlaveConfigTypeDef sSlaveConfig = {0};   

TIM_IC_InitTypeDef Tim_IC_Init_Para;     

 

TIM_Handler.Instance = TIM1;

TIM_Handler.Init.ARRPreLoadEn = TIM_ARR_PRELOAD_ENABLE;       

TIM_Handler.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

TIM_Handler.Init.CounterMode = TIM_COUNTERMODE_UP;

TIM_Handler.Init.RepetitionCounter = 0;  

TIM_Handler.Init.Prescaler = 0;  

TIM_Handler.Init.Period = 0xFFFF;     // max period

 

TIM1_MSP_Pre_Init(&TIM_Handler);         

HAL_TIMER_Base_Init(&TIM_Handler);       

 

sSlaveConfig.SlaveMode = TIM_SLAVE_MODE_RST;  // reset mode

    sSlaveConfig.InputTrigger = TIM_TRIGGER_SOURCE_TI1FP1;  //  the same as channel 1 capture configuration    

    sSlaveConfig.TriggerPolarity = TIM_SLAVE_CAPTURE_ACTIVE_FALLING;         

    sSlaveConfig.TriggerFilter = TIM_TI1_FILTER_LVL(2);  // no filter     

HAL_TIMER_Slave_Mode_Config(&TIM_Handler, &sSlaveConfig);     

 

Tim_IC_Init_Para.TIFilter = TIM_TI1_FILTER_LVL(2);    // no filter    

Tim_IC_Init_Para.ICPolarity = TIM_SLAVE_CAPTURE_ACTIVE_FALLING;         

Tim_IC_Init_Para.ICPrescaler = 0;

Tim_IC_Init_Para.ICSelection = TIM_ICSELECTION_DIRECTTI;     // TI1FP1  

HAL_TIMER_Capture_Config(TIM_Handler.Instance, &Tim_IC_Init_Para,  TIM_CHANNEL_1);     

 

Tim_IC_Init_Para.ICPolarity = TIM_SLAVE_CAPTURE_ACTIVE_RISING;    

Tim_IC_Init_Para.ICSelection = TIM_ICSELECTION_INDIRECTTI;      // TI1FP2   

HAL_TIMER_Capture_Config(TIM_Handler.Instance, &Tim_IC_Init_Para,  TIM_CHANNEL_2);     

 

    TIM1_MSP_Post_Init();   

    

}

 

void TIM1_PWM_Input_Test(uint32_t timer_clock,uint32_t *period,uint32_t *duty)    

{

    Timer_CC1IF = 0;      

Timer_CC2IF = 0;     

    HAL_TIM_Capture_Start(TIM_Handler.Instance, TIM_CHANNEL_1);   

HAL_TIM_Capture_Start(TIM_Handler.Instance, TIM_CHANNEL_2);        

    uint32_t diff = 0;

    while(1)  

    {

        if (TIM1->SR & TIMER_SR_CC1IF) //reset      

{

TIM1->SR = 0;   

while (0 == (TIM1->SR & TIMER_SR_CC2IF)  );     

Capture_Ch2 = TIM1->CCR2;   

 

while (0 == (TIM1->SR & TIMER_SR_CC1IF)  );     

Capture_Ch1 = TIM1->CCR1;

 

Timer_CC1IF = 1;  

Timer_CC2IF = 1;      

TIM1->SR = 0;   

}    

        if(Timer_CC1IF && Timer_CC2IF)  

{

 

if(Capture_Ch1 > Capture_Ch2)

{

diff = (uint32_t)Capture_Ch1 - (uint32_t)Capture_Ch2;

freq = timer_clock / (uint32_t)Capture_Ch1;

duty_cycle = ( diff * 100) / ((uint32_t)Capture_Ch1);

}

else

{

diff = (uint32_t)Capture_Ch1 + (uint32_t)0xFFFF - (uint32_t)Capture_Ch2;

freq = timer_clock / ((uint32_t)0xFFFF + (uint32_t)Capture_Ch1);

duty_cycle = ( diff * 100) / ((uint32_t)0xFFFF + (uint32_t)Capture_Ch1);

}

 

     

            //printfS("Capture_Ch1:%d, Capture_Ch2:%d\n", Capture_Ch1, Capture_Ch2);      

//printfS("External signal frequency:%dHz, duty cycle:%d%%\n", freq, duty_cycle);   

*period = freq;

*duty = duty_cycle;

            Timer_CC1IF = 0;    

            Timer_CC2IF = 0;  

break;  

}

    }

}  

 

 

 

void App_Test(void)

{   

    float ff_getTempHum[2];

    uint16_t fu16_num_up, fu16_num_down;

    uint32_t fu32_Displayup[4]={0},fu32_Displaydown[4]={0},fu32_num_123567p[7]={0},fu32_num_col[2]={0},fu32_num_s=0;

    uint8_t  fu8_Tkey_state = 0xff, fu8_Work_Mode=0;

    uint16_t fu16_set_num_up=0,fu16_set_num_down=0;

    uint8_t  fu8_Hidden_point=0;//Òþ²Øµã

    uint8_t  ret = 0;

    

    LCD_Config();  

    

    SHT30_Init();

 

    BUZZER_Init();

    

    ret = TouchKey_Init();

    if(ret)

    {

        printfS("TouchKey_Init is Fail!\r\n");

    }

    else

    {

        printfS("TouchKey_Init is Success!\r\n");      

    }

    

    //LCD_YR1618A_SelfDisplay();

 

    uint32_t timer_clock;

    

    timer_clock = System_Get_APBClock();

    

    if (System_Get_SystemClock() != System_Get_APBClock())  // if hclk/pclk != 1, then timer clk = pclk * 2  

    {

       timer_clock =  System_Get_APBClock() << 1;    

    }  

     

TIM1_Init();

 

 

    while(1)

    {

        fu8_Tkey_state = TK_TimerSacn_GetKeyVal();//Get_Touch_Key_Value();

        if(fu8_Tkey_state != 0xFF)

        {

            Beep_On(5);

        #ifndef TKEY_WAVEFORM_OUTPUT

            printfS("TK_%d\r\n", fu8_Tkey_state);

        #endif

        }

        TKEY_Calibrate_Process();

 

uint32_t period;

uint32_t duty;

        TIM1_PWM_Input_Test(timer_clock,&period,&duty);    

        HAL_TIM_Capture_Stop(TIM_Handler.Instance, TIM_CHANNEL_1);  

        HAL_TIM_Capture_Stop(TIM_Handler.Instance, TIM_CHANNEL_2);    

        HAL_TIMER_Clear_Capture_Flag(&TIM_Handler, TIM_CHANNEL_1);  

        HAL_TIMER_Clear_Capture_Flag(&TIM_Handler, TIM_CHANNEL_2);  

 

        fu32_Displayup[0]=period/1000%10;//ǧλ,LCDµÚ1¸öλÖÃ(´Ó×óµ½ÓÒ)--5

        fu32_Displayup[1]=period/100%10;//°×λ,LCDµÚ2¸öλÖÃ(´Ó×óµ½ÓÒ)---6

        fu32_Displayup[2]=period/10%10;//ʮλ,LCDµÚ3¸öλÖÃ(´Ó×óµ½ÓÒ)----7

        fu32_Displayup[3]=period/1%10;//¸öλ,LCDµÚ4¸öλÖÃ(´Ó×óµ½ÓÒ)-----8

 

        fu32_Displaydown[0]=duty/1000%10;//ǧλ,LCDµÚ1¸öλÖÃ(´Ó×óµ½ÓÒ)--5

        fu32_Displaydown[1]=duty/100%10;//°×λ,LCDµÚ2¸öλÖÃ(´Ó×óµ½ÓÒ)---6

        fu32_Displaydown[2]=duty/10%10;//ʮλ,LCDµÚ3¸öλÖÃ(´Ó×óµ½ÓÒ)----7

        fu32_Displaydown[3]=duty/1%10;//¸öλ,LCDµÚ4¸öλÖÃ(´Ó×óµ½ÓÒ)-----8

  LCD_YR1618A_VIEW(fu32_Displayup,fu32_Displaydown,fu32_num_123567p,fu32_num_col,fu32_num_s);

System_Delay_MS(1000);

    }

}

 

测试

在PC8引脚产生一个PWM信号,液晶屏查看频率(上四位)和占空比(下四位)变化。

定性查看变化是否正确。

 

有条件可以使用PWM信号源,测量不同频率和占空比的信号,测量精度。

 

9kgMhYAAAAAAA==

 

 

总结

对于采集精度由于没有标准PWM源所以不能精确测量,只能定性测试。

有条件的可以测试下

 

基于官方的HAL库,能进行快速功能验证开发,本次快速实现了一个PWM采集的功能,集合前面的计数器,电压计,PWM发生器,PWM采集器可以综合为一个试用的测试小工具,可以考虑做个盒子,单独引出探头,打造为一个小测试工具,这在嵌入式开发时非常方便有用。

 

 


回复

2691

帖子

3

资源

版主

基于官方的HAL库,能进行快速功能验证开发,本次快速实现了一个PWM采集的功能,集合前面的计数器,电压计,PWM发生器,PWM采集器可以综合为一个试用的测试小工具,可以考虑做个盒子,单独引出探头,打造为一个小测试工具,这在嵌入式开发时非常方便有用。

想法非常不错。


回复
您需要登录后才可以回帖 登录 | 注册

相关帖子
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
    关闭
    站长推荐上一条 1/6 下一条

    About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

    站点相关: 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

    北京市海淀区知春路23号集成电路设计园量子银座1305 电话:(010)82350740 邮编:100191

    电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2022 EEWORLD.com.cn, Inc. All rights reserved
    快速回复 返回顶部 返回列表