Get in touch

Term project for the Digital Systems course: a full adder that adds two 4-bit numbers (A and B) with a carry-in (Cin) and shows the result on 5 LEDs. It is not built from logic gates; it is written directly in AVR assembly on an ATmega16 microcontroller and simulated in Proteus. The aim was to follow an algorithm all the way down to machine code.

Role
Solo project: design, code, simulation, report
Period
Fall 2024-25
Tools
ATmega16, AVR assembly, Microchip Studio 7, Proteus 8.10
Difficulty
Beginner
Status
Done
Top: gate-level 1-bit full adder; bottom: four full adders chained by their carries into a 4-bit adder

The gate-level equivalent of what the code does. Instead of building these gates one by one, the code uses the processor’s add instruction.

How it works

  • Inputs: A on PA0–PA3, B on PB0–PB3, Cin on PB5 (internal pull-ups off).
  • Addition: one 8-bit ADD chain, A + B + Cin, instead of four gate-level adders. Bit 4 of the sum is the carry-out (Cout), since the largest result is 15 + 15 + 1 = 31.
  • Output: the result goes to PORTC. Left to right the LEDs are D1 = Cout (PC4) and D2–D5 = S3–S0 (PC3–PC0), so the result reads as a binary number.
  • Loop: no interrupts or timers; the program keeps polling the inputs and refreshing the result.
  • Size: 30 instructions (60 bytes of flash), 1 MHz clock.
Proteus schematic: nine logic switches on the ATmega16's PORTA and PORTB pins, five LEDs with resistors on PORTC

The circuit in Proteus 8.10. Example: A = 1, B = 12, Cin = 1 gives 14.

Code

The whole program (main.asm):

main.asm

.INCLUDE "M16DEF.INC"

.DEF TEMP = R16
.DEF A = R17
.DEF B = R18
.DEF CIN = R21
.DEF SUM = R19
.DEF CARRY = R20

.ORG 0X0000
    RJMP RESET

RESET:
    LDI TEMP, 0X00
    OUT PORTA, TEMP
    OUT DDRA, TEMP

    OUT PORTB, TEMP
    OUT DDRB, TEMP

    LDI TEMP, 0XFF
    OUT DDRC, TEMP

    LDI A, 0X00
    LDI B, 0X00
    LDI CIN, 0X00

LOOP:
    IN A, PINA
    IN B, PINB
    IN TEMP, PINB
    SBRC TEMP, 5
    LDI CIN, 0X01
    SBRS TEMP, 5
    LDI CIN, 0X00

    MOV TEMP, A
    ADD TEMP, B
    ADD TEMP, CIN
    MOV SUM, TEMP

    LDI TEMP, 0X10
    AND TEMP, SUM
    BRNE SETCARRY
    LDI CARRY, 0X00
    RJMP DISPLAYSUM

SETCARRY:
    LDI CARRY, 0X10

DISPLAYSUM:
    OUT PORTC, SUM

    RJMP LOOP
Flowchart: port setup, read inputs, add, check the carry, write the result to PORTC, loop

Program flowchart.

Machine code

The report lists the 16-bit machine code and program counter address of every line; for example RJMP RESET is 1100 1100 0000 0000 at 0x0000. This was the most instructive part of the project.

If I did it today

  • I would mask A and B to 4 bits with ANDI 0x0F: PB5, which carries Cin, is also added into B and lights PC5. The result on PC0–PC4 is still right, but by luck rather than by design.
  • The CARRY register is computed but never used, since PC4 already shows the carry. I would drop it or drive a separate pin with it.
  • Instead of a single example, I would add a small test covering all 512 input combinations.

Project files

If you would like to talk about this, get in touch.