Вы находитесь на странице: 1из 10

Lecture material taken from:

A d i and Arduino d Atmel At l Studio St di

Suree Pumrin, Ph.D. Semester Se este 2/2556 / 556

Lecture 6 Introduction to the ATmega328 and Arduino, CSE P567, University of Washington, http://courses.cs.washington.edu/courses/csep567/10wi/lectures/lect ures.html. Intro to Arduino, Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson, and Eli Santistevan, Sparkfun Electronics. Beginning C for Arduino, Jack Purdum, Ph.D., Technology in Action, 2012. 2012

Topics Objectives
Programming the Arduino Uno Rev3 (cont.) Creating project on Atmel Studio

Arduino input/output (cont.) Serial Communication Basics Programming Atmel studio

Arduino Uno Rev3


USB (to Computer) RESET

(I2C Bus)

5V / 3.3V / GND

Di it l I\O Digital
PWM(3, 5, 6, 9, 10, 11)

5 6

Fading F di i in and dF Fading di O Out t (Analog ( g or Digital?) g )

A few pins on the Arduino allow for us to modify the output to mimic an analog signal. signal This is done by a technique called: Pulse Width Modulation (PWM)

Concepts: Analog vs. Digital

To create an analog signal, the microcontroller uses a technique q called PWM. By y varying y g the duty y cycle, we can mimic an average analog voltage.

Pulse Pulse Width Modulation (PWM)

Fading Introducing a new command

analogWrite g (pin, p val); pin PWM limited to pins 3, 3 5, 5 6, 9, 10, 11 val 8 bit value (0 255). 0 => > 0V | 255 => > 5V

Move one of M f your LED pins i over to Pin 9

In Arduino, open up: p 01.Basics Fade File Examples

Fade - Code C Review

Fade - Code C Review

Example-- Fading
Challenge a Change the rate of the fading in and out. There are at least two different ways to do this can you figure them out? Challenge b Use 2 (or more) LEDs so that one fades in as the other one fades out. out

Driving Motors or other High Current Loads

NPN Transistor (Common Emitter Amplifier Circuit) )

to Digital Pin Pi 9

Digital Input
Connect digital input to your Arduino using Pins # 0 13 (Although pins # 0 & 1 are also used for programming) Digital Input needs a pinMode command: pinMode (pinNumber, INPUT); Make sure to use ALL CAPS for INPUT To get a digital reading: int buttonState = digitalRead (pinNumber); Digital Input values are only HIGH (On) or LOW (Off)

Digital Sensors
Digital sensors are more straight forward than Analog No matter what the sensor there are only two settings: On and Off Signal is always either HIGH (On) or LOW (Off) Voltage signal for HIGH will be a little less than 5V on your Uno Voltage signal for LOW will be 0V on most systems

Boolean Operators
<Boolean> Description

( ( ( ( ( (

) ) ) ) ) )

== != > >= < <=

( ( ( ( ( (

) ) ) ) ) )

is equal? is not equal? q greater than greater than or equal less than less than or equal


Trimpot T i t (Potentiometer) (P t ti t ) Variable Resistor

fixed end wiper fixed end

Arduino uses a 10 10-bit bit A/D Converter: this means that you get input values from 0 to t 1023
0V0 5 V 1023

int sensorValue = analogRead(A0);

Using Serial Communication

Method used to transfer data between two devices.
Data passes between the computer and Arduino through the USB cable. Data is transmitted as zeros (0) and ones (1) seq sequentially. entiall

Serial Monitor & analogRead()

Initializes the Serial Communication Arduino dedicates Digital I/O pin # 0 to receiving and Digital I/O pin #1 to transmit.

9600 baud data rate prints data to serial bus

Serial Monitor & analogRead()

Opens up a Serial Terminal Window

Analog a og Se Sensors so s 2 Pin Analog Sensors = var. resistor

Take two sensors -- Use the Serial Monitor and find the range of input values you get for each sensor sensor.
M A l R d = _________ MaxAnalogRead MinAnalogRead MinAnalogRead = _________

