Dudas del código para leer bit a bit

Buenas,
Estoy intentando entender como codificar y descodificar mediante codificación manchester:

// **********************************************************************
// Routine to recover clock and timings
// ****************************************************************/
unsigned char Coding_ClkSync(unsigned char numSamples)
{
unsigned int clkT=0;
unsigned int tmp=0, average=0, sample=0;
directionFlag = READ; // Set direction for timer interrupt
Coding_TimerInit(0x00, TIMING); // Initiate timer w/ edge2edge measurement
Coding_Timer_Poll(); // Wait for edge
clkT = Coding_Timer_Poll(); // Set initial measurment as T time
do
{
tmp = Coding_Timer_Poll(); // Catch next edge time
if(tmp < UPPERTIMINGLMT) // Check if edge time is useable
{
if(tmp < (clkT
0.5)){clkT = tmp;} // Time below limit
else if((tmp >= (clkT
0.5)) && (tmp <= (clkT
1.5)))
{
average += tmp; // Accumulate
sample++; // Inc sample count
clkT = (average/sample); // Average
}
else if((tmp >= (clkT
1.5)) && (tmp <= (clkT
2.5)))
{
average += (tmp/2); // Accumulate but sample/2
sample++; // Inc sample count
clkT = (average/sample); // Average
}
else
{
clk2T = 128; // Force default to 2T = 256us
break;
}
}
else
{
clkT = 128; // Force default to
2T = 256us
break;
}
}while(sample < numSamples); // Repeat while < sample size

Coding_Timer_Stop(); // Stop timer
clk2T = (clkT2); // Compute 2T time
0.25); // Compute low T limit
DecodeReadTime.ShortH = (int)(clk2T0.75); // Compute high T limit
0.75); // Compute low 2T limit
DecodeReadTime.LongH = (int)(clk2T*1.25); // Compute high 2T limit
if(sample == numSamples){return SUCCESS0;}
else{return TagErr;}
}

// **********************************************************************
// Routine to set clock and timings
// *********************************************************************/
void Coding_SetClk(unsigned int clk, unsigned int shortL,unsigned int shortH, unsigned int longL, unsigned int longH)
{
clk2T = clk; // Force 2T time
DecodeReadTime.ShortL = shortL; // Force low T limit
DecodeReadTime.ShortH = shortH; // Force high T limit
DecodeReadTime.LongL = longL; // Force low 2T limit
DecodeReadTime.LongH = longH; // Force high 2T limit
}

MI DUDA ES, QUE ES LO QUE HACEN :

unsigned int ShortL;
unsigned int ShortH;
unsigned int LongL;
unsigned int LongH;

GRACIAS.

SALUDOS.

http://www.arduino.cc/en/Reference/UnsignedInt

Te pondría el español, pero desde que leí "número sin firma"...

Gracias,

no me refiero a un entero sin signo (unsigned), si no a las variables:

shortL shortH longL longH

A que se refiere??

No se si viendo el código podras descifrarlo.

Gracias.

Saludos.

Ya me extrañaba que preguntaras eso, jajaja

A falta de código, ya que por lo menos faltan funciones, tiene toda la pinta de que establece el valor mínimo y máximo de duración para el periodo corto y el periodo mínimo y máximo para el periodo largo de la codificación y así no hay fallos con pequeñas variaciones del periodo. En resumen, establece los valores de la tolerancia en la duración del periodo donde se toma cómo válido.

Un saludo

Gracias,

Soy nuevo en la programación de microcontroladores, se nota!! jejeje.

Lo que quiero hacer es lo siguiente, te explico:

Necesito comunicarme con un balasto electronico, el protocolo de comunicaciones es DALI:

Transmisión: 19 bits: 1 bit inicio, 1 byte de direccion, 1 byte de datos, 2 bits de parada.

Recepción: 11 bits: 1 bit inicio, 1 byte de datos, 2 bits de parada.

Por lo que necesito leer bit a bit, porque el protocolo es diferente al serie.

Gracias.

Un Saludo.

Este sería el codigo completo que te viene en el pdf de codificación manchester de ATMEL atmega :

#include “Coding.h”
volatile unsigned char cDataBuff[BUFFSIZE] = {0}; // Read Data buffer
volatile unsigned char *cDataBuffPtr;
// Runtime values for Reader Timings //
unsigned int clk2T=0;
volatile unsigned char numSampleBits = 0;
volatile unsigned int RdTime = 0; //Global var used for edgetiming
// **********************************************************************
// Routine to recover clock and timings
// ****************************************************************/
unsigned char Coding_ClkSync(unsigned char numSamples)
{
unsigned int clkT=0;
unsigned int tmp=0, average=0, sample=0;
directionFlag = READ; // Set direction for timer interrupt
Coding_TimerInit(0x00, TIMING); // Initiate timer w/ edge2edge measurement
Coding_Timer_Poll(); // Wait for edge
clkT = Coding_Timer_Poll(); // Set initial measurment as T time
do
{
tmp = Coding_Timer_Poll(); // Catch next edge time
if(tmp < UPPERTIMINGLMT) // Check if edge time is useable
{
if(tmp < (clkT
0.5)){clkT = tmp;} // Time below limit
else if((tmp >= (clkT
0.5)) && (tmp <= (clkT
1.5)))
{
average += tmp; // Accumulate
sample++; // Inc sample count
clkT = (average/sample); // Average
}
else if((tmp >= (clkT
1.5)) && (tmp <= (clkT
2.5)))
{
average += (tmp/2); // Accumulate but sample/2
sample++; // Inc sample count
clkT = (average/sample); // Average
}
else
{
clk2T = 416.67; // Force default to 2T = 833.3us
break;
}
}
else
{
clkT = 416.67; // Force default to 2T = 833.3us
break;
}
}while(sample < numSamples); // Repeat while < sample size

Coding_Timer_Stop(); // Stop timer
clk2T = (clkT2); // Compute 2T time
0.25); // Compute low T limit
DecodeReadTime.ShortH = (int)(clk2T0.75); // Compute high T limit
0.75); // Compute low 2T limit
DecodeReadTime.LongH = (int)(clk2T*1.25); // Compute high 2T limit
if(sample == numSamples){return SUCCESS0;}
else{return TagErr;}
}

