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 154 ME 157 ME 195 Signal Conditioning ME 106 ME 190 ME 187 ME 106 ME 120 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 Digital pins can be inputs or outputs 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 ATmega328) register determines direction 8-bit register 7 6 5 a ‘1’ in DDRx means…? a ‘0’ in DDRx means…? 4 3 2 1 0 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() setup() loop() 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 ATmega328 D3 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 ; D2 VTG= +5V 1 D0, D1 0 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 Clearing a bit (making it a ‘0’) Bitwise AND the PORTx register with the corresponding complemented bit mask Ex. PORTB | = _BV(3); 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 #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 ATmega328 D3 D2 VTG= +5V 1 D0, D1 0 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 (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 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 uint8_t a_pins; a_pins=PIND; What is the content of a_pins: binary:__________ hex:_____ PD6 PD5 PD4 PD3 PD2 PD1 PD0 VTG 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? PD7 PD6 PD5 PD4 PD3 PD2 PD1 PD0 VTG Reading PORTD Pins Example unsigned char testD; PD7 DDRD=0; PD6 testD=PIND; PD5 What is the content of testD? PD3 binary: 11111001 PD4 PD2 PD1 hex: F9 PD0 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 PORTxn Data Direction bit in DDRx register (read/write) PORTxn bit in PORTx data register (read/write) PINxn PINxn bit in PINx register (read only)