-
Notifications
You must be signed in to change notification settings - Fork 0
/
dccduino.pde
260 lines (217 loc) · 7.22 KB
/
dccduino.pde
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*Enhanced and edited by Dimitri Henning, based on code of 2009 Michael Blank
This program is free software; you can redistribute it and/or modify it under the terms of the
GNU General Public License as published by the Free Software Foundation;
either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
*/
#include <LiquidCrystal.h>
#define RW_PIN 12 //yellow lcd cable
#define ENABLE_PIN 13 //white lcd cable
#define DCC_PIN 4 // Arduino pin for DCC out
// this pin is connected to "DIRECTION" of LMD18200
#define DCC_PWM 5 // must be HIGH for signal out
// connected to "PWM in" of LMD18200
#define joystick_1_x_PIN 3 // analog reading for joystick Poti
#define joystick_1_y_PIN 2 // analog reading for joystick Poti
#define clickPin 2 //input for switch
//Timer frequency is 2MHz for ( /8 prescale from 16MHz )
//Timmer clock = 16MHz/8 = 2MHz oder 0,5usec
#define TIMER_SHORT 0x8D // 58usec pulse length
#define TIMER_LONG 0x1B // 116usec pulse length
// definitions for state machine
#define PREAMBLE 0
#define SEPERATOR 1
#define SENDBYTE 2
unsigned char last_timer=TIMER_SHORT; // store last timer value
unsigned char flag=0; // used for short or long pulse
unsigned char every_second_isr = 0; // pulse up or down
volatile int timer = 0; //helping construct for stuff
boolean changed = false;
unsigned char state= PREAMBLE;
unsigned char preamble_count = 16;
unsigned char outbyte = 0;
unsigned char cbit = 0x80;
// variables for throttle
int locoSpeed=0;
int locoSpeed_temp=0;
volatile int direction = 0;
int last_locoSpeed=0;
int last_direction;
int locoAdr=7; // this is the (fixed) address of the loco
int joystick_1_x;
int joystick_1_y;
LiquidCrystal lcd(RW_PIN, ENABLE_PIN, 8, 9, 10, 11); //initialize display
// buffer for command
struct Message {
unsigned char data[7];
unsigned char len;
} ;
#define MAXMSG 2
// for the time being, use only two messages - the idle msg and the loco Speed msg
struct Message msg[MAXMSG] = {
{ { 0xFF, 0, 0xFF, 0, 0, 0, 0}, 3}, // idle msg
{ { locoAdr, 0x3F, 0, 0, 0, 0, 0}, 4} // locoMsg with 128 speed steps
}; // loco msg must be filled later with speed and XOR data byte
int msgIndex=0;
int byteIndex=0;
//Setup Timer2.
//Configures the 8-Bit Timer2 to generate an interrupt at the specified frequency.
//Returns the time load value which must be loaded into TCNT2 inside your ISR routine.
void SetupTimer2(){
//Timer2 Settings: Timer Prescaler /8, mode 0
//Timmer clock = 16MHz/8 = 2MHz oder 0,5usec
TCCR2A = 0;
TCCR2B = 0<<CS22 | 1<<CS21 | 0<<CS20;
//Timer2 Overflow Interrupt Enable
TIMSK2 = 1<<TOIE2;
//load the timer for its first cycle
TCNT2=TIMER_SHORT;
}
//Timer2 overflow interrupt vector handler
ISR(TIMER2_OVF_vect) {
//Capture the current timer value TCTN2. This is how much error we have
//due to interrupt latency and the work in this function
//Reload the timer and correct for latency.
// for more info, see http://www.uchobby.com/index.php/2007/11/24/arduino-interrupts/
unsigned char latency;
// for every second interupt just toggle signal
if (every_second_isr) {
digitalWrite(DCC_PIN,1);
every_second_isr = 0;
// set timer to last value
latency=TCNT2;
TCNT2=latency+last_timer;
} else { // != every second interrupt, advance bit or state
digitalWrite(DCC_PIN,0);
every_second_isr = 1;
switch(state) {
case PREAMBLE:
flag=1; // short pulse
preamble_count--;
if (preamble_count == 0) { // advance to next state
state = SEPERATOR;
// get next message
msgIndex++;
if (msgIndex >= MAXMSG) { msgIndex = 0; }
byteIndex = 0; //start msg with byte 0
}
break;
case SEPERATOR:
flag=0; // long pulse
// then advance to next state
state = SENDBYTE;
// goto next byte ...
cbit = 0x80; // send this bit next time first
outbyte = msg[msgIndex].data[byteIndex];
break;
case SENDBYTE:
if (outbyte & cbit) {
flag = 1; // send short pulse
} else {
flag = 0; // send long pulse
}
cbit = cbit >> 1;
if (cbit == 0) { // last bit sent, is there a next byte?
byteIndex++;
if (byteIndex >= msg[msgIndex].len) {
// this was already the XOR byte then advance to preamble
state = PREAMBLE;
preamble_count = 16;
} else {
// send separtor and advance to next byte
state = SEPERATOR ;
}
}
break;
}
if (flag) { // if data==1 then short pulse
latency=TCNT2;
TCNT2=latency+TIMER_SHORT;
last_timer=TIMER_SHORT;
} else { // long pulse
latency=TCNT2;
TCNT2=latency+TIMER_LONG;
last_timer=TIMER_LONG;
}
}
}
void setup(void) {
//Set the pins for DCC to "output".
pinMode(DCC_PIN,OUTPUT); // this is for the DCC Signal
pinMode(clickPin, INPUT);
analogWrite(5, 255);
assemble_dcc_msg();
//Start the timer
SetupTimer2();
lcd.begin(20, 4);
lcd.home();
}
void loop(void) {
if (timer > 0){
timer=timer-1;
}
joystick_1_x = (127L * analogRead(joystick_1_x_PIN))/1023;
joystick_1_y = (127L * analogRead(joystick_1_y_PIN))/1023;
lcd.print(" ");
if (joystick_1_x > 100) {
locoSpeed = locoSpeed + 3;
}
if (joystick_1_x < 30) {
locoSpeed = locoSpeed - 3;
}
if (locoSpeed >127) {
locoSpeed = 127;
}
if (locoSpeed <-127) {
locoSpeed = -127;
}
if (digitalRead(clickPin) == LOW) {
locoSpeed=0;
}
delay(10);
lcd.home();
lcd.print("Geschwindigkeit:");
lcd.print(locoSpeed);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("Richtung:");
if (locoSpeed == 0) {
lcd.print("halt ");
}
if (locoSpeed > 0) {
lcd.print("vorwaerts ");
}
if (locoSpeed < 0) {
lcd.print("rueckwaerts ");
}
lcd.setCursor(0,2);
lcd.print("joystick:");
lcd.print(joystick_1_x);
assemble_dcc_msg();
}
void assemble_dcc_msg() {
int i;
unsigned char data, xdata;
/*if (locoSpeed == 1) { // this would result in emergency stop
locoSpeed = 0;
}*/
// direction info first
if (locoSpeed < 0) {
locoSpeed_temp = -locoSpeed;
data = 0;
direction = 0;
} else {
locoSpeed_temp = locoSpeed;
data = 0x80; //forward
direction = 1;
}
data |= locoSpeed_temp;
// add XOR byte
xdata = (msg[1].data[0] ^ msg[1].data[1]) ^ data;
noInterrupts(); // make sure that only "matching" parts of the message are used in ISR
msg[1].data[2] = data;
msg[1].data[3] = xdata;
interrupts();
}