Sie sind auf Seite 1von 22

Programming Microcontrollers

B. Furman 19MAR2011

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

Mechatronics Concept Map


Power Source

User Interface

ME 106 ME 120

Controller
(Hardware & Software)
ME 106

Power Interface
INTEGRATION

ME 106 ME 190 ME 187

Signal Conditioning

ME 106 ME 120

ME 106 ME 154 ME 157 ME 195

Actuator
Sensor
ME 120 ME 297A

System to Control ME 110 ME 182


ME 136 ME 189 ME 154 ME 195 ME 157
BJ Furman 22JAN11

Recap Last Lecture


Binary and hex numbers

Why use hex?

Digital pins can be inputs or outputs

What is the difference?


Which Arduino function do you use? DDRx (x = B, C, or D for ATmega328) register determines direction

Pins are bidirectional for digital I/O


8-bit register
7

a 1 in DDRx means? a 0 in DDRx means?

Test Your Comprehension

Write code to make all pins of PORTD to be outputs (Arduino and alternate)

DDRD = 0xFF; DDRD = 0b11111111; DDRD = 255;

Arduino style pinMode(0, OUTPUT);

pinMode(7, OUTPUT);

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);

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

Structure of an Arduino Program

An arduino program == sketch Must have:


setup() loop() configures pin modes and registers runs the main body of the program forever

/* Blink - turns on an LED for DELAY_ON msec, then off for DELAY_OFF msec, and repeats BJ 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); }

setup()

loop()

// 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 }

like while(1) {}

Where is main() ?

Arduino simplifies things Does things for you

Best Practices and Patterns -1

Programmers block

At a minimum:

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

Program name Description of what the program does Author Revision number Revision date/time Creation date Inputs Outputs Method/algorithm

Even better:

Best Practices and Patterns -2

Avoid hard coding constants

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

Use #define and symbolic names instead

Why?

Symbolic names are usually put in all caps to differentiate from variables See me106.h

How to Twiddle Bits

Recall the example of the seat belt indicator system

ATmega328

D3
D2
VTG= +5V

C code snippet (not full program)


#define LATCHED 0 #define ENGAGED 0 pinMode(0, INPUT); // key switch pinMode(1, INPUT); // belt latch switch pinMode(2, OUTPUT); // lamp pinMode(3, OUTPUT); // buzzer key_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 ;

1 0

D0, D1

Bit Manipulation Practice

See the handout on Bit Manipulation

Setting bits Clearing bits Toggling bits


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

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);

Bit Twiddling Practice

Make Arduino pins 11 13 to be outputs and pins 8 10 to be inputs


Use the Arduino method 2. Use the all-at-once (general) method
1.

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

Use the Arduino method 2. Use the general port-style method


1.

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

Redo Seat Belt Sensor System

Use port-style programming

ATmega328

D3
D2
VTG= +5V

#define LATCHED 0 #define ENGAGED 0 DDRD | = _BV(2) | _BV(3); // D2 and D3 are OUTPUTs PORTD | = _BV(0) | _BV(1); // turn on pull-ups for D0 and D1 current_state = ~PIND; // invert for active-low switches key_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 on else PORTD & = ~( _BV(2) | _BV(3) ); // buzzer and lamp off

1 0

D0, D1

Key on D0 Belt on D1

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) Program can specify a pin to be high (VTG) or low (GND) by writing a corresponding 1 or 0 (respectively) to PORTx register

If configured as output:

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

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 port pins by reading the PINx register
Grabs all eight logic levels at the same time PD7 Ex. PORTD configured as inputs
VTG

uint8_t a_pins; a_pins=PIND; What is the content of a_pins: binary:__________ hex:_____

PD6

PD5
PD4 PD3 PD2 PD1 PD0

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)
PD7 PD6 PD5 PD4 PD3 PD2 PD1 PD0 VTG

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?

Reading PORTD Pins Example


unsigned char testD; DDRD=0; testD=PIND; What is the content of testD?
PD7 PD6

VTG

PD5
PD4 PD3 PD2 PD1 PD0

binary: 11111001
hex: F9

ATmega328 Features

ATmega328 data sheet p. 1

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

ATmega328 Internal Architecture

ATmega328 data sheet pp. 2, 5

PORT Pin Schematics

ATmega328 datasheet, pp. 76-77

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 bit in PORTx data register (read/write) PINxn bit in PINx register (read only)

PORTxn

PINxn

Das könnte Ihnen auch gefallen