From Arduino to Atmel Studio

Status
Not open for further replies.

Tricka90

Member level 1
Joined
Sep 2, 2013
Messages
40
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Visit site
Activity points
385
I'm slowly moving from Arduino IDE to Atmel Studio to program the popular atmega328p. Since the Serial.print() Arduino function is very comfortable and useful, I'm looking for an easy way to display the values of the variables when using Atmel Studio. What is the best way in your opinion?
Of course I could add a series of LEDs to the microcontroller in order to display the values in binary or I could even connect an LCD screen, but these solutions require the use of lots of ports. How can you easily check the variables without Arduino IDE (and without using expensive external tools) ?
 

Thanks for your valuable advices, I'll study and try your solutions. There's another thing which is not perfectly clear to me: lots of people say they don't like Arduino IDE because of the simplified high level libraries. But the truth is Arduino IDE doesn't force you to use libraries (like digitalWrite, analogRead etc.), in fact it allows you to program at low level if you want, setting bits on registers and so on, so what exactly is wrong with using Arduino IDE? Of course, it forces you to maintain setup() and loop() functions but are there other bad things?
 

In my case in particular, the only time I dared to use the Arduino, it was when I needed to develop a project with the Atmega2560 using 4 UARTs, requiring interruption in reception for 3 of them, and the Arduino IDE does not offer native support for this resource. If you wish, you can even add this feature according to some web tutorials, but in this case you will be doing this at your own risk. The fact is that programming in C you know exactly what is being compiled, but in Arduino it is not clear what else is inserted in the code, and if you want high performance (fast response) I'm not sure if it would be the best choice. Anyway, it is great for simple projects to be completed fast, but in some cases I have doubts.
 

Thank you. So that means if I write a low level C code in Atmel Studio and then I write the exact same code in Arduino IDE I won't get the same result because Arduino IDE will alterate it somehow?
 

Even different compilers can build the same code with different approaches. I would not disagree that considering Arduino another language, although CPP like, should make an executable with different performance if compared to a standard C compiler.
 

Since the Serial.print() Arduino function is very comfortable and useful, I'm looking for an easy way to display the values of the variables when using Atmel Studio. What is the best way in your opinion?

Using the ATmega328P USART it is fairly simple to obtain a serial output. The 328P User Manual has example C code for initialize and transmit, etc.

Below is a simple program that sends data to a serial terminal.

Code:
#define F_CPU 16000000        // needed for delay.h

#include <avr/io.h>
#include <util/delay.h>       // needed for _delay_ms() function

void USART_Transmit( unsigned char data );
void USART_Init();
int main(void)
{
unsigned char count = 0;
   	
  /* setup() */	
  DDRB = 0x20;  // PB5 = Arduino pin 13	
  USART_Init(); // set up the USART for 115200 baud
  
  /* loop(); */
  while(1)
    {
	PORTB = PORTB ^ 0x20;  // toggle PORTB bit 5
	count ++;              // increment count
	if(count > 9)          // keep count < 10
	  count = 0;
	USART_Transmit( (count + 48) );  // convert count to ASCII and transmit it
	_delay_ms(500);		
    }
}

/* These functions are copied from the ATmega328P user Manual (20.5, 20.6.1) */

void USART_Init()
{
	/* Set baud rate */
	UBRR0H = 0;      
	UBRR0L = 16;     // UBRR0 = (F_CPU/( 8 * 115200 )) - 1 = 16.36
	/* set double speed mode */
	UCSR0A |= (1<<U2X0);
	/* Enable receiver and transmitter */
	UCSR0B = (1<<RXEN0)|(1<<TXEN0);
	/* Set frame format: 8data, 2stop bit */
	UCSR0C = (1<<USBS0)|(3<<UCSZ00);
}


void USART_Transmit( unsigned char data )
{
	/* Wait for empty transmit buffer */
	while ( !( UCSR0A & (1<<UDRE0)) );

	/* Put data into buffer, sends the data */
	UDR0 = data;
}
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…