In this scenario, you will implement a code that displays on the 7-segment display.
Prerequisites
You need to book one of the AVR laboratory nodes and ensure the video stream is live.
Scenario
Implement an algorithm that displays your birth year (or any other 4-digit number) on the LED 7-segment display.
Result
Observe the number via the video stream.
Start
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.
Use the function display_digit that handles displaying a single digit, as provided in the SUT AVR Assembler Laboratory Node Hardware Reference to bootstrap your code.
Also note you need to define a stack to use delay separately (if you use rcall and ret instructions). It is obligatory.
In this algorithm, you go “full throttle”. You should implement a loop that displays four consecutive digits. At 16 MHz, shield latches responsible for handling the display are close to their limits but still operate fine. Naturally, in a real-world scenario, you would not waste energy implementing it this way. Would you?
Step 1
Compose application definitions and configuration. Set up a stack. We do not use the'.section' directive here, but if you plan to use RAM, '.sections' are required and simplify your code.
; --- Single .equ for RAM End --- .equ SPH, 0x3E ; Stack Pointer High .equ SPL, 0x3D ; Stack Pointer Low .equ SER_PORT, 0x05 ; PORTB I/O address .equ SER_PIN, 0 ; GPIO8 .equ DDRD, 0x0A ; Data Direction Port D .equ DDRB, 0x04 ; Data Direction Port B .equ CLK_PORT, 0x0B ; PORTD I/O address .equ CLK_PIN, 7 ; GPIO7 .equ LAT_PORT, 0x0B ; PORTD I/O address .equ LAT_PIN, 4 ; GPIO4 .equ RAMEND, 0x08FF ; Arduino Uno memory size .section .text .org 0x0000 rjmp RESET reset: ; Initialise Stack Pointer using hi8 and lo8 functions ldi r16, lo8(RAM_END) out SPL, r16 ldi r16, hi8(RAM_END) out SPH, r16
Step 2
Configure pins that control the display as outputs (GPIO 8, GPIO 7, GPIO 4):
; Initialise display control outputs sbi DDRB, SER_PIN ; Set PB0 as output sbi DDRD, CLK_PIN ; Set PD7 as output sbi DDRD, LAT_PIN ; Set PD4 as output
Step 3
Make a loop that calls the 'display_digit' function four times, varying the numbers and positions. Load arguments.
As you use constant variables (the easiest, naturally, use ldi to load registers. Note that digit indexes are zero-based; thus, the leftmost digit is at position 0, and the rightmost is at position 3.
Step4
Copy display_digit function from the manual to your code and put it after the end of your main loop.
main_loop: ; Display Pattern A (D2, D4 ON) ldi r16, PATTERN_A out PORTB, r16 rcall delay_2s ; Display Pattern B (D1, D3 ON) ldi r16, PATTERN_B out PORTB, r16 rcall delay_2s rjmp main_loop
Result validation
You should be able to see the number on the 7-segment display via the video stream.
FAQ
When using the printed version of this manual, please refer to the latest online version for the most up-to-date list of FAQs.
It does not show up at all: 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!