• Blog
Picture

It's the Simple Things

Some basics about Arduino an Microcontroller

7/13/2017

0 Comments

 
Picture
Some basics about Arduino an Microcontroller:


What is Arduino:


Arduino is an opensource electronic platform providing convenient access to a chip/microcontroller (MCU).
It also comes with an IDE and a language to program the chip and gets access to its input ouput.


What is a microcontroleur ?


It's a small and low cost computer on a chip that execute one task at the time.


What is a GPIO ?


GPIO = general purpose input ouput pin.
In Input mode you can read 1 or 0 from the pin.
In Output mode you an write 1 or 0 from the pin.


Gpio are grouped on PORT.


To go deeper:


Usually there GPIO port are associated with 3 registers in the the MCU:


Direction Port Register: Do I have an input or output pin
Input Value Register: Values of my inputs pins
Output Value Register: Value to write my output values.


For example I could have PORTB that controls 8 GPIOs.
The first 4 in Input Mode, the last 4 in Output mode.
I read 0 fo the first to pin, 1 for pins 2 and 4 , and write 1 for pin 5 and 7 and 0 for pins 6 and 8.
I would have my Port like:


DPR: 00001111
IVR: 00000011
OVR:01010000


Often address of registers are given for MCU.




My first simple and easy Arduino program:


We are going to switch on a led by pressing a button.


I need:
  • An Arduino
  • Wires
  • a Led
  • A button
  • a resistor
Install Arduino IDE from their website.
Plug your Arduino on your computer, drivers should installs by themselves (on windows).
Open the IDE and set the COM port .


Schema :


​












​


Code:


Arduino language in based on C and provide convenient method to interact with your MCU.
It supports a lot of them !


The program is divided in 2 parts:
void setup(), to init your pins, variables.
Void loop(), to code the behavior.


You can set an Pin in Input ou Output mode with the pinMode(pinNumber) function.
We are using pin 13 in Output mode ( switch on/off the light) anf pin 4 in Input mode (read the value of the button).


So lets set this two behavior:


pinMode(13, OUTPUT);
pinMode(4,INPUT);


Then in the loop we are constantly reading the value of the pin 4 and depending on the return switch on or of the led.
To read a input you have the method digitalRead(pinNumber). And to write a value you have the method digitalWrite(pinNumber).


Let's code in loop :


int value = digitalRead(4);
if(value == HIGH){
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}


Et voila !


You can upload the code in the board by pressing the downloading Arrow.


To go further:


We could have played directly with registers to execute this programm.
Depending on the board, find the 3 port registers address of pin 13 and the one of pin 4. let's call this portA for pin 4 (port A contains pin 0 to 7) and B for pin 13 ( portB contains pin 8 to 15).


You would have write :
DPRA = 00000000 (all pin in Input 0)
DPRB = 00100000 (all pin in Input 0 except 13 in output 1)


Then you could read the value of register IVRA and write on OVRB


if(( IVRA & (1 << 4)) > 0 ){
OVRB |= (1 << 14);
}
else {
OVRB &= ^(1 << 14);
}

0 Comments



Leave a Reply.

    Author

    Cécile Thiebaut

    Archives

    July 2017
    March 2012

    Categories

    All
    DRIVER
    Gwt
    IOT
    Java

    RSS Feed

Powered by
  • Blog
✕