Beruflich Dokumente
Kultur Dokumente
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
User Interface
ME 106 ME 120
Controller
(Hardware & Software)
ME 106
Power Interface
INTEGRATION
Signal Conditioning
ME 106 ME 120
Actuator
Sensor
ME 120 ME 297A
8-bit register
7
Write code to make all pins of PORTD to be outputs (Arduino and alternate)
pinMode(7, OUTPUT);
Write code to make pins 5, 3, and 1 of PORTD to be outputs, and the rest inputs
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()
like while(1) {}
Where is main() ?
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:
#define LED_PIN = 13; // LED on digital pin 13 #define DELAY_ON = 1000; #define DELAY_OFF = 1000;
Why?
Symbolic names are usually put in all caps to differentiate from variables See me106.h
ATmega328
D3
D2
VTG= +5V
1 0
D0, D1
Bitwise AND the PORTx register with the corresponding complemented bit mask
Bitwise XOR the PORTx register with the corresponding bit mask
Pull-up Resistors
How is it done?
When the pin is configured as an input, SET the corresponding bit in PORTxn Undone by clearing the bit
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
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)
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
PD6
PD5
PD4 PD3 PD2 PD1 PD0
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
VTG
PD5
PD4 PD3 PD2 PD1 PD0
binary: 11111001
hex: F9
ATmega328 Features
http://www.atmel.com/dyn/resources/prod_documents/doc8271.pdf
DDRx
PORTxn
PINxn