Documente Academic
Documente Profesional
Documente Cultură
- Arduino, https://www.arduino.cc/
- Frizing, http://fritzing.org/home/
In acesta secţiune se prezintă IDE-ul MPLABX, folosit în cadrul lucrărilor de laborator. Este realizat de Microchip,
pentru dezvoltarea de aplicaţii cu microcontrolerele.
1. După instalarea programului se lansează mediul de dezvoltare MPLABX IDE 2, se dezvolta un proiect nou,
urmând paşii ilustratii in figurile urmatoare.
2. Se selectează microcontrolerul care va fi utilizat – în cazul de faţă PIC16F887 (pe 8 biţi)
Se editează în fisierul main.c programul ce va rezolva o aplicatie, astfel acesta va fi inclus în proiectul deja
creat anterior. Un prim exemplu de aplicatie software este prezentat în continuare:
/******************************************************************************/
/* Files to Include */
/******************************************************************************/
#if defined(__XC)
#include <xc.h> /* XC8 General Include File */
#elif defined(HI_TECH_C)
#include <htc.h> /* HiTech General Include File */
#endif
/******************************************************************************/
/* User Global Variable Declaration */
/******************************************************************************/
void init_porturi(void);
void init_porturi()
{
// dezactiveaza functia "analog input" pentru toti pinii care au aceasta functie
ANSEL = ANSELH = 0;
/******************************************************************************/
/* Main Program */
/******************************************************************************/
void main(void)
{
unsigned long k; // contor pentru asteptare
// Initializeaza porturile
init_porturi();
// Asteapta un timp
for (k = 0; k < 10000L; k++)
{}
// Asteapta un timp
for (k = 0; k < 10000L; k++)
{}
}
}
La pasul următor acesta va fi compilat si link-editat, prin apăsarea butonului „Build project”.
Fig. 11
Librăria cu componente se deschide prin selectarea pictogramei „Pick parts from libraries”, sau prin apasarea
butonul identificat prin litera „P”.
Asftel se deschide fereastra intitulată „Pick Devices”, în care se regăsesc pe categorii si subcategorii componente
utile în realizarea aplicatiilor.
Selectie componente
In contiunuare se prezinta un exemplu demonstrativ care foloseste MCU Microchip.
Exercitiu
În fereastra cu componente Pick Devices se alege microcontrolerul, în acest exemplu s-a ales pic16f887 – figura 12.a.
La MCU se specifică potenţialele pinilor de alimentare (fig.14) şi calea către programul compilat (fişierul de ieşire
*.hex) în cadrul proiectului MPLAB realizat în prima parte.
Se vor adăuga şi restul de elemente necesare pentru realizarea schemei electronice: rezistoare, leduri, alimentare,
masa. Pentru rezistenţele de conectare a pinilor de intrare la + sau la GND, se alege valoarea uzuală, 10kOhm.
Fig. 13
Valoarea rezistentei de limitare a curentului prin diodă se alege considerând un curent maxim de 20mA, şi căderea
de tensiune în conducţie pe diodă de 2.2V.
Fig. 14
4. Prezentarea mediului Arduino
Folosirea serialei
Selectare exemple
demonstrative
Modalitatea de a cauta informatii
despre functii predefinite
Cautarea librariei
Keypad, de inclus in
IDE-ul Arduino
Zona “setup”
Zona “loop”
Comenzi uzuale
Fig. From the source code to the binary executable code
Increment and decrement operators
Comparison expressions
if and else conditional structure
int a;
int b;
int ledState;
if (a < b) {
ledState = 1;
}
else {
ledState = 0;
}
*****************************************************
If (expression) {
// code executed only if expression is true
}
else {
// code executed only if expression is false
}
case label:
// statements
break;
default:
// statements
}
**********************************************************
float midiCCMessage;
switch (midiCCMessage) {
case 7:
changeVolume();
break;
case 10:
changePan();
break;
default:
LedOff();
}
This code is equivalent to:
float midiCCMessage;
if (midiCCMessage == 7) changeVolume();
else if (midiCCMessage == 10) changePan ();
else LedOff();
The counter is also named index. I'm showing you a real example here:
int i = 0;
for ( ; i < 100 ; i++) {
println(i);
}
This is for the first index. The second one is totally similar:
This i grows from 0 to threshold – 1 then goes back to 0, grows again, infinitely, taking benefits of loop().
There are also other ways to run loops infinitely in a controlled manner.
Creating function prototypes using the Arduino IDE
Let's take an easy example, we want to create a function that sums two integers and produces/returns the
result. There are two arguments that are integer type variables. In this case, the result of the addition of
these two int (integer) values is also an int value. It doesn't have to be, but for this example it is. The
prototype in that case would be:
int mySum(int m, int n);
At last, return result; is the statement that makes the function call resulting into a value. Let's check an
actual example of the Arduino code to understand this better:
void setup() {
Serial.begin(9600); // Let's check an actual example of Arduino code to understand
this better.
}
Void loop() {
// let's sum all integers from 0 to 99, 2 by 2 and display
int currentResult;
for (int i = 0 ; i < 100 ; i++)
{
currentResult = mySum(i,i+1); // sum and store
Serial.println(currentResult); // display to serial monitor
}
delay(2000); make a 2 second pause
}
***********
Now here is a diagram of the firmware execution itself, which is the part that we will work with, in
the next rows:
Fig The firmware life cycle with the main part looping
Accepting the fact that when setup() stops, the loop() function begins to loop, everything in loop() is
continuous. Now look at the same things when delays happen:
Fig. The firmware life cycle with the main part looping and breaking when delay() is called
The whole program breaks when delay() is called. The length of the break depends on the parameter
passed to delay().
Concepts in dealing with that only-one-task property that can feel like a limitation:
• Thread
• Interrupt handler (and subsequent interrupt service routine concept)
- another one: The polling. The polling is a special interrupt case from where we
will begin.
Polling concept – a special interrupt case
You know the poll term. I can summarize it as "ask, wait for an answer, and keep it somewhere". If I wanted
to create a code that reads inputs, and performs something when a particular condition would be verified
with the value of these inputs, I would write this pseudo-code:
setup()
- initialize things
loop()
The interrupt is a signal. When a particular event occurs, an interrupt message is sent to the processor.
Sometimes it is sent externally to the processor (hardware interrupt) and sometimes internally (software
interrupt). This is how the disk controller or any external peripheral informs the processor of the main unit
that it has to provide this or that at the right moment. The interrupt handler is a routine that handles the
interrupt by doing something. For instance, on the move of the mouse, the computer operating system
(commonly called the OS) has to redraw the cursor in another place. It would be crazy to let the processor
itself test each millisecond whether the mouse has moved, because the CPU would be running at 100 percent
utilization. It seems smarter to have a part of the hardware for that purpose. When the mouse movement
occurs, it sends an interrupt to the processor, and this later redraws the mouse. In the case of our installation
with a huge number of inputs and outputs, we can consider handling the user inputs with an interrupt. We
would have to implement what is called an Interrupt Service Routine (ISR), which is a routine called
only when a physical world event occurs, that is, when a sensor value changes or something like that.
Arduino now provides a nice way to attach an interrupt to a function and it is now easy to design an ISR
(even if we'll learn to do that a bit later). For instance, we can now react to the change of the value of an
analog thermal sensor using ISR. In this case, we won't permanently poll the analog input, but we'll let our
low-level Arduino part do that. Only when a value changes (rises or falls) depending on how we have
attached the interrupt, would this act as a trigger and a special function would execute (for instance, the
LCD display gets updated with the new value).
What is a thread?
A thread is a running program flow in which the processor executes a series of tasks, generally looping, but
not necessarily.
With only one processor, it is usually done by time-division multiplexing, which means the processor
switches between the different threads according to time, that is, context switching.
Fig. Time-division multiplexing provides multitasking
More advanced processors provide the multithread feature. These behave as if they would be more than
just one, each part dealing with a task at the same time.
Without going deeper into computer processors, as we aren't dealing with them right now, I can say threads
are nice techniques to use in programming to make tasks run simultaneously.
Unfortunately, the Arduino core doesn't provide multithreading, nor does any other microcontroller.
Because Arduino is an open source hardware project, some hackers have designed a variant of the Arduino
board and created some Freeduino variant providing concurrency, an open source programming language,
and an environment designed especially with multithreading in mind.
Exercitiul 1:
Pas 1. Cod de testat in Arduino IDE
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
}
2.2 Se extrage fisierul zippat descrcat si se copiaza cele doua fisier obtinute
(ARDUINO.IDX and ARDUINO.LIB) in PROTEUS
=> Setup directory: Labcenter\LIBRARY
Optiune selectabila
Optiune selectabila
pentru vizualizarea Optiune selectabila
pentru vizualizarea si
schemei electronice pentru dezvoltarea
configurarea PCB-ului
programului aplicatiei
Fereastra de
lucru, necesara
pentru
construirea Zona pentru
schemei parametrizarea
electronice elementelor
plasate in
fereastra de
void setup() {
// initializeaza fiecare pin ca output, intr-o bucla for:
for (int thisPin = 2; thisPin < 8; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}
void loop() {
// in ordine crescatoare a numerelor pinilor:
for (int thisPin = 2; thisPin < 8; thisPin++) {
// pune iesirea pe nivel High:
digitalWrite(thisPin, HIGH);
// asteapta un interval de timp
delay(timer);
// pune iesirea pe nivel Low:
digitalWrite(thisPin, LOW);
}
// in ordine descrescatoare a numerelor pinilor:
for (int thisPin = 7; thisPin >= 2; thisPin--) {
// pune iesirea pe nivel High:
digitalWrite(thisPin, HIGH);
// asteapta un interval de timp
delay(timer);
// pune iesirea pe nivel Low:
digitalWrite(thisPin, LOW);
}
//---------------------------------------------------------------------------------------------------
Exercitiu de rulat in Frizing
Recapitulare:
Catodul este marcat printr-o tesitura, la LED-urile rotunde, si în general este terminalul scurt.
Pentru modulul Arduino se vor face conexiunile la porturile digitale alese prin
program. Se ruleaza urmatorul program in Arduino IDE si se descarca pe kit-ul
Arduino.