BM-305 Mikrodenetleyiciler Güz 2015 (4. Sunu) (Yrd. Doç. Dr. Deniz Dal)

Slides:



Advertisements
Similar presentations
MICROPROCESSORS AND MICROCONTROLLERS
Advertisements

EMS1EP Lecture 4 Intro to Programming Dr. Robert Ross.
Introduction to Micro-controllers Anurag Dwivedi.
INSTRUCTION SET ARCHITECTURES
Bit Manipulation. Binary Numbers Base 10 numbers are represented by sum of digits times powers of 10. For example: 234 = 2* * *10 0 Binary.
MICRO-CONTROLLER: A microcontroller is the brain of the robot: These are: 1. Integrated Circuits (ICs) 2. Programmable.
Programming Microcontrollers B. Furman 19MAR2011.
©Brooks/Cole, 2003 Chapter 4 Operations on Bits. ©Brooks/Cole, 2003 Apply arithmetic operations on bits when the integer is represented in two’s complement.
 | bit OR & bit AND ~ bit NOT ^ bit EXLUSIVE OR (XOR) > bit RIGHT SHIFT.
Programming the ATmega16
Assembly Language and Computer Architecture Using C++ and Java
Assembly Language and Computer Architecture Using C++ and Java
24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05.
Railway Foundation Electronic, Electrical and Processor Engineering.
AVR Programming CS-212 Dick Steflik. ATmega328P I/O for our labs To get data into and out of our Arduino its a little trickier than using printf and.
Getting the O in I/O to work on a typical microcontroller Activating a FLASH memory “output line” Part 1 Main part of Laboratory 1 Also needed for “voice.
Timers and Interrupts Shivendu Bhushan Summer Camp ‘13.
JavaScript, Third Edition
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
Railway Foundation Electronic, Electrical and Processor Engineering.
Timers and Interrupts Shivendu Bhushan Sonu Agarwal.
Working with Arduino: Lesson #1: Getting Acquainted with the Kit EGN1007.
Lecture 18 Last Lecture Today’s Topic Instruction formats
Atmega32 Architectural Overview
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 1: Basic Concepts (c) Pearson Education, All rights reserved. You may modify.
328eForth for Arduino Uno C. H. Ting February 16, 2012 SVFIG.
AVR Microcontrollers.
I/O Ports CS-280 Dr. Mark L. Hornick 1. CS-280 Dr. Mark L. Hornick 2 Ports are channels from the CPU to external hardware and software Atmega32 has: 4.
CSU0014 Assembly Languages Homepage: Textbook: Kip R. Irvine, Assembly Language for Intel-Based Computers,
Summer 2014 Chapter 1: Basic Concepts. Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, Chapter Overview Welcome to Assembly Language.
Assembly Language for x86 Processors 7th Edition
1 Homework Turn in HW2 tonight HW3 is on-line already Questions?
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
C Operators. CONTENTS CONDITIONAL OPERATOR SIMPLE ASSIGNMENT OPERATOR COMPOUND ASSIGNMENT OPERATOR BITWISE OPERATOR OPERATOR PRECEDENCE.
Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.
Atmel Atmega128 Overview ALU Particulars RISC Architecture 133, Mostly single cycle instructions 2 Address instructions (opcode, Rs, Rd, offset) 32x8 Register.
ATtiny23131 A SEMINAR ON AVR MICROCONTROLLER ATtiny2313.
Timers and Interrupts Anurag Dwivedi. Let Us Revise.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
CSE 351 Number Representation & Operators Section 2 October 8, 2015.
LHO 22 C and the  The Silicon Labs ISE uses the Keil C51 compiler.  The code size is limiter to 2K  C has replaced PL/M (the original Intel high.
Microcontrollers, Microcomputers, and Microprocessors
What is a program? A sequence of steps
Arduino Mega Arduino Mega 2560 Arduino Mega 2560.
CSE 351 Number Representation. Number Bases Any numerical value can be represented as a linear combination of powers of n, where n is an integer greater.
MICROCONTROLLER INTERFACING WITH STEPPER MOTOR MADE BY: Pruthvirajsinh Jadeja ( ) COLLEGE:DIET BRANCH:EC.
AVR Architecture Prepared By: Avdhesh Soni ( ) Sarthak Patel ( ) Akshay Parekh ( ) Fenil Sachla ( ) Guided.
NAM S.B MDLAB. Electronic Engineering, Kangwon National University 1.
Modified Harvard architecture 8-bit RISC single chip microcontroller Complete System-on-a-chip On Board Memory (FLASH, SRAM & EEPROM) On Board Peripherals.
Programming in Arduino Materials:Arduino Board Casperelectronics Pre Pres. Notes Photos from workshop?
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Vadodara Institute of Engineering Subject: Microcontroller interfacing Prepared by: Bipin Malani ( ) Vishwa Pandit ( ) Branch : Electronics.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
Binary Addition The simplest arithmetic operation in binary is addition. Adding two single-digit binary numbers is relatively simple, using a form of carrying:
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
“Atmega32 Architectural Overview” SIGMA INSTITUTE OF ENGINEERING Prepared By: SR.NO NAME OF STUDENT ENROLLMENT 1 Parihar Shipra A Guided By:-
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
BM-305 Mikrodenetleyiciler Güz 2016 (4. Sunu)
Atmega32 Architectural Overview
AVR ATMEGA 8 MICRO-CONTROLLER
UNIT – Microcontroller.
The Arduino Microcontroller: Atmel AVR Atmega 328
Introduction to Microprocessors and Microcontrollers
Working with Arduino: Lesson #1: Getting Acquainted with the Kit
AVR Programming in C Chapter 7
(Dr. Öğr. Üyesi Deniz Dal)
Number Representation & Operators
Presentation transcript:

BM-305 Mikrodenetleyiciler Güz 2015 (4. Sunu) (Yrd. Doç. Dr. Deniz Dal)

Mikrodenetleyici/Mikrokontrolör ANALOG INPUTS

Atmel AVR ATMega328P Mikrodenetleyicisi The ATmega328 is a single chip microcontroller created by Atmel and belongs to the megaAVR series. 8-bit AVR RISC-based microcontroller, 32 KB flash memory with read-while-write capabilities, 2 KB SRAM, 1 KB EEPROM, 23 general purpose I/O pins, 32 general purpose registers, three flexible timers/counters with compare modes, internal and external interrupts, serial programmable USART, SPI serial port, 6-channel 10-bit A/D converter, programmable watchdog timer with internal oscillator

Atmel AVR ATMega328P Mikrodenetleyicisi

Atmel AVR ATMega328P Mikrodenetleyicisinin Pinout Diyagramı

Atmel AVR ATMega328P Mikrodenetleyicisi nin Blok Diyagramı

AVR CPU

Atmel AVR ATMega328P Mikrodenetleyicisinin Bellekleri ( Flash Memory (32 KB) Flash memory, or just flash, is erasable, non-volatile storage used for program storage. When you have compiled a program and you upload it to the device, this is where it’s stored. When your program runs, it is run directly from the flash memory (it is not copied into RAM as most PCs would do). SRAM Memory (2 KB) Static RAM (SRAM) is used for transient program state (such as variables) as well as the program stack and any allocations made by the program. When the program starts, all global variables are initialized in SRAM by a special routine generated automatically by the compiler. EEPROM Memory (1 KB) EEPROM (electrically erasable, programmable, read-only memory) is used for non-volatile persistent storage. It is a good place to write configuration values (such as baud rates, unique IDs, etc.), to keep track of counters over a long term, and to keep static data that isn’t needed frequently.

Atmel AVR ATMega328P Mikrodenetleyicisinin Zamanlayıcıları/Sayıcıları (Timers/Counters) 8 Bit Timer/Counter 0 Timer0 is an 8-bit timer, meaning its counter register can record a maximum value of 255 (the same as an unsigned 8-bit byte). Timer0 is used by native Arduino timing functions such as delay() and millis(). 16 Bit T/C 1 Timer1 is a 16-bit timer, with a maximum counter value of (an unsigned 16- bit integer). The Arduino Servo library uses this timer. 8 Bit T/C 2 Timer2 is an 8-bit timer that is very similar to Timer0. It is used by the Arduino tone() function.

Atmel AVR ATMega328P Mikrodenetleyicisinin Bekçi Köpeği/Koruma Zamanlayıcısı (Watchdog Timer) This device monitors CPU activity and sends reset signals on a periodic basis. This feature becomes useful when the microcontroller is employed in embedded application: if some unexpected situation leads the CPU to get stuck in an infinite cycle, after a short period the watchdog will issue a reset signal so the CPU can recover and resume normal operation.

Atmel AVR ATMega328P Mikrodenetleyicisinin Genel Amaçlı I/O Portları A port is a group of 8 pins or set of pins used for exchanging data with the external World. The Atmel AVR ATMega328P microcontroller has three ports: PORT B (Digital pins 8 to 13) PORT C (Analog input pins) PORT D (Digital pins 0 to 7)

Atmel AVR ATMega328P Mikrodenetleyicisinin Port Saklayıcıları (Registers) 8-bit port registers allow for lower-level and faster manipulation of the I/O pins of the microcontroller on an Arduino board. (Each bit of a register corresponds to a particular pin.) Each port (B, C and D) is controlled by three registers, which are also defined variables in the Arduino language. The DDR (Data Direction Register) determines whether the pin is an INPUT (0) or OUTPUT (1). The PORT register controls whether the pin is LOW (0) or HIGH(1). The PIN register reads the state of INPUT pins set to input with pinMode().

Atmel AVR ATMega328P Mikrodenetleyicisinin Port Saklayıcıları (Registers) PORT D MAPS TO ARDUINO DIGITAL PINS 0 TO 7 DDRD - The PORT D Data Direction Register - read/write PORTD - The PORT D Data Register - read/write PIND - The PORT D Input Pins Register - read only PORT B MAPS TO ARDUINO DIGITAL PINS 8 TO 13 DDRB - The PORT B Data Direction Register - read/write PORTB - The PORT B Data Register - read/write PINB - The PORT B Input Pins Register - read only PORT C MAPS TO ARDUINO ANALOG PINS 0 TO 5 DDRC - The PORT C Data Direction Register - read/write PORTC - The PORT C Data Register - read/write PINC - The PORT C Input Pins Register - read only Each bit of these registers corresponds to a single pin; e.g. the low bit of DDRB, PORTB, and PINB refers to pin PB0. (Digital pin 8) If the low bit of DDRB is 0, Pin 8 is INPUT. Similarly, if the low bit of DDRB is 1, Pin 8 is OUTPUT.

Bitwise AND The bitwise AND operator in C++ is a single ampersand, &, used between two other integer expressions. Bitwise AND operates on each bit position of the surrounding expressions independently, according to this rule: if both input bits are 1, the resulting output is 1, otherwise the output is 0. Another way of expressing this is: 0 & 0 == 0 0 & 1 == 0 1 & 0 == 0 1 & 1 == 1 In Arduino, the type int is a 16-bit value, so using & between two int expressions causes 16 simultaneous AND operations to occur. In a code fragment like: int a = 92; // in binary: int b = 101; // in binary: int c = a & b; // result: , or 68 in decimal. Each of the 16 bits in a and b are processed by using the bitwise AND, and all 16 resulting bits are stored in c, resulting in the value in binary, which is 68 in decimal.

Bitwise AND One of the most common uses of bitwise AND is to select a particular bit (or bits) from an integer value, often called masking. For example, if you wanted to access the least significant bit in a variable x, and store the bit in another variable y, you could use the following code: int x = 5; // binary: 101 int y = x & 1; // now y is 1 x = 4; // binary: 100 y = x & 1; // now y is 0 Bir sayının tek mi çift mi olduğu bu test ile öğrenilebilir.

Bitwise OR The bitwise OR operator in C++ is the vertical bar symbol, |. Like the & operator, | operates independently each bit in its two surrounding integer expressions, but what it does is different (of course). The bitwise OR of two bits is 1 if either or both of the input bits is 1, otherwise it is 0. In other words: 0 | 0 == 0 0 | 1 == 1 1 | 0 == 1 1 | 1 == 1 Here is an example of the bitwise OR used in a snippet of C++ code: int a = 92; // in binary: int b = 101; // in binary: int c = a | b; // result: , or 125 in decimal. Bitwise OR is often used to make sure that a given bit is turned on (set to 1) in a given expression. For example, to copy the bits from a into b, while making sure the lowest bit is set to 1, use the following code: b = a | 1;

Bitwise XOR There is a somewhat unusual operator in C++ called bitwise exclusive OR, also known as bitwise XOR. (In English this is usually pronounced "eks-or".) The bitwise XOR operator is written using the caret symbol ^. This operator is similar to the bitwise OR operator |, except that it evaluates to 1 for a given position when exactly one of the input bits for that position is 1. If both are 0 or both are 1, the XOR operator evaluates to 0: 0 ^ 0 == 0 0 ^ 1 == 1 1 ^ 0 == 1 1 ^ 1 == 0 Another way to look at bitwise XOR is that each bit in the result is a 1 if the input bits are different, or 0 if they are the same. Here is a simple code example: int x = 12; // binary: 1100 int y = 10; // binary: 1010 int z = x ^ y; // binary: 0110, or decimal 6 The ^ operator is often used to toggle (i.e. change from 0 to 1, or 1 to 0) some of the bits in an integer expression while leaving others alone. For example: y = x ^ 1; // toggle the lowest bit in x, and store the result in y.

Bitwise NOT The bitwise NOT operator in C++ is the tilde character ~. Unlike & and |, the bitwise NOT operator is applied to a single operand to its right. Bitwise NOT changes each bit to its opposite: 0 becomes 1, and 1 becomes 0. For example: int a = 103; // binary: int b = ~a; // binary: = -104 You might be surprised to see a negative number like -104 as the result of this operation. This is because the highest bit in an int variable is the so-called sign bit. If the highest bit is 1, the number is interpreted as negative. This encoding of positive and negative numbers is referred to as two's complement. As an aside, it is interesting to note that for any integer x, ~x is the same as -x-1.

Bitshift Operators (Left Shift and Right Shift) There are two bit shift operators in C++: the left shift operator >. These operators cause the bits in the left operand to be shifted left or right by the number of positions specified by the right operand. For example: int a = 5; // binary: int b = a << 3; // binary: , or 40 in decimal int c = b >> 3; // binary: , or back to 5 like we started with When you shift a value x by y bits (x << y), the leftmost y bits in x are lost, literally shifted out of existence: int a = 5; // binary: int b = a << 14; // binary: the first 1 in 101 was discarded

Bitshift Operators (Left Shift and Right Shift) If you are certain that none of the ones in a value are being shifted into oblivion, a simple way to think of the left-shift operator is that it multiplies the left operand by 2 raised to the right operand power. For example, to generate powers of 2, the following expressions can be employed: 1 << 0 == 1 1 << 1 == 2 1 << 2 == 4 1 << 3 == << 8 == << 9 == << 10 ==

Bitshift Operators (Left Shift and Right Shift) When you shift x right by y bits (x >> y), and the highest bit in x is a 1, the behavior depends on the exact data type of x. If x is of type int, the highest bit is the sign bit, determining whether x is negative or not, as we have discussed above. In that case, the sign bit is copied into lower bits, for esoteric historical reasons: int x = -16; // binary: int y = x >> 3; // binary: This behavior, called sign extension, is often not the behavior you want. Instead, you may wish zeros to be shifted in from the left. It turns out that the right shift rules are different for unsigned int expressions, so you can use a typecast to suppress ones being copied from the left: int x = -16; // binary: int y = unsigned(x) >> 3; // binary: If you are careful to avoid sign extension, you can use the right-shift operator >> as a way to divide by powers of 2. For example: int x = 1000; int y = x >> 3; // integer division of 1000 by 8, causing y = 125.

Shorthand (Compound) Assignment Operators Often in programming, you want to operate on the value of a variable x and store the modified value back into x. In most programming languages, for example, you can increase the value of a variable x by 7 using the following code: x = x + 7; // increase x by 7 Because this kind of thing occurs so frequently in programming, C++ provides a shorthand notation in the form of specialized assignment operators. The above code fragment can be written more concisely as: x += 7; // increase x by 7 It turns out that bitwise AND, bitwise OR, left shift, and right shift, all have shorthand assignment operators. Here is an example: int x = 1; // binary: x <<= 3; // binary: x |= 3; // binary: because 3 is 11 in binary x &= 1; // binary: x ^= 4; // binary: toggle using binary mask 100 x ^= 4; // binary: toggle with mask 100 again There is no shorthand assignment operator for the bitwise NOT operator ~; if you want to toggle all the bits in x, you need to do this: x = ~x; // toggle all bits in x and store back in x

Bitwise Operators vs Boolean Operators It is very easy to confuse the bitwise operators in C++ with the boolean operators. For instance, the bitwise AND operator & is not the same as the boolean AND operator &&, for the following reason: They don't calculate numbers the same way. Bitwise & operates independently on each bit in its operands, whereas && converts both of its operands to a boolean value (true==1 or false==0), then returns either a single true or false value. For example, 4 & 2 == 0, because 4 is 100 in binary and 2 is 010 in binary, and none of the bits are 1 in both integers. However, 4 && 2 == true, and true numerically is equal to 1. This is because 4 is not 0, and 2 is not 0, so both are considered as boolean true values.

Bit Math Tutorial

Merhaba Dünya (AVR C Versiyonu) #include int main() { DDRB |= (1<<5); //Define digital pin13/PORTB5 as an output so we can blink our led while(1)//for(;;)//Infinite loop { PORTB |= (1<<5 );//Turn led on (the led included in the Arduino (digital pin 13)) _delay_ms(1000);//Wait 1 second PORTB &= ~(1<<5);//Turn led off _delay_ms(1000);//Wait another second } return 0; } PORTB ^= (1<<5); deyimi de kullanılabilir mi? Neresi setup, neresi loop? Arduino IDE bu kodu derleyebilir mi? PORTB &= 0xDF; deyimi de kullanılabilir mi? PORTB |= 0x20; veya PORTB |= 0b deyimi de kullanılabilir mi? Herhangi bir giriş pininin değeri PINX ile nasıl öğrenilebilir?