MAE3780 Lab06 Prelab Solution 2014
MAE3780 Lab06 Prelab Solution 2014
MAE3780 Lab06 Prelab Solution 2014
FALL 2013
LAB 6: INTRODUCTION TO ATMEL AND ARDUINO
PRELAB
//ThisincludestheClibraryforI/Odevices
#include<avr/io.h>
#defineF_CPU16000000UL
//ThissetstheclocktothecrystalontheUNO
#include<util/delay.h> //ThisincludestheClibraryforthe_ms_delay()method
#include<avr/interrupt.h>//ThisincludestheClibraryforinterrupts
//Youneedtoinsertyoursourcecodehere(oraftermain)
intmain(void)
{
//Andhere
while(1)
//Andhere
//Thisisthe"infinitewhileloop"
}
}
Recall: The microcontroller must never exit its main() function, or it will assume an unknown state
and will not operate as you would expect. Therefore we always include an infinite loop at the end of
main(). Any code that needs to be executed once in the beginning of your program (initialization
code) will go before the loop. Code that needs to be executed continuously will go in the loop.
MA
AE 3780 M E CHATRONIC
CS
FALL 201 3
Solution by A. McNicoll
Name: ______________________________________
In all of the following, lines are provided for your convenience they do not imply anything
about the length of your code.
Make sure to add comments to the code you write.
Before starting to work understanding power on the Arduino
1. Read the power section (it is not long read ALL of it) on the Arduino Uno webpage:
http://arduino.cc/en/Main/ArduinoBoardUno
The 5V pin.
2. Which pin will you use for Vcc in the touch sensor circuit? _________________
9V battery or other power jack
3. What will you connect (in the future) to Vin (if anything)? __________________
_______________________________________________________________________
_______________________________________________________________________
* There are MANY ways of doing the same thing - keep this in mind while checking.
From now on I'll only write one way (full binary mask); you can check equivalence.
The solution is NOT the best/most effective way of writing the code, but the most "basic".
Note, while direct assignment (=) works in many cases for this lab, it is ALWAYS better
practice to use |= or &=. You should always use these; it will make your code more flexible.
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
while(1)
Another way:
{ One
________________________________________________________________
way:
PORTB ^= 0b00100000;
________________________________________________________________
PORTB
&= 0b11011111;
_delay_ms(500);
________________________________________________________________
_delay_ms(500);
PORTB
|= 0b00100000;
________________________________________________________________
________________________________________________________________
_delay_ms(500);
________________________________________________________________
________________________________________________________________
________________________________________________________________
}
}
5. Given the maximum current from part 4, what is the minimal value the resistor R in the touch
sensor circuit should be? Explain.
Worst case scenario is pin configured as output low and sinks current from VCC
In that case, current I = 5/R = .04, so Rmin = 125 ohms.
Pull up/down resistors in this application are typically 1K - 10K.
int main(void)
{ ____________________________________________________________________
_______________________________________________________________________
DDRB
|= 0b00100000; Set PB5 to output. Below, set PB0 to input.
DDRB &= 0b11111110;
There is no need for this line, though, since pins default to INPUT (low).
_______________________________________________________________________
_______________________________________________________________________
while(1)
{
________________________________________________________________
Pin is high, so switch is open
if (PINB & 0b00000001) {
Turn off the LED
PORTB &= 0b11011111;
________________________________________________________________
Otherwise, switch must be closed
} else {
________________________________________________________________
PORTB |= 0b0010000;
________________________________________________________________
Turn on the LED
}
________________________________________________________________
________________________________________________________________
% This repeats forever in the infinite while loop... polling until eternity...
________________________________________________________________
}
}
}
_______________________________________________________________________
int main(void)
|= 0b00100000; Set PB5 to output. Below, set PD2 to input.
{ DDRB
____________________________________________________________________
DDRB &= 0b11111011;
INT0 is located on pin PD2. Again, no real need for this line.
_______________________________________________________________________
EICRA |= 0b00000001; Set the ISC00 bit: "Any logical change on INT0 generates... interrupt..."
_______________________________________________________________________
EIMSK |= 0b00000001; Set the INT0 bit: "External Interrupt Request 0 Enable"
sei(); Allow interrupts to happen (global interrupt enable)
_______________________________________________________________________
while(1)
{
________________________________________________________________
________________________________________________________________
________________________________________________________________
% No need to do anything at all - interrupt is taking care of it.
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
}
}
_______________________________________________________________________
8. Explain, briefly, what the difference is between polling and interrupt when reading sensor
information. Specifically, what happens to the execution, how fast does the microcontroller
react to the input and what can go wrong with each.
Polling's advantage:
- Straightforward to implement - no need to dig around datasheet
- Some things, like analog, need to be polled because there is no interrupt
Polling's disadvantages:
- Slow - only reacts when it checks, which is not necessarily often
- Ties up CPU; can't do anything else while you're checking pins
Interrupt advantages:
- Reacts immediately to interrupt conditions
- Can assign interrupts to all sorts of things; every pin has a PCINT
- Does not clog up main code - run another program if you want
Interrupt disadvantages:
- If not careful about using sei() and cli(), can run code in the middle of
doing something else you didn't want interrupted.
- Subtle variable/access issues - could potentially be trying to change a variable
that was in the middle of getting changed somewhere else, etc.