Analog Sensors Examples:

Sensors Mic Photoresistor Potentiometer Temp Sensor Flex Sensor Accelerometer Variables soundVolume lightLevel dialPosition temperature bend tilt/acceleration

Additional Serial Communication Sending g a Message g

void loop ( ) { Serial.print(Hands on ) ; S i l Serial.print(Learning i t(L i ) ; Serial.println(is Fun!!!) ; }

Serial Communication: Serial Debugging gg g

void loop p() { int xVar = 10; Serial.print ( Variable xVar is ) ; Serial.println ( xVar ) ; }

Serial Communication: Serial Troubleshooting g

void loop p ( ) { Serial.print (Digital pin 9: ); Serial.println (digitalRead(9)); }

D Development l tT Tools l
Atmel Studio 6 is the integrated development environment (IDE) for developing and debugging embedded applications based on Atmel AVR and ARM Cortex-M microcontrollers (MCUs) in C/C++ and assembly code code. The IDE brings together an editor with assisted code writing, a wizard for quickly creating new projects, the Atmel Software Framework with free source code library, a GNU C/C++ Compiler, a powerful simulator, and the front-end for all Atmel programmers and in-circuit debuggers.
2102444 Introduction to Embedded Systems 30

Starting Atmel Studio (1)

Atmel Studio is started by clicking on the Atmel Studio 6.1 shortcut in the Start-Up menu. Once started started, the start page is displayed displayed. From within this page you can create new projects and reopen recently used projects, as well as browse through articles providing tutorials, help and news. news The Start page can also be accessed from ViewStart Page, or Alt+V+G.

2102444 Introduction to Embedded Systems


Figure 1. The Project related section of the Start Page


Starting Atmel Studio (2)

The left section of the start page contains project-related items:
New project - Use this to create a new project. If you are very new to the concept of software development with Atmel Studio, refer to the step-by-step step by step guides guides. The project settings and available options are described in detail in Project Management New example project - To take a step-by-step tour of the available il bl Atmel At l platforms' l tf ' functionalities f ti liti using i th the AVR Software S ft Framework, click this button . Open p p project j - Load an existing gp project, j , not mentioned on the Recent projects pane

Creating a project (1)

On the Start Page, click the New Project option.

Figure 2. Project options

2102444 Introduction to Embedded Systems 33

Creating a project (2)

1. In the New Project dialog box, select Installed Templates. This lists th available the il bl project j tt types. 2. For this example, create an GCC C Executable Project. 3 In the Name box, 3. box type a name for the new project project. 4. In the Location box, select a save location. 5. Atmel Studio will suggest a name in the Solution name box. You can override this name if wanted. 6. Leave the Create directory for solution checkbox checked. 7 Click OK. 7. OK

Ch Choosing i aT Target tD Device i

When a new project is created, the Device Selection dialog is displayed and you will be prompted to select the project target device.



Writing and Compiling Code

#include <avr/io.h> unsigned char z; int main(void) { DDRB = 0xff; while (1) { z = PINB; PORTB = z+1; } }
2102444 Introduction to Embedded Systems 37

Example 1

2102444 Introduction to Embedded Systems


Example 2 Exercise
#include #i l d < <avr/io.h> /i h> #include <util/delay.h> int main(void) { DDRB = 0xff; while (1) { f (int for (i t i = 1; 1 i<= i< 128; 128 i = i*2) { PORTB = i; _delay_loop_2(30000); } for (int i = 128; i> 1; i -= i/2) { PORTB = i; _delay_loop_2(30000); } } } 2102444 Introduction to Embedded Systems 39 40

1. Code Atmel Studio 6.1 Tool Chain Arduino Setup Video 2. Atmel studio PORT Code void setup() { DDRB = B00011111; } void loop() { PORTB = 1; } 3. Datasheet Atmel PORT ON/OFF O O LED P ORT O