ece312/lab_1/answers.md

38 lines
2.0 KiB
Markdown

# Question 1:
Defining a macro to hold a constant value essentially tells the compiler
to fill in that value where it is placed before actually compiling,
while declaring a constant variable creates a value in memory that holds the variable.
The advantage of a macro is that it is smaller, and can take up less code space if used
in certain contexts, and puts some of the work into the preprocessor instead of
the microcontroller. However, macros can sometimes be confusing to work with, as the
compiler errors can be less useful, and you can occasionally have to follow long chains of
definitions. Constant variables are also able to be referenced via pointers which can be
necessary in a context that needs pointers.
# Question 2:
F_CPU is a deginition that affects the math used for the delay functions
(_delay_ms() and _delay_us()). Changing this macro does not change the clock frequency,
as that has to be changed (in the case of the Attiny13A) by setting the the various
clock-related fuse bits, like CLKDIV8, and the CLKSEL bits.
# Question 3:
DDRx is the genericised name for the collection of Data Direction Registers
(DDRA, DDRB, etc.). By setting individual bits in these registers, you change whether the
correlating pin is an input or an ouptut (0 or 1, respectively).
PORTx is similar, it effects the values of these pins. When DDRB has the relevent pin set
as an output, the value of the same bit is set as the output (0 is low, 1 is high). If it
is set as an input, the bit in PORTB determines if the integrated pullup resistor is
connected. (1 means it's connected, 0 means it is not, and the pin is left floating)
It is typically better to use bitwise operates to set individual bits, rather than setting
all of the bits at once so that you don't have to keep track of what each pin should be.
By using the bitwise operators, you can keep the state of the pins that are not immediately
relevant with no decision overhead from the programmer (i.e. you would not have to keep
track of the other pins state, you just need to care about the single pin).