' Picaxe clock - 1st attempt at Digital Clock ' This one uses: picaxe 18x, DS1307, and HC4LED SPI LED (4 digit LED display w/ serial inface) ' ' (C) 4/07 A. Stein, MedCosm, LLC ' ' Info: ' 'My first attempt to make a digital clock. This one uses an HC4LED circa 1985 display 'and a DS1307 to provide the accurate RTC. Rather than just display the time, I've 'played with a few different modes to drive the LED like updating the display via a 'quick display of random numbers which settles at the accurate time. With some work, 'the enable line could be PWM'd to provide for a display that fades in/out. ' 'i2c on the picaxe took a bit of manual reading, but not too bad. ' ' ' TODO: ' buttons to set time, alarm, better display, a box ' ' Version History: ' ' v1 - Initial version, all hardware works! ' v2 - rewrite for cleaner use of variables ' ' ' ' 08M variable table (for convenience) ' b0:b1 b2:b3 b4:b5 b6:b7 b8:b9 b10:b11 b12:b13 ' w0 w1 w2 w3 w4 w5 w6 'pinouts symbol LED=0 'LED pinout symbol LDR=1 'light sensor pinout SYMBOL Clk = 6 'Display Clock is output pin 6 SYMBOL Dat = PIN7 'Display Data is output pin 7 symbol timeInc = PIN0 'inc time button symbol timeDec = PIN1 'dec time button 'vars symbol display = B0 'used as bits by HC4 routines symbol displayBit7 = BIT7 'high bit of display variable symbol rndDigit = B1 'digit to display randomly (0-3) SYMBOL value = W1 'Value to be displayed symbol valueB1=B2 symbol valueB2=B3 symbol tmpW1=w2 'tmp word variables (overlap with byte vars) symbol tmpB1=b4 'tmp byte variables symbol tmpB2=b5 symbol tmpW2=w3 'tmp word variables (overlap with byte vars) symbol tmpB3=b6 'tmp byte variables symbol tmpB4=b7 symbol tmpW3=w4 'warning, changed during display symbol tmpB5=b8 symbol tmpB6=b9 symbol rndW=w5 symbol rndB1=b10 symbol rndB2=b11 symbol time=w6 '24 hr time in decimal (HHMM) symbol timeB1=B12 symbol timeB2=B13 'constants symbol viewingDly=5000 'timing delay symbol numRndDig=5 'number of rnd digits to display 'initialization routines init: low dat 'init display high clk i2cslave %11010000, i2cslow, i2cbyte 'setup i2c with DS1307 (RTC) ' gosub setTime 'set time 'main execution starts here main: gosub readTime if timeInc=1 or timeDec=1 then 'if set buttons pressed if timeInc=1 then 'inc time time=time+1 gosub setTime else 'dec time time=time-1 gosub setTime endif else pause viewingDly 'let viewer read clock gosub displayTime 'do fancy time display endif value=time gosub DisplayValue 'display real time goto main 'display the time somehow 'tmpB1, tmpB2, displayTime: value=time ' time=1234 ' rndDigit=4 for tmpB2=0 to 6 lookup tmpB2, (0, 1, 2, 3, 2, 1, 0), rndDigit for tmpB1=0 to numRndDig gosub dispRndDigit 'display randomized time value next next return 'randomize a particular digit of time dislay 'rndDigit = 0=1s, 1=10s, 2=100s, 3=1000s, 4=all dispRndDigit: random rndW on rndDigit goto r1, rR2, rR3, rAll, r10, r100, r1000 ' on rndDigit goto r1, r10, r100, r1000, rAll, rR3, rR2 'tmpB3, tmpW2, value r1: tmpB3=rndW % 10 value=time/10 value=value*10 tmpW2=tmpB3*1 value = value + tmpW2 tmpW2= time % 1 value = value + tmpW2 goto endRndDigit r10: tmpB3=rndW % 10 value=time/100 value=value*100 tmpW2=tmpB3*10 value = value + tmpW2 tmpW2= time % 10 value = value + tmpW2 goto endRndDigit r100: tmpB3=rndW % 10 value=time/1000 value=value*1000 tmpW2=tmpB3*100 value = value + tmpW2 tmpW2= time % 100 value = value + tmpW2 goto endRndDigit r1000: tmpB3=rndW % 10 value=time/10000 value=value*10000 tmpW2=tmpB3*1000 value = value + tmpW2 tmpW2= time % 1000 value = value + tmpW2 goto endRndDigit rR3: tmpW2=rndW % 1000 value=time/1000 value=value*1000 value = value + tmpW2 goto endRndDigit rR2: tmpW2=rndW % 100 value=time/100 value=value*100 value = value + tmpW2 goto endRndDigit rAll: value=rndW % 10000 endRndDigit: GOSUB DisplayValue return 'set time to 11:59:00, 25/12/03 setTime: ' writei2c 0, ($00, $59, $11, $03, $25, $12, $03, $10) ' writei2c 0, ($00, $25, $01, $03, $25, $12, $03, $10) writei2c 0, ($00, timeB1, timeB2, $00,$00,$00, $00) return 'reads time from DS1307, values are all in BCD 'time, tmpB3, tmpB4 (BCDtoDec), value readTime: ' readi2c 0, (timeB1, timeB2, tmpB3) 'read sec, min, hour (test) readi2c 0, (tmpB3, timeB1, timeB2) 'read sec, min, hour (real) ' SerTxD (#tmpB3, " ", #tmpB4, " ", #tmpB5, 13, 10) value=timeB1 gosub BCDtoDec 'convert from hex to decimal tmpB3=value value=timeB2 gosub BCDtoDec 'convert from hex to decimal time=value*100+tmpB3 return 'convert hex to decimal 'value, tmpB4 BCDtoDec: tmpB4 = value / 16 * 10 'conv 16's column value = value & $0F + tmpB4 return ' HC4LED.BS1 ' This program demonstrates the use of the HC4LED display module ' HC4LED Pinout: ' Pin1 = +5VDC (White wire) ' Pin2 = Gnd ' Pin3 = Blank (Must connect to Gnd to enable display) ' Pin4 = No Connection ' Pin5 = Clock ' Pin6 = Data ' ' ' To display values: ' Set the "zeros" variable to 0=No leading zeros; or 1=Show leading zeros ' Set the variable "value" to 0 to 9999 ' Then use "GOSUB DisplayValue" to show the value on the display ' ' Each segment of the display is addressable, so you can create letters ' and symbols. ' To display custom symbols: ' Set the variables "segments(0)" thru "segments(3)" (segments(0) is on the right) ' simply add the segment values that you want on ' Then use "GOSUB DisplaySegments" to show the segments on the display ' ' ---4--- ' | | ' 2 8 ' | | ' |---1---| ' | | ' 64 16 ' | | ' --32--- ' ' For example "F" would be 4+2+1+64 = 71 ' ' {$STAMP BS1} ' {$PBASIC 1.0} ' Next available variables are: BIT8,B6,W3 'display a specified value on the HC4LED 'tmpB5, tmpB6, value, Clk DisplayValue: ' Call with "value" set to the value to be displayed LOW Clk tmpB6 = value % 10 GOSUB DisplayDigit PULSOUT Clk,1 tmpB6= value / 10 % 10 GOSUB DisplayDigit PULSOUT Clk,1 tmpB6= value / 100 % 10 GOSUB DisplayDigit PULSOUT Clk,1 tmpB6= value / 1000 % 10 GOSUB DisplayDigit HIGH Clk RETURN DisplayDigit: ' Called by DisplayValue subroutine LOOKUP tmpB6,(126,24,109,61,27,55,115,28,127,31),B0 DisplaySegments: ' Call with B0 containing the segments for the next digit FOR tmpB5 = 1 TO 7 Dat = displayBit7 PULSOUT Clk,1 display = display * 2 NEXT Dat = BIT7 RETURN