diff --git a/content/08_embedded_programming.md b/content/08_embedded_programming.md index 624af4be23e700f2d37e9f9a4f85eca7f1602922..4f640c93bee1e2e7cb3f1a8e5c7489b0dcf423c2 100644 --- a/content/08_embedded_programming.md +++ b/content/08_embedded_programming.md @@ -40,6 +40,8 @@ If this doesn't work after programming, there are the usual suspects to check. - Are your solder joints smooth and shiny? - Is your schematic ok? Your LED will need a resistor of an appropriate value (I used 499 ohms) in parallel. + + ### Using the Button as a Contact Switch @@ -76,6 +78,7 @@ int main(void) { } {{< /highlight >}} +<video controls src="/img/08_button_contact.mp4"></video> ### Using the Button as a Toggle @@ -87,7 +90,7 @@ Let's ignore the button for now, and use a timer to blink the LED. As the ATTINY The only initialization steps are to set the clock and timer prescalers. Page 31 of the datasheet gives the options for the former. We'll use the full 20MHz provided by the resonator. Page 8 gives the options for the timer. I'll use a prescaler of 1024 so that my timer ticks on the order of microseconds instead of nanoseconds. Reading the timer, or setting it to a particular value, is easy: just use the `TCNT0` register. -There's only one issue left: even though we scale the clock down by a factor of 1024, the maximum amount of time the timer can record is 255 * 1024 / 20,000,000 = 0.013056 seconds. If we turn the timer off and on with this period, we might notice it's dimmer than before but we'll never be able to see it change from on to off or vice versa. To get around this I'll let the timer tell me when 195 * 1024 / 20,000,000 ~ 10ms have passed, and I'll only toggle the LED when this happened 250 times. So we should see the LED blink every 2.5 seconds. (The time to evaluate the additional instructions is negligible compared to the amount of time we're waiting for the timer.) +There's only one issue left: even though we scale the clock down by a factor of 1024, the maximum amount of time the timer can record is 255 * 1024 / 20,000,000 = 0.013056 seconds. If we turn the timer off and on with this period, we might notice it's dimmer than before but we'll never be able to see it change from on to off or vice versa. To get around this I'll let the timer tell me when 195 * 1024 / 20,000,000 ≈ 10ms have passed, and I'll only toggle the LED when this happened 250 times. So we should see the LED blink every 2.5 seconds. (The time to evaluate the additional instructions is negligible compared to the amount of time we're waiting for the timer.) {{< highlight c >}} #include <avr/io.h> @@ -182,3 +185,5 @@ int main(void) { } {{< /highlight >}} +<video controls src="/img/08_button_toggle.mp4"></video> + diff --git a/static/img/08_button_contact.mp4 b/static/img/08_button_contact.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..a01e1bd4f0f24b4e4b64c723e2e23ddb114f8d02 Binary files /dev/null and b/static/img/08_button_contact.mp4 differ diff --git a/static/img/08_button_toggle.mp4 b/static/img/08_button_toggle.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..a57793b6ddb148e460242d23fd283e20111ad436 Binary files /dev/null and b/static/img/08_button_toggle.mp4 differ diff --git a/static/img/08_led_on.jpg b/static/img/08_led_on.jpg new file mode 100644 index 0000000000000000000000000000000000000000..1adbcb5c8dede52bffe021a5a92139aa89b8a56f Binary files /dev/null and b/static/img/08_led_on.jpg differ