Decode C sequence

Go To Last Post
27 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello people,

 

Can somebody please explain me the logic/algorithm behind this code. I could figure out a couple things but wasn't able to connect them.

 

This section is for BiPhase encoded data capture:

void biphase_capture(void) 
{ 
 
#ifdef DEBUG_EMDB409
  sbi(PORTA,DEC5);
#endif
  uint8_t y,delta; 
  uint16_t capt; 
  uint16_t icr = ICR1;    
 
  TCCR1B ^= (1<<ICES1);             // Switchover of sensitive ICP edge 
  capt = icr - last_capture; 
  last_capture = icr;               // counter1 is now free-running 
 
  if(capt>255) delta = 255; else delta = (uint8_t)capt; 
  if (debug_mode > 2) {  
     if(debug_mode==4) store_pulse_lenght(delta);
     else {                  
       store_pulse(delta);  
       return; 
      }
   } 
  y = 0; 
  delta += halfDataRate >> (TOLERANCE_SHIFT);   // dynamic resolution (dependent on datarate)   !!!!! >> 2 
  delta -= halfDataRate;        // always subtract datarate/2 
  if (delta > halfDataRate) {   // when longer than that, try to subtract once more 
    y++; 
    delta -= halfDataRate; 
  } 
 
  if (delta > halfDataRate) {        // check overflow of counter and range 
                                    // this check is also dynamic, depends on current datarate 
                                    // the faster datarate the more precise must the signal be 
    if (last_valid == 0) { 
      store_bit(bit_pos^1, 0);      // rtf feature 
      store_bit ( 0, 1);            // store bad bit 
      last_valid = 1;  
    } 
    bit_pos = 0; 
 
  } else { 
 
    if (y) { 
      store_bit(1, 0); 
      bit_pos = 0; 
      last_valid = 0; 
    } else { 
      if (bit_pos++) { 
        store_bit(0, 0); 
        bit_pos = 0; 
        last_valid = 0; 
      } 
    } 
  } 
 
#ifdef DEBUG_EMDB409
  cbi(PORTA,DEC5);
#endif

 

 

This section is for Manchester encoded data capture:

void manchester_capture(void) { 

#ifdef DEBUG_EMDB409
  sbi(PORTA,DEC5); //debug port set
#endif 
  uint8_t y, delta; 
  uint16_t capt; 
  uint16_t icr = ICR1; // T/C 1 input capture register
 
  edge = TCCR1B; //Timer/Counter 1 Control and Status Register
  TCCR1B = edge ^ (1<<ICES1);   //T/C 1 Control and Status Register Bit    // Switchover of sensitive ICP edge 
  capt = icr - last_capture; 
  last_capture = icr;               // counter1 is now free-running 
 
 
  if(capt>255) delta = 255; else delta = (uint8_t)capt; 
 
  if (debug_mode > 2) {  
    if(debug_mode==4) 
      store_pulse_lenght(delta);
    else {                  
      store_pulse(delta);  
      return; 
    }
  } 

  if ((first != 0) && (forward_link_type == EM4150)) {                     // according to the first pulse length, manchester decoder is synchronized  
  
    first = 0;   
    last_bit = 0; 
    capt = maxCaptureTimeLow + icr; 
    if (capt < (uint16_t)(2*halfDataRate) )  //with setup latency of about 12 RF clocks 
      bit_pos = last_bit = 1; 
    else 
      store_bit(last_bit, 0); 
    return; 
  } 
 
  y = 0; 
  delta += halfDataRate >> 2;   // dynamic resolution (dependent on datarate) 
//  delta += (halfDataRate >> TOLERANCE_SHIFT);   // dynamic resolution (dependent on datarate) 
  delta -= halfDataRate;        // always subtract datarate/2 
  if (delta > halfDataRate) {   // when longer than that, try to subtract once more 
    y++; 
    delta -= halfDataRate; 
  } 
 
  if ((delta<<1) > halfDataRate) {     // check overflow of counter and range 
                                        // this check is also dynamic, depends on current datarate 
                                        // the faster datarate the more precise must the signal be 
 
    if(last_valid == 0) {      
 
      last_bit = ((~edge)>>ICES1)&1;   // rtf feature 4026 
      store_bit(last_bit, 0);           // rtf feature 4026  
      store_bit ( 1, 1 );               // store bad bit 
      last_valid = 1; 
      bit_pos++;                        // change! 4026 
 
    } 
    last_bit = 0; 
  } else { 
 
    if (y) { 
      last_bit = ((~edge)>>ICES1)&1; 
      store_bit(last_bit, 0); 
      last_valid = 0; 
      bit_pos = 0; 
    } else { 
      if (bit_pos++) { 
        store_bit(last_bit, 0); 
        bit_pos = 0; 
        last_valid = 0; 
      } 
    } 
  } 
 
#ifdef DEBUG_EMDB409
  cbi(PORTA,DEC5);
#endif
} 

 

Really appreciate if someone could give me any input.

 

 

Thanks

AM

Amateur programmer.
Believe when I tell you that my struggle on here is real.

Last Edited: Wed. Sep 27, 2017 - 05:18 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What does this have to do with the previous questions in this thread??

 

For a new question, start a new thread!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I tried posting it on the Compilers and General Programming forum; it wasnt getting published. So I had asked a moderator to help out - it was suggested that I post it here. Is there a specific forum for posts like these?

Amateur programmer.
Believe when I tell you that my struggle on here is real.

Last Edited: Wed. Sep 27, 2017 - 03:34 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I've split/moved it. PM sent.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How to properly post source code: http://www.avrfreaks.net/comment...

 

So which bits don't you understand?

 

Do you actually understand how BiPhase encoding works? 

Clearly, that is going to be fundamental to understanding the code ...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Atmel has an app note on Manchester Coding Basics #9164   using that as a google search will help you understand how biphase encoding/decoding works.

 

Jim

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes I do. A transition happens from High to Low or Low to High at the beginning of each bit period. If 0 is transmitted - there will an additional transition in the middle of the bit period - in case of 1 no transition in the middle of the bit period.

Amateur programmer.
Believe when I tell you that my struggle on here is real.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

azimmali wrote:
Can somebody please explain me the logic/algorithm behind this code

The Author of that code should be able to explain it!

 

Where did you find it? Have you asked there?

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

ki0bk wrote:
Atmel has an app note on Manchester Coding Basics #9164

Right here: http://www.atmel.com/images/atmel-9164-manchester-coding-basics_application-note.pdf

 

It explains the principles, and includes example code ...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

BiPhase in that document is defined the opposite of what I posted on here. Which convention is true?

Amateur programmer.
Believe when I tell you that my struggle on here is real.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thats what I followed from EM. The ATMEL document has it defined the opposite. Are there two conventions like the Manchester?

Amateur programmer.
Believe when I tell you that my struggle on here is real.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 2

How to properly post source code: http://www.avrfreaks.net/comment...

I would think that someone with 70+ posts would have figured out how to post source code by now. 

Greg Muth

Portland, OR, US

Atmel Studio 7.0 on Windows 10

Xplained/Pro/Mini Boards mostly

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

azimmali wrote:

BiPhase in that document is defined the opposite of what I posted on here. Which convention is true?

 

I don't think it maters if it is H/L or L/H, but whether a change occurs mid way or not.

 

So if the bit is backwards, just invert it.  You decide which coding/decoding is right for you.

Or in this case, how the algorithm above decodes it. 

 

Jim

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for showing me that.

Amateur programmer.
Believe when I tell you that my struggle on here is real.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So say if some chips are encoded one way and some the other way - the decoding should support both right? (sorry if this was a dumb question)

Amateur programmer.
Believe when I tell you that my struggle on here is real.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What chips are we talking about?

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

That's entirely up to you: it is your code - so you choose whether you want it to be specific to a particular chip or not ...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ooops - transponders

Amateur programmer.
Believe when I tell you that my struggle on here is real.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#define BUFFSIZE 128
#define UPPERTIMINGLMT 5000
#define SAMPLING 0
#define TIMING 1
#define MANCHESTER 0
#define BIPHASE1 1
#define BIPHASE2 2
#define INVERTED
//#define NONINVERTED
// Error codes
#define SyncErr 2
#define BitErr 3
#define TagErr 4
// Declare your global variables (extern) here
extern struct DecodeTiming DecodeReadTime;
extern volatile unsigned char cDataBuff[BUFFSIZE];

This is from the global definitions on that file - why do they have two versions of BiPhase? indecision

Amateur programmer.
Believe when I tell you that my struggle on here is real.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

azimmali wrote:
why do they have two versions of BiPhase? 

See #14 ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Can you post a link to where you found this code, as I don't see those defined values in the code snippet you posted, and my crystal ball is in the shop for repairs just now.

 

 

Jim

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

http://www.atmel.com/images/atme...

 

 

Crystal Ball..repairs..? wait, what?

Amateur programmer.
Believe when I tell you that my struggle on here is real.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

azimmali wrote:
why do they ... ?

Why don't you ask "them" ?

 

You know who "they" are - we don't.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oh shoot. Ok.

Amateur programmer.
Believe when I tell you that my struggle on here is real.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

azimmali wrote:
why do they have two versions of BiPhase?

 

I don't know for sure, that is not explained in the Atmel doc or in their code.

In the code they define it and have a function prototype but no code.

 

Jim

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok. No worries. I will try to understand whats being done. The algorithm in both cases are different.

Amateur programmer.
Believe when I tell you that my struggle on here is real.