Send text over the USART (PIC16F628A)

Set up the PIC16F628A hardware USART for 9600 baud at 4 MHz and send "Hello World!" to the serial terminal. Covers SPBRG, TXSTA, RCSTA and TXREG.

  • PIC16F628A
  • Intermediate
  • Watch the USART Terminal widget (Peripherals tab)

Open the example in the simulator

Choosing the chip

The PIC16F84A has no serial port, so this example targets the PIC16F628A. Its first line, list p=16f628a, makes the simulator switch chips when you assemble.

The baud rate

The USART derives its bit rate from the oscillator. With BRGH = 1 (high speed) the formula is baud = Fosc / (16 × (SPBRG + 1)). For 9600 baud at 4 MHz, SPBRG = 25 gives 4,000,000 / (16 × 26) = 9615 baud, 0.16% fast: well within what a serial receiver tolerates. The SPBRG calculator finds the value for other speeds.

Turning the USART on

init_usart writes three registers:

  • SPBRG = 25 (bank 1): the baud rate above.
  • TXSTA = b'00100100' (bank 1): TXEN = 1 enables the transmitter, BRGH = 1 selects high speed, SYNC = 0 means asynchronous.
  • RCSTA = b'10010000' (bank 0): SPEN = 1 switches the RB1/RB2 pins over to the USART, CREN = 1 enables the receiver.

Sending a character

send_char waits until the transmitter is free, then writes the byte in W to TXREG. It polls TRMT in TXSTA with BTFSS; TRMT is set once the shift register has sent the previous byte. Polling TXIF in PIR1 instead is a little faster, because TXREG can take the next byte while the previous one is still shifting out.

send_hello_world calls send_char once per letter and finishes with a carriage return (0x0D) and line feed (0x0A).

Try it

  1. Open the example, assemble and run it. Switch the widget dock to Peripherals: "Hello World!" appears in the USART Terminal again and again.
  2. Load the USART Echo example from the Examples menu, run it and type in the terminal: each character comes back.
  3. Change the message, or replace the letter-by-letter calls with a DT table and a loop.

The complete program

This is examples/usart-hello.asm, the same file the simulator loads.

; PIC16F628A USART Hello World Example
; This example demonstrates basic USART communication
; Sends "Hello World!" repeatedly via USART

    list p=16f628a
    
    ; Configuration bits (simplified for our assembler)
    ; _FOSC_INTRCIO = Internal RC oscillator
    ; _WDTE_OFF = Watchdog timer disabled
    ; _PWRTE_ON = Power-up timer enabled
    ; _MCLRE_ON = MCLR pin enabled
    ; _CP_OFF = Code protection disabled
    ; _LVP_OFF = Low voltage programming disabled
    ; _BOREN_OFF = Brown-out reset disabled
    ; _CLKOUTEN_OFF = CLKOUT function disabled
    ; _IESO_OFF = Internal/External switchover disabled
    ; _FCMEN_OFF = Fail-safe clock monitor disabled

    ; Variables
    cblock 0x20
        counter
        temp
    endc

    ; Reset vector
    org 0x0000
    goto main

    ; Interrupt vector
    org 0x0004
    goto interrupt_handler

    ; Main program
main:
    ; The PIC16F628A's internal oscillator runs at 4 MHz when the configuration
    ; word selects it; there's no oscillator register to set up
    
    ; Initialize USART
    call init_usart
    
    ; Send welcome message
    call send_hello_world
    
    ; Main loop
main_loop:
    ; Wait a bit
    call delay_1s
    
    ; Send the message again
    call send_hello_world
    
    goto main_loop

; Initialize USART for 9600 baud at 4MHz
init_usart:
    ; Set baud rate (9600 baud at 4MHz)
    bsf STATUS, RP0   ; Set Bank 1
    movlw 25          ; SPBRG = 25 for 9600 baud
    movwf SPBRG
    
    ; Configure TXSTA (Bank 1)
    movlw b'00100100' ; Enable transmit, 8-bit mode
    movwf TXSTA
    
    ; Configure RCSTA (Bank 0)
    bcf STATUS, RP0   ; Clear Bank 1 (back to Bank 0)
    movlw b'10010000' ; Enable serial port, 8-bit receive
    movwf RCSTA
    
    return

; Send "Hello World!" message via USART
send_hello_world:
    ; Send "Hello World!\r\n"
    movlw 'H'
    call send_char
    movlw 'e'
    call send_char
    movlw 'l'
    call send_char
    movlw 'l'
    call send_char
    movlw 'o'
    call send_char
    movlw ' '
    call send_char
    movlw 'W'
    call send_char
    movlw 'o'
    call send_char
    movlw 'r'
    call send_char
    movlw 'l'
    call send_char
    movlw 'd'
    call send_char
    movlw '!'
    call send_char
    movlw 0x0D         ; Carriage return
    call send_char
    movlw 0x0A         ; Line feed
    call send_char
    return

; Send a single character via USART
send_char:
    bsf STATUS, RP0   ; Set Bank 1 to access TXSTA
    btfss TXSTA, TRMT ; Wait for transmit buffer empty
    goto send_char
    bcf STATUS, RP0   ; Clear Bank 1 (back to Bank 0)
    movwf TXREG
    return

; Delay about 0.15 s at 4 MHz (200 x 250 passes of 3 cycles)
delay_1s:
    movlw 200
    movwf counter
delay_outer:
    movlw 250
    movwf temp
delay_inner:
    decfsz temp, f
    goto delay_inner
    decfsz counter, f
    goto delay_outer
    return

; Interrupt handler
interrupt_handler:
    ; Simple interrupt handler - just return
    retfie

; End of program

    end

Open it in the simulator

Next