Micro Controller

  • Published on
    17-Dec-2015

  • View
    4

  • Download
    0

Embed Size (px)

DESCRIPTION

presentasi mekatronik kuliah microcontroller

Transcript

<p>MicrocontrollerMekatronika, STT MandalaIsi KuliahArsitektur Microcomputer Microcontrollers untuk aplikasi rekayasaApa itu microcontroller?Bagaimana menggunakan microcontrollers?Platform perangkat keras ArduinoProgramming the ArduinoBasic stepsDigital I/OAnalog I/O</p> <p>Sistem Mekatronika</p> <p>Arsitektur Mikrokomputer</p> <p>MicrocomputerMicroprocessor: chip VLSI dgn rangkaian digital yg mengerjakan aritmetika, logika, komunikasi dan kontrolALU: arithmetic logic unit, mengeksekusi fungsi matematika dalam binerInstruction decoder: interpretasi instruksi dari memory oleh control unit dan disimpan di instruction register. Setiap instruksi me merintahkan ALU untuk melakukan manipulasi bit seperti penambahan biner atau fungsi logika, dan disimpan di register. Hasil dari ALU juga disimpan dalam data register dan ditransfer ke memory oleh control unitBus: jalur komunikasi (jaringan saraf komputer)ROM : penyimpanan data secara permanenRAM: penyimpanan data selama program berjalanSRAM: static RAM : data dalam flip-flops selama memory ada dayaDRAM : dynamic RAM, penyimpanan capacitive harus di refreshed karena charge leakageEPROM: erasable programmable EEPROM : electrically EPROM</p> <p>MicrocomputerRISC : Reduced instruction-set computer, jika instruksi2 yg digunakan hanya sedikit maka disebut RISC microcomputerBahasa mesin: komunikasi ke dan dari microprocessor terjadi melaluiinput/output (I/O) yg terhubung ke bus. Dalam mekatronika : ADC, DAC D/D merupakan interface antara microcomputer dan switch, sensor dan aktuator Bahasa Assembly: bahasa yg dpt digunakan untuk memprogram Microprocessor, yg terdiri dari perintah2 dasar seperti ADD, MOVAssembler: mengkonversi bahasa assembly menjadi bahasa mesin sehingga microprocessor mengerti dan mengeksekusi instruksiHigh level language: program dengan bahasa tingkat tinggi : basic, C, dll</p> <p>MicrocontrollerMicrocontroller:Microprocessor dalam sebuah IC PIC-Atmel-Intel 8051dll</p> <p>Microcontroller?A small computer usually implemented on a single IC that contains a central processing unit (CPU), some memory, and peripheral devices such as counter/timers, analog-to-digital converters, serial communication hardware, etc.http://www.amazon.com/AVR-Pin-20MHz-32K-ATMega328/dp/B004G5AVS6ATmega328the brain of the Arduino</p> <p>Atmega 328 pin mapping</p> <p>Penggunaan MicrocontrollerCarPhoneToothbrushMicrowave ovenCopierTelevisionPC keyboardAppliances Platform ArduinoAtmel ATmega328 microcontroller14 digital I/O pins6 with PWM6 analog I/O pins32 kB (-2 kB)Flash memory2 kB RAM1 kB EEPROM16 MHz clock</p> <p>FTDIUSB chipDigital PinsAnalog PinsUSBjackMicrocontrollerpowerjackVoltageregulatorPwr/GND PinsICSPHeaderResetButtonPowerLEDPin 13 LEDRx + TxLEDshttp://arduino.cc/Programming the ArduinoAn arduino program == sketchMust have:setup()loop()setup()configures pin modes and registersloop()runs the main body of the program foreverlike while(1) {}Where is main() ?Arduino simplifies thingsDoes things for you/* Blink - turns on an LED for DELAY_ON msec, then off for DELAY_OFF msec, and repeats*/const byte ledPin = 13; // LED on digital pin 13const int DELAY_ON = 1000;const int DELAY_OFF = 1000;</p> <p>// setup() method runs once, when the sketch starts</p> <p>void setup(){ // initialize the digital pin as an output: pinMode(ledPin, OUTPUT); }</p> <p>// loop() method runs forever,// as long as the Arduino has power</p> <p>void loop() { digitalWrite(ledPin, HIGH); // set the LED on delay(DELAY_ON); // wait for DELAY_ON msec digitalWrite(ledPin, LOW); // set the LED off delay(DELAY_OFF); // wait for DELAY_OFF msec}This slide is meant as a quick overview of an Arduino sketch. Well unpack and describe the pieces in more detail later.</p> <p>Think of setup() as what you would do when you first get in a rental car or a car you have borrowed from a friend: adjust the mirrors, adjust the steering wheel, the seats, change the stereo channel settings, etc. The car can be driven by anyone, but everyone likes to customize it for their comfort.</p> <p>main(){ init(); setup(); while (1) loop();}</p> <p>And the reason that they do it this way is so it's guaranteed that init() gets called, which is critical for Arduino initialization. Using setup()A digital pin can either be an output or an input Outputyour program determines what the voltage on a pin is (either 0V (LOW or logic 0) or 5V (HIGH or logic 1)Information is sent outInputthe world outside the microcontroller determines the voltage applied to the pinInformation is taken inconst byte ledPin = 13; // LED on digital pin 13</p> <p>void setup(){ // initialize the digital pin as an output: pinMode(ledPin, OUTPUT); }where can you find out aboutthe commands, etc?http://arduino.cc/en/Reference/ExtendedpinMode()sets whether a pin is an input or an outputledPin byte constant assigned the value of 13OUTPUT is a macro defined constantWhich has the value 1INPUT is a macro ?Emphasize that the distinction between inputs and outputs. As an example, you might have the class envision that the front row of students in the class are digital pins (everyone else are internals elements of the microcontroller). Designate one student as an OUTPUT. Have the rest of the students (in unison) tell the OUTPUT student to raise his or her hand. (Have the designated OUTPUT follow the command!). Then have the rest of the class (in unison) tell the OUTPUT to put his or her hand down. Thus, the microcontroller has sent information out, and the pin designated as an OUTPUT has responded accordingly.</p> <p>Similarly designate one student in the front row as an INPUT. Have the rest of the class ask the INPUT student what the state of the room light is (is it on or off?). Have the INPUT student report to the rest of the class the state of the room light.</p> <p>Explain the declaration of ledPin as a const byte variable. Explain the advantage over #define ledPin 13</p> <p>Digital HIGH = = 5V (usually taken as logical 1) Digital LOW = = 0V (usually taken as logical 0)</p> <p>wiring.h in arduino/hardware/cores/arduino #DEFINEs OUTPUT as 0x1 and INPUT as 0x0</p> <p>Blinking the LED in loop()digitalWrite()Causes the voltage on the indicated pin to go HIGH (+5V) or LOW (0V)Note: must first configure the pin to be an outputTo make pin go to 5V (high):digitalWrite(pin_num,HIGH);Best to #define pin num.To make pin go to 0V (low):digitalWrite(pin_num,LOW);delay()Causes the program to wait for a specified time in milliseconds#define LED_PIN 13 // LED on digital pin 13#define DELAY_ON 500 // in ms#define DELAY_OFF 100</p> <p>void setup(){ // initialize the digital pin as an output: pinMode(LED_PIN, OUTPUT); }void loop() { digitalWrite(LED_PIN, HIGH); // turn LED on delay(DELAY_ON); // wait for DELAY_ON ms digitalWrite(LED_PIN, LOW); // turn LED off delay(DELAY_OFF); // wait for DELAY_OFF ms}http://arduino.cc/en/Reference/Extendedwiring.h in arduino/hardware/cores/arduino #DEFINEs OUTPUT as 0x1</p> <p>Pull-up Resistor ConceptATmega328PD3VTG= +5V01ATmega328PD3VTG= +5V01Pull-up resistor OFFPull-up resistor ONPull-up resistorCode to Set Up Button PinsTwo steps:Make the pin an INPUTpinMode()Turn the pull-up resistor ondigitalWrite() a 1 to the pin</p> <p>const byte SW0 = 12; // button SW0const byte SW1 = 8; // button SW1const byte SW2 = 7; // button SW2const byte SW3 = 4; // button SW3</p> <p>void setup() { pinMode(SW0, INPUT); // make SW0 an INPUT digitalWrite(SW0, HIGH); // turn on pullup resistor</p> <p>etc.}(See full_test.pde for a more elegant approach to setting up button pins)Digital I/O Example - Problem StatementWrite a program to turn on the blue of the RGB LED (connected to digital pin 6) when SW0 is pressed (off otherwise)Pseudocode:define pin assignmentsconfigure pins (which are input, which are output)loop foreverif SW0 button is pressedmake pin 6 highelsemake pin 6 lowDigital I/O Example - Pin Assignment and ConfigurationRefine the pseudocode:define pin assignmentsconst byte RGB_blue_pin = 6;const byte SW0_pin = 12;configure pins (in function setup())RGB_blue_pinmake it an _______SW0_pinmake it an ______turn on pull-up resistor on SW0 pinpin will read high (1) until pulled low (0)see schematicvoid setup() { pinMode(RGB_blue_pin, OUTPUT); pinMode(SW0_pin, INPUT); digitalWrite(SW0_pin, HIGH);}OUTPUTINPUT</p> <p>Be careful. This program relies on the mode for pin 6 starting out as an input and pullup resistor off. To be safe, you ought to have a digitalWrite(RGB_blue_pin, LOW);Digital I/O Example - loop() AlgorithmRefine the pseudocode, cont.:loop forever (use function loop())If button is not pressed:voltage on button pin 12 will be _______make pin 6 voltage low (LED will go off or stay off)If button is pressed:voltage on button pin 12 will be _______make pin 6 voltage high (LED will go on or stay on)void loop() { if(digitalRead(SW0_pin) == LOW) { digitalWrite(RGB_blue_pin, HIGH); } else { digitalWrite(RGB_blue_pin, LOW); }}high (5V)low (0V)</p> <p>Digital I/O Example - Arduino ProgramArduino programSuppose a change to the specifications:LED is on until button pressed, then offContrast mechatronic approach vs. non-mechatronicre-wire, orre-programthe mechatronics approach separates the sensing elements from the control elements/* Blue_LED_button_cntrl1 - turns on blue LED when SW0 on Experimenter board is pressed, off otherwise*/</p> <p>/* pin assignments */const byte RGB_blue_pin = 6;const byte SW0_pin = 12;</p> <p>/* configure pins */void setup() { pinMode(RGB_blue_pin, OUTPUT); pinMode(SW0_pin, INPUT); digitalWrite(SW0_pin, HIGH);}</p> <p>/* loop forever */void loop(){ if(digitalRead(SW0_pin) == LOW) digitalWrite(RGB_blue_pin, HIGH); else digitalWrite(RGB_blue_pin, LOW);}This example very simple. A microcontroller is certainly not needed to implement the function of turning on an LED when a button is pressed. This functionality could be achieved much more easily and less expensively by directly wiring the switch to the LED (through a current limiting resistor, of course!). However, a small change in requirements, like that given above, might lead to time consuming and expensive efforts to meet the new functionality using the non-mechatronic approach. The mechatronic approach may be faster and less expensive to implement. It also lends itself to greatly enhanced flexibility and functionality. How about implementing a delay? Or having the LED go on only after the button has been pressed twice or twice within a given time window, etc.</p> <p>Toyota problem might be fixed by modifying the software: if brake pedal AND accelerator are pressed at the same time, the brake has priority or the maximum speed is limited to X mi/hr./* Blue_LED_button_cntrl1 - turns on blue LED when SW0 on Experimenter board is pressed, off otherwise*/</p> <p>/* pin assignments */const byte RGB_blue_pin = 6;const byte SW0_pin = 12;</p> <p>/* configure pins */void setup() { pinMode(RGB_blue_pin, OUTPUT); pinMode(SW0_pin, INPUT); digitalWrite(SW0_pin, HIGH);}</p> <p>/* loop forever */void loop(){ if(digitalRead(SW0_pin) == LOW) digitalWrite(RGB_blue_pin, HIGH); else digitalWrite(RGB_blue_pin, LOW);}Digital I/O Example - ModificationModify Arduino program, so that LED is on until button is pressed, then turns offHow?Pin assignments?setup()?Need to turn on the LED!loop()?Swap values of second argument in digitalWrite callsPin assignments stay the same - no change to the external hardware or its connection to the Arduinosetup() - add a line to turn on the LEDloop() - swap the arguments in the digitalWrite() function calls: if button is pressed, then LOW, else, HIGHComparison of Digital I/O Programs/* Blue_LED_button_cntrl1 - turns on blue LED when SW0 on Experimenter board is pressed, off otherwise */</p> <p>/* pin assignments */const byte RGB_blue_pin = 6;const byte SW0_pin = 12;</p> <p>/* configure pins */void setup() { pinMode(RGB_blue_pin, OUTPUT); pinMode(SW0_pin, INPUT); digitalWrite(SW0_pin, HIGH);}</p> <p>/* loop forever */void loop(){ if(digitalRead(SW0_pin) == LOW) digitalWrite(RGB_blue_pin, HIGH); else digitalWrite(RGB_blue_pin, LOW);}/* Blue_LED_button_cntrl2 - turns off blue LED when SW0 on Experimenter board is pressed, on otherwise */</p> <p>/* pin assignments */const byte RGB_blue_pin = 6;const byte SW0_pin = 12;</p> <p>/* configure pins */void setup() { pinMode(RGB_blue_pin, OUTPUT); pinMode(SW0_pin, INPUT); digitalWrite(SW0_pin, HIGH); digitalWrite(RGB_blue_pin, HIGH);}</p> <p>/* loop forever */void loop(){ if(digitalRead(SW0_pin) == LOW) digitalWrite(RGB_blue_pin, LOW); else digitalWrite(RGB_blue_pin, HIGH);}Analog In with Serial OutRead the POTNote: analog voltage!0 V 05 V 1023Blink an LED at a rate proportional to the pot voltageOutput the pot voltage to the serial monitorInitialize with Serial.begin()Map voltage to delayWrite a line with Serial.print or Serial.println#define MAX_DELAY_TIME 1000 // max delay in ms#define MIN_DELAY_TIME 10 // min delay in ms#define MAX_POT_VALUE 855 // max pot reading#define MIN_POT_VALUE 0 // min pot reading</p> <p>const byte potPin = 1; // pot output on pin 1const byte ledPin = 6; // blue LED on pin 6unsigned int potVoltage = 0; // value of pot voltageunsigned int delay_ms;void setup() { pinMode(ledPin, OUTPUT); pinMode(potPin, INPUT); Serial.begin(9600); // init serial comm at 9600 bps}void loop() { potVoltage = analogRead(potPin); // read pot delay_ms = map(potVoltage,MIN_POT_VALUE,MAX_POT_VALUE,MIN_DELAY_TIME,MAX_DELAY_TIME); Serial.print("sensor = " ); // print to monitor Serial.print(potVoltage); Serial.print(" delay, ms = " ); Serial.println(delay_ms); // print delay and linefeed digitalWrite(ledPin, HIGH); // turn the LED on delay(delay_ms); // wait for delay_ms digitalWrite(ledPin, LOW); // turn the LED off: delay(delay_ms); // wait for delay_ms }POT_input_Serial_Out.pdeSo far, weve been dealing with digital I/O: just on or off. Now we want to work with a sensor that gives an output signal that can vary in a continuous (non-discrete) way over 0V to 5V.</p> <p>The analog pins can be used as digital pins if desired.</p> <p>Effect of Using delay()Leads to poor (slow) performance as delay time increasesTry to avoid long delaysUse millis() instead Check for time exceeding millis() + delay_timeEx. POT_in_Serial_Out.pdeNote also the use of #ifdef for conditional compilationNote how roll-over of millis() is handledTry to avoid using delay() for relatively long delays.</p> <p>Note also the use of #define to replace magic numbers and for conditional compilation.</p> <p>Analog Out (PWM) ConceptNo facility exists on most microcontrollers to directly output an a...</p>