-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelayLine.c
141 lines (121 loc) · 3.54 KB
/
delayLine.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <[email protected]> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. Jonas Bo Jalling
* ----------------------------------------------------------------------------
*
* File: delayLine.c
* Author: Jonas Bo Jalling
*
*/
// CONFIG
#pragma config WDTE = OFF // Watchdog Timer (WDT disabled)
#pragma config CP = OFF // Code Protect (Code protection off)
#pragma config MCLRE = OFF // Master Clear Enable (GP3/MCLR pin fuction is digital I/O, MCLR internally tied to VDD)
#define _XTAL_FREQ 4000000UL
#define RxD GP3
#define DS_CLK GP1
#define DS_D GP0
#define DS_LE GP2
#define WAIT_FOR_COMMAND0 0
#define WAIT_FOR_COMMAND1 1
#define WAIT_FOR_COMMAND2 2
#define WAIT_FOR_COMMAND3 3
#define WAIT_FOR_DATA 4
#define READ_SAWTOOTH 5
#include <xc.h>
void main(void) {
OPTION = 192; // Disable GPWU, GPPU
OSCCAL = 0;
TRISGPIO = 8; // RxD input, others output
DS_CLK = 0;
DS_LE = 0;
DS_D = 0;
unsigned char carryFlag;
unsigned char bitCounter;
unsigned char rxByte;
unsigned char state = WAIT_FOR_COMMAND0;
unsigned char sawToothByte = 0;
unsigned char dummyByteCount;
while (1)
{
// Reset
bitCounter = 9; // 8 bytes + stop-bit
carryFlag = 1;
rxByte = 0;
while (RxD)
{
// Wait for UART start-bit
}
__delay_us(52);
while (bitCounter != 0)
{
__delay_us(87);
if (RxD)
{
rxByte |= carryFlag;
}
carryFlag <<=1;
bitCounter--;
}
// Byte received - parse it
switch (state)
{
case WAIT_FOR_COMMAND0:
dummyByteCount = 0;
if (rxByte == '@')
state = WAIT_FOR_COMMAND1;
break;
case WAIT_FOR_COMMAND1:
if (rxByte == '@')
state = WAIT_FOR_COMMAND2;
break;
case WAIT_FOR_COMMAND2:
if (rxByte == 'H')
state = WAIT_FOR_COMMAND3;
break;
case WAIT_FOR_COMMAND3:
if (rxByte == 'n')
state = WAIT_FOR_DATA;
break;
case WAIT_FOR_DATA:
dummyByteCount++;
if (dummyByteCount == 10)
state = READ_SAWTOOTH;
break;
case READ_SAWTOOTH:
sawToothByte = rxByte;
state = WAIT_FOR_COMMAND0;
break;
default:
state = WAIT_FOR_COMMAND0;
}
// We have received the correct byte, send it to the delayline
if (sawToothByte)
{
DS_LE = 1;
bitCounter = 8;
char mask;
while (bitCounter != 0)
{
mask = (sawToothByte & 0x01);
sawToothByte >>= 1;
if (mask)
{
DS_D = 1;
}
else
{
DS_D = 0;
}
DS_CLK = 1;
DS_CLK = 0;
bitCounter--;
}
DS_LE = 0;
sawToothByte = 0;
}
}
}