completeBare-Metal MPASM Assembly

Bare-Metal PIC Microcontroller Assembly (MPASM) Library

Bare-metal MPASM assembly firmware for Microchip PIC16F873 and PIC16F84A: RB4 Interrupt-on-Change radio receiver pulse decoding, Timer1 signal-loss watchdog, and hardware CCP1 PWM duty modulation.

  • Assembly
  • MPASM
  • PIC16
  • PWM
  • Timers
  • Microchip
  • Embedded Systems
Bare-Metal PIC Microcontroller Assembly (MPASM) Library

Project Overview

A bare-metal firmware library written in Microchip MPASM Assembly for the PIC16F873 and PIC16F84A 8-bit Harvard-architecture microcontrollers, developed across embedded systems coursework at the University of Brighton (First Class 79% / A) and independent hardware builds.

The project focuses on direct hardware register configuration, bank switching (STATUS, RP0), edge-triggered interrupt servicing, hardware timers, and hardware PWM generation without compiler abstractions.


Core Circuit: PIC16F873 Reference Setup

The reference hardware uses a PIC16F873 clocked by an external crystal oscillator with ICSP programming pins (PGD/PGC) for flashing and debugging via a Microchip PICkit programmer:

PIC16F873 Core Circuit Schematic Figure 1: PIC16F873 reference circuit showing the external crystal oscillator with 20 pF load capacitors, ICSP programming pins (PGD/PGC), and a current-limited LED output on RB0.


Hardware Architecture & Target Registers

The flagship assembly routine (16F873_RX_to_PWM.asm) decodes standard 1 ms to 2 ms radio-controlled (RC) receiver pulses arriving on RB4 and maps the measured pulse width onto the hardware CCP1 PWM output on PORTC, 2, with a hardware watchdog fail-safe if signal frames stop arriving:

Feature Hardware Implementation Target Registers
Microcontroller Microchip PIC16F873 (28-pin DIP) PORTA, PORTB, PORTC, ADCON1
RC Pulse Capture RB4 Interrupt-on-Change + Timer0 (1:16 prescaler, 16 µs ticks) INTCON, OPTION_REG, TMR0
Fail-Safe Watchdog Timer1 ~65.5 ms overflow timeout forcing PWM safely OFF on signal loss TMR1H, TMR1L, PIR1
PWM Output Hardware CCP1 in PWM mode clocked by Timer2 (~984 Hz) CCP1CON, CCPR1L, T2CON, PR2
Diagnostic Outputs Direct byte latching on Port C LEDs (16F873_RX_to_LEDs_PORTC.ASM) TRISC, PORTC

Interrupt-Driven Pulse Decoder & PWM Flow

On a rising edge at RB4, the interrupt service routine clears TMR0 and resets the TMR1 signal-loss watchdog. On the falling edge, it reads TMR0, subtracts the 1 ms base offset (0x3E / 62 ticks), clamps the result to the 1 ms active range, shifts left twice (RLF) to scale into an 8-bit duty cycle, and writes CCPR1L:

Interrupt-Driven RC Pulse Decoder and PWM Flowchart Figure 2: Main loop and Interrupt Service Routine (ISR) flowchart for PORTB pin-change detection, Timer0 pulse-width capture, and PWM duty-cycle updates.


Register-Level MPASM Assembly Excerpt

Excerpt from firmware_asm/16F873_RX_to_PWM.asm handling RB4 edge detection, Timer0 pulse capture, Timer1 watchdog reset, and scaling into the CCP1 PWM register:

ISR
    BCF     INTCON,GIE          ; Disable interrupts inside ISR
    MOVWF   W_TEMP              ; Save W register
    MOVF    STATUS,W
    MOVWF   STATUS_TEMP         ; Save STATUS register

    MOVF    PORTB,W
    MOVWF   CURR_PORTB
    XORWF   PREV_PORTB,W        ; Identify which PORTB bits changed
    ANDLW   0x10                ; Mask RB4 (bit 4)
    BTFSC   STATUS,Z            ; Ignore changes on other pins
    GOTO    ISR_EXIT

    MOVF    CURR_PORTB,W
    MOVWF   PREV_PORTB
    BTFSS   CURR_PORTB,0x04     ; Rising or falling edge on RB4?
    GOTO    HITOLO              ; Falling edge: end of RC pulse
    GOTO    LOTOHI              ; Rising edge: start of RC pulse

LOTOHI
    CLRF    TMR0                ; Start pulse-width measurement
    CLRF    TMR1H               ; Reset Timer1 signal-loss watchdog
    CLRF    TMR1L
    BCF     PIR1,0              ; Clear TMR1IF overflow flag
    GOTO    ISR_EXIT

HITOLO
    MOVF    TMR0,W              ; Capture elapsed ticks
    MOVWF   PWM_TEMP
    MOVLW   0x3E                ; Subtract 1 ms base offset (62 ticks)
    SUBWF   PWM_TEMP,F
    BTFSC   STATUS,C
    GOTO    CHECK_UPPER
    MOVLW   0xFF                ; Under 1 ms: force OFF state
    MOVWF   PULSEWID
    GOTO    ISR_EXIT

Included Assembly Firmware Modules

  • 16F873_RX_to_PWM.asm: Interrupt-driven RC radio receiver pulse decoder with Timer0 width capture, Timer1 65.5 ms fail-safe timeout, and CCP1 hardware PWM output.
  • 16F873_RX_to_LEDs_PORTC.ASM: Decodes RC receiver pulses into an 8-bit value driving Port C diagnostic LEDs and CCP1 PWM.
  • PWM.asm & PWM_Simple.asm: Hardware Timer2 (TMR2) and PR2 period configuration for CCP1 pulse-width modulation.
  • SimplePWMTest.asm: Bench test routine for checking PWM duty-cycle output behaviour.

Source Code & Academic Attribution

  • GitHub Repository: HarryRogers073/pic-microcontroller-code
  • Author: Harry Rogers (University of Brighton)
  • Target Silicon: Microchip PIC16F873, PIC16F84A
  • Toolchain & Programmer: Microchip MPLAB X IDE, MPASM Assembler, MPLAB XC8, PICkit 3
  • Attribution & Provenance:
    • MPASM Assembly Firmware (firmware_asm/): All register-level assembly routines (16F873_RX_to_PWM.asm, 16F873_RX_to_LEDs_PORTC.ASM, PWM.asm, PWM_Simple.asm, SimplePWMTest.asm) were written and tested by Harry Rogers.
    • Embedded C Drivers (firmware_c/): Motor control, ultrasonic ranging, and sensor routines developed for university robotics projects by Harry Rogers & Albie Gullis. The character LCD driver (lcd.c) was adapted from a tutorial by Khaled Magdy (DeepBlueEmbedded).
    • Microchip register include files (p16f873.inc, <xc.h>) are copyright Microchip Technology Inc.