FM0导引头及0-1对应编码为:
#define YES 1
#define NO 0
#define HEADER_LEN 18
#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)
{
if(htim->Instance == TIM1)
{
FM0_Encoder(&Encode);
}
}
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;
else if(FM0->code[FM0->len_temp - FM0->len] == '1')
toggle = NO;
if(num == 1)
{
if((toggle == YES))
{
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))
{
HAL_GPIO_TogglePin(GPIOC, VCONT1_Pin);
HAL_GPIO_TogglePin(GPIOC, VCONT2_Pin);
}
num++;
}
}
else if(FM0->sta == TIMER_START)
{
HAL_GPIO_WritePin(GPIOC, VCONT1_Pin, GPIO_PIN_RESET);
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);
HAL_TIM_Base_Stop_IT(&htim1);
}
}
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';
delay_us(200);
HAL_TIM_Base_Start_IT(&htim1);
Encode.sta = TIMER_START;
}
例子:TAG_back((const uint8_t *)RN16_str, RN16_LEN);
|