Библиотека на С в XOD

found a C library for an industrial encoder on the Internet. is it possible to apply this in XOD? an alternative sketch for working with industrial optical encoders.
This sketch has not only increased accuracy, but is able to count pulses at a fairly decent speed.
It is also necessary to take into account that the CyberLib library is required for this sketch to work. Unfortunately, this library does not work with all Arduinos, at the moment it is Arduino UNO, Mega, Leonardo.
The sketch itself is below.

Sketch

// Connect the CyberLib library. #include “CyberLib.h” // A bunch of auxiliary variables (moved outside the interrupt function to speed up its work). volatile unsigned char In0 = 0; volatile unsigned char In1 = 0; volatile unsigned char PredIn0 = 0; volatile unsigned char PredIn1 = 0; volatile unsigned char DG01_status = 0; volatile long stepFront; volatile bool flgcnt; // Our counter (since we are counting edges, our counter must be divided by 4). volatile long counter = 0; // Encoder interrupt handler. void encoders () {In0 = D10_Read; // In0 - counting direction of the 1st counter (port 10) In1 = D11_Read; // In1 - counting input of the 1st counter (port 11) if (PredIn1! = In1) {DG01_status | = 2; if (DG01_status! = 3) {stepFront = 2; flgcnt = (In0 == 0); } Else {stepFront = 1; if (In1 == 1) flgcnt = (In0 == 1); else flgcnt = (In0 == 0); } If (flgcnt) counter + = stepFront; else counter - = stepFront; PredIn1 = In1; } If (PredIn0! = In0) {DG01_status | = 1; if (DG01_status! = 3) {stepFront = 2; flgcnt = (In1 == 0); } Else {stepFront = 1; if (In0 == 1) flgcnt = (In1 == 0); else flgcnt = (In1 == 1); } If (flgcnt) counter + = stepFront; else counter - = stepFront; PredIn0 = In0; }} void setup () {Serial.begin (9600); D10_In; D11_In; // Remember the states of the ports. In0 = PredIn0 = D10_Read; In1 = PredIn1 = D11_Read; // Start interrupts. StartTimer1 (encoders, 20); } void loop () {// Print the counter to the monitor (since we are counting edges, our counter must be divided by 4). Serial.println (counter / 4); // We use delay_ms (CyberLib libraries), not the standard delay so as not to interfere with interrupts. delay_ms (1000); }

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.