-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupport.c
82 lines (77 loc) · 2.09 KB
/
support.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "support.h"
void ConvertCharactertoNibbles(uint8_t input_character, uint8_t* high_nibble, uint8_t* low_nibble){
*high_nibble = (input_character & 0xF0)>>1;//Get High Nibble
*low_nibble = (input_character & 0xF)<<3;//Get Low Nibble
}
int CheckEven(uint8_t nibble){
nibble ^= nibble >> 4;
nibble ^= nibble >> 2;
nibble ^= nibble >> 1;
return nibble & 1; // return 0 if it is even and 1 if it is odd
}
uint8_t AddParity(uint8_t nibble){
uint8_t p0 = 0;
uint8_t p1 = 0;
uint8_t p2 = 0;
if(CheckEven(nibble & 0x38) == 1){
p0 = 1;
}
if(CheckEven(nibble & 0x58) == 1){
p1 = 1;
}
if(CheckEven(nibble & 0x70) == 1){
p2 = 1;
}
nibble |= p0;
nibble |= (p1<<1);
nibble |= (p2<<2);
return nibble;
}
uint8_t CorrectInputCharacter(uint8_t character){
if(CheckEven(character & 0x39) == 1
&& CheckEven(character & 0x5A) == 1
&& CheckEven(character & 0x74) == 1) //incorrect at d1
{
character ^= 0x10;
}
else if (CheckEven(character & 0x39) == 1
&& CheckEven(character & 0x5A) == 1) //incorrect at d0
{
character ^= 0x8;
}
else if (CheckEven(character & 0x39) == 1
&& CheckEven(character & 0x74) == 1) // incorrect at d2
{
character ^= 0x20;
}
else if (CheckEven(character & 0x5A) == 1
&& CheckEven(character & 0x74) == 1) // incorrect at d3
{
character ^= 0x40;
}
else if (CheckEven(character & 0x39) == 1) // incorrect at p0
{
character ^= 0x1;
}
else if (CheckEven(character & 0x5A) == 1) // incorrect at p1
{
character ^= 0x2;
}
else if (CheckEven(character & 0x74) == 1) // incorrect at p2
{
character ^= 0x4;
}
else if (CheckEven(character & 0x80)== 1)
{
character ^= 0x80; //incorrect at first bit
}
return character;
}
uint8_t GetData(uint8_t high_nibble,uint8_t low_nibble){
high_nibble = (high_nibble & 0x78) << 1;
low_nibble = low_nibble >> 3;
return high_nibble | low_nibble;
}