• Manchester Decode by using PWM Input Capture Function via stm32 MCU


    MCU ST STM32F091RC

    Hardware Interface Port, here we use PB01, TIM14, Channel 1.

    Manchester code comes from TDA5225 receiver PP2 PIN. Carrier frequncy = 315MHz, data baudrate is about 9600 bit/s, However, this firmware we support much wide rate.

    See the code here:

      1 /*******************************************************************************
      2   * @file    tda5225_value_tim14.c
      3   * @author  Milo lu
      4   * @brief   Capture mancherster code by using tim14 pwm capture/Compare function.
      5   *         
      6   @Versions description:
      7   * v1.0.0
      8   * 1. adapted,first functional draft 
      9 */
     10 /* Includes ------------------------------------------------------------------*/
     11 #include "tda5225_value_tim14.h"
     12 
     13 /** @addtogroup STM32F0xx_HAL_Examples
     14   * @{
     15   */
     16 
     17 /** @addtogroup Templates
     18   * @{
     19   */
     20 
     21 /* Private typedef -----------------------------------------------------------*/
     22 #define MANCHESTER_LENGTH       32
     23 /* Private define ------------------------------------------------------------*/
     24 #define COMBINATION_BITS        0xC000
     25 /*          ____    
     26   0 ->10 ->     |____   */
     27 #define MANCHESTER_0            0x8000
     28 /*                ____
     29   1 ->01 ->  ____|      */
     30 #define MANCHESTER_1            0x4000
     31 #define FDEV_ERROR              0x01F4
     32 /* Private macro -------------------------------------------------------------*/
     33 /* Private variables ---------------------------------------------------------*/
     34 /* Timer handler declaration */
     35 TIM_HandleTypeDef       g_h_tim14;
     36 /* Timer Input Capture Configuration Structure declaration */
     37 TIM_IC_InitTypeDef      sICConfig;
     38 CaptureStepTypeDef     CAPTURE_STEP = RFPREAMBLE;
     39 
     40 uint32_t uwPulseTM_Baseline_Val,uwPulseTM_Baseline_DVal;
     41 uint16_t uw_Current_Pulse_Tim,uw_Last_Pulse_Tim;
     42 uint16_t uwIC1_Start_Val,uwIC1_End_Val,uwPulse_Approximated_Val;
     43 uint16_t uwPulseTIM_BUF[8];
     44 uint16_t RF_MANCHESTER_DATA_BUF[MANCHESTER_LENGTH];
     45 uint8_t  Manchester_Index;
     46 uint8_t  RF_DATA_BUF[(MANCHESTER_LENGTH>>1)];
     47 /* Private function prototypes -----------------------------------------------*/
     48 HAL_StatusTypeDef manchester_decoding(unsigned int dat,unsigned char *manchester_out);
     49 HAL_StatusTypeDef LF_TIM14_PWM_Init(void);
     50 HAL_StatusTypeDef RF_Receved_Start(FunctionalState k);
     51 /* Private functions ---------------------------------------------------------*/
     52 /**--------------------------- Manchester --------------------------------*
     53   * @brief  decoding manchester code
     54   * @param  dat--input manchester code
     55   * @param  *manchester_out data output
     56   * @retval HAL status
     57   * byte encoding manchester format
     58   *          |     ____|
     59   * 1 ->01 ->|____|    |
     60   *          |         |
     61   *          |____     |
     62   * 0 ->10 ->|    |____|
     63   *          |         |
     64   */
     65 HAL_StatusTypeDef manchester_decoding(unsigned int dat,unsigned char *manchester_out)
     66 {
     67     unsigned char i;
     68     unsigned int k;
     69     *manchester_out=0;
     70     for(i=0;i<8;i++)
     71     {
     72       k=dat&COMBINATION_BITS;
     73       if(k==MANCHESTER_1)
     74       {
     75         *manchester_out<<=1;
     76         *manchester_out+=1;
     77       }
     78       else if(k==MANCHESTER_0)
     79       {
     80         *manchester_out<<=1;
     81       }
     82       else
     83       {
     84         return HAL_ERROR;
     85       }
     86       dat<<=2;
     87     }
     88     return HAL_OK;
     89 }
     90 
     91 /**--------------------------- TIM14 --------------------------------*
     92   * 
     93   * @brief  Initializes the TIM1 PWM Time Base according to the specified
     94   *         parameters in the TIM_HandleTypeDef and create the associated handle.
     95   *         PWM capture/compare function by using tim14
     96   *         5000 CNT * 9600 = 48MHz
     97   * @param  None
     98   * @retval None
     99   */
    100 HAL_StatusTypeDef LF_TIM14_PWM_Init(void)
    101 {
    102   /*##-1- Configure the TIM peripheral #######################################*/
    103   /* TIM14 configuration: Input Capture mode ---------------------
    104      The external signal is connected to TIM14 CH1 pin (PB01)  
    105      The Both edge is used as active edge,
    106      The TIM14 CCR2 is used to compute the frequency value 
    107   ------------------------------------------------------------ */
    108   /*Set TIM14 instance*/
    109   g_h_tim14.Instance = TIM14;
    110   /* Initialize TIM14 peripheral as follows:
    111        + Period = 0xFFFF
    112        + Prescaler = 0
    113        + ClockDivision = 0
    114        + Counter direction = Up
    115   */
    116   g_h_tim14.Init.Period            = 0xFFFF;
    117   g_h_tim14.Init.Prescaler         = 0;
    118   g_h_tim14.Init.ClockDivision     = 0;
    119   g_h_tim14.Init.CounterMode       = TIM_COUNTERMODE_UP;
    120   g_h_tim14.Init.RepetitionCounter = 0;
    121   g_h_tim14.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
    122   if(HAL_TIM_IC_Init(&g_h_tim14) != HAL_OK)
    123   {
    124     /* Configuration Error */
    125     return HAL_ERROR;
    126   }
    127   /*##-2- Configure the Input Capture channel ################################*/ 
    128   /* Configure the Input Capture of channel 1 */
    129   sICConfig.ICPolarity  = TIM_ICPOLARITY_BOTHEDGE;
    130   sICConfig.ICSelection = TIM_ICSELECTION_DIRECTTI;
    131   sICConfig.ICPrescaler = TIM_ICPSC_DIV1;
    132   sICConfig.ICFilter    = 0;  
    133   if(HAL_TIM_IC_ConfigChannel(&g_h_tim14, &sICConfig, TIM_CHANNEL_1) != HAL_OK)
    134   {
    135     /* Configuration Error */
    136     return HAL_ERROR;
    137   }
    138   return HAL_OK;
    139 }
    140 HAL_StatusTypeDef RF_Receved_Start(FunctionalState k)
    141 {
    142   /*##-3- Start/Stop the Input Capture in interrupt mode ##########################*/
    143   if(k==ENABLE)
    144   {
    145     if(HAL_TIM_IC_Start_IT(&g_h_tim14, TIM_CHANNEL_1) != HAL_OK)
    146     {
    147       /* start Error */
    148       return HAL_ERROR;
    149     }
    150   }
    151   else if(k==DISABLE)
    152   {
    153     if(HAL_TIM_IC_Stop_IT(&g_h_tim14, TIM_CHANNEL_1) != HAL_OK)
    154     {
    155       /* stop Error */
    156       return HAL_ERROR;
    157     }
    158   }
    159   return HAL_OK;
    160 }
    161 /**
    162   * @brief  Conversion complete callback in non blocking mode 
    163   * @param  htim : hadc handle
    164   * @retval None
    165   */
    166 void userRF_TIM14_CaptureCallback(TIM_HandleTypeDef *htim)
    167 {
    168   static uint8_t preamble_cnt = 0,bits_of_byte;
    169   static uint16_t one_byte;
    170   
    171   if ( (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)&&(htim->Instance == TIM14))
    172   {
    173     /* Get the Input Capture value */ 
    174     uwIC1_Start_Val = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
    175     /* Capture computation */
    176     uw_Current_Pulse_Tim = (uwIC1_Start_Val - uwIC1_End_Val);
    177     
    178     /* get approximated value */
    179     uwPulse_Approximated_Val = uw_Current_Pulse_Tim;
    180     switch(CAPTURE_STEP)
    181     {
    182     case RFPREAMBLE:
    183       {
    184         if(uw_Current_Pulse_Tim > uw_Last_Pulse_Tim)
    185         {
    186           if(uw_Current_Pulse_Tim - uw_Last_Pulse_Tim < 200)
    187           {
    188             /* wait for a stable frequence */
    189             uwPulseTIM_BUF[preamble_cnt ++]= uwPulse_Approximated_Val;
    190           }
    191         }
    192         else if(uw_Last_Pulse_Tim - uw_Current_Pulse_Tim < 200)
    193         {
    194           /* wait for a stable frequence */
    195           uwPulseTIM_BUF[preamble_cnt ++]= uwPulse_Approximated_Val;
    196         }
    197         else 
    198         {
    199           preamble_cnt = 0;
    200         }
    201         uw_Last_Pulse_Tim = uw_Current_Pulse_Tim;
    202         
    203         /* got a stable frequence */
    204         if(preamble_cnt >= 8)
    205         {
    206           /* Frequency computation: for this example TIMx (TIM14) is clocked by
    207            APB1Clk ,Here we do not using frequence value but pulse time*/
    208           /*frequence = HAL_RCC_GetPCLK1Freq() / uw_Current_Pulse_Tim;*/
    209           uwPulseTM_Baseline_Val = uwPulseTIM_BUF[0];
    210           uwPulseTM_Baseline_Val += uwPulseTIM_BUF[1];
    211           uwPulseTM_Baseline_Val += uwPulseTIM_BUF[2];
    212           uwPulseTM_Baseline_Val += uwPulseTIM_BUF[3];
    213           uwPulseTM_Baseline_Val += uwPulseTIM_BUF[4];
    214           uwPulseTM_Baseline_Val += uwPulseTIM_BUF[5];
    215           uwPulseTM_Baseline_Val += uwPulseTIM_BUF[6];
    216           uwPulseTM_Baseline_Val += uwPulseTIM_BUF[7];
    217           uwPulseTM_Baseline_Val>>=3;
    218           uwPulseTM_Baseline_DVal=(uwPulseTM_Baseline_Val<<1);
    219           Manchester_Index = 0;
    220           bits_of_byte=0;
    221           one_byte=0;
    222           CAPTURE_STEP = RFHEADER;
    223         }
    224         break;
    225       }
    226     case RFHEADER:
    227       {
    228         /*uwPulseTIM_BUF[preamble_cnt++]=uwPulse_Approximated_Val;*/
    229         if((uwPulse_Approximated_Val<(uwPulseTM_Baseline_DVal+FDEV_ERROR))&&
    230           (uwPulse_Approximated_Val>(uwPulseTM_Baseline_DVal-FDEV_ERROR)))
    231         {
    232           /* start from high-to-low transition on PB01 */
    233           one_byte<<=1;
    234           /* got header */
    235           one_byte+=1;
    236           bits_of_byte++;
    237           CAPTURE_STEP = RF_DATA_RISING_EDGE;
    238         }
    239         else if((uwPulse_Approximated_Val<(uwPulseTM_Baseline_Val+FDEV_ERROR))&&
    240                (uwPulse_Approximated_Val>(uwPulseTM_Baseline_Val-FDEV_ERROR)))
    241         {
    242           /*continue waiting for header...*/
    243         }
    244         else
    245         {
    246           /*failed ...repeat*/
    247           one_byte = 0;
    248           bits_of_byte = 0;
    249           CAPTURE_STEP = RFHEADER;
    250         }
    251         break;
    252       }
    253     case RF_DATA_RISING_EDGE:/*Get low time*/
    254       {
    255         /*wait for falling edge trigger*/
    256         CAPTURE_STEP = FR_DATA_FALLING_EDGE;
    257         /*uwPulseTIM_BUF[preamble_cnt++]=uwPulse_Approximated_Val;*/
    258         if((uwPulse_Approximated_Val<(uwPulseTM_Baseline_DVal+FDEV_ERROR))&&
    259           (uwPulse_Approximated_Val>(uwPulseTM_Baseline_DVal-FDEV_ERROR)))
    260         {
    261           one_byte<<=1;
    262           bits_of_byte++;
    263           if(bits_of_byte >= 16)
    264           {
    265             RF_MANCHESTER_DATA_BUF[Manchester_Index++] = one_byte;
    266             if(Manchester_Index>=MANCHESTER_LENGTH)
    267             {
    268               CAPTURE_STEP = RFEND;/*end*/
    269             }
    270             one_byte = 0;
    271             bits_of_byte = 0;
    272           }
    273           one_byte<<=1;
    274           bits_of_byte++;
    275           if(bits_of_byte >= 16)
    276           {
    277             RF_MANCHESTER_DATA_BUF[Manchester_Index++] = one_byte;
    278             if(Manchester_Index>=MANCHESTER_LENGTH)
    279             {
    280               CAPTURE_STEP = RFEND;/*end*/
    281             }
    282             one_byte = 0;
    283             bits_of_byte = 0;
    284           }
    285         }
    286         else if((uwPulse_Approximated_Val<(uwPulseTM_Baseline_Val+FDEV_ERROR))&&
    287                (uwPulse_Approximated_Val>(uwPulseTM_Baseline_Val-FDEV_ERROR)))
    288         {
    289           one_byte<<=1;
    290           bits_of_byte++;
    291           if(bits_of_byte >= 16)
    292           {
    293             RF_MANCHESTER_DATA_BUF[Manchester_Index++] = one_byte;
    294             if(Manchester_Index>=MANCHESTER_LENGTH)
    295             {
    296               CAPTURE_STEP = RFEND;/*end*/
    297             }
    298             one_byte = 0;
    299             bits_of_byte = 0;
    300           }
    301         }
    302         break;
    303       }
    304     case FR_DATA_FALLING_EDGE:/*Get high time*/
    305       {
    306         /*wait for rising edge trigger*/
    307         CAPTURE_STEP = RF_DATA_RISING_EDGE;
    308         /*uwPulseTIM_BUF[preamble_cnt++]=uwPulse_Approximated_Val;*/
    309         if((uwPulse_Approximated_Val<(uwPulseTM_Baseline_DVal+FDEV_ERROR))&&
    310           (uwPulse_Approximated_Val>(uwPulseTM_Baseline_DVal-FDEV_ERROR)))
    311         {
    312           one_byte<<=1;
    313           one_byte+=1;
    314           bits_of_byte++;
    315           if(bits_of_byte >= 16)
    316           {
    317             RF_MANCHESTER_DATA_BUF[Manchester_Index++] = one_byte;
    318             if(Manchester_Index>=MANCHESTER_LENGTH)
    319             {
    320               CAPTURE_STEP = RFEND;/*end*/
    321             }
    322             one_byte = 0;
    323             bits_of_byte = 0;
    324           }
    325           one_byte<<=1;
    326           one_byte+=1;
    327           bits_of_byte++;
    328           if(bits_of_byte >= 16)
    329           {
    330             RF_MANCHESTER_DATA_BUF[Manchester_Index++] = one_byte;
    331             if(Manchester_Index>=MANCHESTER_LENGTH)
    332             {
    333               CAPTURE_STEP = RFEND;/*end*/
    334             }
    335             one_byte = 0;
    336             bits_of_byte = 0;
    337           }
    338         }
    339         else if((uwPulse_Approximated_Val<(uwPulseTM_Baseline_Val+FDEV_ERROR))&&
    340                (uwPulse_Approximated_Val>(uwPulseTM_Baseline_Val-FDEV_ERROR)))
    341         {
    342           /* Rising edge = 1 */
    343           one_byte<<=1;
    344           one_byte+=1;
    345           bits_of_byte++;
    346           if(bits_of_byte >= 16)
    347           {
    348             RF_MANCHESTER_DATA_BUF[Manchester_Index++] = one_byte;
    349             if(Manchester_Index>=MANCHESTER_LENGTH)
    350             {
    351               CAPTURE_STEP = RFEND;/*end*/
    352             }
    353             one_byte = 0;
    354             bits_of_byte = 0;
    355           }
    356         }
    357         else
    358         {
    359           one_byte = 0;
    360           bits_of_byte = 0;
    361           CAPTURE_STEP = RFEND;/*end*/
    362         }
    363         break;
    364       }
    365     case RFEND:
    366       {
    367         for(uint8_t i = 0;i < Manchester_Index;i++)
    368         {
    369           if(manchester_decoding(RF_MANCHESTER_DATA_BUF[i],&RF_DATA_BUF[i]) != HAL_OK)
    370           {
    371             break;
    372           }
    373         }
    374         if((RF_DATA_BUF[0]==0x2A)&&(RF_DATA_BUF[1]==0x1D) || (RF_DATA_BUF[0]==0x6D))
    375         {
    376           userSirenPwmPulsesOutput(10);
    377         }
    378         
    379         for(uint8_t i = 0; i < Manchester_Index; i++)
    380         {
    381           RF_DATA_BUF[i] = 0;
    382         }
    383         preamble_cnt = 0;
    384         CAPTURE_STEP = RFPREAMBLE;
    385         break;
    386       }
    387     default:
    388       {
    389         break;
    390       }
    391     }
    392     
    393     uwIC1_End_Val = uwIC1_Start_Val;
    394   }
    395 }
    396 /**
    397   * @}
    398   */
    399 
    400 /**
    401   * @}
    402   */
    403 
    404 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
    tda5225_value_tim14.c
     1 /**
     2   ******************************************************************************
     3   * @file    rf_spi2_setup.h 
     4   * @author  MCD Application Team
     5   * @brief   Header for main.c module
     6   ******************************************************************************
     7   * @attention
     8   *
     9   * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
    10   *
    11   * Redistribution and use in source and binary forms, with or without modification,
    12   * are permitted provided that the following conditions are met:
    13   *   1. Redistributions of source code must retain the above copyright notice,
    14   *      this list of conditions and the following disclaimer.
    15   *   2. Redistributions in binary form must reproduce the above copyright notice,
    16   *      this list of conditions and the following disclaimer in the documentation
    17   *      and/or other materials provided with the distribution.
    18   *   3. Neither the name of STMicroelectronics nor the names of its contributors
    19   *      may be used to endorse or promote products derived from this software
    20   *      without specific prior written permission.
    21   *
    22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    23   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    24   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    25   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
    26   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    27   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    28   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    29   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    30   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    31   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    32   *
    33   ******************************************************************************
    34   */
    35   
    36 /* Define to prevent recursive inclusion -------------------------------------*/
    37 #ifndef _TDA5225_VALUE_TIM14_H
    38 #define _TDA5225_VALUE_TIM14_H
    39 /* Includes ------------------------------------------------------------------*/
    40 #include "stm32f0xx_hal.h"
    41 #include <stdio.h>
    42 #include <string.h>
    43 
    44 #include "sr_tim2_setup.h" 
    45 
    46 /* Exported types ------------------------------------------------------------*/
    47 typedef enum
    48 {
    49   RFPREAMBLE = 0,
    50   RFHEADER,
    51   RF_DATA_RISING_EDGE,
    52   FR_DATA_FALLING_EDGE,
    53   RFEND,
    54 }CaptureStepTypeDef;
    55 /* Exported constants --------------------------------------------------------*/
    56 /* Exported macro ------------------------------------------------------------*/
    57 /* Exported functions ------------------------------------------------------- */
    58 HAL_StatusTypeDef LF_TIM14_PWM_Init(void);
    59 HAL_StatusTypeDef RF_Receved_Start(FunctionalState k);
    60 #endif /* __MAIN_H */
    61 
    62 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
    tda5225_value_tim14.h

    Thanks, Enjoy.

    End.

  • 相关阅读:
    用grunt搭建自动化的web前端开发环境-完整教程
    SQL Server:触发器详解
    利用junit对springMVC的Controller进行测试
    jquery-barcode:js实现的条码打印
    16个良好的 Bootstrap Angularjs 管理后台主题
    Spring Security 4 Hello World Annotation+XML
    intellij 13新建javaweb项目并用tomcat 7启动
    JavaScript类和继承:constructor属性
    javascript 的面相对象
    javascript call apply bind caller callee 的用法
  • 原文地址:https://www.cnblogs.com/lumao1122-Milolu/p/12835084.html
Copyright © 2020-2023  润新知