# Programming Microcontrollers B. Furman 19MAR2011.

## Presentation on theme: "Programming Microcontrollers B. Furman 19MAR2011."— Presentation transcript:

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 Controller (Hardware & Software) System to Control Sensor Signal Conditioning Power Interface Actuator User Interface Power Source BJ Furman 22JAN11 ME 106 ME 154 ME 157 ME 195 ME 120 ME 297A ME 106 ME 120 ME 106 ME 190 ME 187 ME 110 ME 136 ME 154 ME 157 ME 182 ME 189 ME 195 ME 106 ME 120 ME 106 INTEGRATION

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

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

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

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 repeats BJ Furman rev. 1.1 Last rev: 22JAN2011 */

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;

How to Twiddle Bits Recall the example of the seat belt indicator system CC code snippet (not full program) ATmega328 D0, D1 V TG = +5V 0 1 D2 D3 #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 ;

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 :

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 1. 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

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 D0, D1 V TG = +5V 0 1 D2 D3 #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 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)  If configured as output: Program can specify a pin to be high (V TG ) 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)

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  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:_____

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

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

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