Data & asynchronous transmission to use in wireless communications

Hi all!

First, let me say i'm new to this forum so i will try to not break any rules. :wink:

I am using 2 Arduino UNO and both need to be able to Tx and Rx just not at the same time. They will use ligh as communication which means they will control external array of LEDs.

I will point out the things that i already tried and didn´t work. Basically i want:

To be able to transmit data using manchester codification @ frequencies above or equal 200kHz: i need to send data and i don't want program to generate interruptions while i'm doing it. So i tried write directly to pins..

->I used MANCHESTER library but the max Tx i could achieve without errors was 20kHz (not enough) although i set a timer way below that. This Library writes directly to pin using digitalWrite()
->Then i found a page where was described how to improve data output: http://arduino.cc/forum/index.php/topic,36729.0.html
There, i found that if i used asm commands, in this case PORTB (pin 8-13) to write, the data output would improve. I changed the code, deleted some lines which weren't needed and ..and it did...but not as much as i desired. I got ~100kHz sending 16 '0's but when i tried sending 16 '1' only 9 were sent :S (i don't have a clue for this one...).
-> i tried a different approach...using TimerOne library. As i stated above i need to send data as a burst, i don't need to check...just to send PERIOD. But i dont have a clue how to do it with timers. How can i send data 1 and 0 without calling interrupts?

i need to be able to receive but there is no point in Tx@1MHz if i can't receive above a much lower value.
-> On the receive end i will have a NRZ signal which will work @half the frequency of the previous Tx signal. I use a PLL for this...but i cant setup one until i have the Tx frequency required.

So, i would like some advices on how to get higher frequencies to transmit data...

Thank you for your time,
Fernando Oliveira

How do you receive the data? I understand you send it using LEDs, but what's the reception hardware? Have you tried direct port readings on the receiving side? It helps a lot to help you if you show us your current code.

Hi! and thank you for support...

The receiving side has a photodetector that receives light: higher levels of light = '1' ...then there is some electronic stages(filtering and amplifier)...and on the last stage there is a decoder. This decoder is a Manchester to NRZ decoder. This means that at the receiving end, arduino will try to get a NRZ signal.

The current code receiver is based on the MANCHESTER library... The library was done thinking on RF systems but also works with IR and visible light as long there is hardware able to do it. In my case, i use a LED emmiter and not a RF emmiter/receiver.

Transmission:
Problems with this code is that HIGH and LOW have different times (sometimes), because the way i am writing to the ports

#include <VLC.h>

#define TxPin 8  //the digital pin to use to transmit data

unsigned int Tdata = 0xffff;  //the 16 bits to send

void setup()
{                
MANCHESTER.SetTxPin(TxPin);      // sets the digital pin as output default 4
MANCHESTER.SetPW(2);     // sets the pulse half width
}//end of setup

void loop()
{
  cli();
 
   MANCHESTER.Transmit(Tdata);
   sei();
 //delayMicroseconds(5);
}//end of loop

Receiving end:

#include <MANCHESTER.h>

#define RxPin 4

void setup()
{ 
 MANCHESTER.SetRxPin(RxPin); //user sets rx pin default 4
 MANCHESTER.SetTimeOut(1000); //user sets timeout default blocks
 Serial.begin(9600);   // Debugging only
}//end of setup

void loop()
{
 unsigned int data = MANCHESTER.Receive();
 Serial.println(data);
}//end of loop

Th above code was taken from examples but it works.
Basically waits for 3 '0's and 1 '1' and then starts receiving....

VLC.cpp
I made some changes from the real MANCHESTER library so i could have less response times...

#include "VLC.h"


//#define HALF_BIT_INTERVAL 1000 // microseconds

MANCHESTERClass::MANCHESTERClass() //constructor
{
  TxPin = TxDefault;
  pinMode(TxPin, OUTPUT); // sets the digital pin 4 default as output
}//end of constructor

void MANCHESTERClass::SetPW(unsigned int interval)
{
	HALF_BIT_INTERVAL = interval; // user sets the pulse width of half bit
}//end of set half pulse width

