Lecture Programming Micro Controllers

22
Programming Microcontrollers B. Furman 19MAR2011

Transcript of Lecture Programming Micro Controllers

Page 1: Lecture Programming Micro Controllers

Programming Microcontrollers

B. Furman

19MAR2011

Page 2: Lecture Programming Micro Controllers

Learning Objectives

Explain and apply best practices in writing a program for a microcontroller

Explain the structure of a program for the Arduino

Explain the concept of a ‘bit mask’ and use it to determine if a bit is clear or set

Use bit-wise logical operators to create bit masks and extract bits

Page 3: Lecture Programming Micro Controllers

Mechatronics Concept Map

Controller(Hardware & Software)

System toControl

Sensor

SignalConditioning

PowerInterface

Actuator

UserInterface

PowerSource

BJ Furman 22JAN11

ME 106ME 154ME 157ME 195

ME 120ME 297A

ME 106ME 120

ME 106ME 190ME 187

ME 110ME 136ME 154ME 157

ME 182ME 189ME 195

ME 106ME 120

ME 106

INTEGRATION

Page 4: Lecture Programming Micro Controllers

Recap Last Lecture

Binary and hex numbers Why use hex?

Digital pins can be inputs or outputs What is the difference?

Pins are bidirectional for digital I/O Which Arduino function do you use? DDRx (x = B, C, or D for ATmega328) register

determines direction 8-bit register

a ‘1’ in DDRx means…? a ‘0’ in DDRx means…?

01234567

Page 5: Lecture Programming Micro Controllers

Test Your Comprehension Write code to make all pins of PORTD to be

outputs (Arduino and alternate) DDRD = DDRD = 0b11111111;

DDRD = 255;

Write code to make pins 5, 3, and 1 of PORTD to be outputs, and the rest inputs DDRD = 0b00101010; DDRD = 0x2A; DDRD | = (1<<5) | (1<<3) | (1<<1);

0xFF;pinMode(0, OUTPUT);

pinMode(7, OUTPUT);

Arduino style

pinMode(1, OUTPUT);pinMode(3, OUTPUT);pinMode(5, OUTPUT);

Arduino style

Page 6: Lecture Programming Micro Controllers

Structure of an Arduino Program

An arduino program == ‘sketch’ Must have:

setup() loop()

setup() configures pin modes and

registers loop()

runs the main body of the program forever

like while(1) {…}

Where is main() ? Arduino simplifies things Does things for you

/* Blink - turns on an LED for DELAY_ON msec, then off for DELAY_OFF msec, and repeatsBJ Furman rev. 1.1 Last rev: 22JAN2011*/#define LED_PIN= 13; // LED on digital pin 13#define DELAY_ON = 1000;#define DELAY_OFF = 1000;

