N01

advertisement

N01 page 1 of 11

Arduino

open source microprocessor development system

Uno - Atmel AVR ATmega328 CPU (14 I/O pins)

Due - Atmel SAM3X8E ARM Cortex-M3 CPU (54 I/O pins)

Mega - Atmel AVR ATmega2560 (54 I/O pins) home http://www.arduino.cc/en/ getting started http://www.arduino.cc/en/Guide/HomePage different boards http://store.arduino.cc/category/11 download software http://www.arduino.cc/en/Main/Software examples http://www.arduino.cc/en/Tutorial/HomePage language reference http://www.arduino.cc/en/Reference/HomePage standalone CPU http://arduino.cc/en/Tutorial/ArduinoToBreadboard schematic http://arduino.cc/en/uploads/Main/Arduino_Uno_Rev3-schematic.pdf

EAGLE 6.0 files http://arduino.cc/en/uploads/Main/arduino_Uno_Rev3-02-TH.zip

Arduino UNO specs http://arduino.cc/en/Main/ArduinoBoardUno

Microcontroller ATmega328

Operating Voltage 5V

Input Voltage (recommended) 7-12V

Input Voltage (limits)

Digital I/O Pins

6-20V

14 (of which 6 provide PWM output)

Analog Input Pins

DC Current per I/O Pin

DC Current for 3.3V Pin

Flash Memory

SRAM

EEPROM

Clock Speed

Cost

6

40 mA

50 mA

32 KB (ATmega328) of which 0.5 KB used by bootloader

2 KB (ATmega328)

1 KB (ATmega328)

16 MHz

$25 to $30

N01 page 2 of 11

Arduino Datatypes (Variables)

progams are called "sketches" strongly typed programming language based on "Wiring" - very similar to C

MATLAB generally uses 64b floating point variables and does not require explicit declaration must declare variables for Arduino bit values 0 = low = off = false = space 1= high = on = true = mark

8 bits = 1 byte

4 bits = 1 nibble

8b = 1B each byte has an upper and lower nibble http://www.arduino.cc/en/Reference/HomePage boolean logic 8b 0 = false or 1 = true char char byte int character 8b integer 8b signed integer integer

8b

16b unsigned signed

ASCII

-128 to 127

0 to 255

-32,768 to 32,767 unsigned int integer long integer

16b unsigned 0 to 65,535

32b signed -2,147,483,648 to 2,147,483,647 unsigned long float double string integer float float character

32b unsigned 0 to 4,294,967,295

32b signed single precision ±3.4x10

38

(7 decimal digits)

64b signed double precision ±1.8x10

308

(15 decimal digits)

ASCII array

// examples boolean a, b = false; // logical char c, d = “Q”; // character char e, f = -17; // 8b signed byte g, h = 5; // 8b unsigned int i, j = -3; // 16b signed --- most common unsigned int k, l = 5; // 16b unsigned long m, n = -3; // 32b signed unsigned long o, p = 5; // 32b unsigned float q, r = 7.5; // single precision float double s, t = -9.7; // double precision float char u[] = “Arduino”; // 7 chars plus 1 end-of-string null char char v[8] = “Arduino”; // same as above int w[9], x[4] = { 7, 9, -3, 85 }; // array of int variables – uses 0 for first index

N01

ASCII Table

page 3 of 11

N01 page 4 of 11

Number Systems

Decimal base 10 - radix 10 ten numerals 0 1 2 3 4 5 6 7 8 9

10

2

10

1

10

0

10

-1

10

-2

367.29 =

3 6 7 . 2 9

Binary base 2 hundreds tens ones radix point tenths hundredths two numerals 0 1

2

7

2

6

2

5

2

4

2

3

2

2

2

1

2

0

0101 0110 =

0 1 0 1 0 1 1 0

= 86 decimal

128 64 32 16 8 4 2 1

2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0

0100 1001 =

0 1 0 0 1 0 0 1

= 73 decimal

128 64 32 16 8 4 2 1

2

3

2

2

2

1

2

0

2

-1

2

-2

2

-3

2

-4

0111.0101 =

0 1 1 1 . 0 1 0 1

= 7 5/16 dec

8 4 2 1 1/2 1/4 1/8 1/16

N01

Hexadecimal base 16 sixteen numerals 0 1 2 3 4 5 6 7 8 9 A B C D E F

10 11 12 13 14 15

16

2

16

1

16

0

3B7 hex =

3 B 7

= 3 * 256 + 11 * 16 + 7 * 1 = 951 decimal

256 16 1

3B7 hex =

3 B 7

0011 1011 0111 binary

Addition

0011 0101 binary

+ 0001 1001

0100 1110

Multiplication

0 0 1 1 x 0 1 0 1

0 0 1 1

0 0 0 0

0 0 1 1

0 0 0 0

0 0 0 1 1 1 1

53 dec

25

78

3 x 5

15

35 hex

19

4E page 5 of 11

N01

Sign Convention

Mark sign most significant bit (msb) contains sign and remaining bits are absolute value

0 = positive, 1 = negative unfortunately does not conform to arithmetic

0000 1001 = +9 dec using mark sign

1000 0011 = -3 dec using mark sign sum 1000 1100 = -12 dec using mark sign

Offset sign (biased) smallest integer is most negative number largest integer is most positive number example using -127 offset (bias)

0000 0000 = -127 dec

0111 1111 = 0 dec

1000 0000 = 1 dec

1111 1111 = 128 dec unfortunately does not conform to arithmetic

1000 1000 = +9 dec using -127 offset

