C/C++ Code and Arduino Code: Sistemas Embebidos Oscar Acevedo, PHD

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

C/C++ Code and Arduino Code

Sistemas Embebidos
Oscar Acevedo, PhD
Introduction

• Each microcontroller has a native IDE provided by the manufacturer

• This software allows programming the microcontroller by means of a programming


language like C/C++
• This is the case for ATMEL Studio

• Arduino IDE is built upon an ATMEL C/C++ compiler (AVRdude)


Arduino IDE

• Arduino programming presents differences when compared to ATMEL studio


• It does not promote the use of traditional C/C++ structure

• An important difference is you have no direct access to the main function

• Arduino encourages the use of functions instead of direct register programming


Arduino vs. C/C++
C/C++ Arduino Code
// programa de prueba blink
/*
#define F_CPU 16000000UL // frecuencia XTAL Blink
*/
#include <avr/io.h>
#include <util/delay.h> void setup()
{
int main() // initialize digital pin LED_BUILTIN as an output.
{ pinMode(LED_BUILTIN, OUTPUT);
// inicializacion }
DDRC= 0B00100000; //PC5 salida, resto entrada
// the loop function runs over and over again forever
// ciclo infinito void loop()
while (1) {
{ digitalWrite(LED_BUILTIN, HIGH); // turn the LED on
PORTC= 0B00100000; //hacer PC5 1 delay(1000); // wait for a second
_delay_ms(1000); digitalWrite(LED_BUILTIN, LOW); // turn the LED off
PORTC= 0B00000000; //hacer PC5 0 delay(1000); // wait for a second
_delay_ms(1000); }
}

return 0;
}
Arduino vs. C/C++

• Because Arduino use an ATMEL compiler, you can move any C/C++ code to Arduino
• It may not be the same the other way around

• You can put libraries at the beginning, outside any function

• Initialization code goes into Arduino setup() function

• Any code inside the infinite loop goes into Arduino loop() function
Arduino vs. C/C++

Original C/C++ Arduino


// programa de prueba blink
// programa de prueba blink
#define F_CPU 16000000UL // frecuencia XTAL
#include <avr/io.h>
#include <avr/io.h>
#include <util/delay.h>
#include <util/delay.h>
void setup()
int main()
{
{
// inicialización
// inicializacion
DDRC= 0B00100000; //PC5 salida, resto entrada
DDRC= 0B00100000; //PC5 salida, resto entrada
}
// ciclo infinito
void loop()
while (1)
{
{
PORTC= 0B00100000; //hacer PC5 1
PORTC= 0B00100000; //hacer PC5 1
_delay_ms(1000);
_delay_ms(1000);
PORTC= 0B00000000; //hacer PC5 0
PORTC= 0B00000000; //hacer PC5 0
_delay_ms(1000);
_delay_ms(1000);
}
}

return 0;
}
Fin de la presentación

You might also like