// **********************************************************************
// Routine to set clock and timings
// *********************************************************************/
void Coding_SetClk(unsigned int clk, unsigned int shortL,unsigned int shortH, unsigned int longL, unsigned int longH)
{
clk2T = clk; // Force 2T time
DecodeReadTime.ShortL = shortL; // Force low T limit
DecodeReadTime.ShortH = shortH; // Force high T limit
DecodeReadTime.LongL = longL; // Force low 2T limit
DecodeReadTime.LongH = longH; // Force high 2T limit
}
// **********************************************************************
// Routine to encode a Manchester data stream
// Pass in the number of bits to send
// Pulls from cDataBuff
// *********************************************************************/
unsigned char Coding_ManchesterEncode(unsigned char numBits)
{
volatile unsigned int cNumBits = 0,i;
cDataBuffPtr = &cDataBuff[0]; // Place pointer at beginning of buffer
directionFlag = WRITE; // Set direction for timer interrupt
Coding_TimerInit((clk2T/2), SAMPLING); // Init timer w/ periodic interrupt
for(i=0; i<numBits; i++) // Repeat until all bits sent
{
if(cNumBits == 8) // When full byte is read
{
cDataBuffPtr++; // Increment pointer to next byte in buffer
if(cDataBuffPtr == &cDataBuff[0]){i=numBits+1;}
cNumBits = 0; // Clear bit counter
}
if((*cDataBuffPtr & 0x80) == 0x80) // Check bit value, process logic one
{
cbi(IOPORT,DATAOUT); // Set I/O low
Coding_Timer_Poll(); // Catch next interrupt
sbi(IOPORT,DATAOUT); // Set I/O high
Coding_Timer_Poll(); // Catch next interrupt
}
else
{
sbi(IOPORT,DATAOUT); // Set I/O high
Coding_Timer_Poll(); // Catch next interrupt
cbi(IOPORT,DATAOUT); // Set I/O low
Coding_Timer_Poll(); // Catch next interrupt
}
*cDataBuffPtr = *cDataBuffPtr<<1; // Shift buffer to get next bit
cNumBits++; // Increment number of bits sent
}
return 0;
}
// **********************************************************************
// Routine to synchronize to manchester edge
// *********************************************************************/
unsigned char Coding_ManchesterSync(unsigned char maxSample)
{
unsigned char i=0;
unsigned int tmp;
unsigned char cOutput = SyncErr;
directionFlag = READ; // Set direction for timer interrupt measurement
tmp = Coding_Timer_Poll(); // Wait for edge
while(i++ < maxSample) // Repeat until sample size is meet
{
tmp = Coding_Timer_Poll(); // Catch next edge time
if(tmp > UPPERTIMINGLMT){break;} // Check if edge time is useable
{
//2T time found, check starting logic value
if(tst(IOPIN,DATAIN) == 0){cOutput = SUCCESS0;}
else{cOutput = SUCCESS1;}
break;
}
}
return cOutput;
}
// **********************************************************************
// Routine to decode a Manchester bit
// Pass in the previous bit logic value
// *********************************************************************/
unsigned char Coding_ManchesterDecode(unsigned char cBit)
{
unsigned char cOutput = BitErr;
unsigned int tmp;
tmp = Coding_Timer_Poll(); // Catch next edge time
if(tmp < UPPERTIMINGLMT) // Check if edge time is useable
{
// Check edge time and determine next Logic value //
{cOutput = cBit ^ 0x01;} // invert cBit for logical change
// Next edge time is short
{
tmp = Coding_Timer_Poll();
{cOutput = cBit;} // bit stays the same
else{cOutput = BitErr;} // Un-paired short time
}
else {cOutput = BitErr;} // Edge time outside limits
}
return cOutput;
}

Necesito implementar esto:

Transmisión:
19 bits:
1 bit inicio, 1 byte de direccion, 1 byte de datos, 2 bits de parada.

Recepción:
11 bits:
1 bit inicio, 1 byte de datos, 2 bits de parada.

Pues eso ya tendrás que currártelo tu, jejeje

Estoy muy ocupado ahora, ya que tengo que entregar bastantes trabajos para después de navidad, más luego estudiar.

Un saludo