Официальный сайт компании Arduino по адресу arduino.cc
01
#include <avr/interrupt.h>
02
03
const
byte
Pins[8] = {5, 6, 7, 8, 9, 10, 11, 12};
04
05
struct
listPWM {
06
int
period;
07
phase;
08
09
* port;
10
pin;
11
OCR;
12
} pinList[8];
13
14
ISR(TIMER2_COMPA_vect) {
15
t = OCR2A;
16
n = OCR2A - 2;
17
18
for
(
i = 0; i < 8; i++) {
19
if
( ((pinList[i].OCR - t) & 0xff) < 2) {
20
*(pinList[i].port) |= pinList[i].pin;
21
}
22
else
(((pinList[i].OCR - t) & 0xff) < ((n - t) & 0xff) )
23
n = pinList[i].OCR;
24
25
OCR2A = n;
26
27
28
ISR(TIMER2_OVF_vect) {
29
i = 0; i < 8; i++) *(pinList[i].port) &= ~pinList[i].pin;
30
31
32
void
setup
() {
33
Serial
.begin(115200);
34
35
36
p = digitalPinToPort(Pins[i]);
37
pinMode (Pins[i], OUTPUT);
38
pinList[i].port = portOutputRegister(p);
39
pinList[i].pin = digitalPinToBitMask(Pins[i]);
40
pinList[i].OCR = 200;
41
pinList[i].period = 450 * random(2, 16);
42
pinList[i].phase = 0;
43
44
45
TCCR2A = 0b00000000;
//COM2A1:COM2A0:COM2B1:COM2B0:x:x:WGM21:WGM20
46
TCCR2B = 0b00000110;
//ICNC1:ICES1:X:WGM13:WGM12:CS12:CS11:CS10
47
//режим 0 прескаллер 256 - ШИМ 244Гц.
48
OCR2A = 0;
49
TIMSK2 = 0b00000011;
//xxxxx:OCIEB:OCIEA:TOIE
50
51
52
loop
53
static
uint32_t om = millis();
54
uint32_t nm = millis();
55
56
(nm - om > 10) {
57
om = nm;
58
59
pinList[i].phase+=10;
60
(pinList[i].phase > pinList[i].period) pinList[i].phase = 0;
61
pinList[i].OCR = map(pinList[i].phase,0,pinList[i].period,177,254);
62
63
64
https://youtu.be/TrYYu718xF8
Базаришь. На десятичном счётчике влёхкую замучу, но без рандома))) (но не хочу)
Как много "но", однако...
"Не могу и не хочу...")
01
#include <avr/interrupt.h>
02
03
const
byte
Pins[8] = {5, 6, 7, 8, 9, 10, 11, 12};
04
05
struct
listPWM {
06
int
period;
07
int
phase;
08
09
byte
* port;
10
byte
pin;
11
byte
OCR;
12
} pinList[8];
13
14
ISR(TIMER2_COMPA_vect) {
15
byte
t = OCR2A;
16
byte
n = OCR2A - 2;
17
18
for
(
byte
i = 0; i < 8; i++) {
19
if
( ((pinList[i].OCR - t) & 0xff) < 2) {
20
*(pinList[i].port) |= pinList[i].pin;
21
}
22
else
if
(((pinList[i].OCR - t) & 0xff) < ((n - t) & 0xff) )
23
n = pinList[i].OCR;
24
}
25
OCR2A = n;
26
}
27
28
ISR(TIMER2_OVF_vect) {
29
for
(
byte
i = 0; i < 8; i++) *(pinList[i].port) &= ~pinList[i].pin;
30
}
31
32
void
setup
() {
33
Serial
.begin(115200);
34
35
for
(
byte
i = 0; i < 8; i++) {
36
byte
p = digitalPinToPort(Pins[i]);
37
pinMode (Pins[i], OUTPUT);
38
pinList[i].port = portOutputRegister(p);
39
pinList[i].pin = digitalPinToBitMask(Pins[i]);
40
pinList[i].OCR = 200;
41
pinList[i].period = 450 * random(2, 16);
42
pinList[i].phase = 0;
43
}
44
45
TCCR2A = 0b00000000;
//COM2A1:COM2A0:COM2B1:COM2B0:x:x:WGM21:WGM20
46
TCCR2B = 0b00000110;
//ICNC1:ICES1:X:WGM13:WGM12:CS12:CS11:CS10
47
//режим 0 прескаллер 256 - ШИМ 244Гц.
48
OCR2A = 0;
49
TIMSK2 = 0b00000011;
//xxxxx:OCIEB:OCIEA:TOIE
50
}
51
52
void
loop
() {
53
static
uint32_t om = millis();
54
uint32_t nm = millis();
55
56
if
(nm - om > 10) {
57
om = nm;
58
for
(
byte
i = 0; i < 8; i++) {
59
pinList[i].phase+=10;
60
if
(pinList[i].phase > pinList[i].period) pinList[i].phase = 0;
61
pinList[i].OCR = map(pinList[i].phase,0,pinList[i].period,177,254);
62
}
63
}
64
}
https://youtu.be/TrYYu718xF8
Базаришь. На десятичном счётчике влёхкую замучу, но без рандома))) (но не хочу)
Как много "но", однако...
"Не могу и не хочу...")