Skip to content

Commit 833210e

Browse files
committed
add new stage course
1 parent 4847e59 commit 833210e

File tree

8 files changed

+291
-1
lines changed

8 files changed

+291
-1
lines changed

CTCStepping/CTCStepping.ino

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
void setup() {
2+
Serial.begin(9600);
3+
pinMode(13, OUTPUT);
4+
TCCR1A = 0;
5+
TCCR1B = _BV(WGM12) | _BV(CS11) | _BV(CS10); // 256kHz - 4Hz
6+
OCR1A = 1024;
7+
TIMSK1 = _BV(OCIE1A);
8+
DDRD |= B11111100;
9+
DDRB |= B00110000;
10+
}
11+
12+
#define SA 0
13+
#define DA 1
14+
#define SX 2
15+
#define SY 3
16+
#define SZ 4
17+
#define DX 5
18+
#define DY 6
19+
#define DZ 7
20+
#define TCK 8
21+
22+
volatile uin16_t step = 1024;
23+
24+
void loop() {
25+
uint16_t v = map(analogRead(A0), 0, 1023, 2, 65535);
26+
t = v;
27+
Serial.println(v);
28+
delay(100);
29+
}
30+
31+
ISR(TIMER1_COMPA_vect) {
32+
OCR1A = step >> TCK;
33+
PORTD = (step & B11111100) | (PORTD & B00000011);
34+
PORTB = ((step & B00000011) << PB4) | (PORTB & B11001111);
35+
}

CTCTickStep/CTCTickStep.ino

+171
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
namespace CNC {
2+
3+
4+
struct alignas(uint16_t) Step {
5+
uint16_t ticks : 13;
6+
uint8_t axis : 2;
7+
uint8_t dir : 1;
8+
};
9+
10+
Step ring[256];
11+
uint8_t wr;
12+
uint8_t rd;
13+
14+
enum ErrorBits {
15+
EUNDERFLOW,
16+
EOVERFLOW,
17+
};
18+
19+
uint8_t error;
20+
uint32_t tickPerSec;
21+
float mmPerRevo = 40;
22+
float stepPerRevo = 800;
23+
24+
enum TickRate : uint32_t {
25+
Tick_2MHz = 2000000,
26+
Tick_250kHz = 250000,
27+
Tick_62500Hz = 62500,
28+
Tick_15625Hz = 15625,
29+
};
30+
31+
void begin(TickRate tickRate = Tick_62500Hz) {
32+
cli();
33+
uint8_t cs = 0;
34+
switch (tickRate) {
35+
case Tick_2MHz: cs = _BV(CS11); break; // 2MHz - 8kHz
36+
case Tick_250kHz: cs = _BV(CS11) | _BV(CS10); break; // 250kHz - 977Hz
37+
case Tick_62500Hz: cs = _BV(CS12); break; // 62.5kHz - 244Hz
38+
case Tick_15625Hz: cs = _BV(CS12) | _BV(CS10); break; // 15625Hz - 61Hz
39+
default: cs = 0; break;
40+
};
41+
tickPerSec = tickRate;
42+
43+
TCCR1A = 0;
44+
TCCR1B = _BV(WGM12) | cs;
45+
OCR1A = 255;
46+
TIMSK1 = _BV(OCIE1A);
47+
PORTD &= B00000011;
48+
PORTB &= B11001111;
49+
PORTB |= B00000001;
50+
DDRD |= B11111100;
51+
DDRB |= B00110001;
52+
wr = 0;
53+
rd = 0;
54+
error = 0;
55+
sei();
56+
}
57+
58+
void enable(bool on) {
59+
if (on) {
60+
PORTB &= ~B00000001;
61+
} else {
62+
PORTB |= B00000001;
63+
}
64+
}
65+
66+
bool ringw(Step const &step) {
67+
cli();
68+
uint8_t p = wr;
69+
if (p + 1 == rd) {
70+
sei();
71+
return false;
72+
}
73+
ring[p] = step;
74+
wr = p + 1;
75+
sei();
76+
return true;
77+
}
78+
79+
bool ringr(Step &step) {
80+
uint8_t p = rd;
81+
if (p == wr) {
82+
error |= _BV(EUNDERFLOW);
83+
return false;
84+
}
85+
step = ring[p];
86+
rd = p + 1;
87+
return true;
88+
}
89+
90+
void ISR_TIMER1_COMPA_vect() {
91+
cli();
92+
Step step;
93+
if (!ringr(step)) {
94+
sei();
95+
return;
96+
}
97+
OCR1A = step.ticks;
98+
switch (step.axis) {
99+
case 0:
100+
bitWrite(PORTD, PD5, step.dir);
101+
bitToggle(PORTD, PD2);
102+
break;
103+
case 1:
104+
bitWrite(PORTD, PD6, step.dir);
105+
bitToggle(PORTD, PD3);
106+
break;
107+
case 2:
108+
bitWrite(PORTD, PD7, step.dir);
109+
bitToggle(PORTD, PD4);
110+
break;
111+
case 3:
112+
bitWrite(PORTB, PB5, step.dir);
113+
bitToggle(PORTB, PB4);
114+
break;
115+
}
116+
sei();
117+
}
118+
119+
void send(uint8_t axis, uint8_t dir, uint16_t ticks) {
120+
Step step;
121+
step.ticks = ticks;
122+
step.axis = axis;
123+
step.dir = dir;
124+
ringw(step);
125+
}
126+
127+
uint8_t lastError() {
128+
cli();
129+
uint8_t e = error;
130+
error = 0;
131+
sei();
132+
return e;
133+
}
134+
135+
uint16_t mmPerSec_to_tickPerStep(float mmPerSec) {
136+
float mmPerStep = mmPerRevo / stepPerRevo;
137+
float stepPerSec = mmPerSec / mmPerStep;
138+
float tickPerStep = tickPerSec / stepPerSec;
139+
return uint16_t(tickPerStep) - 1;
140+
}
141+
142+
}
143+
144+
ISR(TIMER1_COMPA_vect) {
145+
CNC::ISR_TIMER1_COMPA_vect();
146+
}
147+
148+
void setup() {
149+
Serial.begin(9600);
150+
CNC::begin();
151+
CNC::enable(true);
152+
}
153+
154+
void loop() {
155+
long mmps = map(analogRead(SDA), 0, 1023, 10, 200);
156+
uint16_t tick = CNC::mmPerSec_to_tickPerStep(mmps);
157+
CNC::send(0, 1, tick);
158+
uint8_t error = CNC::lastError();
159+
if (error) {
160+
CNC::enable(false);
161+
Serial.print("ERROR:");
162+
Serial.print(error, BIN);
163+
Serial.print(" SPEED:");
164+
Serial.print(mmps);
165+
Serial.print(" TICKS:");
166+
Serial.println(tick);
167+
delay(1000);
168+
CNC::begin();
169+
CNC::enable(true);
170+
}
171+
}

