Atmega328p Introduction for Digital and PWM Output Brion L Fuller II Robotics Club.

Slides:



Advertisements
Similar presentations
Khaled A. Al-Utaibi Interfacing an LED The Light Emitting Diode (LED) Applications DC Characteristics & Operation Interfacing to.
Advertisements

EMS1EP Lecture 4 Intro to Programming Dr. Robert Ross.
ARDUINO CLUB What we’re really doing. BASICS The setup() and loop() functions.
Electrical team Arduino tutorial I BY: JOSHUA arduini
Real-Time Library: RTX
Lab7: Introduction to Arduino
Anurag Dwivedi & Rudra Pratap Suman.  Open Source electronic prototyping platform based on flexible easy to use hardware and software.
What is Arduino?  Arduino is a ATMEL 168 micro-controller kit designed specially for small projects  User friendly IDE(Integrated Development Environment)
Embedded Sumo 1T4 – 1T5 UTRA.
Encoders, Motors, Power, Mini Project #1 10/24/2014.
Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.
Living with the Lab Using Your Arduino, Breadboard and Multimeter EAS 199A Fall 2011 Work in teams of two!
 | bit OR & bit AND ~ bit NOT ^ bit EXLUSIVE OR (XOR) > bit RIGHT SHIFT.
Intro to Programming and Microcontrollers. Activity Group into pairs and sit back-to-back. Pick one person who is going to draw. The other person will.
Timers in Hardware ECE152. Overview Why are timers important – Watchdog – Task switching – Accurate time of day Can use polling or interrupts.
8-Bit Timer/Counter 0 Counter/Timer 0 and 2 (TCNT0, TCNT2) are nearly identical. Differences: -TCNT0 can run off an external 32Khz clock (Tosc) or the.
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.
Timers and Interrupts Shivendu Bhushan Summer Camp ‘13.
Introduction to Arduino Prepared by R. Lamond.  “Arduino is an open-source electronics prototyping platform based on flexible, easy- to-use hardware.
Basic Circuits – Lab 2 Arduino and Sensors Xmedia Spring 2011.
Embedded Programming and Robotics
Timers and Interrupts Shivendu Bhushan Sonu Agarwal.
Working with Arduino: Lesson #1: Getting Acquainted with the Kit EGN1007.
Lab 1 - Microcontrollers Complete the program template below being sure to select the correct parameters to meet the requirements (see the Microcontroller.
MCU: Interrupts and Timers Ganesh Pitchiah. What’s an MCU ?
Arduino. What is it? A open-source software suite and single-board microcontroller. Allows easy and affordable prototyping of microcontroller applications.
Introduction to the Arduino
1 Lab 1: Introduction. 2 Configure ATMEL AVR Starter Kit 500 (STK500), a prototyping/development board for Lab1. ATmega16 ( V) is the chip used.
Tweaking Your Simon Adding a photoresistor and changing code Instruction by Pete Lewis and Linz Craig.
Advanced uC Session Speaker : Chiraag Juvekar Jan 13, 2011 Speaker : Chiraag Juvekar Jan 13, 2011.
ECS642U Embedded Systems Cyclic Execution and Polling William Marsh.
Timers and Interrupts Anurag Dwivedi. Let Us Revise.
1 - Remove LED from 13 and GND - Bring out your breadboard from HW#4 Arduino Overview:
Interrupt On a very basic level, an interrupt is a signal that interrupts the current processor activity. It may be triggered by an external event (change.
Microcontrollers, Microcomputers, and Microprocessors
1 Introduction to Haptics Introduction to the Hapkit board Allison M. Okamura Stanford University.
Robotics Grant Agreement No LLP UK-LEONARDO-LMP Project acronym: CLEM Project title: Cloud services for E-Learning in Mechatronics Technology.
Programming in Arduino Materials:Arduino Board Casperelectronics Pre Pres. Notes Photos from workshop?
Arduino Training For You! Learn Programming at Your Own Pace! Chapter 1 A course developed from the text book: “Introduction to Arduino A Piece of Cake!”
:Blink Blink: Er. Sahil Khanna
Vishwakarma government engineering college Prepare by. Hardik Jolapara( ) LCD Interfacing with ATmega16.
Istituto Tecnico Industriale A.Monaco EURLAB Control a Servo Motor If you want to swing an robot arm or … steer a robot you need a special motor (Servo).
Arduino “Getting Started” Instructor : Dr Matthew Miss Khin Yi Kyaw
Harpeth Hall Jan 2016 Introduction to Arduino Prepared for Harpeth Hall Winterim January 2016.
Arduino.
Microcontroller basics
If you want to swing an robot arm or …
Val Manes Department of Math & Computer Science
UTA010 : Engineering Design – II
An Arduino Workshop A Microcontroller.
Welcome to Arduino A Microcontroller.
Arduino - Introduction
Introduction to Arduino Microcontrollers
The Arduino Microcontroller: Atmel AVR Atmega 328
Timer/Counter Modified from Dr. Lam Phung’s Slides.
Roller Coaster Design Project
Continuing with LED’s and Arduino
Working with Arduino: Lesson #1: Getting Acquainted with the Kit
CSCI1600: Embedded and Real Time Software
Chapter 1 Introduction of Arduino
Arduino : Introduction & Programming
Intro to Micro Controllers
CSCI1600: Embedded and Real Time Software
CTY SAR FCPS Shawn Lupoli, Elliot Tan
Lab #1: Getting Started.
Arduino Uno circuit basics
Introduction to Arduino
Arduino Board.
Arduino म्हणजे काय?.
Presentation transcript:

Atmega328p Introduction for Digital and PWM Output Brion L Fuller II Robotics Club

ARDUINO NANO

This is how to connect an LED to a pin on the micro controller. This one we used D6 according to the silkscreen. Which is PortD Pin 6 when programming.

The led can be wired in two different ways. Use the Pin as groundUse the Pin as 5v

Start up AVR Studios Create a new project (C++) // this is the header file that tells the compiler what pins and ports, etc // are available on this chip #include // define what pins the LEDs are connected to // in reality, PD6 is really just '6' #define LED PD // Some macros that make the code more readable #define output_low(port,pin) port &= ~(1<<pin) #define output_high(port,pin) port |= (1<<pin) #define set_input(portdir,pin) portdir &= ~(1<<pin) #define set_output(portdir,pin) portdir |= (1<<pin) // this is just a program that 'kills time' in a calibrated method void delay_ms(uint8_t ms) { uint16_t delay_count = F_CPU / 17500; volatile uint16_t i; while (ms != 0) { for (i=0; i != delay_count; i++); ms--; } } int main(void) { // initialize the direction of PORTD #6 to be an output set_output(DDRD, LED);set_output(DDRD, LED); while (1) { // turn on the LED for 200ms output_high(PORTD, LED);output_high(PORTD, LED); delay_ms(200);delay_ms(200); // now turn off the LED for another 200ms output_low(PORTD, LED);output_low(PORTD, LED); delay_ms(200);delay_ms(200); // now start over } }

Setup This part of the code is called a header file that has instructions on how to convert parts of codes to the microcontroller we are using. The file is usually found in C:\WinAVR\avr\include\avr // this is the header file that tells the compiler what pins and ports, etc // are available on this chip #include Definitions help translate code to make programs easier to read and troubleshot for people // define what pin the LED is connected to // in reality, PD6 is really just '6' #define LED PD // Some macros that make the code more readable #define output_low(port,pin) port &= ~(1<<pin) #define output_high(port,pin) port |= (1<<pin) Port &= ~(1<<pin) sets the pin to ground or zero Port |= (1<<pin) sets the pin to high or one #define set_input(portdir,pin) portdir &= ~(1<<pin) #define set_output(portdir,pin) portdir |= (1<<pin) This part is important. The microcontroller needs to know what pins are going to be receiving information and what pins will be giving information.

Important Delay // this is just a program that 'kills time' in a calibrated method void delay_ms(uint16_t ms) { uint16_t delay_count = F_CPU / 17500; volatile uint16_t i; while (ms != 0) { for (i=0; i != delay_count; i++); ms--; } } Our processor runs at 16Mhz which can be found on a datasheet. That is 16,000,000 (1/s) which is 62.5ns. This is a very important number to remember for any processor because this defines the time it takes for a line of code to finish. This defined function makes delays easier by giving a format of delay(N ms) based on the CPU frequency.

Important Delay // this is just a program that 'kills time' in a calibrated method void delay_ms(uint8_t ms) { uint16_t delay_count = F_CPU / 17500; volatile uint16_t i; while (ms != 0) { for (i=0; i != delay_count; i++); ms--; } } Our processor runs at 16Mhz which can be found on a datasheet. That is 16,000,000 (1/s) which is 62.5ns. This is a very important number to remember for any processor because this defines the time it takes for a line of code to finish. This defined function makes delays easier by giving a format of delay(N ms) based on the CPU frequency. (Only up to 255ms) // this delay is for seconds void delay_s(uint8_t s) { uint16_t delay_count = F_CPU / 17500; volatile uint16_t i; for (s; s != 0; s--){ delay_ms(250); delay_ms(250); delay_ms(250); delay_ms(250); } }

Main Program //This is the start of the program int main(void){ This lets the processor know when to start executing a program //initialize the direction of PortD #6 to output set_output(DDRD, LED); This is the setup phase for the program. Most of the time global variables and default direction lines should be place near to the main function //repeat program forever because 1 is always true while (1) { // turn on the LED for 200ms output_high(PORTD, LED);output_high(PORTD, LED); delay_ms(200);delay_ms(200); // now turn off the LED for another 200ms output_low(PORTD, LED);output_low(PORTD, LED); delay_ms(200);delay_ms(200); // now start over } This is our program finally. This is a simple blink LED for 200ms at a time.

Exercises 1.Make the LED blink twice as fast 2.Make the LED blink twice as slow 3.Change the line that sets the pin direction so that its an input, what happens? (Turn off the light and observe. This effect will be explained later...) 4.Make the LED blink REALLY fast (like, only 5ms delay) What happens? Turn off the light and wave the board around. 5.Make the LED blink out morse code for SOS (3 short blinks, a pause, 3 long blinks, a pause, 3 short blinks, a long pause...)

But what about dimming the LED? Is it too bright not bright enough? I don’t want to replace the resistor everytime I would like to change the intensity. Well that’s where PWM comes in. GREAT NOW THE LED IS ON OR OFF

Back to the pin mapping. We have modified some software to PWM in the last exercise but in all honestly that is a very processor hungry activity and what if you want to do other things while PWM. That is where some of these random functions in the ( ) are put to use. These all have special function that are connected to the pins and can perform specialized tasks to free up processor time. For PWM, we will look at the pins that have (OC_ _) this tells us that there are build in hardware timers at these pins.

#define pwm6(pwm) OCR0A = pwm Here is the setup that goes near the top of the main function // Motor Initialization routine -- this function must be called // before you use any of the above functions { //Configure for inverted PWM output on motor and clear on TCCR0A = b ; TCCR0B = b ; //set the pwm to 0% pwm6(0); } Now we can change the simple output to the LED from on or off to dimming while (1) { // turn on the LED for at 50% duty cycle pwm6(127); delay_ms(200);delay_ms(200); // now turn off the LED to 100% pwm6(255); delay_ms(200);delay_ms(200); //now turn off the LED pwm6(0); delay_ms(200);delay_ms(200); // now start over } }

00001 // this is the header file that tells the compiler what pins and ports, etc // are available on this chip #include // define what pins the LEDs are connected to // in reality, PD6 is really just '6' #define LED // Some macros that make the code more readable #define output_low(port,pin) port &= ~(1<<pin) #define output_high(port,pin) port |= (1<<pin) #define set_input(portdir,pin) portdir &= ~(1<<pin) #define set_output(portdir,pin) portdir |= (1<<pin) #define pwm6(pwm) OCR0A = pwm // this is just a program that 'kills time' in a calibrated method void delay_ms(uint8_t ms) { uint16_t delay_count = F_CPU / 17500; volatile uint16_t i; while (ms != 0) { for (i=0; i != delay_count; i++); ms--; } } // this delay is for seconds void delay_s(uint8_t s) { uint16_t delay_count = F_CPU / 17500; volatile uint16_t i; for (s; s != 0; s--){ delay_ms(250); delay_ms(250); delay_ms(250); delay_ms(250); } } int main(void) { // initialize the direction of PORTD #6 to be an output set_output(DDRD, LED);set_output(DDRD, LED); //Configure for inverted PWM output on motor and clear on TCCR0A = b ; TCCR0B = b ; //set the pwm to 0% pwm6(0); while (1) { // turn on the LED for at 50% duty cycle pwm6(127); delay_ms(200);delay_ms(200); // now turn off the LED to 100% pwm6(255); delay_ms(200);delay_ms(200); //now turn off the LED pwm6(0); delay_ms(200);delay_ms(200); // now start over } }

Exercises 1.Make the LED brighten and dim 2.Change the Pin that the LED is on 3.Replace the LED with a motor