' === LED47Count.bas =========== 2010.04.21 =========== Ron Hackett === ' Program runs on a PICAXE-08M & controls a MAX7219 LED display ' driver, which is connected to a 4-digit, 7-segment LED display. ' The program counts from 0 to 9999 in an infinite loop. ' Hardware interface to the MAX7219 symbol maxclok = 4 ' data is valid on rising edge of the clock pin symbol maxdata = 1 ' data bits are shifted out this pin to the 7219 symbol maxload = 2 ' pulse this output to transfer data to LEDs ' Register addresses for the MAX7219 symbol decode = 9 ' decode register; specify digits to decode symbol brite = 10 ' intensity (brightness) register; 15 = 100% symbol scan = 11 ' scan-limit register; specify how many digits symbol on_off = 12 ' 1 = display on; 0 = display off ' === Variables === symbol outword = w0 ' concatenation of maxreg and outbyte symbol outbyte = b0 ' data to be transmitted to the MAX7219 symbol maxreg = b1 ' MAX7219 register that receives data symbol index = b2 ' used in subroutine for...next loop symbol tths = b3 ' used in counting (bintoascii requires it) symbol thos = b4 ' used in counting symbol hnds = b5 ' used in counting symbol tens = b6 ' used in counting symbol ones = b7 ' used in counting symbol counter = w4 ' used to count from 0 to 9999 ' === Directives === #com 3 ' specify com port #picaxe 08M ' specify PICAXE processor ' ========================= Begin Main Program ======================== setfreq m8 ' Initialize the MAX7219 maxreg = scan ' set scan limit for digits 0-3 outbyte = 3 gosub shout16 maxreg = brite ' set brightness to 5 (15 = 100%) outbyte = 5 gosub shout16 maxreg = decode ' set BCD decoding for digits 0-3 outbyte = %00001111 gosub shout16 maxreg = on_off ' turn display on outbyte = 1 gosub shout16 do ' main counting loop for counter = 0 to 9999 bintoascii counter,tths,thos,hnds,tens,ones thos = thos - 48 hnds = hnds - 48 tens = tens - 48 ones = ones - 48 maxreg = 1 ' thousands digit (1st from left) outbyte = thos gosub shout16 maxreg = 2 ' hundreds digit(2nd from left) outbyte = hnds gosub shout16 maxreg = 3 ' tens digit (3rd from left) outbyte = tens gosub shout16 maxreg = 4 ' ones digit (4th from left) outbyte = ones gosub shout16 next counter loop ' ============== End Main Program - Subroutines Follow =============== shout16: ' Shift 16-bit word to the MAX7219 (MSB first) for index = 1 to 16 ' MAX7219 specifies a 16-bit word if bit15 = 1 then ' set sdata to correspond to bit15 high maxdata else low maxdata endif pulsout maxclok,1 ' pulse clock line outword = outword * 2 ' shift outword left for next MSB next index pulsout maxload,1 ' load the data word into MAX7219 return