rem LED_Display.bas Shows time, Date and messages on the 24-character green LED Display rem It has a photocell for auto-dimming and an IR detector to change the message rem Two custom messages may be stored in the clock chip NVRAM from the wooden keyboard. rem Time and date are kept by a battery-backed clock board using a I2C interface. rem It is a BOB-00099 from SparkFun electronics, using a Dallas DS1307 chip. rem This program can change to and from Daylight-saving time and advance or retard minutes. rem A separate PICAXE is used to receive IR commands. Its progran is InfraRed.bas rem A message can be displayed at each of two hours for an hour or until cancelled by IR. rem John saunders 4/30/2008 #picaxe 18X #gosubs 256 rem variables symbol AutoInc = bit0 symbol TwoDigit = bit1 symbol AdjMode = bit3 symbol AdjHour = bit4 symbol Iter = b1 symbol BitPos = b2 symbol Mask = b3 symbol Addr = b4 symbol CharData = b5 symbol DECData = b6 symbol Units = b7 symbol IRMode = b8 symbol CoData = b9 symbol KbdAddr = b10 symbol OldHour = b11 symbol OldIRMode = b12 symbol ADCData = w1 rem outputs symbol SRCLK = 0 'Shift register clock symbol sda = 1 'RTC sda i2c line symbol DispWR = 2 'Display write line - low active symbol DispBL = 3 'Display blanking PWM symbol scl = 4 'RTC sdl i2c line symbol SRIN = 5 'Shift register data in symbol CLR = 6 'Shift register & display clear - low active symbol Reply = 7 'Signifies when HIGH that prcessor is ready to receive a serial byte rem inputs symbol IRFlag = INPUT0 'Co-processor has a byte to send symbol PCIN = 1 'Photocell analog input symbol TempIN = 2 'Temperature sensor analog input, 10mv per deg F symbol IRSel = INPUT6 'RTC square-wave output, pin 15 symbol IRPort = 7 'Serial data from co-processor rem data DATA 0,(1,2,4,8,16,32,64) 'Mask of bits for DispChar DATA 7,("SunMonTueWedThuFriSat") DATA 28,("JanFebMarAprMayJunJulAugSepOctNovDec") DATA 64, ("John loves Elaine ") DATA 88, ("Take Your Pills John! ") symbol BannerStartLoc = 40 symbol BannerEndLoc = 67 rem alarm hours symbol MorningAlarm = 7 symbol AfternoonAlarm = $17 startup: HIGH CLR HIGH SRCLK HIGH DispWR LOW DispBL LET AutoInc = 1 LET AdjMode = 0 SETFREQ m4 IF IRSel = 1 THEN NotKeyboard LET IRMode = 3 NotKeyboard: I2CSLAVE %11010000,i2cslow,i2cbyte main: branch IRMode,(TimeDate,DispFixedMsg,DispFixedMsg,DispVarMsg,DispVarMsg,DispSeconds,AdjustMinutes,AdjustHours) TimeDate: 'Default - includes temperature: LET Addr = 0 READi2c 2,(DecData) 'Bits 0-3 are hours, 4&5 are tens of hours GOSUB DispBCD LET CharData = ":" GOSUB DispChar READi2c 1,(DecData) 'Bits 0-3 are minutess, 4-6 are tens of minutes GOSUB DispBCD LET CharData = " " GOSUB DispChar READi2c 3,(DecData) 'Bits 0-3 are day of week LET DECData = 3 * DECData + 4 'Day of week strings start at 7, Sunday is day 1 FOR Iter = 0 to 2 READ DECData,CharData GOSUB DispChar INC DECData NEXT LET CharData = " " GOSUB DispChar READi2c 5,(CharData) 'Bits 0-3 are month, 4 is tens of month LET DECData = CharData & $0F IF CharData < 10 THEN notoct LET DECData = DECData + 10 notoct: LET DECData = 3 * DECData + 25 'Day of week strings start at 7, Sunday is day 1 FOR Iter = 0 to 2 READ DECData,CharData GOSUB DispChar INC DECData NEXT LET CharData = " " GOSUB DispChar READi2c 4,(DecData) 'Bits 0-3 are day of month, 4-6 are tens of days GOSUB DispBCD DispTemp: READADC10 TempIN,ADCData LET DECData = ADCData / 4 LET Addr = 20 LET TwoDigit = 1 GOSUB DispDec LET CharData = $22 GOSUB DispChar LET CharData = "F" GOSUB DispChar GOTO AutoDim AdjustHours: LET Addr = 14 LET AdjHour = 1 LET CharData = "H" GOSUB DispChar LET CharData = "O" GOSUB DispChar LET CharData = "U" GOSUB DispChar LET CharData = "R" GOSUB DispChar GOTO AdjustMode AdjustMinutes: LET Addr = 14 LET AdjHour = 0 LET CharData = "M" GOSUB DispChar LET CharData = "I" GOSUB DispChar LET CharData = "N" GOSUB DispChar AdjustMode: LET Addr = 10 LET CharData = "A" GOSUB DispChar LET CharData = "D" GOSUB DispChar LET CharData = "J" GOSUB DispChar LET AdjMode = 1 DispSeconds: LET Addr = 0 READi2c 2,(DecData) 'Bits 0-3 are hours, 4&5 are tens of hours GOSUB DispBCD LET CharData = ":" GOSUB DispChar READi2c 1,(DecData) 'Bits 0-3 are minutess, 4-6 are tens of minutes GOSUB DispBCD LET CharData = ":" GOSUB DispChar READi2c 0,(DecData) 'Bits 0-3 seconds, 4-6 are tens of seconds GOSUB DispBCD GOTO AutoDim AutoDim: READADC PCIN,DECData 'Dims the display according to ambient light LET DECData = 266 - DECData PWMOUT DispBL,70,DECData READi2c 2,(DecData) 'Bits 0-3 are hours, 4&5 are tens of hours IF Decdata <> OldHour THEN IF DecData = MorningAlarm OR DecData = AfternoonAlarm THEN LET OldIRMode = IRMode LET IRMode = 2 ELSE LET IRMode = OLDIRMode PULSOUT CLR,100 ENDIF ENDIF LET OldHour = Decdata 'See if co-processor has byte to send IF IRFlag = 0 THEN main 'No data ready HIGH Reply SERIN IRPort,N2400_4,CoData LOW Reply PULSOUT CLR,100 DO PAUSE 1 LOOP UNTIL IRFlag = 0 'Byte received 'Determine if IR code or Keyboard data IF IRSel = 0 THEN KBDinput 'This line is high for IR codes IF AdjMode = 0 THEN NonAdjustMode LET Iter = AdjHour + 1 READi2c Iter,(CharData) 'Bits 0-3 are minutes, 4-6 are tens of minutes LET Decdata = CharData & %00001111 'minutes LET CharData = CharData / 16 'tens of minutes LET DecData = 10 * CharData + DecData IF CoData = 116 THEN IncTime 'Grey up arrow IF CoData = 117 THEN DecTime 'Grey up arrow BackToBCD: LET CharData = DecData // 10 LET DecData = DecData / 10 LET DecData = 16 * DecData LET Chardata = Decdata | CharData WRITEi2c Iter,(CharData) IF CoData > 115 THEN NonAdjustMode LET AdjMode = 0 NonAdjustMode: LOOKDOWN CoData,(0,1,2,3,4,5,51,52),IRMode 'Translate IR code:buttons 1-5 and grey L & R arrows GOTO main IncTime: INC DecData GOTO BackToBCD DecTime: DEC DecData GOTO BackToBCD rem Procedure for changing either variable message rem Turn off. The keyboard connects with a DB9 extension cable rem Set the rear left switch down, turn on,then operate the IR remote once. rem Select the variable message to change with F3 or F4. Then start with CLEAR rem return or line feed goes back to start. backspace erases. rem After each keypress wait for redraw before next key KBDinput: 'Process keyboard control codes LET Iter = 6 LOOKDOWN CoData,($D2,$D3,$0A,$0D,$08,$90),Iter BRANCH Iter,(VarMsg1,VarMsg2,gocr,gocr,gobs,ClearVarMsg) IF IRMode < 3 OR IRMode > 4 THEN main IF CoData < " " OR CoData > "~" THEN main LET DecData = 24 * IRMode - 64 LET Units = 24 * IRMode - 40 IF KbdAddr > DECData AND KbdAddr < Units THEN KbdAddrOK LET KbdAddr = DECData KbdAddrOK: WRITEi2c KbdAddr,(CoData) INC KbdAddr GOTO main VarMsg1: LET IRMode = 3 GOTO main VarMsg2: LET IRMode = 4 GOTO main gobs: LET DecData = 24 * IRMode - 63 IF KbdAddr < DecData THEN main LET CharData = " " DEC KbdAddr WRITEI2c KbdAddr,(CharData) GOTO main gocr: LET KbdAddr = 24 * IRMode - 64 goto main 'Message processing DispFixedMsg: LET Addr = 0 FOR Iter = BannerStartLoc TO BannerEndLoc LET DECData = 24 * IRMode + Iter READ DECData,CharData GOSUB DispChar NEXT GOTO AutoDim DispVarMsg: LET DecData = 24 * IRMode - 64 LET Units = DecData + 23 LET Addr = 0 FOR Iter = DecData TO Units READi2c Iter,(CharData) GOSUB DispChar NEXT GOTO AutoDim ClearVarMsg: LET DecData = 24 * IRMode - 64 LET Units = DecData + 23 LET CharData = " " FOR Iter = DecData TO Units WRITEi2c Iter,(CharData) GOSUB DispChar NEXT GOTO AutoDim END 'Sub-routines DispChar: 'shifts 8 bits address then 8 bits character LSB first, then load display rem 31 sec 95 bytes for 624 calls = 40 ms per char FOR BitPos = 0 TO 4 READ BitPos,Mask LET Mask = Addr & Mask LOW SRIN IF Mask = 0 then Addrbitislow HIGH SRIN Addrbitislow: PULSOUT SRCLK,1 NEXT LOW SRIN PULSOUT SRCLK,1 HIGH SRIN 'CU is high PULSOUT SRCLK,1 PULSOUT SRCLK,1 FOR BitPos = 0 TO 6 READ BitPos,Mask LET Mask = CharData & Mask LOW SRIN IF Mask = 0 then Charbitislow HIGH SRIN Charbitislow: PULSOUT SRCLK,1 NEXT PULSOUT SRCLK,1 PULSOUT DispWR,1 IF AutoInc = 1 THEN INC Addr ENDIF RETURN DispDec: 'Displays DECData (byte) at current address position in Digital IF TwoDigit = 1 THEN TwoOnly LET CharData = DECData / 100 LET DECData = DecData // 100 LET CharData = CharData + "0" GOSUB DispChar TwoOnly: LET CharData = DECData / 10 LET DECData = DecData // 10 LET CharData = CharData + "0" GOSUB DispChar LET CharData = DECData + "0" GOSUB DispChar RETURN DispBCD: BCDTOASCII DECData,CharData,Units GOSUB DispChar LET CharData = Units GOSUB DispChar RETURN