pulseIN() - converting the code to ATMEL STUDIO

Hi,

I have been working on a project with ARDUINO UNO and have written all of the code in ARDUINO IDE. Now Im in situation where I need to move all of the code from ARDUINO’s IDE to ATMEL STUDIO. Most of the function I have figured out on how to convert them etc.: direct port control and such. But I am facing an issue re-writing pulseIN() function for ATMEL STUDIO. It is a crucial part in the project since I need to read on the length of pulse width and act accordingly of the length. The length of the signal needs to be returned in microseconds.

The chip that I am working with is ATXMega32e5. The internal oscillator is running at 32Mhz.

I have read the forum and found the pulseIN() functions “guts” and tried to re-write it.
So far this is my take on trying to convert the pulseIN():

#include <avr/io.h>

unsigned long RC_VALUE;

void pulseIn()
{
	char RC_PIN_STATE = (PIND & (1<<PD2));
	
	//timeout zone
	unsigned long numloops = 0;
	unsigned long maxloops = 500000;
	unsigned long width = 0;
	// wait for any previous pulse to end
	while ( RC_PIN_STATE == 1)
	{
		RC_PIN_STATE = (PIND & (1<<PD2)); //keep reading the pin until it changes the state
		if (numloops++ == maxloops) break;
	}
	// wait for the pulse to start
	while (RC_PIN_STATE == 0)
	{
		RC_PIN_STATE = (PIND & (1<<PD2)); //keep reading the pin until it changes the state
		if (numloops++ == maxloops) break;
	}
	
	// wait for the pulse to stop @ here we are measuring the pulse width = incrementing the WIDTH value by one each cycle. atmega328 1 micro second is equal to 16 cycles.
	while (RC_PIN_STATE == 1)
	{
		RC_PIN_STATE = (PIND & (1<<PD2));
		if (numloops++ == maxloops) break;
		width++;
	}

	RC_VALUE = width/16;
}

int main(void)
{
	DDRD = (0<<PD2); // port PD2 is an input for the RC signal
	DDRD = (1<<PD3); // port is and output for LED
	
	//char RC_PIN_STATE = (PIND & (1<<PD2)); // read the state of pin PD2 and put the state into variable
	
    while(1)
    {
      pulseIn();
	  if(RC_VALUE > 1000 && RC_VALUE < 1395)
	  {
		  PORTD = (1<<PD3); // turn LED on
	  }
		
    	else if(RC_VALUE > 1600 && RC_VALUE < 2200)
		{
		PORTD = (0<<PD3); // turn LED off
		}	
    }
}

And this is the original pulseIN() code:

unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
{
	// cache the port and bit of the pin in order to speed up the
	// pulse width measuring loop and achieve finer resolution.  calling
	// digitalRead() instead yields much coarser resolution.
	uint8_t bit = digitalPinToBitMask(pin);
	uint8_t port = digitalPinToPort(pin);
	uint8_t stateMask = (state ? bit : 0);
	unsigned long width = 0; // keep initialization out of time critical area
	
	// convert the timeout from microseconds to a number of times through
	// the initial loop; it takes 16 clock cycles per iteration.
	unsigned long numloops = 0;
	unsigned long maxloops = microsecondsToClockCycles(timeout) / 16;
	
	// wait for any previous pulse to end
	while ((*portInputRegister(port) & bit) == stateMask)
		if (numloops++ == maxloops)
			return 0;
	
	// wait for the pulse to start
	while ((*portInputRegister(port) & bit) != stateMask)
		if (numloops++ == maxloops)
			return 0;
	
	// wait for the pulse to stop
	while ((*portInputRegister(port) & bit) == stateMask) {
		if (numloops++ == maxloops)
			return 0;
		width++;
	}

	// convert the reading to microseconds. The loop has been determined
	// to be 20 clock cycles long and have about 16 clocks between the edge
	// and the start of the loop. There will be some error introduced by
	// the interrupt handlers.
	return clockCyclesToMicroseconds(width * 21 + 16); 
}

Please help me to correct my code or to rewrite the pulseIN() or explain me how to do it, since Im new to moving away from comfortable ARDUINO :slight_smile:

Thanks in advance guys!

I thought that Atmel Studio was supposed to be able to compile and link Arduino sketches without modifications. Why can't you do that?

Yes it is able to load them up, but you have to select your Arduino IDE folder + board. Problem is that ATXMega32e5 is not in that list. I have tried downloading Xmegaduino from GitHub and stuffing that in, but with no luck. Therefore I need some advice, maybe there is some otherway in solving this or only way to go is to rewrite everything and my problem lies that I need help in rewriting the "pulseIn( PIN , HIGH or LOW, Timeout). As I have written in the above post, not sure if my code is any good.