void MANCHESTERClass::SetTxPin(char pin)
{
	if (pin <=7)
		TxPin = pin; // user sets the digital pin as output
	else if ((pin >= 8) || (pin <= 13))
		TxPin = pin - 8; // user sets the digital pin as output
	DDRB = DDRB | B0000001 | (1 << TxPin);
  //pinMode(TxPin, OUTPUT); // sets the digital pin 4 default as output
}//end of set transmit pin

void MANCHESTERClass::Transmit(unsigned int data)
{
  unsigned char byteData[2] = { data  >> 8, data & 0xFF};
  TransmitBytes(2, byteData);
}

void MANCHESTERClass::TransmitBytes(unsigned char numBytes, unsigned char *data)
{
  // Setup last send time so we start transmitting in 10us
  //lastSend = micros() - HALF_BIT_INTERVAL + 10;
   
  // Send 14 0's
  for( int i = 0; i < 14; i++) //send capture pulses
     sendzero(); //end of capture pulses
    
  // Send a single 1
  sendone(); //start data pulse
 
  // Send the user data
  for (unsigned char i = 0; i < numBytes; i++)
  {
    unsigned int mask = 0x01; //mask to send bits
	
	for (char j = 0; j < 8; j++)
    {
	unsigned int c = data[i] & mask;
		switch( c ) 
		{
			case 0:
				sendzero();
				break;
			case 1 :
				sendone();
				break;
		}
      mask = mask << 1; //get next bit
    }//end of byte
  }//end of data
}//end of send the data

void MANCHESTERClass::sendzero(void)
{

 
  //digitalWrite(TxPin, HIGH);
  PORTB = B00000001;
 
  delayMicroseconds(HALF_BIT_INTERVAL);
  //digitalWrite(TxPin, LOW);
  PORTB = B00000000;

  //lastSend = micros();
}//end of send a zero

void MANCHESTERClass::sendone(void)
{

  //digitalWrite(TxPin, LOW);
  PORTB = B00000000;

delayMicroseconds(HALF_BIT_INTERVAL);
  //digitalWrite(TxPin, HIGH);
  PORTB = B00000001;
}//end of send one

MA

Thank you

The receiving side has a photodetector that receives light

What hardware do you use there (links to datasheets)? Are you sure the hardware is able to receive with 200kHz and more?

You changed the Manchester class to use direct port manipulation but you seemed to have fixed the TX pin to pin 8. BTW, these are not assembler commands that are just the register names you assign directly values to within C.

delayMicroseconds(HALF_BIT_INTERVAL);

but

//#define HALF_BIT_INTERVAL 1000 // microseconds

Seems you changed other things not mentioned here. I guess you should post the whole code you are using.

If you haven't changed the reception part too, it's executing the interrupt routine with 12.5kHz and this should be at least twice the half-bit frequency you set in the transmission part. There you set it to 500kHz (although you won't reach that but that's what the pulse length is) so you're sending 40 times as fast as your receiving. Did I miss something?

pylon:
The receiving side has a photodetector that receives light

What hardware do you use there (links to datasheets)? Are you sure the hardware is able to receive with 200kHz and more?

The rise and fall time are typically 100ns which gives 10MHz.
BPW34S from Vishay: http://www.vishay.com/docs/81521/bpw34.pdf

pylon:
You changed the Manchester class to use direct port manipulation but you seemed to have fixed the TX pin to pin 8. BTW, these are not assembler commands that are just the register names you assign directly values to within C.

delayMicroseconds(HALF_BIT_INTERVAL);

but

//#define HALF_BIT_INTERVAL 1000 // microseconds

Seems you changed other things not mentioned here. I guess you should post the whole code you are using.

The class where i changed the TxPin was made so i could build a small routine where the user only had to insert the pin number and the program would adapt to the respective port. I would bypass the delays from digitalwrite and writing would be faster. Lets say it's a work in progress... :wink:

This is a comment that i forgot to clean "//#define HALF_BIT_INTERVAL 1000 // microseconds".
The real HALF_BIT_INTERVAL is defined on VLC.h: "unsigned int HALF_BIT_INTERVAL;"

