Arduino Mega Arduino Mega 2560 Arduino Mega 2560.

Slides:



Advertisements
Similar presentations
MICROPROCESSORS AND MICROCONTROLLERS
Advertisements

The 8051 Microcontroller and Embedded Systems
Lab7: Introduction to Arduino
C Examples 1.
What is Arduino?  Arduino is a ATMEL 168 micro-controller kit designed specially for small projects  User friendly IDE(Integrated Development Environment)
Embedded System Spring, 2011 Lecture 9: I/O Programming Eng. Wazen M. Shbair.
MICRO-CONTROLLER: A microcontroller is the brain of the robot: These are: 1. Integrated Circuits (ICs) 2. Programmable.
Lab 3 General MIDI Explorer with Record/Playback
Programming Microcontrollers B. Furman 19MAR2011.
1 Lab2: A/D Converter. 2 This circuit connects a variable voltage to an A/D port on the AVR mcu. Your software running on the AVR mcu will read the digital.
 | bit OR & bit AND ~ bit NOT ^ bit EXLUSIVE OR (XOR) > bit RIGHT SHIFT.
Programming the ATmega16
Serial Peripheral Interface (SPI)
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.
Sensors And Micro-Controllers
An Embedded C Program 1 Mainly from textbook: Embedded C Programming and the Atmel AVR 2 nd Edition Barnett, Cox and O’Cull.
Embedded Programming and Robotics Lesson 2 C Programming Refresher C Programming1.
Robotics Research Laboratory Louisiana State University.
You must be knowing about Digital Integrated Circuits (ICs) right ? For example 7404: Hex Inverter 7408: Quad 2-input AND gate 7410: Triple 3-input NAND.
ARDUINO PROGRAMMING Working with the Arduino microcontroller.
 C is general-purpose structured programming language or high level language.  It was developed by Dennis Ritchie in 1970s at Bell laboratories. 
Embedded Systems Programming 1 ETEE 3285 Topic HW3: Coding, Compiling, Simulating.
1 EKT 225 MICROCONTROLLER I CHAPTER 3 I/O PORT PROGRAMMING.
UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)
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.
RS232 #use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7
Robotics Research Laboratory Louisiana State University.
Sparkfun Electronics ATtiny85 Arduino Quick Reference Sheet
Timers and Interrupts Anurag Dwivedi. Let Us Revise.
Warmup – 16FEB2012 This one is for practice. I have paper if you need it. Suppose there are eight, single-pole, single-throw (SPST) switches connected.
Robotics Research Laboratory Louisiana State University.
Input Interface – Microprocessor
I NTRODUCTION TO PIC PROGRAMMING By : S HERIF H ARHASH.
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.
Presented by Sadhish Prabhu
C Examples 1. Download Links dsPIC30F4011/4012 Data Sheet dsPIC30F4013/3014 dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference.
Microcontrollers, Microcomputers, and Microprocessors
Analog to Digital Converter (ADC)
Chapter Microcontroller
Mark Neil - Microprocessor Course 1 Decoding and Using a 4x4 Keyboard.
Bit Manipulation in 'C' 'C' bit operators
Embedded Systems Lecture 4 January 20, 2016.
Embedded systems and sensors 1 Part 2 Interaction technology Lennart Herlaar.
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.
Microcontroller basics Embedded systems for mortals.
Introduction to AVR Name : 1) Abhishek Yadav ) Prakash Giri ) Kheni Niral ) Bhadresh Langadiya Branch.
Programming PIC 16F84A in Assembly. PIC16F84 pin-out and required external components.
5 th sem electrical GUIDED BY: Prof. KETAN PATEL.
80C51 Block Diagram 1. 80C51 Memory Memory The data width is 8 bits Registers are 8 bits Addresses are 8 bits – i.e. addresses for only 256.
Sensing Algorithm using IR Sensor and ADC Soong-sil University. Robotics 기 정 두 원.
1 Lab 4: D/A Converter Lab 4: D/A Converter This is a simple resistive network for a D/A converter Port 1, Port 0 are digital inputs ==> 00 (minimum),
One more PIC18F252 example and revision for exam B222L Branislav Vuksanovic, UoP, ECE.
Microprocessors A practical approach..
Sitarambhai Naranjibhai Patel Institute of Technology and Research Centre, Umrakh, Bardoli. A Presentation On “ 16x2 LCD Interfacing with AVR atmega32.
1. PIC ADC  PIC18F877 has 8 analog input channels i.e. port A pins(RA0 to RA5) and port E pins(RE1 and RE2). These pins are used as Analog input pins.
INTERFACING HARDWARE WITH MICROCONTROLLER GUIDED BY: Prof. ASIF SIR 1. AKSHAY KIRAN 2. DEEP PARIKH 3. JIGAR PATEL 4. TILAK PATEL ,05,D2D06,09.
AVR ATMEGA 8 MICRO-CONTROLLER
COMP2121: Microprocessors and Interfacing
The Arduino Microcontroller: Atmel AVR Atmega 328
Session 3,4.
8051 Programming in C rhussin.
I/O Ports in AVR Sepehr Naimi
AVR Programming in C Chapter 7
AVR Programming in C Chapter 7
I/O Ports in AVR Sepehr Naimi
ADC and DAC Programming in AVR
8051 Micro Controller.
Presentation transcript:

