其他分享
首页 > 其他分享> > FM0编码keil

FM0编码keil

作者:互联网

#define YES                1 //翻转
#define NO                 0 //不翻
#define HEADER_LEN       18//FM0 HEADER
#define RN16_LEN       16
#define READ_LEN       33
#define WRITE_LEN       17

const char preamble_str[HEADER_LEN] = "000000000000101011";
const char RN16_str[RN16_LEN] = "1001101001100101";
const char READ_str[READ_LEN] =   "11111110000000011001101001100101";
const char WRITE_str[WRITE_LEN] = "01001101001100101";

#define DATA_LEN    128 
struct master_data
{  
    uint8_t data[DATA_LEN];
    uint8_t code[DATA_LEN];
    uint8_t temp[1];
    uint8_t len;
    uint8_t len_temp;
    
    enum flag{RX_WAIT, CHECKING, RX_OK, TIMER_START, ERR} sta;
};

struct master_data  Encode = {{0}, {0}, {0}, 0, 0, RX_WAIT}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) //注意初始化定时器,fm0速度和定时器速度正相关
{
    if(htim->Instance == TIM1)//除了定时器方式还可以用延时,但是延时可能会被更高级中断break
    {
            FM0_Encoder(&Encode);  //串口处理
    }
}

/**
  * @brief  FM0编码
  * @param  master_data FM0 数据
  * @retval none
  * @e.g.
  */
void FM0_Encoder(struct master_data *FM0)
{
    static uint8_t num = 0, toggle = 0;

    if(FM0->len)
    {
        if((FM0->code[FM0->len_temp - FM0->len] == '0'))
            toggle = YES; // toggle
        else if(FM0->code[FM0->len_temp - FM0->len] == '1')
            toggle = NO;  //NO toggle

        if(num == 1)//one period completed
        {
            if((toggle == YES))//&& (MAX41470_flag)  
            {
                HAL_GPIO_TogglePin(GPIOC, VCONT1_Pin);
                HAL_GPIO_TogglePin(GPIOC, VCONT2_Pin);
            }
            FM0->len--;
            num = 0;
        }
        else
        {
            if((order != 'E') || ((FM0->len_temp - FM0->len) != 16))//16位置是冲突码
            {
                HAL_GPIO_TogglePin(GPIOC, VCONT1_Pin);
                HAL_GPIO_TogglePin(GPIOC, VCONT2_Pin);
            }
            num++;
        }
    }
    else if(FM0->sta == TIMER_START)//FM0 send completed
    {
        HAL_GPIO_WritePin(GPIOC, VCONT1_Pin, GPIO_PIN_RESET);GPIO_PIN_RESET-GPIO_PIN_SET 模拟
        HAL_GPIO_WritePin(GPIOC, VCONT2_Pin, GPIO_PIN_SET);
        memset(&FM0->code, 0, sizeof(FM0->code));
        FM0->sta = RX_WAIT;
        FM0->len_temp = 0;
        num = 0;
        HAL_UART_Receive_IT(&huart1, FM0->temp, 1);
        __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);       // 使能 IDLE中断
        HAL_TIM_Base_Stop_IT(&htim1);
    }
}

/**
  * @brief  标签返回信号
  * @param  data 返回帧数
  * @param  len  返回帧长
  * @retval none
  * @e.g.
  */
void TAG_back(const uint8_t *data, uint8_t len)
{
#define END_LEN   1
    uint8_t len_temp = len + HEADER_LEN + END_LEN;

    order = 'E';
    Encode.sta = RX_OK;

    Encode.len_temp = len_temp;
    Encode.len = len_temp;

    memcpy(Encode.code, preamble_str, HEADER_LEN);
    memcpy(Encode.code+HEADER_LEN, data, len);

    Encode.code[Encode.len-1] = '1'; //end bit
    delay_us(200);
  
    HAL_TIM_Base_Start_IT(&htim1); //或者在main函数里面根据标志位开启
    Encode.sta  = TIMER_START;
}


例子:TAG_back((const uint8_t *)RN16_str, RN16_LEN);

————————————————
版权声明:本文为CSDN博主「Sky_Lannister」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Fei_Yang_YF/article/details/124257784

 

标签:编码,HAL,temp,keil,LEN,FM0,len,GPIO
来源: https://www.cnblogs.com/icaowu/p/16325337.html