On the VLC.cpp there is a function SetPW (HPW) where 'HPW' is half pulse width.
So, in the file, on the setup area, when i write: MANCHESTER.SetPW(2), i'm saying that my full period is 4us which means 250kHz.
If i want to write a '1' ('0')i have to send '01' (10') with identical duty-cycle. So, if i transform those bits back to NRZ i will get 125kHz. Hope this is clear..

pylon:
If you haven't changed the reception part too, it's executing the interrupt routine with 12.5kHz and this should be at least twice the half-bit frequency you set in the transmission part. There you set it to 500kHz (although you won't reach that but that's what the pulse length is) so you're sending 40 times as fast as your receiving. Did I miss something?

My bad for not explaining well what i did...

  1. I used the libraries MANCHESTER which are open-source to everyone and reached the same conclusion than you: i could transmit and receive at a maximum frequency of 12.5kHz with that library.
  2. Since i needed more Bandwidth i studied the library code and changed it so i could write to the port @ frequencies above 12.5kHz. I reached a point where i was happy with 250kHz. NOTE: i didn't tried or change the code on the receiver.
  3. But, analysing the signal through oscilloscope i realized that although the frequency was 250kHz the duty-cycle was not always perfect which could be a lot of trouble on the receiver.

So what i'm asking:
(a) I need to transmit a manchester coded signal above 200kHz and receive a NRZ signal with half transmit frequency. While it's transmitting or receiving the routines can not be interrupted. Can i do it with the method above (writing/reading directly to ports) or is there a better, more efficient way with a timer?

Thank you very much pylon.

Best Regards,
Fernando Oliveira

So what i'm asking:
(a) I need to transmit a manchester coded signal above 200kHz and receive a NRZ signal with half transmit frequency. While it's transmitting or receiving the routines can not be interrupted. Can i do it with the method above (writing/reading directly to ports) or is there a better, more efficient way with a timer?

I forgot to add something: there is possibility of using an external clock synchronized with NRZ signal i want to receice. Decoder is based on PLL and i have those 2 signal available if i want...

Cheers

  1. But, analysing the signal through oscilloscope i realized that although the frequency was 250kHz the duty-cycle was not always perfect which could be a lot of trouble on the receiver.

Show us the scope output so we know what you're talking about. You have to get very clear what you're trying to achieve and what constraints you have. In frequencies above 100kHz you have to take care about the timing of individual statements. To have a balanced output of 0s and 1s you may have to insert calculations on one side just to use an equal amount of time as the other side.

You seem to have modified the library quite heavily. To enable us to reproduce your setup you have to show us all the code you're using, else we always talk about different things.

Manchester coding can be a reliable method of sending, but it is not the fastest.

sbright33:
Manchester coding can be a reliable method of sending, but it is not the fastest.

The reason i use manchester coding is to be able to recover clock on the receiving device. I think 100-150khz wouldn't be that fast... Also a decoder wouldn't be so difficult to achieve using a PLL, for example.

Show us the scope output so we know what you're talking about.

Will do once i get home, probably tomorrow.

You have to get very clear what you're trying to achieve and what constraints you have. In frequencies above 100kHz you have to take care about the timing of individual statements. To have a balanced output of 0s and 1s you may have to insert calculations on one side just to use an equal amount of time as the other side.

I agree completely and that is what i was trying to do but i wasn't getting the expected results.
If you look closely the files that i put at the end of this post you will notice i don't have any code related to receiver.
If you compare my modified library with original, (only the transmitting part) you will see:

  • statement 'if' changed to a 'switch' (i think LABELS and/or switch statement are equally timed);
  • digitalWrite(..) changed to PORTB statements (faster pin writes);
  • lastsend was used as a way to time correctly port writes and it worked really well at low frequencies. But, at the frequencies i want to transmit the introduced delay was too much. I probably have to re-think or re-work this solution...

You seem to have modified the library quite heavily. To enable us to reproduce your setup you have to show us all the code you're using, else we always talk about different things.

NOTICE: I haven't wrote, changed or used any part for receiving side. Right now i'm more worried on having Tx freq above 200kHz with 50% Duty-cycle.
Files are as follows:
Transmit.ino (Main program)
VLC.cpp & VLC.h (Modified Libraries)

TRANSMISSION.INO

 #include <VLC.h>

#define TxPin 8  //the digital pin to use to transmit data

unsigned int Tdata = 0xffff;  //the 16 bits to send

void setup()
{                
MANCHESTER.SetTxPin(TxPin);      // sets the digital pin as output default 4
MANCHESTER.SetPW(2);     // sets the pulse half width
}//end of setup

void loop()
{
  cli();
  MANCHESTER.Transmit(Tdata);
  sei();
}//end of loop

VLC.H

 /*
This code is based on the Atmel Corporation Manchester
Coding Basics Application Note.

http://www.atmel.com/dyn/resources/prod_documents/doc9164.pdf

Quotes from the application note:

"Manchester coding states that there will always be a transition of the message signal
at the mid-point of the data bit frame.
What occurs at the bit edges depends on the state of the previous bit frame and
does not always produce a transition. A logical “1” is defined as a mid-point transition
from low to high and a “0” is a mid-point transition from high to low.
*/

#ifndef VLC_h
#define VLC_h

#define TxDefault 4 //the digital pin to use to transmit data

#define TimeOutDefault -1 //the timeout in msec default blocks

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#include <pins_arduino.h>
#endif

class MANCHESTERClass
{
  public:
    MANCHESTERClass(); //the constructor
	void SetPW(unsigned int interval); //  user sets the pulse width of half bit
    void SetTxPin(char pin); //set the arduino digital pin for transmit. default 4.
    void Transmit(unsigned int data); //transmit 16 bits of data
    void TransmitBytes(unsigned char numBytes, unsigned char *data); // transmit a byte array
    
  private:
    void sendzero(void);
    void sendone(void);
    unsigned char TxPin;
	unsigned int HALF_BIT_INTERVAL;
};//end of class MANCHESTER


extern MANCHESTERClass MANCHESTER;

#endif

VLC.cpp

#include "VLC.h"

MANCHESTERClass::MANCHESTERClass() //constructor
{
  TxPin = TxDefault;
  pinMode(TxPin, OUTPUT); // sets the digital pin 4 default as output
}//end of constructor

void MANCHESTERClass::SetPW(unsigned int interval)
{
	HALF_BIT_INTERVAL = interval; // user sets the pulse width of half bit
}//end of set half pulse width

void MANCHESTERClass::SetTxPin(char pin)
{
	if (pin <=7)
		TxPin = pin; // user sets the digital pin as output
	else if ((pin >= 8) || (pin <= 13))
		TxPin = pin - 8; // user sets the digital pin as output
	DDRB = DDRB | B0000001 | (1 << TxPin);
  //pinMode(TxPin, OUTPUT); // sets the digital pin 4 default as output
}//end of set transmit pin

void MANCHESTERClass::Transmit(unsigned int data)
{
  unsigned char byteData[2] = { data  >> 8, data & 0xFF};
  TransmitBytes(2, byteData);
}

void MANCHESTERClass::TransmitBytes(unsigned char numBytes, unsigned char *data)
{
  // Setup last send time so we start transmitting in 10us
  //lastSend = micros() - HALF_BIT_INTERVAL + 10;
   
  // Send 14 0's
  for( int i = 0; i < 14; i++) //send capture pulses
     sendzero(); //end of capture pulses
    
  // Send a single 1
  sendone(); //start data pulse
 
  // Send the user data
  for (unsigned char i = 0; i < numBytes; i++)
  {
    unsigned int mask = 0x01; //mask to send bits
	
	for (char j = 0; j < 8; j++)
    {
	unsigned int c = data[i] & mask;
		switch( c ) 
		{
			case 0:
				sendzero();
				break;
			case 1 :
				sendone();
				break;
		}
      mask = mask << 1; //get next bit
    }//end of byte
  }//end of data
  sendzero(); //end of capture pulses
  sendzero(); //end of capture pulses
}//end of send the data

void MANCHESTERClass::sendzero(void)
{
  //digitalWrite(TxPin, HIGH);
  PORTB = B00000001;
 
  delayMicroseconds(HALF_BIT_INTERVAL);
  //digitalWrite(TxPin, LOW);
  PORTB = B00000000;

}//end of send a zero

void MANCHESTERClass::sendone(void)
{
  //digitalWrite(TxPin, LOW);
  PORTB = B00000000;

delayMicroseconds(HALF_BIT_INTERVAL);
  //digitalWrite(TxPin, HIGH);
  PORTB = B00000001;
}//end of send one

MANCHESTERClass MANCHESTER;

Thanks for support,
Fernando Oliveira

I would make this part faster:

  // Send the user data
  for (unsigned char i = 0; i < numBytes; i++)
  {
    unsigned int mask = 0x01; //mask to send bits
	
	for (char j = 0; j < 8; j++)
    {
	unsigned int c = data[i] & mask;
		switch( c ) 
		{
			case 0:
				sendzero();
				break;
			case 1 :
				sendone();
				break;
		}
      mask = mask << 1; //get next bit
    }//end of byte
  }//end of data

my version:

  // Send the user data
  for (unsigned char i = 0; i < numBytes; i++) {
    for (uint8_t mask = 0x01; mask; mask <<= 1) {
      if (data[i] & mask) {
        sendone();
      } else {
        sendzero();
      }
    }//end of byte
  }//end of data

This not only make the code more compact but also faster.

delayMicroseconds(HALF_BIT_INTERVAL);

This does wait at least 4 us even if the parameter is less than 4.

To get a more constant timing I would declare the sendone and sendzero methods inline.

UPDATE:
I finally achieved a transmission above 200kHz, 240kHz more exactly.
Like you said Pylon, can not use delaymicroseconds(..) due to the delays inserted. Then i used nop asm delays and i got what i wanted.

__asm__("nop\n\t");

each one is 62.5 ns so with help of oscilloscope i timed them individually. Result is shown below.

#include "VLC.h"

MANCHESTERClass::MANCHESTERClass() //constructor
{
  TxPin = TxDefault;
  pinMode(TxPin, OUTPUT); // sets the digital pin 4 default as output
}//end of constructor

void MANCHESTERClass::SetPW(unsigned int interval)
{
	HALF_BIT_INTERVAL = interval; // user sets the pulse width of half bit
}//end of set half pulse width

void MANCHESTERClass::SetTxPin(char pin)
{
	if (pin <=7)
		TxPin = pin; // user sets the digital pin as output
	else if ((pin >= 8) || (pin <= 13))
		TxPin = pin - 8; // user sets the digital pin as output
	DDRB = DDRB | B0000001 | (1 << TxPin);
  //pinMode(TxPin, OUTPUT); // sets the digital pin 4 default as output
}//end of set transmit pin

void MANCHESTERClass::Transmit(unsigned int data)
{
  unsigned char byteData[2] = { data  >> 8, data & 0xFF};
  TransmitBytes(2, byteData);
}

void MANCHESTERClass::TransmitBytes(unsigned char numBytes, unsigned char *data)
{
  // Setup last send time so we start transmitting in 10us
   
 /* 
  // Send 14 0's
  for( int i = 0; i < 14; i++) {
	  //send capture pulses - sendzero()
			PORTB = B00000001;
			__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
			__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
			__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
			__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
			PORTB = B00000000;
			__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
			__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
			__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
			//__asm__("nop\n\t");
      }
	
	//end of capture pulses

	// Send a single 1 - start data pulse
	PORTB = B0000000;
	__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
	__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
	__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
	__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
	PORTB = B00000001;
	__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t");
	*/

  // Send the user data
  for (unsigned char i = 0; i < numBytes; i++) {
	for (uint8_t mask = 0x01; mask; mask <<= 1) {
      if (data[i] & mask) {
			//sendone()
			PORTB = B00000000;
			__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
			__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
			__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
			__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
			PORTB = B00000001;
			__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
			__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
      } 
	  else {
			//sendzero()
			PORTB = B00000001;
			__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
			__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
			__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
			__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
			PORTB = B00000000;
			__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
			__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
      }
    }//end of byte
  }//end of data
  //sendzero(); //end of capture pulses
  //sendzero(); //end of capture pulses
}//end of send the data


MANCHESTERClass MANCHESTER;

Got some error bits, especially when i send many 1's but i can live with that.

Working on the receiving part...will make an update once i get some results.

Thank you for support,
Fernando Oliveira

Hello once again.... :slight_smile:

Finally i got a steady transmission signal with steady 125kHz. So i need the receiver to actually receive something.
But, surprise...new problems ...

What i was trying to do was get The timer2 to have interrupts every 8us (125kHz) and i managed to do it:
(RxPin is 4 and it's connected to a simple switch, pinT2 is 5 and i'm using Arduino UNO)

VLC.cpp

static char RxPin = 4;
static char pinT2 = 5;


static int rx_sample = 0;
static int rx_last_sample = 0;
static uint8_t rx_count = 0;
static uint8_t rx_sync_count = 0;
static uint8_t rx_mode = RX_MODE_IDLE;

static unsigned int rx_manBits = 0; //the received manchester 32 bits
static unsigned char rx_numMB = 0; //the number of received manchester bits
static unsigned char rx_curByte = 0;

static unsigned char rx_maxBytes = 2;
static unsigned char rx_default_data[2];
static unsigned char* rx_data = rx_default_data;

void MANRX_SetRxPin(char pin)
{
  RxPin = pin;
  	if (pin <=7){
		RxPin = pin;			// user sets the digital pin as input
		DDRD &= ~(1<<RxPin);	//pinMode(RxPin, INPUT);
	}	
	else if ((pin >= 8) || (pin <= 13)){
		RxPin = pin - 8;		// user sets the digital pin as input
		DDRB &= ~(1<<RxPin);	//pinMode(RxPin, INPUT);
	}
}//end of set reception pin

void MANRX_SetupReceive()
{
	DDRD &= ~(1<<RxPin);	// default pinMode(RxPin, INPUT);
	/*
	Timer 2 is used with a ATMega328. The base clock is 16MHz. 
	 - If we don't use any clock divider which gives 62.5 nS per count.
	 - If we use /8 as clock divider resulti is 0.5 usS per count.

	PS = 1 (Prescaler)
	1 / (16,000,000 / PS) = 62.5nS/count

	PS = 8 (Prescaler)
	1 / (16,000,000 / PS) = 0.5uS/count

	NRZ frequency is half the Tx signal (Manchester Encoded)
	So, F_NRZ = 125kHz => 8us/bit (time of each bit)
	PS=1 => 8us/62.5ns = 128
	PS=1 => 8us/0.5us = 16
	*/

  TCCR2A = _BV(WGM21); // reset counter on match
  TCCR2B &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12));// reset clock select register, and starts the clock
  TCCR2B |= _BV(CS11);//counts every 0.5usec with 16 Mhz clock    
  OCR2A = 15; // interrupt every 16 counts (0->15)
  TIMSK2 = _BV(OCIE2A); // Turn on interrupt
  TCNT2 = 0; // Set counter to 0
}

