'********************************************************************** '** '** leds.bsc : PICBsc led functions '** assumes the SmartUPS controller card and the mapping '** Sin --> b,0 '** Clk --> b,1 '** Strobe --> b,2 '** Copyright (c) 2004, Kyle A. York '** All rights reserved '** '********************************************************************** ' ' external functions: ' led_init() ' initialize the LED functions ' ' led_graph(led_graph_which, led_graph_value) ' set the appropriate graph (LED_GRAPH_LEFT or LED_GRAPH_RIGHT) ' to a value from 0..100% ' ' led_set(led_which, led_state) ' set a single led (0 <= which <= 5) to either ' LED_STATE_ON or LED_STATE_OFF ' ' led_update() ' force an update; normally not necessary ' ' this module reserves all names starting with led_ ' constant byte LED_GRAPH_LEFT = 0 constant byte LED_GRAPH_RIGHT = 1 constant byte LED_STATE_OFF = 0 constant byte LED_STATE_ON = 1 constant byte LED_DRIVER_SIN = 0 ' port b,0 = Sin constant byte LED_DRIVER_CLOCK = 1 ' port b,1 = clock constant byte LED_DRIVER_STROBE = 2 ' port b,2 = strobe word led_state ' the current LED state word led_shift byte led_bit byte led_ii byte led_which ' ' note : the delays are likely not necessary ' but they also don't hurt. ' proc led_update() led_shift = ~led_state ' ' clear the strobe & clock ' pinlow b, LED_DRIVER_STROBE pinlow b, LED_DRIVER_CLOCK delay 0.001, 0 ' ' 16 bits ' for led_ii = 0 to 15 led_bit = led_shift & 1 led_shift = led_shift / 2 if (led_bit) then pinhigh b, LED_DRIVER_SIN else pinlow b, LED_DRIVER_SIN end if delay 0.001, 0 ' ' set the clock ' pinhigh b, LED_DRIVER_CLOCK delay 0.001, 0 ' delay ' ' ' clock low pinlow b, LED_DRIVER_CLOCK next ' ' strobe pinhigh b, LED_DRIVER_STROBE delay 0.001, 0 pinlow b, LED_DRIVER_STROBE delay 0.001, 0 end proc proc led_init() _trisb = _trisb & $07 ' clear the low 3 bits for output led_state = 0 call led_update() end proc proc led_graph(led_which, led_bit) led_shift = (1 << (led_bit + 10) / 20) - 1 if (LED_GRAPH_LEFT = led_which) then led_state = (led_state & %111111111100000) | led_shift else led_state = (led_state & %000001111111111) | (led_shift << 11) end if call led_update() end proc proc led_set(led_which, led_bit) led_shift = 1 led_shift = led_shift << (led_which + 5) led_state = led_state & ~led_shift if (led_bit) then led_state = led_state | led_shift end if call led_update() end proc