{{ This version of the program assumes the pins go HIGH when pushing a button }} CON ' DON'T TOUCH! _xinfreq = 5_000_000 _clkmode = xtal1 + pll16x NORTH_B = %0001 SOUTH_B = %0010 WEST_B = %0100 EAST_B = %1000 CON ' Modify as needed 'PIN ASSIGNMENTS NORTH = 3 SOUTH = 4 EAST = 5 WEST = 6 RATE = 7 LED_1 = 24 ' Lit when the slowest speed is selected LED_2 = 25 LED_3 = 26 LED_4 = 27 ' Lit when the fastest speed is selected RX = 21 TX = 20 MINVAL = "0" VAR long debug_stack[100] byte tx_byte byte tx_print OBJ ser : "FullDuplexSerial" ' used for communicating with the mount fds : "FullDuplexSerial" ' used for debugging PUB Main | t, leds dira[LED_4 .. LED_1] := %1111 ' set LED pins to outputs leds := %0001 ' intialize LED pattern to slowest slew rate outa[LED_4 .. LED_1] := leds ' update LEDs ser.start(RX,TX,0,9600) ' start serial object cognew(debug,@debug_stack) t := clkfreq / 50 + cnt repeat waitcnt(t += clkfreq / 50) ' repeat 50 times per second (matches PWM frequency) tx_byte := 0 if ina[RATE] ' if rate button is pressed leds <<= 1 ' shift to next slew rate if leds == %1_0000 ' if highest slew rate previously selected leds := %0001 ' reset to slowest slew rate outa[LED_4 .. LED_1] := leds ' update the LEDs debounce(RATE) ' wait for rate button to settle t := clkfreq / 50 + cnt ' re-establish loop timer if ina[NORTH] ' if North button is pressed tx_byte |= (>| leds - 1) << 4 | NORTH_B ' transmit a byte containing the rate in the high nibble and the button in the low nibble elseif ina[SOUTH] ' rate is %0000_xxxx, %0001_xxxx, %0010_xxxx, or %0011_xxxx tx_byte |= (>| leds - 1) << 4 | SOUTH_B ' dir is %xxxx_0001, %xxxx_0010, %xxxx_0100, or %xxxx_1000 if ina[WEST] tx_byte |= (>| leds - 1) << 4 | WEST_B elseif ina[EAST] tx_byte |= (>| leds - 1) << 4 | EAST_B if tx_byte <> 0 ser.tx(tx_byte + MINVAL) tx_print := tx_byte + MINVAL PRI Debounce(pin) | t t := clkfreq / 100 + cnt repeat until cnt - t > 0 if ina[pin] t := clkfreq / 100 + cnt PRI Debug fds.start(31,30,0,115200) repeat if tx_print fds.tx(tx_print) tx_print -= MINVAL fds.tx(" ") fds.dec(tx_print >> 4) fds.tx(" ") if tx_print & NORTH_B fds.str(string("North ")) if tx_print & SOUTH_B fds.str(string("South ")) if tx_print & WEST_B fds.str(string("West ")) if tx_print & EAST_B fds.str(string("East ")) fds.tx($0D) tx_print := 0