void MANRX_BeginReceive(void)
{
  rx_maxBytes = 2;
  rx_data = rx_default_data;
  rx_mode = RX_MODE_PRE;
}

void test_t2(char pin)
{
	if (pin <=7){
		pinT2 = pin; // user sets the digital pin as output
		DDRD = DDRD | B0000001 | (1<<pinT2);	//pinMode(TxPin, OUTPUT);
		PORTD |= _BV(pinT2);
	}
	else if ((pin >= 8) || (pin <= 13)){
		pinT2 = pin - 8; // user sets the digital pin as output
		DDRB = DDRB | B0000001 | (1<<pinT2);	//pinMode(TxPin, OUTPUT);
		PORTB |= _BV(pinT2);
	}
}


unsigned int MANRX_GetMessage(void)
{
  return (((int)rx_data[0]) << 8) | (int)rx_data[1];
}


ISR(TIMER2_COMPA_vect)
{
	PORTD |= (1<<pinT2);
	unsigned char test = (PIND & (1<<RxPin));
	rx_data[1] = test;
	rx_data[0] = 0x00;
	PORTD &= ~(1 << pinT2);
}

VLC.h - class Manchester refers to transmission and it's working

/*
This code is based on the Atmel Corporation Manchester
Coding Basics Application Note.

http://www.atmel.com/dyn/resources/prod_documents/doc9164.pdf

Quotes from the application note:

"Manchester coding states that there will always be a transition of the message signal
at the mid-point of the data bit frame.
What occurs at the bit edges depends on the state of the previous bit frame and
does not always produce a transition. A logical “1” is defined as a mid-point transition
from low to high and a “0” is a mid-point transition from high to low.
*/

