{"id":780,"date":"2018-08-28T22:22:28","date_gmt":"2018-08-28T21:22:28","guid":{"rendered":"http:\/\/coding.moris.org\/?p=780"},"modified":"2021-04-03T19:00:07","modified_gmt":"2021-04-03T18:00:07","slug":"programming-arduino-in-atmel-studio","status":"publish","type":"post","link":"https:\/\/priscimon.net\/coding\/2018\/08\/28\/programming-arduino-in-atmel-studio\/","title":{"rendered":"Programming Arduino in Atmel Studio"},"content":{"rendered":"\n<p>As a fun way to improve my C, I started programming the Arduino using Atmel Studio instead of the friendlier Arduino IDE.<\/p>\n\n\n\n<p>Below is my very first program. It blinks a red LED and a white LED according to a preset pattern.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted lang:c decode:true\">#define F_CPU 16000000UL\n\n#include &lt;avr\/io.h&gt;\n#include &lt;util\/delay.h&gt;\n\nstruct Led\n{\n    int pin;\n    int blinks;\n    int on_duration;\n    int off_duration;\n};\n\nvoid delay_ms(int ms)\n{\n    while (ms-- &gt; 0) {\n        _delay_ms(1);\n    }\n}\n\nvoid blink(const struct Led* const led)\n{\n    for (int i = 0; i &lt; led-&gt;blinks; i++) {\n        PORTB |= led-&gt;pin;\n        delay_ms(led-&gt;on_duration);\n        PORTB &amp;= ~led-&gt;pin;\n        delay_ms(led-&gt;off_duration);\n    }\n}\n\nint main(void)\n{\n    const struct Led white_led = { 1&lt;&lt;PB6, 10, 100, 500 };\n    const struct Led red_led = { 1&lt;&lt;PB7, 10, 500, 1000 };\n\n    DDRB |= white_led.pin;\n    DDRB |= red_led.pin;\n    while (1) {\n        blink(&amp;white_led);\n        blink(&amp;red_led);\n    }\n}<\/pre>\n\n\n\n<p>With C in Atmel Studio and the AVR-LIBC library, IO ports are manipulated by changing bit patterns in the registers associated with the relevant ports. This requires a good understanding of bitwise operations in C despite the availability of macros to simplify the task.<\/p>\n\n\n\n<p>For example, to set pin 12 of the Arduino to output mode, bit 6 of register DDRB must be set to 1. To do so requires an <code>|<\/code> (OR) operation with a bit pattern operand where bit 6 is set to 1 and the rest set to 0, so that the states of the other bits in the register are not disturbed.<\/p>\n\n\n\n<p>Using macros <code>DDRB<\/code> and <code>PB6<\/code>&nbsp;defined in AVR-LIBC, this is done like this: <span class=\"lang:c decode:true  crayon-inline \">DDRB |= 1 &lt;&lt; PB6<\/span>&nbsp;.<\/p>\n\n\n\n<p>If you are new to C and are unfamiliar with macros, you might wonder about that statement. Besides, <code>DDRB<\/code> and <code>PB6<\/code> are not referenced anywhere else in my program, so how does this line of code work?<\/p>\n\n\n\n<p><code>DDRB<\/code> is a macro that expands into C code to dereference the address of the register associated with setting the mode for pin 12, and <code>PB6<\/code> is just a symbolic constant for the value 6. In the statement above, by shifting the value 1 left by 6 positions, we create a new value which is then applied to the bit pattern stored at the dereferenced address with an <code>|<\/code> operation to turn bit 6 of the register to 1. In this case, this sets pin 12 to output mode.<\/p>\n\n\n\n<p>In a nutshell, the sequence of operations is as follows.<\/p>\n\n\n\n<p>Step 1:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted lang:default decode:true\">1 &lt;&lt; 6 = 01000000<\/pre>\n\n\n\n<p>Step 2:<\/p>\n\n\n\n<p>Assuming the register DDRB is initially <code>00000001<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted lang:default decode:true\">00000001 | 01000000 = 01000001<\/pre>\n\n\n\n<p>In my C program, the result of step 1 is assigned to struct field <code>Led.pin<\/code> and is used as the second operand for the operation in step 2.<\/p>\n\n\n\n<p>It took about an hour to refresh my knowledge of bitwise operations, but the real challenge was interpreting the <a href=\"https:\/\/www.arduino.cc\/en\/uploads\/Main\/arduino-mega2560_R3-sch.pdf\">Arduino schema<\/a> and the <a href=\"http:\/\/ww1.microchip.com\/downloads\/en\/DeviceDoc\/Atmel-2549-8-bit-AVR-Microcontroller-ATmega640-1280-1281-2560-2561_datasheet.pdf\">information in datasheets<\/a>, especially to find the right registers to manipulate.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a fun way to improve my C, I started programming the Arduino using Atmel Studio instead of the friendlier Arduino IDE. Below is my very first program. It blinks a red LED and a white LED according to a preset pattern. #define F_CPU 16000000UL #include &lt;avr\/io.h&gt; #include &lt;util\/delay.h&gt; struct Led { int pin; int [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-780","post","type-post","status-publish","format-standard","hentry","category-general"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3I4g9-cA","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/posts\/780","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/comments?post=780"}],"version-history":[{"count":25,"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/posts\/780\/revisions"}],"predecessor-version":[{"id":1316,"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/posts\/780\/revisions\/1316"}],"wp:attachment":[{"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/media?parent=780"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/categories?post=780"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/tags?post=780"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}