This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:multiasm:exercisesbook:avr:sut:scenarios:avr1 [2026/05/02 12:11] – pczekalski | en: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, | ||
| + | |||
| + | ** 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): | ||
| + | |||
| + | ** Step 1 **\\ | ||
| + | Compose an empty application with an infinite loop: | ||
| + | <code asm> | ||
| + | .org 0x0000 | ||
| + | rjmp RESET | ||
| + | | ||
| + | RESET: | ||
| + | |||
| + | LOOP: | ||
| + | rjmp LOOP | ||
| + | </ | ||
| + | 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 | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | ** Step 3 **\\ | ||
| + | Configure GPIO 13 for output: | ||
| + | <code asm> | ||
| + | ... | ||
| + | RESET: | ||
| + | ldi r16, 1 << PB5 ; Set bit 5 | ||
| + | out DDRB, r16 ; Set PB5 as output | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | ** Step 4 **\\ | ||
| + | Prepare your delay function and place it at the end of the code (after 'rjmp LOOP' | ||
| + | <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 | ||
| + | </ | ||
| + | |||
| + | ** 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 | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | ** 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 " | ||
| + | |||
| + | **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. | ||
| + | // | ||
| + | |||