Arduino Mega 2560

Arduino Mega 2560

Memory-Mapped I/O Datasheet Page: 399 Pin Mapping ATmega2560 Memory-Mapped I/O Datasheet Page: 399

AVR Microcontroller I/O PORT Programming: PORTs must be programmed before used as an INPUT(s) or an OUTPUT(s) PORTs have some other functions as ADC, Timers, Counters, Interrupts, Serial Communications, etc. PORTX Number of Pins PORTA 8-bit bi-directional I/O port (PA7..PA0) PORTB 8-bit bi-directional I/O port (PB7..PB0) PORTC 8-bit bi-directional I/O port (PC7..PC0) PORTD 8-bit bi-directional I/O port (PD7..PD0) PORTE 8-bit bi-directional I/O port (PE7..PE0) PORTF 8-bit bi-directional I/O port (PF7..PF0) Analog inputs to the A/D Converter PORTG 6-bit bi-directional I/O port (PG5..PG0) PORTH 8-bit bi-directional I/O port (PH7..PH0) PORTJ 8-bit bi-directional I/O port (PJ7..PJ0) PORTK 8-bit bi-directional I/O port (PK7..PK0) PORTL 8-bit bi-directional I/O port (PL7..PL0)

Each PORT has three I/O registers PORTx (PORTB) DDRx (Data Direction Register, DDRB) PINx (Port INput pins, PINB) DDRx is used to assign (set) the port as an INPUT or an OUTPUT For INPUT DDRBx pin = 0 (0b0000_0000) For OUTPUT DDRBx pin = 1 (0b1111_1111) Port(s) must be initialized as an input or an output in start of the code. To set port B as an output, DDRBx port bits must be all ONEs. Otherwise data will not go from port register to the pins of AVR. Upon reset, DDRX register become zero (Reset makes port as an INPUT, 0x00)

