百度百科
差分曼彻斯特码,在信号位开始时改变信号极性,表示逻辑"0";在信号位开始时不改变信号极性,表示逻辑"1"
NanoLockit
差分曼彻斯特码,在信号位开始时改变信号极性,表示逻辑"1";在信号位开始时不改变信号极性,表示逻辑"0"
#ifndef _DIFF_MANCHESTER_H_
#define _DIFF_MANCHESTER_H_
//Constant indicates 1, and change indicates 0.
// #define DIFF_MANCHESTER_CHANGE_INDICATE_0
// NanoLockit
//Constant indicates 0, and change indicates 1.
#define DIFF_MANCHESTER_CHANGE_INDICATE_1
unsigned char get_bit_value(unsigned char *buf, unsigned char bit);
void set_bit_value(unsigned char *buf, unsigned char bit, unsigned char value);
void diff_manchester_encode(unsigned char *src_code, unsigned char *dst_code);
void diff_manchester_decode(unsigned char *src_code, unsigned char *dst_code);
#endif
#include "diff_manchester.h"
unsigned char get_bit_value(unsigned char *buf, unsigned char bit)
{
unsigned char value;
unsigned char offset = bit / 8;
unsigned char pos = bit % 8;
value = buf[offset] & (1 << pos);
return value == 0 ? 0:1;
}
void set_bit_value(unsigned char *buf, unsigned char bit, unsigned char value)
{
unsigned char offset = bit / 8;
unsigned char pos = bit % 8;
if(value) {
buf[offset] |= (1 << pos);
} else {
buf[offset] &= ~(1 << pos);
}
}
#ifdef DIFF_MANCHESTER_CHANGE_INDICATE_0
void diff_manchester_encode(unsigned char *src_code, unsigned char *dst_code)
{
unsigned char bit = 0;
unsigned char bit_value;
unsigned char encode_last_bit_value;
for(bit=0; bit<80; bit++) {
bit_value = get_bit_value(src_code, bit);
if(bit == 0) {
if(bit_value == 1) {
set_bit_value(dst_code, bit*2, 1);
set_bit_value(dst_code, bit*2+1, 0);
encode_last_bit_value = 0;
} else {
set_bit_value(dst_code, bit*2, 0);
set_bit_value(dst_code, bit*2+1, 1);
encode_last_bit_value = 1;
}
} else {
if(bit_value == 1) {
if(encode_last_bit_value == 1) {
set_bit_value(dst_code, bit*2, 1);
set_bit_value(dst_code, bit*2+1, 0);
encode_last_bit_value = 0;
} else {
set_bit_value(dst_code, bit*2, 0);
set_bit_value(dst_code, bit*2+1, 1);
encode_last_bit_value = 1;
}
} else {
if(encode_last_bit_value == 1) {
set_bit_value(dst_code, bit*2, 0);
set_bit_value(dst_code, bit*2+1, 1);
encode_last_bit_value = 1;
} else {
set_bit_value(dst_code, bit*2, 1);
set_bit_value(dst_code, bit*2+1, 0);
encode_last_bit_value = 0;
}
}
}
}
}
void diff_manchester_decode(unsigned char *src_code, unsigned char *dst_code)
{
unsigned char bit = 0;
unsigned char f_value, s_value;
unsigned char src_code_last_bit_value;
for(bit=0; bit<80; bit++) {
f_value = get_bit_value(src_code, bit * 2);
s_value = get_bit_value(src_code, bit * 2 + 1);
if(bit == 0) {
if(0 == f_value) {
set_bit_value(dst_code, bit, 0);
} else {
set_bit_value(dst_code, bit, 1);
}
} else {
if(0 == src_code_last_bit_value) {
if(0 == f_value) {
set_bit_value(dst_code, bit, 1);
} else {
set_bit_value(dst_code, bit, 0);
}
} else {
if(0 == f_value) {
set_bit_value(dst_code, bit, 0);
} else {
set_bit_value(dst_code, bit, 1);
}
}
}
src_code_last_bit_value = s_value;
}
}
#else
void diff_manchester_encode(unsigned char *src_code, unsigned char *dst_code)
{
unsigned char bit = 0;
unsigned char bit_value;
unsigned char encode_last_bit_value;
for(bit=0; bit<80; bit++) {
bit_value = get_bit_value(src_code, bit);
if(bit == 0) {
if(bit_value == 1) {
set_bit_value(dst_code, bit*2, 1);
set_bit_value(dst_code, bit*2+1, 0);
encode_last_bit_value = 0;
} else {
set_bit_value(dst_code, bit*2, 0);
set_bit_value(dst_code, bit*2+1, 1);
encode_last_bit_value = 1;
}
} else {
if(bit_value == 1) {
if(encode_last_bit_value == 0) {
set_bit_value(dst_code, bit*2, 1);
set_bit_value(dst_code, bit*2+1, 0);
encode_last_bit_value = 0;
} else {
set_bit_value(dst_code, bit*2, 0);
set_bit_value(dst_code, bit*2+1, 1);
encode_last_bit_value = 1;
}
} else {
if(encode_last_bit_value == 0) {
set_bit_value(dst_code, bit*2, 0);
set_bit_value(dst_code, bit*2+1, 1);
encode_last_bit_value = 1;
} else {
set_bit_value(dst_code, bit*2, 1);
set_bit_value(dst_code, bit*2+1, 0);
encode_last_bit_value = 0;
}
}
}
}
}
void diff_manchester_decode(unsigned char *src_code, unsigned char *dst_code)
{
unsigned char bit = 0;
unsigned char f_value, s_value;
unsigned char src_code_last_bit_value;
for(bit=0; bit<80; bit++) {
f_value = get_bit_value(src_code, bit * 2);
s_value = get_bit_value(src_code, bit * 2 + 1);
if(bit == 0) {
if(0 == f_value) {
set_bit_value(dst_code, bit, 0);
} else {
set_bit_value(dst_code, bit, 1);
}
} else {
if(0 == src_code_last_bit_value) {
if(0 != f_value) {
set_bit_value(dst_code, bit, 1);
} else {
set_bit_value(dst_code, bit, 0);
}
} else {
if(0 != f_value) {
set_bit_value(dst_code, bit, 0);
} else {
set_bit_value(dst_code, bit, 1);
}
}
}
src_code_last_bit_value = s_value;
}
}
#endif