Programming Microcontrollers B Furman 19 MAR 2011 Learning
Programming Microcontrollers B. Furman 19 MAR 2011
Learning Objectives n n 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 ME 106 (Hardware & Software) Power Interface INTEGRATION ME 106 ME 154 ME 157 ME 195 Signal Conditioning ME 106 ME 190 ME 187 ME 106 ME 120 Actuator Sensor System to Control ME 110 ME 182 ME 136 ME 189 ME 154 ME 195 ME 157 ME 120 ME 297 A BJ Furman 22 JAN 11
Recap Last Lecture n Binary and hex numbers ¨ n Digital pins can be inputs or outputs ¨ n Why use hex? What is the difference? Pins are bidirectional for digital I/O Which Arduino function do you use? ¨ DDRx (x = B, C, or D for ATmega 328) register determines direction ¨ n 8 -bit register ¨ ¨ 7 6 5 a ‘ 1’ in DDRx means…? a ‘ 0’ in DDRx means…? 4 3 2 1 0
Test Your Comprehension n Write code to make all pins of PORTD to be outputs (Arduino and alternate) DDRD = 0 x. FF; ¨ DDRD = 0 b 1111; ¨ DDRD = 255; ¨ n Arduino style pin. Mode(0, OUTPUT); pin. Mode(7, OUTPUT); Write code to make pins 5, 3, and 1 of PORTD to be outputs, and the rest inputs DDRD = 0 b 00101010; ¨ DDRD = 0 x 2 A; ¨ ¨ DDRD | = (1<<5) | (1<<3) | (1<<1); Arduino style pin. Mode(1, OUTPUT); pin. Mode(3, OUTPUT); pin. Mode(5, OUTPUT);
Structure of an Arduino Program n An arduino program == ‘sketch’ ¨ Must have: n n ¨ setup() loop() configures pin modes and registers loop() n runs the main body of the program forever ¨ ¨ like while(1) {…} Where is main() ? n n 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: 22 JAN 2011 */ #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: pin. Mode(LED_PIN, OUTPUT); } // loop() method runs forever, // as long as the Arduino has power void loop() { digital. Write(LED_PIN, HIGH); // set the LED on delay(DELAY_ON); // wait for DELAY_ON msec digital. Write(LED_PIN, LOW); // set the LED off delay(DELAY_OFF); // wait for DELAY_OFF msec }
Best Practices and Patterns -1 n Programmer’s block ¨ At a minimum: n n n ¨ Program name Description of what the program does Author Revision number Revision date/time Even better: n n 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: 22 JAN 2011 */
Best Practices and Patterns -2 n Avoid ‘hard coding’ constants ¨ Use #define and symbolic names instead n Why? Symbolic names are usually put in all caps to differentiate from variables ¨ See me 106. h ¨ #define LED_PIN = 13; // LED on digital pin 13 #define DELAY_ON = 1000; #define DELAY_OFF = 1000;
How to Twiddle Bits n Recall the example of the seat belt indicator system ¨ ATmega 328 D 3 C code snippet (not full program) #define LATCHED 0 #define ENGAGED 0 pin. Mode(0, INPUT); // key switch pin. Mode(1, INPUT); // belt latch switch pin. Mode(2, OUTPUT); // lamp pin. Mode(3, OUTPUT); // buzzer key_state=digital. Read(0); belt_state=digital. Read 1); if(key_state==ENGAGED) if(belt_state==LATCHED) digital. Write(3, LOW); digital. Write(2, LOW); else digital. Write(2, HIGH); digital. Write(3, HIGH); else ; D 2 VTG= +5 V 1 D 0, D 1 0
Bit Manipulation Practice n 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 n Setting a bit (making it a ‘ 1’) ¨ Bitwise OR the PORTx register with the corresponding bit mask n n Clearing a bit (making it a ‘ 0’) ¨ Bitwise AND the PORTx register with the corresponding complemented bit mask n n Ex. PORTB | = _BV(3); Ex. PORTB & = ~( _BV(3) ); Toggling a bit (making it flip) ¨ Bitwise XOR the PORTx register with the corresponding bit mask n Ex. PORTB ^ = _BV(3);
Bit Twiddling Practice n 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. n Check if pin 9 is high If pin 9 is high, make pin 13 high and pin 11 low ¨ n Else both pins 13 should be low Use the Arduino method 2. Use the general port-style method 1.
Pull-up Resistors n Pins configured as INPUTS can be ‘pulled up’ to VTG ¨ Why is this useful? n Puts an input pin in a known state (logic high) if no external influence has pulled it down (to logic low) n Example of a switch connected between a pin and ground ¨ How is it done? n When the pin is configured as an input, SET the corresponding bit in PORTxn n Undone by clearing the bit
Redo Seat Belt Sensor System n Use port-style programming #define LATCHED 0 #define ENGAGED 0 DDRD | = _BV(2) | _BV(3); // D 2 and D 3 are OUTPUTs PORTD | = _BV(0) | _BV(1); // turn on pull-ups for D 0 and D 1 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 ATmega 328 D 3 D 2 VTG= +5 V 1 D 0, D 1 0 Key on D 0 Belt on D 1
Recap ATmega Digital I/O n Pins are bi-directional. Can configure as: Inputs – _______ determines the pin voltage ¨ Outputs – ______ determines the pin voltage ¨ Direction determined by bits in DDRx register ¨ n ¨ Where x is B, C, D for the ATmega 328 (and DDRx corresponds to all 8 pins associated with the port) If configured as output: n 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)
Recap ATmega Digital I/O, cont. ¨ If n pins configured as input, this means: External device can pull pin voltage high or low ¨ n 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 PD 7 ¨ Ex. PORTD configured as inputs ¨ uint 8_t a_pins; a_pins=PIND; What is the content of a_pins: binary: _____ hex: _____ PD 6 PD 5 PD 4 PD 3 PD 2 PD 1 PD 0 VTG
Recap ATmega Digital I/O, cont. ¨ If n pins configured as input, cont. : Can turn pull-up resistors on or off by writing a 1 or 0 to corresponding pins in PORTx ¨ n 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, test. D § What is the value of test. D in binary and hex? ¨ PD 7 PD 6 PD 5 PD 4 PD 3 PD 2 PD 1 PD 0 VTG
Reading PORTD Pins Example unsigned char test. D; PD 7 DDRD=0; PD 6 test. D=PIND; PD 5 What is the content of test. D? PD 3 PD 4 binary: 11111001 PD 2 hex: F 9 PD 0 PD 1 VTG
ATmega 328 Features ATmega 328 data sheet p. 1 http: //www. atmel. com/dyn/resources/prod_documents/doc 8271. pdf
ATmega 328 Internal Architecture ATmega 328 data sheet pp. 2, 5
PORT Pin Schematics ATmega 328 datasheet, pp. 76 -77
ATmega 328 Port Pin Details See the ATmega 328 data sheet, pp. 76 -94 n Port pin functionality is controlled by three register (special memory location) bits: n ¨ DDRx n Data Direction bit in DDRx register (read/write) ¨ PORTxn n PORTxn bit in PORTx data register (read/write) ¨ PINxn n PINxn bit in PINx register (read only)
- Slides: 22