Skip to content

Tutorial — Practice 1: Math operations

Learn by programming: follow each step, write your own code, then validate with the lab questions and exam. Adapt pins and routines to your board when needed.

What you will build

An ALU calculator with switches on PORTB/PORTD, result on LEDs, and operation change via RA0.

Practice 1 schematic with PIC18F4550, switches and LEDs
Schematic: operands on RB0–RB7 and RD0–RD7, results on RC/RE LEDs, pushbutton on RA0. — Practica 1 — Operaciones matematicas · UNEXPO
  1. 1

    Wire the hardware per schematic

    Operands on PORTB and PORTD (DIP switches with pull-up). Result on LATC and LATE (LEDs). Pushbutton on RA0 to change operation. Review the schematic before writing code.
  2. 2

    Configure port directions

    RB and RD as inputs (`TRISB`/`TRISD` = 0xFF`). RC and RE as outputs (`TRISC`/`TRISE` = 0x00`). RA0 input for the button. Read operands with `MOVF PORTB, W` and `MOVF PORTD, W`.
    
    								        MOVLW   0xFF
            MOVWF   TRISB
            MOVWF   TRISD
            CLRF    TRISC
            CLRF    TRISE
            BSF     TRISA, 0    ; RA0 entrada
    							
  3. 3

    Implement one ALU operation

    Start with add: `MOVF PORTB, W` → `ADDWF PORTD, W` → result in W → `MOVWF LATC`. Once it works, duplicate the routine for subtract (`SUBWF`), OR (`IORWF`), etc.
    
    								suma:
            MOVF    PORTB, W
            ADDWF   PORTD, W
            MOVWF   LATC
    							
  4. 4

    Add multiply and software divide

    `MULWF` leaves the product in `PRODH:PRODL`—display both bytes on RC and RE. Division has no native instruction: subtract the divisor repeatedly and count how many times it fits.
  5. 5

    Use RA0 to switch modes

    Store the current mode in a RAM variable (`operacion`). Each press on RA0 increments the mode (0…8 then back to 0). In the main loop, branch to the routine matching `operacion`.
  6. 6

    Simulate, test on hardware, and document

    Verify each mode in Proteus with different switch combinations. On the physical board, repeat tests. Complete the pre/post lab report with screenshots and an explanation of each operation.

Before you code — pre-lab

Pre-laboratory — reference research

Answers aligned with the official PDF (Practice 1, UNEXPO). Use them to guide your research: analyze them, expand with the datasheet, and write your report in your own words.

Q 1 Minimum system wiring for the PIC18F4550

The minimum system lets the PIC oscillate, reset and run on stable power:

- Power: VDD = +5 V DC and VSS = ground on the correct pins.
- HS oscillator (20 MHz): 20 MHz crystal between OSC1 and OSC2, with 1 nF capacitors from each crystal pin to ground (per the lab sheet).
- Reset (MCLR): 10 kΩ pull-up from MCLR to VDD, 10 nF from MCLR to ground, optional reset pushbutton from MCLR to ground.
- Programming: ICSP (PGC/PGD/MCLR/VDD/VSS) when using PICkit 2/3.

With CONFIG FOSC = HS the PIC uses the external crystal.

Q 2 Explain the PIC18F4550 ports used in this lab

This lab uses five ports with different roles:

| Port | Direction | Role |
| --- | --- | --- |
| **PORTB** | Input (`TRISB = 0xFF`) | Operand **A** — 8-bit DIP switch (RB0–RB7) |
| **PORTD** | Input (`TRISD = 0xFF`) | Operand **B** — second DIP switch (RD0–RD7) |
| **PORTC** | Output (`TRISC = 0`) | **Low** result byte on LEDs (RC0–RC7 → `LATC`) |
| **PORTE** | Output (`TRISE = 0`) | **High** byte for multiply (RE0–RE7 → `LATE`) |
| **PORTA** | RA0 input | Pushbutton to **change operation** |

Each digital port uses TRISx (direction), PORTx (read) and LATx (output latch). Switches use 10 kΩ pull-ups; LEDs use 220–330 Ω series resistors.

Q 3 What is the ALU?

The ALU (*Arithmetic Logic Unit*) is the CPU block that runs operations on register data: add, subtract, AND, OR, XOR, complement, increments, etc.

On the PIC18F4550 the ALU mainly works with WREG and data memory registers. It executes one instruction per cycle (except some two-cycle instructions). Results may land in WREG, a file register, or special pairs like PRODH:PRODL (multiply).

Q 4 How many operations can the PIC18F4550 ALU perform?

The ALU does not expose a fixed "mode count", but the PIC18 instruction set includes dozens of 8-bit arithmetic and logic operations, including:

- Arithmetic: add (`ADDWF`, `ADDLW`), subtract (`SUBWF`, `SUBLW`), inc/dec (`INCF`, `DECF`), multiply (`MULWF`).
- Logic: AND (`ANDWF`), OR (`IORWF`), XOR (`XORWF`), complement (`COMF`).
- Shifts/rotates: `RLCF`, `RRCF`, `RLNCF`, `RRNCF`.

Integer division has no dedicated PIC18 instruction and is done in software (repeated subtraction), as required in this lab.

Q 5 Which instructions handle logical and arithmetic operations on this MCU?

Instructions used directly in Practice 1:

