96 righe
1.9 KiB
C++
96 righe
1.9 KiB
C++
#include <TimerOne.h>
|
|
|
|
const int pinData = 6;
|
|
const int pinStatus = 7;
|
|
|
|
const int timerDelay = 500;
|
|
|
|
void setup(void)
|
|
{
|
|
pinMode(pinData, OUTPUT);
|
|
//Timer1.initialize(60);
|
|
//Timer1.attachInterrupt(clockCycle);
|
|
|
|
Timer1.initialize(timerDelay);
|
|
Timer1.attachInterrupt(clockCycle);
|
|
Timer1.stop();
|
|
|
|
pinMode(pinStatus, INPUT_PULLUP);
|
|
attachInterrupt(digitalPinToInterrupt(pinStatus), statusCycle, CHANGE);
|
|
|
|
Serial.begin(9600);
|
|
}
|
|
|
|
volatile long lastChange = 0;
|
|
volatile long lastStart = 0;
|
|
volatile int x = 0;
|
|
volatile int dataWord = 0;
|
|
volatile int dataState = 0;
|
|
volatile int dataDelay = 0;
|
|
volatile int packetDelay = 0;
|
|
volatile int packetTail = 0;
|
|
volatile int clkState = LOW;
|
|
|
|
void statusCycle() {
|
|
long timeNow = millis();
|
|
long changeDiff = timeNow - lastChange;
|
|
lastChange = timeNow;
|
|
if (changeDiff >= 10) {
|
|
lastStart = timeNow;
|
|
|
|
dataWord = (0 + x);
|
|
dataState = 8;
|
|
dataDelay = 0;
|
|
packetDelay = 0;
|
|
packetTail = 100;
|
|
x = (x + 1) % 8;
|
|
clkState = HIGH;
|
|
Timer1.initialize(timerDelay);
|
|
Timer1.start();
|
|
}
|
|
if (timeNow - lastStart >= 7 && changeDiff >= 2) {
|
|
//dataWord = 0b1000000011 | (('A' + x) << 2);
|
|
|
|
}
|
|
}
|
|
|
|
void clockCycle(void)
|
|
{
|
|
int dataBit = HIGH;
|
|
if (clkState == LOW) {
|
|
clkState = HIGH;
|
|
} else {
|
|
clkState = LOW;
|
|
}
|
|
if (packetDelay > 0) {
|
|
packetDelay--;
|
|
} else if (dataDelay > 0) {
|
|
dataDelay--;
|
|
dataBit = LOW;
|
|
} else if (dataState > 0) {
|
|
int bitToSend = (dataWord >> (dataState - 1)) & 1;
|
|
//int bitToSend = dataWord & 1;
|
|
//dataWord = dataWord >> 1;
|
|
dataBit = !bitToSend ? LOW : HIGH;
|
|
dataState--;
|
|
} else if (packetTail > 0) {
|
|
packetTail--;
|
|
dataBit = LOW;
|
|
} else {
|
|
//digitalWrite(pinData, HIGH);
|
|
Timer1.stop();
|
|
}
|
|
digitalWrite(pinData, dataBit);
|
|
}
|
|
|
|
void loop(void)
|
|
{
|
|
delay(500);
|
|
|
|
/*noInterrupts();
|
|
dataWord = 0b1000000011 | (('A' + x) << 2);
|
|
dataState = 16;
|
|
x = (x + 1) % 30;
|
|
interrupts();*/
|
|
}
|