Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
en:multiasm:exercisesbook:avr:sut:scenarios:avr1 [2026/05/03 17:56] pczekalskien:multiasm:exercisesbook:avr:sut:scenarios:avr1 [2026/05/03 19:19] (current) pczekalski
Line 1: Line 1:
 +====== AVR1: Hello World on AVR ======
 +
 +In this scenario, you will implement a classical "Hello World" application. For microcontrollers, the "Hello World" application is a blinking LED. Because we use remote observation via an integrated video camera stream, the proper LED frequency is at most 1 or 2Hz, depending on the connection quality. In this scenario, you will use blocking nested loops and manual calculation of the ticks needed to run them, to obtain precise ''delay'', as presented in the chapter [[..:..:..:arduinouno|]].\\\\
 +
 +** Prerequisites **\\
 +You need to book one of the AVR laboratory nodes and ensure the video stream is live.
 +
 +** Scenario **\\
 +Create an application that toggles the built-in LED on and off 1 time per second (the full cycle is then 2s). There are 4 LEDs on the shield you can observe; use GPIO 13 (the built-in LED on the Arduino Uno).
 +
 +** Result **\\
 +Flashing LED, observable via video stream.
 +
 +** Start **\\
 +Mind to use AVR GCC syntax (as in the instruction): node compilation facilities are preconfigured, and you do not need to build a Makefile; still, it is necessary to follow the exact AVR GCC syntax, e.g., in the case of ''.equ''.
 +
 +** Step 1 **\\
 +Compose an empty application with an infinite loop:
 +<code asm>
 +.org 0x0000
 +    rjmp RESET
 +    
 +RESET:
 +
 +LOOP:
 +   rjmp LOOP
 +</code>
 +Compile and check for any errors. Correct errors (if any), re-compile, and upload to the device. Naturally, nothing will happen now: your code is dummy, but you will ensure proper toolkit operation.
 +
 +** Step 2 **\\
 +Add to your code declarations regarding GPIO control ports for GPIO 13:
 +<code asm>
 +.equ DDRB,  0x04
 +.equ PORTB, 0x05
 +.equ PB5, 5                 ; PB5 is GPIO 13, and it is a built-in LED
 +...
 +</code>
 +
 +** Step 3 **\\
 +Configure GPIO 13 for output:
 +<code asm>
 +...
 +RESET:
 +    ldi r16, 1 << PB5        ; Set bit 5
 +    out DDRB, r16            ; Set PB5 as output 
 +...
 +</code>
 +
 +** Step 4 **\\
 +Prepare your delay function and place it at the end of the code (after 'rjmp LOOP'). Also, modify it accordingly to meet your requested delay time. Note that the CPU's base frequency is 16 MHz. Refer to the chapter [[..:..:..:arduinouno|]] on how to calculate the number of loops. The function below gives you approximately a 0.5s delay (506ms).
 +<code asm>
 +...
 +delay:
 +    ldi r20, 43     ; Outer loop
 +outer_loop:
 +    ldi r18, 250    ; Mid loop
 +mid_loop:
 +    ldi r19, 250    ; Inner loop
 +inner_loop:
 +    dec r19
 +    brne inner_loop
 +    dec r18
 +    brne mid_loop
 +    dec r20
 +    brne outer_loop
 +    ret
 +</code>
 +
 +** Step 5 **\\
 +Implement the main loop:
 +<code asm>
 +...
 +LOOP:
 +    sbi PORTB, PB5           ; Turn LED off
 +    rcall delay
 +    cbi PORTB, PB5           ; Turn LED on
 +    rcall delay
 +    rjmp LOOP
 +...
 +</code>
 +
 +** Result validation **\\
 +The LED should be flashing on an implemented time basis. Note that some irregularity may is observed due to the nature of video streaming over the network. It is natural and OK. If you want to measure more precise timing, increase the period, e.g. to 5s.
 +
 +** FAQ **\\
 +When using the printed version of this manual, please refer to the latest online version of this document to obtain the most up-to-date list of FAQs.\\
 +
 +**It does not flash**: Did you compile and upload to the device? Those are separate steps: it is not enough to just compile, but you also need to "flash" the MCU. Also, check your video stream if it "ticks" - the time embedded into the video stream should change. Your code may be working OK, but the video stream can be frozen, so you cannot see it working properly!
 +
 +**I need a longer delay**: To obtain a delay function with a period of about 2.5s, you need to introduce a fourth loop (an outer loop) in the delay; 3 will not provide you enough ticks. Eventually, you can switch to 16-bit counters.
 +//
 +
  
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0