230 likes | 280 Views
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
E N D
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
PowerSource SignalConditioning PowerInterface UserInterface Actuator Sensor System toControl ME 110 ME 136 ME 154 ME 157 ME 182 ME 189 ME 195 Mechatronics Concept Map ME 106 ME 120 Controller(Hardware & Software) ME 106 ME 190 ME 187 ME 106 INTEGRATION ME 106 ME 120 ME 106 ME 154 ME 157 ME 195 ME 120 ME 297A BJ Furman 22JAN11
7 6 5 4 3 2 1 0 Recap Last Lecture Audience participation required! • 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…?
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); Arduino style pinMode(0, OUTPUT); pinMode(7, OUTPUT); 0xFF; Arduino style pinMode(1, OUTPUT); pinMode(3, OUTPUT); pinMode(5, OUTPUT);
Structure of an Arduino Program /* 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 } • 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
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 code snippet (not full program) ATmega328 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 ; D2 VTG= +5V 1 D0, D1 0
Challenge: Make bits 5 and 3 of PORTB high and the rest low Bit Manipulation Practice • See the handout on Bit Manipulation • Setting bits • Clearing bits • Toggling bits
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 • 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 • Use the Arduino method • 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 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 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)
VTG PD7 PD6 PD5 PD4 PD3 PD2 PD1 PD0 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 uint8_t a_pins; a_pins=PIND; What is the content of a_pins: binary:__________ hex:_____
VTG PD7 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) • 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?
VTG PD7 PD6 PD5 PD4 PD3 PD2 PD1 PD0 Reading PORTD Pins Example unsigned char testD; DDRD=0; testD=PIND; What is the content of testD? 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 • PORTxn bit in PORTx data register (read/write) • PINxn • PINxn bit in PINx register (read only)