DHTTest/DHTTest.ino

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <DHT11.h>
2+
3+
DHT11 dht(11);
4+
5+
void setup() {
6+
Serial.begin(9600);
7+
}
8+
9+
void loop() {
10+
int h = dht.readHumidity();
11+
if (h == DHT11::ERROR_CHECKSUM || h == DHT11::ERROR_TIMEOUT) {
12+
Serial.println(DHT11::getErrorString(h));
13+
return;
14+
}
15+
int t = dht.readTemperature();
16+
if (t == DHT11::ERROR_CHECKSUM || t == DHT11::ERROR_TIMEOUT) {
17+
Serial.println(DHT11::getErrorString(t));
18+
return;
19+
}
20+
Serial.print("Humidity: ");
21+
Serial.println(h);
22+
Serial.print("Temperature: ");
23+
Serial.println(t);
24+
}

ServoByDelay/ServoByDelay.ino

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
void setup() {
2+
pinMode(12, OUTPUT);
3+
Serial.begin(115200);
4+
}
5+
6+
void setAngle(int angle) {
7+
// angle: 0-180
8+
// us: 500-2500
9+
// int us = 500 + 2000 / 180 * angle;
10+
int us = map(angle, 0, 180, 500, 2500);
11+
digitalWrite(12, 1);
12+
delayMicroseconds(us);
13+
digitalWrite(12, 0);
14+
delayMicroseconds(20000 - us);
15+
}
16+
17+
void loop() {
18+
int value = analogRead(A0);
19+
int angle = map(value, 0, 1023, 0, 180);
20+
setAngle(angle);
21+
}

dontTouchMe/dontTouchMe.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ void setup() {
99
void loop() {
1010
int enable = digitalRead(8);
1111
if (enable == 0) {
12-
int hz = 800 + 500 * sin(millis() * 0.0005);
12+
int hz = 800 + 500 * sin(millis() * 0.0005);
1313
// Serial.println(hz);
1414
int step = 1000000 / hz;
1515
digitalWrite(7, LOW);

humanIRDetect/humanIRDetect.ino

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
void setup() {
2+
// put your setup code here, to run once:
3+
Serial.begin(9600);
4+
pinMode(3, INPUT);
5+
pinMode(13, OUTPUT);
6+
}
7+
8+
void loop() {
9+
// put your main code here, to run repeatedly:
10+
int value = digitalRead(3);
11+
digitalWrite(13, value);
12+
Serial.println(value);
13+
}

servoWithOCR/servoWithOCR.ino

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
void setup() {
2+
Serial.begin(9600);
3+
pinMode(9, OUTPUT);
4+
pinMode(10, OUTPUT);
5+
TCCR1A = _BV(COM1A1) | _BV(COM1B1);
6+
TCCR1B = _BV(WGM13) | _BV(CS11);
7+
ICR1 = 40000;
8+
TIMSK1 = _BV(TOIE1);
9+
sei();
10+
}
11+
12+
volatile bool ov = 0;
13+
14+
void loop() {
15+
if (ov) {
16+
ov = 0;
17+
Serial.println("overflow");
18+
}
19+
OCR1B = map(analogRead(A0), 0, 1023, 500, 2500);
20+
}
21+
22+
ISR(TIMER1_OVF_vect) {
23+
ov = 1;
24+
}

stepNema17/stepNema17.ino

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const int EN = 8;
88
const int XLIM = 9;
99
const int YLIM = 10;
1010
const int ZLIM = 11;
11+
const int ASTP = 12;
12+
const int ADIR = 13;
1113
const int ABORT = A0;
1214
const int HOLD = A1;
1315
const int RESUME = A2;

0 commit comments

Comments
 (0)