ucl 1.0

# Copyright (c) 2005 by Wayne C. Gramlich
# All rights reserved.

library $pic16f876
library $uart

package pdip
    pin 1 = mclr
    pin 2 = ra0_in, name = en
    pin 3 = ra1_unused
    pin 4 = ra2_out, name = x_dir
    pin 5 = ra3_out, name = fault
    pin 6 = ra4_unused
    pin 7 = ra5_out, name = x_step
    pin 8 = vss
    pin 9 = clkin
    pin 10 = osc2
    pin 11 = rc0_out, name = y_dir
    pin 12 = rc1_out, name = y_step
    pin 13 = rc2_out, name = z_dir
    pin 14 = rc3_unused
    pin 15 = rc4_unused
    pin 16 = rc5_unused
    pin 17 = tx
    pin 18 = rx
    pin 19 = vss2
    pin 20 = vdd
    pin 21 = rb0_out, name = z_step
    pin 22 = rb1_out, name = a_dir
    pin 23 = rb2_out, name = a_step
    pin 24 = rb3_in, name = probe
    pin 25 = rb4_in, name = detect
    pin 26 = rb5_out, name = mode
    pin 27 = rb6_out, name = q_dir
    pin 28 = rb7_out, name = q_step

configure cp=off, debug=off, wrt=off, cpd=off, lvp=off
configure boden=off, pwrte=off, fosc=hs

constant microsecond = 5
constant delay_amount = 10 * microsecond

global command byte

origin 0

procedure main
    arguments_none
    returns_nothing

    # Turn of general interrupts:
    $gie := 0

    # Initilize serial port:

    # Do Baud Rate selection and asynch. serial port enable:
    # Prescaler = low:
    $brgh := 1
    # Baud rate = 19200 Baud:
    $spbrg := 64
    # Asynchronous mode:
    $sync := 0
    # Serial port enable:
    $spen := 1
    $txif := 0

    # Enable the transmitter:
    # 8-bit mode:
    $tx9 := 0
    # Enable transmitter:
    $txen := 1

    # Enable the receiver:
    # 8-bit mode:
    $rx9 := 0
    # Disable address:
    $adden := 0
    # Continuous receive enable:
    $cren := 1
    # Serial receive enable:
    $sren := 1

    call $uart_crlf_put()
    call $uart_byte_put('D')
    call $uart_byte_put('e')
    call $uart_byte_put('b')
    call $uart_byte_put('u')
    call $uart_byte_put('g')
    call $uart_crlf_put()

    # Initialize step and direction:
    a_step := 0
    a_dir := 0
    x_step := 0
    x_dir := 0
    y_step := 0
    y_dir := 0
    z_step := 0
    z_dir := 0

    # Command loop:
    loop_forever
	# Wait for a character:
	command := $uart_byte_get()
	if command = 'A'
	    # Increment A:
	    a_dir := 1
	    a_step := 1
	else_if command = 'X'
	    # Increment X:
	    x_dir := 1
	    x_step := 1
	else_if command = 'Y'
	    # Increment Y:
	    y_dir := 1
	    y_step := 1
	else_if command = 'Z'
	    # Increment Z:
	    z_dir := 1
	    z_step := 1
	else_if command = 'a'
	    # Decrement A:
	    a_dir := 0
	    a_step := 1
	else_if command = 'x'
	    # Decrement X:
	    x_dir := 0
	    x_step := 1
	else_if command = 'y'
	    # Decrement Y:
	    y_dir := 0
	    y_step := 1
	else_if command = 'z'
	    # Decrement Z:
	    z_dir := 0
	    z_step := 1
	else_if command = '\r\'
	    # Carriage-return/line-feed:
	    call $uart_crlf_put()
	else
	    # Unrecognized command:
	    command := '?'

	# Clear the step pulses after waiting for 10usec:
	delay delay_amount
	    do_nothing
	a_step := 0
	x_step := 0
	y_step := 0
	z_step := 0

	# Echo command:
	call $uart_byte_put(command)


