/* Carrera DIGITAL 124/132/143 arduino protocol generation: First Step * * Dipl.-Ing. Peter Niehues CC BY-NC-SA 3.0 * * This is an example software to decode carrera data protocol. For further information (german only) * visit http://www.wasserstoffe.de/carrera/protocoll-encode/ * * This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. * To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/. * * This license allows you to remix, tweak, and build upon my work non-commercially, as long as * you credit Peter Niehues and license the new creations under the identical terms. * */ const int dataOutPin = 3; // connection to MOSFET driver circuit const long wordRateMicros = 7500; // send a new data word every 7,5 ms unsigned long intervalWordMicros = 0; // will store the time difference to last word(microseconds) unsigned long previousWordMicros = 0; // will store last time when a word has been send (microseconds) unsigned long currentMicros = 0; // will store the current runtime (microseconds) void setup() { ////// Serial.begin(115200); // initialize serial bus pinMode(dataOutPin, OUTPUT); // initialize the dataPin as an output digitalWrite( dataOutPin, HIGH ); // set output Pin to HIGH, racetrack voltage is default 20 V } ////// void loop() { ////// currentMicros = micros(); // store current runtime intervalWordMicros = currentMicros - previousWordMicros; // calculate interval if (intervalWordMicros > wordRateMicros) { // is time to start a new data word? previousWordMicros = currentMicros; // new data word starts manchesterEncode( 648 );} // binary: 0000001010001000, controller 2, speed 4 } ////// void manchesterEncode(word dataWord){ ////// const int halfClockMicros = 40; // experimental value for half clock rate of 50 microseconds on 16 MHz Arduino Uno int bitPos = 13; // bit counter for position of bit in dataWord int currentBit = 0; // store current bit while ( !currentBit ){ // currentBit = ( dataWord >> bitPos ) & 1; // store bit on position bitPos in currentBit, currentBit becomes 0 or 1 bitPos--;} // switch to next postion bitPos++; // get back to start bit do{ // currentBit = ( dataWord >> bitPos ) & 1; // store bit on position bitPos in currentBit, currentBit becomes 0 or 1 if ( currentBit == 1 ){ // digitalWrite( dataOutPin, HIGH ); // delayMicroseconds ( halfClockMicros ); // digitalWrite( dataOutPin, LOW); // delayMicroseconds ( halfClockMicros );} // else{ // digitalWrite( dataOutPin, LOW ); // delayMicroseconds ( halfClockMicros ); // digitalWrite( dataOutPin, HIGH ); // delayMicroseconds ( halfClockMicros );} // bitPos --; // goto next bit } while ( bitPos >= 0 ); // until all bits are processed digitalWrite(dataOutPin, HIGH ); // make sure to leave subroutine always with digital level HIGH } ////// /* * End of Carrera protocol generation sketch: First Step */