#ifndef VLC_h
#define VLC_h

#define TxDefault 8 //the digital pin to use to transmit data
#define RxDefault 4 //the digital pin to use to receive data


#define RX_MODE_PRE 0
#define RX_MODE_SYNC 1
#define RX_MODE_DATA 2
#define RX_MODE_MSG 3
#define RX_MODE_IDLE 4

#define TimeOutDefault -1 //the timeout in msec default blocks

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#include <pins_arduino.h>
#endif

class MANCHESTERClass
{
  public:
    MANCHESTERClass(); //the constructor
	void SetPW(unsigned int interval); //  user sets the pulse width of half bit
    void SetTxPin(char pin); //set the arduino digital pin for transmit. default 4.
    void Transmit(unsigned int data); //transmit 16 bits of data
    void TransmitBytes(unsigned char numBytes, unsigned char *data); // transmit a byte array

    
  private:
    unsigned char TxPin;
	unsigned int HALF_BIT_INTERVAL;
};//end of class MANCHESTER

// Cant really do this as a real C++ class, since we need to have
// an ISR
extern "C"
{
	//for debugging only - tests T2 interrupts
	extern void test_t2(char pin);

    //set the arduino digital pin for receive. default 4.
    extern void MANRX_SetRxPin(char pin);
    
    //begin the timer used to receive data
    extern void MANRX_SetupReceive();
	
	// begin receiving 16 bits
    extern void MANRX_BeginReceive(void);


    
    // fetch the received message
    extern unsigned int MANRX_GetMessage();

}

