๐Ÿ‡ฎ๐Ÿ‡น IT ๐Ÿ‡ฌ๐Ÿ‡ง EN

๐Ÿ”„ Servo G90 360 in AVR Assembly
From stop to rotation with r17

For absolute beginners and advanced. The secret is all in the r17 value.
๐Ÿ“ฆ for beginners

Wardrobe metaphor

Imagine a wardrobe with many drawers. Each drawer has 8 compartments.
In our case: address 4 (DDRB) and address 5 (PORTB) are two adjacent drawers.

Each compartment corresponds to a physical pin of the microcontroller.
Compartment number 5 (bit 5) corresponds to physical pin D13 (the one with the built-in LED on Arduino).

DDRB drawer (address 4):
Each switch decides if the pin can "talk" (OUTPUT) or only "listen" (INPUT).
If you put 1 in compartment 5 of drawer 4 โ†’ pin D13 becomes OUTPUT.

PORTB drawer (address 5):
Each switch sends current (5V) or not (0V) to the pin.
If you put 1 in compartment 5 of drawer 5 โ†’ pin D13 goes to 5V (LED ON).
If you put 0 โ†’ pin D13 goes to 0V (LED OFF).

This is why it works:

; Set Bit in register 4, bit 5 โ†’ compartment 5 of drawer 4 = 1 (D13 OUTPUT) sbi 4,5 ; Set Bit in register 5, bit 5 โ†’ compartment 5 of drawer 5 = 1 (D13 = 5V, LED ON) sbi 5,5 ; Clear Bit in register 5, bit 5 โ†’ compartment 5 of drawer 5 = 0 (D13 = 0V, LED OFF) cbi 5,5
๐Ÿšช This is not magic

It's just opening and closing switches in drawers. The exact same principle applies to any programmable peripheral: sound cards, car ECUs, other microcontrollers.

๐ŸŽฅ Video: G90 360 in action

Servo G90 360 controlled in pure assembly, no IDE, no libraries, directly from the browser.

๐ŸŽฏ The code for G90 360 servo

; Brown GND, Red +5V, Yellow PWM on D13 .org 0 ; start from beginning (reset vector) rjmp init .org 0x68 ; skip interrupt table, continue from here init: sbi 4,5 ; DDRB bit5 = 1 (pin D13 OUTPUT) loop: sbi 5,5 ; start pulse rcall wait1 ; pause (decided by r17) cbi 5,5 ; end pulse rcall wait1 ; pause (same duration) rjmp loop wait1: ldi r17, 31 ; <-- THIS VALUE DECIDES THE BEHAVIOR wait: dec r16 brne wait dec r17 brne wait ret

๐Ÿ“Œ The r17 value determines the pulse duration. 31 = 1.5ms = servo stopped at center.

step 1

๐Ÿ”Œ Connections

Servo G90 360Arduino
Brown (GND)GND
Red (+5V)5V
Yellow (PWM signal)D13 (pin 13)

How to upload the code:
Go to costycnc.it/avr1/compiler.html, paste the code, click "Assemble" then "Connect".

step 2

๐ŸŽฎ How the servo behaves

A G90 360 servo (continuous rotation) interprets the pulse duration as command:

r17 valuePulse durationBehavior
25-301.0-1.4 msRotates one way (e.g. clockwise)
311.5 msSTOP at center
32-371.6-2.0 msRotates opposite way (e.g. counterclockwise)

The farther the value from 31, the faster it rotates.

๐Ÿงฎ Why r17=31 gives 1.5ms?

With 16MHz clock, 31 ร— 512 cycles โ‰ˆ 15,872 cycles โ‰ˆ 0.99ms. With two calls and surrounding code, you get about 1.5ms and 18.5ms.

โณ Why does uninitialized r16 work?

At power-on, all general registers (including r16) are 0. First dec r16 โ†’ 0-1 = 255, then 254... down to 0. It works, but in complex programs you should initialize with ldi r16, 0.

step 3

๐Ÿ“‹ Instructions used

InstructionMeaning (simple explanation)
.org N"address where code starts" (e.g. .org 0 = start from beginning, .org 0x68 = skip interrupt table)
rjmp label"jump to label" (go forward or backward in code)
sbi a,b"set bit b in register a" (compartment b of drawer a = 1)
cbi a,b"clear bit b in register a" (compartment b of drawer a = 0)
rcall label"call subroutine label" (go there, do stuff, then return)
ret"return" (after rcall)
ldi r,val"load value val into register r" (e.g. ldi r17,31 = r17=31)
dec r"decrement register r" (r = r - 1)
brne label"branch if not equal" (if last operation didn't give zero, jump to label)
๐ŸŽ›๏ธ advanced

Same principle everywhere

If you understand this, you understand any programmable peripheral.

Example 1: Realtek sound card
The driver writes bits to specific registers to raise/lower volume. Same thing.

Example 2: Car and OBD2
The ECU has hundreds of registers. Speed, RPM, temperature are numbers in registers.

Example 3: Other microcontrollers
STM32, ESP32, PIC: names change, but the principle is identical.

๐Ÿ“ Test yourself

Question: What happens if you change r17 from 31 to 28?

Show answer

Shorter pulse (1.3-1.4ms). The servo rotates one way. The smaller r17, the faster.