| Lab operation | MPASM instructions |
| --- | --- |
| Add | `ADDWF` |
| Subtract | `SUBWF` |
| Multiply | `MULWF` → result in `PRODH:PRODL` |
| Divide (software) | `MOVF`, `SUBWF`, `ADDWF`, `INCF`, `BCF`/`BSF`, `GOTO` (loop) |
| OR / AND / XOR | `IORWF`, `ANDWF`, `XORWF` |
| Complement | `COMF` |
| Rotate, no carry | `RLNCF` |

Setup and control instructions include `MOVLW`, `MOVWF`, `CLRF`, `BCF`, `BSF`, `BTFSC`, `BTFSS`, `CALL`, `RETURN`, `GOTO` for port init, switch reads and operation selection.

Do not copy verbatim into your report. Your instructor expects your own wording, calculations and schematic. Use this guide to compare and improve your work.

Design your solution

Design before coding

The lab sheet asks for pseudocode and a flowchart before the .ASM. Write yours first, then unlock the reference guide.

  1. 1. Pseudocode
  2. 2. Flowchart
  3. 3. Reference

Write your pseudocode

Include: port setup, main loop, CASE/SEGÚN for 9 modes, RA0 to change mode, software division routine.

0 characters (minimum 80)

Build and adapt the code

Practica · UNEXPO

Lab Practice 1

Minimum PIC18F4550 system. Operands on ports B and D (switches). Result on ports C and E (LEDs). RA0 pushbutton changes the active operation.

Pin / portRole in the program
PORTBOperand A (8-bit DIP switch)
PORTDOperand B (8-bit DIP switch)
LATCResult bits 0–7 (LEDs)
LATEResult bits 8–15 (LEDs; multiplication)
RA0Pushbutton — cycles the active operation

Press RA0 to cycle modes (wraps to 0 after the last).

  1. Create an MPASM project for PIC18F4550 (20 MHz HS crystal).
  2. Use the practice schematic (PDF page 2).
  3. Switches on RB0–RB7 and RD0–RD7 with 10 kΩ pull-ups.
  4. LEDs on RC0–RC7 and RE0–RE7 with 220–330 Ω resistors.
  5. Pushbutton on RA0 to ground with pull-up.

After building — post-lab

Post-laboratory — reference research

Answers to the closing questions in the PDF. Use them as a starting point and add what you observed in Proteus or on hardware.

Q 1 What is the difference between a hardware operation and a software operation?

Hardware (native ALU): the PIC runs one instruction and the ALU returns the result in one or a few cycles. Examples: `ADDWF`, `SUBWF`, `MULWF`, `IORWF`/`ANDWF`/`XORWF`.

Software (algorithm): there is no single instruction; you write a routine. Example: integer divide — repeat subtraction while dividend ≥ divisor.

Orally: *"Addition uses ADDWF in hardware; division is our algorithm because PIC18 has no DIV instruction."*

Q 2 Why can you not always use all of PORTC as output?

Pins RC6 and RC7 have alternate functions (UART TX/RX). When EUSART is enabled, they are not simple GPIO. This lab uses `TRISC = 0` because UART is not used, but your report must explain that not every pin is a dedicated GPIO at all times.

In your final report add your own conclusions about PIC18 limits (8 bits, software division, pins shared with UART).

Check what you learned

Exams by difficulty level

Open each level when you are ready. Basic is public; intermediate and UNEXPO levels require a CALETAS account to submit answers.

Basic Basic level — Lab brief review 4 questions

Straightforward questions about the schematic and lab description. Good before the lab session.

0 of 0 answered

In Practice 1, the button that changes the operation is on…
Input operands are on ports…
The result is mainly displayed on…
Division in this practice is implemented…
Intermediate Intermediate level — Ports and ALU 4 questions

TRIS/LAT/PORT registers, ALU and arithmetic instructions. Report-style questions.

0 of 0 answered

What does the PIC18F4550 ALU do within the execution cycle?
To read a switch state on RB3, which register should you read?
Why can RC6 and RC7 prevent using all of PORTC as simple outputs in some builds?
The key difference between hardware and software operations in this lab is…
UNEXPO UNEXPO level — Code and oral exam 10 questions

Professor-style questions: line-by-line .ASM analysis, registers, memory and instructions.

0 of 0 answered

In Practice 1 code, variables `modo`, `oper_a`, and `oper_b` are reserved in RAM from address 0x20. What memory type is that?
When `division_software` finishes, the quotient is in WREG. Which RAM variable accumulates it before `MOVF cociente, W`?
In `op_mul`, the `MULWF oper_a` instruction leaves the 16-bit product in…
What does `MOVLW 0x0F` followed by `MOVWF ADCON1` at program start do?
In `mostrar_resultado`, why write to `LATC` and `LATE` instead of `PORTC`/`PORTE`?
Instructions from `inicio:` through `END`, where are they stored when programming the PIC?
After `CALL division_software` in `op_div`, which instruction puts the quotient in `PRODL` for display?
In `op_rot`, instruction `RLNCF WREG, W` on operand A corresponds to…
What is the value of `TRISB` right after `MOVLW 0xFF` / `MOVWF TRISB` during init?
If asked orally: "What does `revisar_pulsador` do when it detects an edge on RA0?", the correct answer is…

Tutorial hub · Exercises