extern MANCHESTERClass MANCHESTER;

#endif

MAIN PROGRAM

#include <VLC.h>
#include <avr/io.h>    

#define RxPin 4
#define pinT2 5

void setup()
{
  test_t2(pinT2); // tests T2 frequency interrupts
  
  MANRX_SetRxPin(RxPin); // Set digital TX pin
  MANRX_SetupReceive(); // Prepare interrupts
  MANRX_BeginReceive(); // Begin receiving data
  
  Serial.begin(115200);   // Debugging only
}//end of setup

void loop()
{
  unsigned int data = MANRX_GetMessage();
  MANRX_BeginReceive();
  //unsigned int data = 0x0010;
  Serial.println(data);
}//end of loop

So there are a couple of things happenning that i really don't understand.

(1) When i erase this 1st line (while keeping the second) my interruptions appear every 250us instead of 8us. What am i missing here?

TCCR2B &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12));// reset clock select register, and starts the clock
  TCCR2B |= _BV(CS11);//counts every 0.5usec with 16 Mhz clock

(1.1) While the 1st line is deleted i can change switch on pin 4 and serial monitor updates accordingly and that is fine. But why doesn't update when that 1st line is present?

(2) Now i add this line to ISR:

ISR(TIMER2_COMPA_vect)
{
	[b]TCNT2 = 0; // Set counter to 0[/b]
	PORTD |= (1<<pinT2);
        .......
}

why the serial monitor updates then stops nad only updates again when i reset?

(2.1) And why he updates a few times pin4 status when he is high and only half dozen when it's low? :S

(3)Wasn't supposed to interrupts have the same timming? Depending on the code i get less frequency....
For example...the code in ISR from VLC.cpp drops my freq to 120kHz and it even have 1/10 of what is needed.

Plz enlight me...

Thanks for support,
Fernando Oliveira