ucl 1.0

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

library $pic16f676

package pdip
    pin 1 = power_supply
    pin 2 = clkin
    pin 3 = an3, name=coil0_in
    pin 4 = mclr
    pin 5 = rc5_out, name=coil0a, mask=coil0a_mask
    pin 6 = rc4_out, name=coil0b, mask=coil0b_mask
    pin 7 = rc3_out, name=coil1a, mask=coil1a_mask
    pin 8 = rc2_out, name=coil1b, mask=coil1b_mask
    pin 9 = rc1_in, name=step
    pin 10 = rc0_in, name=dir
    pin 11 = ra2_in, name=mode
    #pin 12 = vref
    pin 12 = ra1_in
    pin 13 = an0, name=coil1_in
    pin 14 = ground

configure cpd=off, cp=off, boden=off, mclre=on, pwrte=off, wdte=off, fosc=hs

constant microsecond = 5

origin 0

# This program just turns on the H-bridge full power with
# not attempt to sense over current.  Thus, this program
# should only be used if there is a current limiting resistor
# in series with each coil.

procedure main
    arguments_none
    returns_nothing

    local index byte
    local previous bit
    local pulse byte
    local coil0_mask byte
    local coil1_mask byte
    local coil0_current byte
    local coil0_limit byte
    local coil1_current byte
    local coil1_limit byte

    # Set A/D clock source to Tad = 32 x Tocs = 32 x .2uS = 6.4uS.
    # A/D time = 11 x TAd = 11 x 6.4uS = 70.4uS.
    $adcon1 := 0x20

    pulse := 0
    coil0_limit:= 28
    coil0_mask := coil0a_mask
    coil1_limit := 28
    coil1_mask := coil1a_mask
    previous := 0
    index := 0
    $rc := 0

    # Set ADCS<2:0> to 010 = Fosc/32:
    $adcs0 := 0
    $adcs1 := 1
    $adcs2 := 0

    # Right justify the 10-bit value into ADR
    $adfm := 1
    $adon := 1
    loop_forever
	# Step/Dir process:
	if step
	    if !previous
		# We have a positive step edge:
		if dir
		    index := index + 1
		else
		    index := index - 1
	    previous := 1
	else
	    previous := 0

	# Ripple table:
	if index@1
	    if index@0
		# index & 3 = 3:
		coil0_mask := coil0a_mask
		coil1_mask := 0
	    else
		# index & 3 = 2:
		coil0_mask := 0
		coil1_mask := coil1a_mask
	else
	    if index@0
		# index & 3 = 1:
		coil0_mask := coil0b_mask
		coil1_mask := 0
	    else
		# index & 3 = 0:
		coil0_mask := 0
		coil1_mask := coil1b_mask
	$rc := coil0_mask | coil1_mask