void setup(){ // initialize the digital pin as an output: pinMode(LED_PIN, OUTPUT); }

// loop() method runs forever,// as long as the Arduino has power

void loop() { digitalWrite(LED_PIN, HIGH); // set the LED on delay(DELAY_ON); // wait for DELAY_ON msec digitalWrite(LED_PIN, LOW); // set the LED off delay(DELAY_OFF); // wait for DELAY_OFF msec}

Page 7: Lecture Programming Micro Controllers

Best Practices and Patterns -1 Programmer’s block

At a minimum: Program name Description of what

the program does Author Revision number Revision date/time

Even better: Creation date Inputs Outputs Method/algorithm

/* Blink - turns on an LED for DELAY_ON msec, then off for DELAY_OFF msec, and repeatsBJ Furman rev. 1.1 Last rev: 22JAN2011*/

Page 8: Lecture Programming Micro Controllers

Best Practices and Patterns -2 Avoid ‘hard coding’

constants Use #define and

symbolic names instead Why?

Symbolic names are usually put in all caps to differentiate from variables

See me106.h

#define LED_PIN = 13; // LED on digital pin 13#define DELAY_ON = 1000;#define DELAY_OFF = 1000;

Page 9: Lecture Programming Micro Controllers

How to Twiddle Bits Recall the example of the seat belt

indicator system C code snippet (not full program)

ATmega328

D0, D1

VTG= +5V

0

1

D2

D3

#define LATCHED 0#define ENGAGED 0pinMode(0, INPUT); // key switchpinMode(1, INPUT); // belt latch switchpinMode(2, OUTPUT); // lamp

pinMode(3, OUTPUT); // buzzerkey_state=digitalRead(0);belt_state=digitalRead1);if(key_state==ENGAGED) if(belt_state==LATCHED) digitalWrite(3, LOW); digitalWrite(2, LOW); else digitalWrite(2, HIGH); digitalWrite(3, HIGH); else ;

Page 10: Lecture Programming Micro Controllers

Bit Manipulation Practice See the handout on Bit Manipulation

Setting bits Clearing bits Toggling bits

Make bits 5 and 3 of PORTB high and the rest low

Challenge:

Page 11: Lecture Programming Micro Controllers

Summary of Bit Manipulation Setting a bit (making it a ‘1’)

Bitwise OR the PORTx register with the corresponding bit mask Ex. PORTB | = _BV(3);

Clearing a bit (making it a ‘0’) Bitwise AND the PORTx register with the

corresponding complemented bit mask Ex. PORTB & = ~( _BV(3) );

Toggling a bit (making it flip) Bitwise XOR the PORTx register with the

corresponding bit mask Ex. PORTB ^ = _BV(3);

Page 12: Lecture Programming Micro Controllers

Bit Twiddling Practice

Make Arduino pins 11 – 13 to be outputs and pins 8 – 10 to be inputs1. Use the Arduino method

2. Use the ‘all-at-once’ (general) method Check if pin 9 is high

If pin 9 is high, make pin 13 high and pin 11 low Else both pins 13 should be low

1. Use the Arduino method

2. Use the general port-style method

Page 13: Lecture Programming Micro Controllers

Pull-up Resistors Pins configured as INPUTS can be ‘pulled

up’ to VTG Why is this useful?

Puts an input pin in a known state (logic high) if no external influence has pulled it down (to logic low)

Example of a switch connected between a pin and ground

How is it done? When the pin is configured as an input, SET the

corresponding bit in PORTxn Undone by clearing the bit

Page 14: Lecture Programming Micro Controllers

Redo Seat Belt Sensor System

Use port-style programmingATmega328

D0, D1

VTG= +5V

0

1

D2

D3

#define LATCHED 0#define ENGAGED 0DDRD | = _BV(2) | _BV(3); // D2 and D3 are OUTPUTsPORTD | = _BV(0) | _BV(1); // turn on pull-ups for D0 and D1current_state = ~PIND; // invert for active-low switcheskey_state=current_state & ( _BV(0) )belt_state=current_state & ( _BV(1) )if(key_state==ENGAGED) if(belt_state==LATCHED) PORTD & = ~( _BV(2) | _BV(3) ); // buzzer and lamp off else PORTD | = ( _BV(2) | _BV(3) ); // buzzer and lamp onelse PORTD & = ~( _BV(2) | _BV(3) ); // buzzer and lamp off

Key on D0Belt on D1

Page 15: Lecture Programming Micro Controllers

Recap ATmega Digital I/O Pins are bi-directional. Can configure as:

Inputs – _______ determines the pin voltage Outputs – ______ determines the pin voltage Direction determined by bits in DDRx register

Where x is B, C, D for the ATmega328 (and DDRx corresponds to all 8 pins associated with the port)

If configured as output: Program can specify a pin to be high (VTG) or low (GND) by

writing a corresponding 1 or 0 (respectively) to PORTx register

Ex. To make Port D pins 7, 3, and 4 low, and the rest high PORTD=___________; (write in binary, then in hex)

Page 16: Lecture Programming Micro Controllers

Recap ATmega Digital I/O, cont.

If pins configured as input, this means: External device can pull pin voltage high or low

i.e. take up to VTG or take down to GND

You can determine the state of the portpins by reading the PINx register

Grabs all eight logic levels at the same time Ex. PORTD configured as inputs

PD0

PD1

PD2

PD3

PD4

PD5

PD6

PD7

VTG

uint8_t a_pins;

a_pins=PIND;

What is the content of a_pins:

binary:__________

hex:_____

Page 17: Lecture Programming Micro Controllers

Recap ATmega Digital I/O, cont.

If pins configured as input, cont.: Can turn pull-up resistors on or off by

writing a 1 or 0 to corresponding pins in PORTx

A pull-up resistor internally connects a pin to VTG to give it a defined state (logic high, i.e., 1)

Ex. Write the code that will: Make Port D pins inputs Turn on pull-up resistors Read the voltages on the pins and store

them in a variable, testD What is the value of testD in binary

and hex? PD0

PD1

PD2

PD3

PD4

PD5

PD6

PD7 VTG

Page 18: Lecture Programming Micro Controllers

Reading PORTD Pins Example

unsigned char testD;

DDRD=0;

testD=PIND;

What is the content of testD?

binary: 11111001

hex: F9 PD0

PD1

PD2

PD3

PD4

PD5

PD6

PD7 VTG

Page 19: Lecture Programming Micro Controllers

ATmega328 Features

ATmega328 data sheet p. 1

http://www.atmel.com/dyn/resources/prod_documents/doc8271.pdf

Page 20: Lecture Programming Micro Controllers

ATmega328 Internal Architecture

ATmega328 data sheet pp. 2, 5

Page 21: Lecture Programming Micro Controllers

PORT Pin Schematics

ATmega328 datasheet, pp. 76-77

Page 22: Lecture Programming Micro Controllers

ATmega328 Port Pin Details See the ATmega328 data sheet, pp. 76-94 Port pin functionality is controlled by three register (special memory location) bits: DDRx

Data Direction bit in DDRx register (read/write)

PORTxn PORTxn bit in PORTx data register (read/write)

PINxn PINxn bit in PINx register (read only)