SIF协议
- 一次传输一帧数据,传输结束后要求线路空闲状态为低电平,每次传输需一次性完整传输所有数据。
- 数据的电平遵守 TTL 规范。
1.1 信号定义
Tosc定义: 250us <Tosc<2ms,推荐值:500us
1、同步信号:
2、Bit(0)表示方法:
3、Bit(1)表示方法:
1.2 代码实现
需要定义一个中断定时器,定时器周期为一个Tosc,中断处理函数判断是否有数据需要发送。下面是发送数据的处理函数。
#define SIF_VERSION 1
#define SIF_SYNC 53
#define SIF_SEND_COUNT 3
volatile static uint8_t sif_sync_tosc = 0;
volatile static uint8_t sif_send_tosc = 0;
volatile static uint8_t state_mode = 0;
volatile static int8_t bit_cnt = 7;
volatile static uint8_t byte_cnt = 0;
static uint8_t result[64] = {0};//需要发送的数据
volatile static uint8_t length = 0;//数据的长度
static void sif_send_data_handle(uint8_t state)
{
switch(state)
{
case SYNC_SIGNAL://同步模式
if (sif_sync_tosc < SIF_SYNC - 2) {
sif_turn_off();
} else {
sif_turn_on();
}
sif_sync_tosc++;
if (sif_sync_tosc >= SIF_SYNC)
{
state_mode = SEND_DATA;
sif_sync_tosc = 0;
bit_cnt = 7;
byte_cnt = 0;
sif_send_tosc = 0;
}
break;
case SEND_DATA: //发送数据
static uint8_t res;
uint8_t count = SIF_SEND_COUNT;
static uint8_t nums = sizeof(uint8_t) * 8;
uint8_t *p = (uint8_t *)result;
sif_send_tosc = sif_send_tosc % count;
uint8_t data = (p[byte_cnt] >> bit_cnt) & 0x1;
if (data)
{
if (sif_send_tosc == 0)
{
sif_turn_off();
sif_send_tosc ++;
}
else if (sif_send_tosc == 1)
{
sif_turn_on();
sif_send_tosc ++;
} else {
sif_send_tosc = 0;
}
}
else
{
if (sif_send_tosc == 0)
{
sif_turn_off();
sif_send_tosc++;
}
else if (sif_send_tosc == 2)
{
sif_turn_on();
sif_send_tosc = 0;
} else
{
sif_send_tosc++;
}
}
if (sif_send_tosc == 0)
{
if (--bit_cnt < 0)
{
byte_cnt++;
bit_cnt = 7;
}
if (byte_cnt >= length)
{
state_mode = SEND_DATA_COMPLETE;
break;
}
}
break;
case SEND_DATA_COMPLETE://数据发送完成,将标志位清0
state_mode = 0;
sif_turn_off();
length = 0;
memset(result, 0, sizeof(result));
break;
default:
break;
}
}