2934 |
jacho |
1 |
#include <SPI.h> |
|
|
2 |
|
|
|
3 |
void command(char* buffer, unsigned int len) |
|
|
4 |
{ |
|
|
5 |
for (int i = 0; i < len; i++) { |
|
|
6 |
delay(10); |
|
|
7 |
digitalWrite(SS, 0); |
|
|
8 |
SPI.transfer(buffer[i]); |
|
|
9 |
digitalWrite(SS, 1); |
|
|
10 |
} |
|
|
11 |
} |
|
|
12 |
|
|
|
13 |
void goTo(unsigned int pos) |
|
|
14 |
{ |
|
|
15 |
char buffer[4]; |
|
|
16 |
buffer[0] = 0x60; |
|
|
17 |
buffer[1] = pos >> 16; |
|
|
18 |
buffer[2] = (pos >> 8) & 0xff; |
|
|
19 |
buffer[3] = pos & 0xff; |
|
|
20 |
command(buffer, 4); |
|
|
21 |
} |
|
|
22 |
|
|
|
23 |
void setParam(unsigned int reg, unsigned int val, unsigned int len) |
|
|
24 |
{ |
|
|
25 |
char buffer[4]; |
|
|
26 |
buffer[0] = reg; |
|
|
27 |
|
|
|
28 |
/* BUG: reg? */ |
|
|
29 |
for (int i = 0; i < len; i++) |
|
|
30 |
buffer[i + 1] = (reg >> (8 * ((len - 1) - i))) & 0xff; |
|
|
31 |
|
|
|
32 |
command(buffer, 1 + len); |
|
|
33 |
} |
|
|
34 |
|
|
|
35 |
void setup() |
|
|
36 |
{ |
|
|
37 |
Serial.begin(9600); |
|
|
38 |
SPI.begin(SPI_MASTER, MSBFIRST, SPI_MODE3, SPI_CLOCK_DIV32); |
|
|
39 |
|
|
|
40 |
setParam(5, 0xd0, 2); // ACC |
|
|
41 |
setParam(6, 0xd0, 2); // DEC |
|
|
42 |
setParam(7, 0, 2); // MAX_SPEED |
|
|
43 |
Serial.println("Testovaci program pro HBSTEP01A"); |
|
|
44 |
} |
|
|
45 |
|
|
|
46 |
int readInt() |
|
|
47 |
{ |
|
|
48 |
char line[100]; |
|
|
49 |
int i = 0; |
|
|
50 |
|
|
|
51 |
while (true) { |
|
|
52 |
if (Serial.available()) { |
|
|
53 |
byte c = Serial.read(); |
|
|
54 |
|
|
|
55 |
if (c == '\n' || c == '\r') { |
|
|
56 |
Serial.println(); |
|
|
57 |
line[i] = '\0'; |
|
|
58 |
return atoi(line); |
|
|
59 |
} |
|
|
60 |
|
|
|
61 |
Serial.write(c); |
|
|
62 |
line[i++] = c; |
|
|
63 |
} |
|
|
64 |
} |
|
|
65 |
} |
|
|
66 |
|
|
|
67 |
void loop() |
|
|
68 |
{ |
|
|
69 |
int speed = readInt(); |
|
|
70 |
|
|
|
71 |
int dir = speed > 0; |
|
|
72 |
if (!dir) |
|
|
73 |
speed = -speed; |
|
|
74 |
|
|
|
75 |
char buffer[4]; |
|
|
76 |
buffer[0] = 0x50 | dir; |
|
|
77 |
buffer[1] = (speed >> 16) & 0xff; |
|
|
78 |
buffer[2] = (speed >> 8) & 0xff; |
|
|
79 |
buffer[3] = speed & 0xff; |
|
|
80 |
|
|
|
81 |
command(buffer, 4); |
|
|
82 |
} |