0111 1100 = -3 dec using -127 offset sum 0000 0100 = -123 dec using -127 offset often used for bipolar analog-to-digital converters (ADC) example using 12b ADC for ±10V range

0000 0000 0000 = -10V

0111 1111 1111 = 0 V

1111 1111 1111 = +10V page 6 of 11

N01

Two’s complement standard method used to store all signed integers step one step two invert all bits add one

0000 0011 = +3 dec using two’s complement invert all bits 1111 1100 add one 1111 1101 = -3 dec using two’s complement msb indicates sign but remaining bits are not absolute value like mark sign conforms to arithmetic

0000 1001 = +9 dec using two’s complement

1111 1101 = -3 dec using two’s complement sum 0000 0110 = +6 dec using two’s complement negation is reversible

1111 1101 = -3 dec using two’s complement invert all bits 0000 0010 add one 0000 0011 = +3 dec using two’s complement page 7 of 11

N01

Floating Point

IEEE 754-2008 includes special forms for zero, +∞, -∞ and NaN

Single precision – 7 decimal digits of precision mark sign in msb

8b exponent power of 2 with -127 offset sign binary fraction

0 01111100 00100000000000000000000 =

(−1) 0

× (1 + 0.25) × 2

(124–127)

= 1.25 × 2

−3

= 0.15625

1 01111101 00101100000000000000000 =

(−1) 1 × (1 + 0.34375) × 2 (125–127) = -1.34375× 2

−2

= -0.3359375

Double precision – 15 decimal digits of precision page 8 of 11

N01 page 9 of 11

Arduino prgramming language compiled language very similar to C can communicate back to PC through serial port monitor using Serial functions must use semi-colon at end of each line (except #define and #include) variable names are case sensitive - "Mmm" not same as "mmm" variables are local to functions unless declared in intial global block

// remainder of line is comment

/* block of lines comment */

// permanent constants - global text substitution used during compilation

// NO SEMI-COLON AT END !

#define M1_enable 6

// insert large block of text from file during compiliation

// NO SEMI-COLON AT END !

#include <avr/pgmspace.h>

// integer constants a1 = 137; // decimal a2 = B11011; // binary - leading "B" a3 = 0x7B; // hex - leading "0x"

Sections of Code

1) initial block for #define, #include and global variables

2) void setup() { // code executed once }

3) void loop() { // infinite loop }

4) subroutines

N01

// stepper - one stepping motor

// HJSIII, 14.01.13

////////////////////////////////////////////////////////////

// define pin names

// wire colors for Oriental Vexta

#define coilA 2 // black

#define coilB 3 // blue

#define coilC 4 // green

#define coilD 5 // red

// yellow AC common

// white BD common

// declare global variables int row; // 16b signed byte abcd; // 8b unsigned byte table[8] = { 8, 4, 2, 1, 8, 4, 2, 1 }; // wave drive

//byte table[8] = { 9, 12, 6, 3, 9, 12, 6, 3 }; // two phase

//byte table[8] = { 8, 12, 4, 6, 2, 3, 1, 9 }; // half step

////////////////////////////////////////////////////////////

// setup routine runs once when you press reset void setup() {

// initialize serial communication at 9600 bits per second:

Serial.begin(9600);

// configure digital outputs

pinMode( coilA, OUTPUT );

pinMode( coilB, OUTPUT );

pinMode( coilC, OUTPUT );

pinMode( coilD, OUTPUT );

// send intial position

row = 0;

abcd = table[ row ];

digitalWrite( coilA, bitRead( abcd, 3 ) );

digitalWrite( coilB, bitRead( abcd, 2 ) );

digitalWrite( coilC, bitRead( abcd, 1 ) );

digitalWrite( coilD, bitRead( abcd, 0 ) );

}

// bottom of setup

////////////////////////////////////////////////////////////

// infinite loop void loop() {

// declare local variables

int nStep, delayStep; // 16b signed

// length of delay [sec]

// Vexta, ULN2803A, 5V breadboard supply, no load

// 35 msec single step, 25 msec two phase, 35 msec half step

delayStep = 35;

// move

Serial.println( "Forward" ); page 10 of 11

N01

nStep = 200;

moveStep( nStep, delayStep );

delay( 1000 );

Serial.println( "Back" );

nStep = -200;

moveStep( nStep, delayStep );

delay( 1000 );

}

////////////////////////////////////////////////////////////

// function to index through stepper table

// INPUTS

// nStep = signed number of steps

// positive moves down through table

// negative moves up through table

// delayStep = delay between steps [msec]

// OUTPUTS

// none void moveStep( int nStep, int delayStep ){

// declare local variables

int i, n, dir; // 16b signed

// number of steps and direction

n = abs( nStep );

dir = constrain( nStep, -1, 1 );

// count steps

if( n > 0 ){

for( i=0; i<n; i++ ){

// next row in table

row = row + dir;

if( row < 0 ){ row = 7; }

if( row > 7 ){ row = 0; }

// send new position

abcd = table[ row ];

digitalWrite( coilA, bitRead( abcd, 3 ) );

digitalWrite( coilB, bitRead( abcd, 2 ) );

digitalWrite( coilC, bitRead( abcd, 1 ) );

digitalWrite( coilD, bitRead( abcd, 0 ) );

Serial.println(abcd); // show time

// delay

delay( delayStep );

} // bottom of for

} // bottom of if

} // bottom of moveStep

////////////////////////////////////////////////////////////

// bottom of stepper page 11 of 11

Download