hi,
i have been working on project arduino uno , have written of code in arduino ide. im in situation need move of code arduino's ide atmel studio. of function have figured out on how convert them etc.: direct port control , such. facing issue re-writing pulsein() function atmel studio. crucial part in project since need read on length of pulse width , act accordingly of length. length of signal needs returned in microseconds.
the chip working atxmega32e5. internal oscillator running @ 32mhz.
i have read forum , found pulsein() functions "guts" , tried re-write it.
so far take on trying convert pulsein():
and original pulsein() code:
please me correct code or rewrite pulsein() or explain me how it, since im new moving away comfortable arduino
thanks in advance guys!
 							i have been working on project arduino uno , have written of code in arduino ide. im in situation need move of code arduino's ide atmel studio. of function have figured out on how convert them etc.: direct port control , such. facing issue re-writing pulsein() function atmel studio. crucial part in project since need read on length of pulse width , act accordingly of length. length of signal needs returned in microseconds.
the chip working atxmega32e5. internal oscillator running @ 32mhz.
i have read forum , found pulsein() functions "guts" , tried re-write it.
so far take on trying convert pulsein():
code: [select]
#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 previous pulse end
	while ( rc_pin_state == 1)
	{
		rc_pin_state = (pind & (1<<pd2)); //keep reading pin until changes state
		if (numloops++ == maxloops) break;
	}
	// wait pulse start
	while (rc_pin_state == 0)
	{
		rc_pin_state = (pind & (1<<pd2)); //keep reading pin until changes state
		if (numloops++ == maxloops) break;
	}
	
	// wait pulse stop @ here measuring pulse width = incrementing width value 1 each cycle. atmega328 1 micro second equal 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 input rc signal
	ddrd = (1<<pd3); // port , output led
	
	//char rc_pin_state = (pind & (1<<pd2)); // read state of pin pd2 , put state 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 original pulsein() code:
code: [select]
unsigned long pulsein(uint8_t pin, uint8_t state, unsigned long timeout)
{
	// cache port , bit of pin in order speed the
	// pulse width measuring loop , achieve finer resolution.  calling
	// digitalread() instead yields 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 timeout microseconds number of times through
	// initial loop; takes 16 clock cycles per iteration.
	unsigned long numloops = 0;
	unsigned long maxloops = microsecondstoclockcycles(timeout) / 16;
	
	// wait previous pulse end
	while ((*portinputregister(port) & bit) == statemask)
		if (numloops++ == maxloops)
			return 0;
	
	// wait pulse start
	while ((*portinputregister(port) & bit) != statemask)
		if (numloops++ == maxloops)
			return 0;
	
	// wait pulse stop
	while ((*portinputregister(port) & bit) == statemask) {
		if (numloops++ == maxloops)
			return 0;
		width++;
	}
	// convert reading microseconds. loop has been determined
	// 20 clock cycles long , have 16 clocks between edge
	// , start of loop. there error introduced by
	// interrupt handlers.
	return clockcyclestomicroseconds(width * 21 + 16); 
}please me correct code or rewrite pulsein() or explain me how it, since im new moving away comfortable arduino

thanks in advance guys!
i thought atmel studio supposed able compile , link arduino sketches without modifications. why can't that?
 							
            						 					Arduino Forum  						 						 							 >   					Using Arduino  						 						 							 >   					Programming Questions  						 						 							 >   					pulseIN() - converting the code to ATMEL STUDIO  						 					
arduino
 
  
Comments
Post a Comment