Role of PINx and PORTx Registers: To read data present at the pins, we should read the PINx register (To bring data into CPU from pins we read contents of the PINx register) To send data out to pins we use the PORTx register Internal PULL-UPs of AVR: To use internal pull-ups for INPUT. DDRx register must be zero PORTx register set to 0xFF (to use internal pull-ups) AVR Microcontroller has Four valued Logic 1 X (Unknown, Don't Care) Z (High Impedance or Open Circuit, Floating state, uncertain state)

C Data Types for the AVR:

I/O PORT Programming: Write an AVR program to send values 0x00 to PORTF and 0xFF to PORTK. #include <avr/io.h> int main (void) { DDRF = 0xFF; DDRK = 0xFF; while(1) PORTF = 0x00; PORTK = 0xFF; } return 0;

Time Delay By using a for loop By using predefined C functions (library) Using AVR timers Crystal frequency is the important factor in calculating time delays Complier used to compile the C program (code)

Write a program to toggle PORTK continuously with a 250ms delay Write a program to toggle PORTK continuously with a 250ms delay. Crystal frequency is 16MHz. #include <avr/io.h> void delay100ms(void) { unsigned int i; for(i=0;i<42150;i++); } int main (void){ DDRK = 0xFF; while(1) PORTK = 0xFF; delay100ms(); PORTK = 0x00; return 0; #include <avr/io.h> #include<util/delay.h> int main (void) { DDRK = 0xFF; while(1) PORTK = 0xFF; _delay_ms(250); PORTK = 0x00; } return 0;

LEDs are connected to pins of PORTK LEDs are connected to pins of PORTK. Write a program that shows the count from 00H to FFH (0000 0000 to 1111 1111) on the LEDs. #include <avr/io.h> #include<util/delay.h> int main (void) { DDRK = 0xFF; while(1) PORTK++; _delay_ms(200); } return 0;

Write a program to get a byte of data from PORTF, and then send it to PORTK. #include <avr/io.h> int main (void) { DDRK = 0xFF; // OUTPUT DDRF = 0x00; // INPUT while(1) PORTK = PINF; } return 0; #include <avr/io.h> int main (void) { unsigned char temp; DDRK = 0xFF; // OUTPUT DDRF = 0x00; // INPUT while(1) temp = PINF; PORTK = temp; } return 0;

I/O PORT Bit-wise Programming: Bit-wise Operators in C: AND (&), OR (|), EX-OR (^), INVERTER (~), SHIFT RIGHT (>>), SHIFT LEFT (<<)

Write a program to toggle only bit 4 of PORTK continuously without disturbing the rest of the pins #include <avr/io.h> #include<util/delay.h> int main (void) { DDRK = 0xFF; // OUTPUT while(1) PORTK = PORTK | 0b00010000; //set bit 4 of PORTK _delay_ms(100); PORTK = PORTK & 0b11101111; //set clear 4 of PORTK } return 0;

Write a program to get the status of bit 5 of PORTF and send it to bit 7 of PORTK continuously. #include <avr/io.h> int main (void) { DDRF = DDRF & 0b11011111; // pin 5 of PORTF is INPUT DDRK = DDRK | 0b10000000; // pin 7 of PORTK is OUTPUT while(1) if (PINF & 0b00100000) //check pin 1 of PINF PORTK = PORTK | 0b10000000; //set pin 7 of PORTK else PORTK = PORTK & 0b01111111; //clear pin 7 of PORTK } return 0;

A door sensor is connected to bit 1 of PORTF, and LED (buzzer) is connected to bit 7 of PORTK. Write a program to monitor door sensor and, when it opens, turn on the LED (buzzer). #include <avr/io.h> int main (void) { DDRF = DDRF & 0b11111101; // pin 1 of PORTF is INPUT DDRK = DDRK | 0b10000000; // pin 7 of PORTK is OUTPUT while(1) if (PINF & 0b00000010) //check pin 1 of PINF PORTK = PORTK | 0b10000000; //set pin 7 of PORTK else PORTK = PORTK & 0b01111111; //clear pin 7 of PORTK } return 0;

according to the following table: Write a program to read pins 0 and 1of PORTF, and issues a value (data or number) to PORTK according to the following table: #include <avr/io.h> int main (void) { unsigned char z; DDRK = 0xFF; DDRF = 0x00; while(1) z = PINF; z = z & 0b00000011; switch (z) case (0): PORTK = 0x0F; break; } case (1): PORTK = 0x55; pin 1 Pin 0 Value 0x0F 1 0x55 0xAA 0xF0 case (2): { PORTK = 0xAA; break; } case (3): PORTK = 0xF0; return 0;

Compound assignment operator in C: To reduce coding (typing), we can use compound statements for bit-wise operators in C. Write a program to get the status of bit 5 of PORTF and send it to bit 7 of PORTK continuously. #include <avr/io.h> int main (void) { DDRF &= 0b11011111; // pin 5 of PORTF is INPUT DDRK |= 0b10000000; // pin 7 of PORTK is OUTPUT while(1) if (PINF & 0b00100000) //check pin 1 of PINF PORTK |= 0b10000000; //set pin 7 of PORTK else PORTK &= 0b01111111; //clear pin 7 of PORTK } return 0;

Bit-wise shift operation in C: There are two bit-wise shift operators. Examples of shift operators in C: 0b00010000 >> 3 = 0b00000010 //shifting right 3 times 0b00010000 << 3 = 0b10000000 //shifting left 3 times 1 << 3 = 0b00001000 //shifting left 3 times Bit-wise shift operation and bit manipulation: For improving code clarity; Instead of writing “0b00100000”, we can write “0b00000001 << 5” or simply “1 << 5” OR Sometimes we need numbers like 0b11101111. To generate such a number, first shift then invert it. “0b11101111” can be written as “~ (1<<4)”

Write a program to get the status of bit 5 of PORTF and send it to bit 7 of PORTK continuously. #include <avr/io.h> int main (void) { DDRF = DDRF & ~(1<<5); // pin 5 of PORTK is INPUT: DDRK &= (~ (1<<5)); DDRK = DDRK | (1<<7); // pin 7 of PORTF is OUTPUT: DDRF |= (1<<7); while(1) if (PINF & (1<<5)) //check pin 1 of PINF PORTK = PORTK | (1<<7); //set pin 7 of PORTK: PORTK |= (1<<7); else PORTK = PORTK & ~(1<<7); //clear pin 7 of PORTK PORTK &= ~(1<<7); } return 0;

A door sensor is connected to bit 1 of PORTF, and LED (buzzer) is connected to bit 7 of PORTK. Write a program to monitor door sensor and, when it opens, turn on the LED (buzzer). #include <avr/io.h> #define SENSOR 1 #define LED 7 int main (void) { DDRF &= ~(1<<SENSOR); // pin 1 of PORTF is INPUT DDRK |= (1<<LED); // pin 7 of PORTK is OUTPUT while(1) if (PINF & (1<<SENSOR)) //check pin 1 of PINF PORTK |= (1<<LED); //set pin 7 of PORTK else PORTK &= ~(1<<LED); //clear pin 7 of PORTK } return 0;

To generate more complicated numbers, simply OR the numbers. Example: one in position B7 and another in B4. (1<<7)|(1<<4)