Quote Originally Posted by mister_e View Post
post your code,

EDIT: open DT-INT file, usually you could need to comment out some lines...
Here is my code, but not quite sure what lines in DT_INTS-14.bas I would need to comment out??

Code:
'< FL_PIC16F886 >' ' First valid PIC found within the first 200 lines will
                    ' highlight AND set device.
'< FL_PBPW >'       ' OR < FL_PBPL >
'< FL_MPASM >'      ' OR  < FL_PM >
'***********************************************************************
'*  Name    : DS1337clockedTemp.pbp                                    *
'*  Author  : John R. Ellis                                            *
'*  Notice  : Copyright (c) 2010 LodeStar Associates, Inc.             *
'*          : All Rights Reserved                                      *
'*  Started : 07/26/2011                                               *
'*  Version : 1.0                                                      *
'*  Notes   : 1) This code was designed/devloped on an EasyPic6        *
'*  Compiler: PICBASIC PRO Compiler 2.60C from microEngineering Labs   *
'*  Device  : 16F886 w/ serial 2x16 or 2x8 LCD as installed on         *
'*            ProtoBoard connected via Port Extension to an EasyPic6.  *
'*  Memory  : 9,614 bytes of Program Memory required.                  *
'*  Ref:    : 1) RTC portions of this code are adapted from code       *
'*          : written by Tonigalea (see URL ref below), but modified   *
'*          : to run on an EasyPic6 with 18F4550 MCU and a DS1337 RTC. *
'*http://www.picbasic.co.uk/forum/showthread.php?t=662&highlight=DS1337*
'*                                                                     *
'***********************************************************************
' -----------------------[ Program Description ]------------------------
' PICBASIC PRO program to set RTC & demo operation of a LCD/4-bit mode *
'  1) Initializes 2x16 or 2x8 LCD display (if installed) with EasyPic6.*
'  2) Time/date/Alarm1 must have already been programmed into the      *
'     DS1337 Real-time-clock before running this program.              *
'     (IMPORTANT: user must use available SetTimeDate program to       *
'      program DS1337 prior to programming this program into the MCU)  *
'  3) Alarm1 from the DS1337 is used as RB2 interrupt on change by MCU.*
'  4) Also displays date/time on LCD (if installed) every second.      *
'  5) Measures temperature on every Alarm1 interrupt and displays it.  *
'  6) If battery is so low to drop Vr<=3.39 vdc, LED_RED will blink 5x *
'     as a low voltage warning monitor.                                *
'***********************************************************************
' -----------------------[ Revision History ]---------------------------
' Version 1.0   Started on 07/19/11
' ------------[ NOTES for use with EasyPic6 development board ]---------
    ' Make sure that SW6.8 on the EasyPic6 is set to ON position before
    ' powerup for LCD's requiring backlight.
    'For use with 2x16 LCD (when installed):
'     - Turn on Port Expander switches SW6.1,SW6.2,SW6.3,SW6.4 & SW6.5.
'     - Turn on COG LCD 2x16 switches SW10.1, SW10.2, SW10.3, SW10.4,
'       SW10.5 & SW10.6.
    'For interface to DS1337 RTC:
'     - TO provide battery backup, place two Shottky diodes in 'or'
'       configuration between the Vdd (pin-8) of the DS1337 and the Vcc
'       source and the + terminal of the backup battery.
'     - I2C communication lines should be connected to 4.7K pull-ups.
'     - DS1337 INTA & INTB lines should be connected to pull-up resistsors.
'     - Turn off LEDs connected to I2C communication lines.
'-----------------[ 16F886 Port/PIN Connections ]------------------
'I/O pin connections to the 18F4550 MCU are as follows:
'PORTA.0 (02) output to LCD D4 & also multiplexed as input from LM34.
'PORTA.1 (03) output to LCD D5.
'PORTA.2 (04) output to LCD D6 & also mulitplexed as Vref+ input.
'PORTA.3 (05) output to LCD D7.
'PORTA.4 (06) output to LCD RS.
'PORTA.5 (07) ouput to LCD E.
'PORTB.0 (21) output as I2C SDA to DS1337.
'PORTB.1 (22) output as I2C SCL to DS1337.
'PORTB.2 (23) input as interrupt on change from pin-3 (_INTA) of DS1337 RTC.
              ' Also senses pushbutton for manual interrupt for RangeFinder.
'PORTB.3 (24) input from SQW/_INTB pin-7 of DS1337 RTC.
'PORTB.4 (25) set as A/D Channel 11 for analog input from LM34 output
'PORTB.5 (26) unconnected
'PORTB.6 (27) connected as PGC for ICSP connection.
'PORTB.7 (28) connected as PGD for ICSP connection.
'PORTC.0 (11) output RC0 to control external power to SRF02 & LM34.
'PORTC.1 (12) output RC1 to MSEL on WiFi Module
'PORTC.2 (13) output RC2 to LCD R/W or to _RES_PD on WiFi module
'PORTC.3 (14) reserved to connect as Vbus to 470 pf capacitor for USB
'PORTC.4 (15) output RC4 to a Red LED for low voltage warning.
'PORTC.5 (16) output RC5 to Green LED for routine entry indicator.
'PORTC.6 (17) output as TX to WiFi module RXD.
'PORTC.7 (18) output as RX to WiFi module TXD.
'--------------[ Define EEPROM usage ]-----------------------------------
   '07  = mon      ' Month of program startup
   '08  = date     ' Day of month of program startup
   '09  = yr       ' Year of program startup
   '10  = J        ' Index for counting interrupts received
   '11  = hr       ' Hour time from RTC at MCU startup time
   '12  = MINs     ' Minutes time from RTC at MCU startup time
   '13  = sec      ' Seconds time from RTC at MUC startup time
   '14  = A1hr     ' Alarm1 hour time from RTC after setting RTC
   '15  = A1MINs   ' Alarm1 minutes time from RTC after setting RTC
   '16+J = hr      ' Log Hour of interrupt
   '17+J = MINs    ' Log Minutes of interrupt
   '18+J = sec     ' Log Seconds of interrupt
   '19+J = temp    ' Log temperature at time of intercept
   '236            ' Last legitimate EEPROM logging address
   '240 = ID(0)    ' LSB of device ID
   '241 = ID(1)    ' MSB of device ID
   '242 = mon      ' Month when clock was set
   '243 = date     ' Date when clock was set
   '244 = yr       ' Year when clock was set
'INCLUDE "CodeSize.pbp"     'FOR TEST ONLY
DEFINE Measure 1
;@ StartSize(LOOKUP)        'FOR TEST ONLY
Pause 10     ' This statement is placed at beginning of code for ICD use
@ ERRORLEVEL -306             ; turn off crossing page boundary message
'Disable Debug                ' Uncomment for test only
' Set configuration fuses for the MCU
' To use standard config include file, comment out below statement
@ __config   _CONFIG1, _HS_OSC & _WDT_OFF & _MCLRE_OFF & _LVP_OFF & _CP_OFF
'--------------[ Define Hardware / Set Registers ]------------------------
    DEFINE OSC 16
    DEFINE I2C_SLOW 1   ' Set i2c to the standard speed
    DEFINE I2C_HOLD 1   ' Enable receiving i2c device to pause communication
    INCLUDE "Modedefs.Bas"    ' Mode definitions for Debug,Serin/out,
                              ' Shiftin/out, Xin/out.
    INCLUDE "ALLDIGITAL.pbp"  ' Sets all registers for digital ops.
                              ' User must make sure AllDigital.pbp file
                              ' is in same or higher directory location as
                              ' this source code before compiling.
        'DEFINE SHOWDIGITAL 1 ' When uncommented will show analog settings
                              ' in Assembler Results window.
    INCLUDE "DT_INTS-14.bas"  ' Base Interrupt System
    INCLUDE "ReEnterPBP.bas"  ' Include if using PBP high priority interrupts
                              ' Allows re-entry to PBP from a High Priority
                              ' ASM interrupt. Must have DT_INTS-14.bas
                              ' loaded first.
    TRISA = 0           ' Set all PortA initally to outputs for LCD use
    TRISB = %00011100   ' RB2 & RB3 set as RTC Alarm1 & Alarm2 inputs
                        ' PORTB.2 is also an interrupt from manual switch GND
                        ' PORTB.4 is set at A/D Channel 11
    TRISC = %10000000   ' Set PortC to all outputs except PortC.7 which is
                        ' reserved as RX input.
                        ' PortC.1 is output for MSEL to WiFi module
                        ' PortC.2 is used for the LCD R/W connection when
                        ' LCD present..otherwise as _RES_PD to Wifi Module.
                        ' PortC.4 is output to LED_RED
                        ' PortC.5 is output to LED_GRN
CLEAR
'--- Setup Interrupts ----------------------------------------------------
ASM
INT_LIST  macro      ; IntSource,        Label,  Type, ResetFlag?
        INT_Handler      RBC_INT,       _Alarm,   PBP,  yes
        INT_Handler      LVD_INT,     _VoltLow,   PBP,  yes
    endm
    INT_CREATE               ; Creates the interrupt processor
ENDASM
'ENABLE DEBUG
' Per DT, DT_INTS already takes care of setting registers
' but this code doesn't work unles below registers set as indicated:
    IOCB = %00000100     ' Enable RB2 as Interrupt on Change pin for RTC Alarm
    WPUB = 0             ' Disable all PortB pull-ups
'--- Variables & Aliases -------------------------------------------------
CNT         VAR BYTE      ' Counter for timing
CNT = 0
D_LAY       VAR BYTE      ' Stores delay value
ADR         VAR BYTE[9]   ' Array stores IP address
deviceID1   VAR BYTE      ' Serial # of MCU (MSB)
deviceID2   VAR BYTE      ' Serial # of MCU (LSB)
ext_pwr     VAR PortC.0   ' MCU Controlled external power to SRF02 & LM34
I           VAR Byte      ' Index for loop counters
ID          VAR BYTE[8]   ' Array for storing device ID parameters
J           VAR BYTE      ' Index to EEPROM address for logging
LCD_Flag    VAR Bit       ' Flag when set indicates LCD is installed/used
LED_RED     VAR PortC.4   ' Red LED
LED_GRN     VAR PortC.5   ' Green LED used to indicate Routine entries
logflag     VAR BYTE      ' Flag used in ISR to indicate to log time stamp
LowVolt     VAR BIT       ' Flag that is set when MCU power source is low volt
MSEL        VAR PORTC.1   ' iChip Mode Select
_RES_PD     VAR PORTC.2   ' iCHIP RESET/Power-Down
OK          VAR BYTE      ' Captures K in I/OK FTP return messages for testing
SCL         VAR PORTB.1   ' I2C clock pin
SDA         VAR PORTB.0   ' I2C data pin
Spare_1     VAR PORTB.7   ' PGD for ICSP & Spare I/O for normal ops
Spare_2     VAR PORTB.6   ' PGC for ICSP & Spare I/O for normal op
temp        VAR WORD      ' Stores measured temperature
TX          VAR PORTC.6   ' Serial interface to iWiFI module
RX          VAR PORTC.7
'ENABLE DEBUG
'--- Initialize ----------------------------------------------------------
    ' User must set FLAGS for current configuration
        LowVolt = 0       ' Initalize low voltage Flag
        LCD_Flag = 0      ' Clear if LCD un-installed; set otherwise
    'CCPR1L = 0                 ' Not used in this app
    'CCPR2L = 0                 ' Not used in this app
    'CCP1CON =   %00001100      ' CCP1, PWM mode...not used in this app
    'CCP2CON =   %00001100      ' CCP2, PWM mode...not used in this app
    PR2     =   249             ' 0-1000 duty range
    T2CON   =   %00000101       ' TMR2 on, prescaler 1:4
'--- Read MCU Serial Number is stored in 4 ea device ID Locations
' at memory locations Addresses $2000 to $2003 but can't be read at run time
    deviceID1 = ID(0)           ' MSB of MCU Device Serial Number
    deviceID2 = ID(1)           ' LSB of MCU Device Serial Number
    WRITE 240, ID(0)            ' Write fixed values to EEPROM for test
    WRITE 241, ID(1)
'*******************SETUP FOR USING DS1337 Real Time Clock**************
    ' Setup Hardware for uart
        DEFINE HSER_BAUD 115200
        DEFINE HSER_RCSTA 90h
        DEFINE HSER_TXSTA 24h
        DEFINE HSER_CLROERR 1
    ' Aliased Variables for CLock
        Alarm1     VAR PORTB.2     ' Alarm1 input from DS1337 INTA (pin-3)
        'Alarm2     VAR PORTB.3     ' Alarm2 input from DS1337 INTB (pin-7)
        'SCL        VAR PORTB.1     ' I2C clock pin
        'SDA        VAR PORTB.0     ' I2C data pin
        RTCdevice CON $D0  ' Device write address = %11010000 for RTC
        ' This is a list of allowable clock contrl settings, one which
        ' must be setup in the SetTimeAndDate subroutine for intended use.
        '(see data sheet pgs 9-10 for explanation)
        'contrl   CON %00000000 ' Starts oscillar, sets the SQW/OUT to
                                ' 1Hz pulse, no Alarm interrupts enabled.
                                ' WORKS as intended!!
        'contrl   CON %00000001 ' Starts oscillator, sets the SQW/OUT to
                                ' 1 HZ pulse, & enables INTA interrupt from
                                ' A1F.  WORKS as intended!!
        'contrl   CON %00000010 ' Starts oscillator, sets the SQW/OUT to
                                ' 1 HZ pulse, enables INTA interrupt from
                                ' A2F. WORKS AS intended!!
        'contrl   CON %00000011 ' Starts the oscillator, sets the SQW/OUT
                                ' to 1 Hz, enables INTA interrupt from A1F
                                ' or from A2F.  WORKS as intended!!
        'contrl   CON %000xx100 ' Starts oscillator, no interrupts enabled.
                                  ' WORKS as intended!
        'contrl   CON %000xx101 ' Starts oscillator, enables INTA interrupt
                                ' from A1F, INTB/SQW off. WORKS OK!
        'contrl   CON %000xx110 ' Starts oscillator, enables INTB interrupt
                                ' from A2F. WORKS OK!
        'contrl   CON %000xx111 ' Starts oscillator, enables INTA interrupt
                                ' from A1F or INTB from A2F. WORKS OK!
                                ' Both interrupts staggered 30 secs apart.
        'contrl   CON %00011111 ' Starts oscillator, sets RS1 & RS2,
                                ' enables INTA interrupt from A1F
                                ' or A2F starts 32.768khz on SQW output.
                                ' WORKS OK!
        'contrl   CON %00011001 ' Starts oscillator, sets RS1 & RS2,
                                ' clears INTCN to enable INTA interrupt
                                ' from A1F with 32.768kHz on SQW output.
                                ' SQW output WORKS OK but INTA doesn't!
        'contrl   CON %00011010 ' Starts oscillator, sets RS1 & RS2,
                                ' clears INTCN to enable INTA interrupt
                                ' from A2F with 32.768kHz on SQW. WORKS OK!
    ' RTC Address definitions
        SecReg      CON $00   ' seconds address (00 - 59)
                              ' Must set MSB of SecReg to a 0 to enable RTC
        MinReg      CON $01   ' minutes address (00 - 59)
        HrReg       CON $02   ' hours address (01 - 12) or (00 - 23)
        DayReg      CON $03   ' day address (1 - 7)
        DateReg     CON $04   ' date address (01 - 28/29, 30, 31)
        MonReg      CON $05   ' month address (01 - 12)
        YearReg     CON $06   ' year address (00 - 99)
    ' Alarm 1 Address definitions
        Alm1sec     CON $07  ' Alarm 1 seconds address (00 - 59)
        Alm1min     CON $08  ' Alarm 1 minutes address (00 - 59)
        Alm1hr      CON $09  ' Alarm 1 hours address (01 - 12) or (00 - 23)
        Alm1Day     CON $0A  ' Alarm 1 day address (1 - 7)
    ' Alarm 2 Address definitions..not used in this application
        'Alm2min     CON $0B  ' Alarm 2 minutes address (00 - 59)
        'Alm2hr      CON $0C  ' Alarm 2 hours address (01 - 12) or (00 - 23)
        'Alm2Day     CON $0D  ' Alarm 2 day address (1 - 7)
    ' Alias of Clock register addresses
       ContReg     CON $0E    ' CONTROL register address
       StatusReg   CON $0F    ' STATUS register address
    ' Clock Variables
        sec         VAR BYTE  ' seconds
        MINs        VAR BYTE  ' minutes
        hr          VAR BYTE  ' hours
        day         VAR BYTE  ' day of week (Sunday = 7)
        date        VAR BYTE  ' date in month
        mon         VAR BYTE  ' month
        yr          VAR BYTE  ' year
    ' ALARM1 VARIABLES
        A1sec       VAR BYTE  ' seconds
        A1MINs      VAR BYTE  ' minutes
        A1hr        VAR BYTE  ' hours
        A1day       VAR BYTE  ' day
    ' ALARM2 VARIABLES..not used in this application
        'A2MINs      VAR BYTE  ' minutes
        'A2hr        VAR BYTE  ' hours
        'A2day       VAR BYTE  ' day
'*****************SETUP FOR USING LM34 Temperature Sensor**************
    'DEFINE osc 8           ' Using a 16 MHz oscillator..already defined
    DEFINE ADC_BITS 10      ' Set A/D for 10-bit operation
    DEFINE ADC_CLOCK 5      ' Set A/D clock Fosc/16
    DEFINE ADC_SAMPLEUS 50  ' Set A/D sampling time @ 50 uS
    samples VAR WORD        ' Multiple A/D sample accumulator
    sample  VAR BYTE        ' Holds number of samples to take
    value   VAR WORD        ' Holds LM34 measured temperature value
    samples = 0             ' Clear samples accumulator on entry
'WRITE 3,3      'EEPROM test to see if program executes to here
'*********SETUP FOR USING ConnectOne Mini Socket iWiFi module**********
BOOT:   'iCHIP SET UP
  HIGH _RES_PD     ' Set high for normal ops
  PAUSE 500        ' Delay to stabilize coming out of power down mode
  LOW MSEL         ' Exit SERIALNET mode and return iChip to normal AT+i mode
  PAUSE 10000
  HIGH MSEL        ' Set high for normal AT+i ops
  'SEROUT2 PORTC.0,16468,["TEMP OUTSIDE ",DEC temp,$d,$a,$d ,$a] 'test not used
  PAUSE 500
'WRITE 3,3      'EEPROM test to see if program executes to here
  SEROUT2 TX,6,[ "AT+I",$d ,$a]
  WRITE 4,4       'EEPROM test to see if program executes to here..it does
  SERIN2 RX ,6,2500,BROKE,[ WAIT("I/OK")]
'WRITE 5,5   'EEPROM test to see if program executes to here
     ' ABOVE TEST INDICATESInterface to iChip is NOT working!
  'SEROUT2 PORTC.0,16468 , [ "iChip OK",$d,$a ]  'test not used
  'SEROUT2 PORTC.0,16468 , [ "TEST_" , DEC3 CNT , 13 , 10 ]  'test not used
  SEROUT2 TX ,6 , [ "AT+iIPA?" , $d , $a ]
  SERIN2 RX ,6 , 2500 ,BOOT , [ DEC ADR[0] , DEC ADR[1] , DEC ADR[2] , DEC ADR[3] ]
  WRITE 10, ADR[0] ' Write the received IP address to EEPROM as a test
  WRITE 11, ADR[1]
  WRITE 12, ADR[2]
  WRITE 13, ADR[3]
  GOSUB DIP
  PAUSE 2000
  'SEROUT2 TX,6,["AT+iWLWM=1",$d,$a] ' Sets WEP enabled with 64bit key
  'SERIN2 RX ,6,2500,BROKE,[ WAIT("I/OK") ]
  'SEROUT2 TX,6,["AT+iWLKI=1",$d,$a] ' Sets the Wireless LAN transmission WEP-Key index
  'SERIN2 RX ,6,2500,BROKE,[ WAIT("I/OK") ]
  'SEROUT2 TX,6,["AT+iWLKn1=xxxxxxxxxx",$d,$a] ' Sets the Wireless LAN WEP key
  'SERIN2 RX ,6,2500,BROKE,[ WAIT("I/OK") ]
  SEROUT2 TX,6,["AT+iWLSI=Buckskin",$d,$a] ' Sets the destination Wireless LAN SSID
  SERIN2 RX ,6,2500,BROKE,[ WAIT("I/OK") ]
  SEROUT2 TX,6,["AT+iWWW",$d,$a]        ' Activates iChip's internal web server
  SERIN2 RX,6,1000,BOOT,[WAIT("I/(")]
'########### END OF SETUPS ##################
 
GOTO JumpPoint ' Jump over all subroutines so they don't execute on powerup
' ----------------------[ START SUBROUTINES ]--------------------------
InitializeDisplay:  ' Subroutine to initialize 2X16 or 2x8 LCD display
'=================
  '-----SETUP FOR USING 2x16 or 2x8 LCD THAT IS INSTALLED IN EASYPIC6--------
     IF LCD_Flag = 1 THEN
           TRISA = 0   ' Set PortA for all outputs for use with LCD
     ' LCD DEFINES FOR USING 2x16 LCD with PortA in EASYPIC6
           DEFINE LCD_DREG PORTA    ' Use PORTA for LCD Data
           DEFINE LCD_DBIT 0        ' Use lower(4) 4 bits of PORTA
                                      ' PORTA.0 thru PORTA.3 connect to
                                      ' LCD DB4 thru LCD DB-7 respectively
           DEFINE LCD_RSREG PORTA   ' PORTA for RegisterSelect (RS) bit
           DEFINE LCD_RSBIT 4       ' PORTA.4 pin for LCD's RS line
           DEFINE LCD_RWREG PORTC   ' LCD read/write port
           DEFINE LCD_RWBIT 2       ' LCD read/write bit
           DEFINE LCD_EREG PORTA    ' PORTA for Enable (E) bit
           DEFINE LCD_EBIT 5        ' PORTA.5 pin for LCD's E line
           DEFINE LCD_BITS 4        ' Using 4-bit bus
           DEFINE LCD_LINES 2       ' Using 2 line Display
           DEFINE LCD_COMMANDUS 1500' Command Delay (uS)
           DEFINE LCD_DATAUS 44     ' Data Delay (uS)
    ' DEFINE LCD Control Constants
           Line1   CON 128          ' Point to beginning of line 1 ($80)
           Line2   CON 192          ' Point to beginning of line 2 ($C0)
    ENDIF
    ' Test the LCD during initialization
      IF LCD_Flag = 1 THEN
           LCDOut $fe,1:FLAGS=0:Pause 250    ' Clear Display
           LCDOUT $fe,Line1,"POWER ON"       ' Display on 1st line
           LCDOUT $fe,Line2,"LCD TEST"       ' Display on 2nd line
           PAUSE 2000
      ENDIF
Return
SetTimeAndDate:  ' Subroutine to set current time, date and alarms
'==============
    ' Blink LED_GRN 2X times to indicate entered SetTimeAndDate
           FOR I = 0 TO 1
               HIGH LED_GRN
               PAUSE 500
               LOW LED_GRN
               PAUSE 500
           NEXT
    ' Initialize the display & clock
        PAUSE 100           ' Wait for LCD to startup after power on
        ' Initialize clock variables to 0
            yr=0:date =0:mon =0:day=0:hr=0:MINs=0:sec=0
            A1sec=0:A1MINs=0:A1hr=0:A1day=0
            'A2MINs=0:A2hr=0:A2day=0
        ' The BCD constants below set the RTC to: 17:17:00 on 08-02-2011
            hr=$17
            MINs=$17
            sec=$00
            day=$03
            mon=$08
            date=$02
            yr=$11
        ' Per MAXIM TechSupport, proper sequencing requires setting
        ' INTCN = 1, & A2IE=A1IE=0 before setting alarms.
            I2CWRITE SDA, SCL, RTCdevice, ContReg,[%00000100]
            PAUSE 10
        'Define ALARM1 FOR 17:18:00 & to alarm when mins/secs match (1/hr).
            ' This requires A1M1=A1M2=0 and A1M3=A1M4=1
            ' (A1xxx + $80 or A1xxx + 128d sets bit 7 as A1Mx = 1)
            A1hr   = %10011010  '$17 + $80 = %10011010, for A1M3 = 1
                                'Bit6 must be 0 for 24 hour clock
            A1MINs = %00011000  '$18 = %00011000, for A1M2 = 0
            A1sec  = $00        '$00 = %00000000, for A1M1 = 0
            A1day  = %10000110  '$06 + $80 = %10000110, for A1M4 = 1
                                'DY/_DT Bit6 = 0 for using Day of month alarm
        'Define ALARM2 FOR 14:01:00 & to alarm once per minute (00 secs)
        'Disabled in this application
            ' This requires A2M2 = A2M3 = A2M4 = 1
            ' (A2xxx + $80 or A2xxx + 128d sets bit 7 as A2Mx = 1)
            'A2hr   = $14 + $80  '$14  + $80 = %10010100, for A2M3 = 1
                                'Bit6 must be 0 for 24 hour clock
            'A2MINs = $01 + $80  '$01 + $80 = %10000001, for A2M2 = 1
            'A2day  = $07 + $80  '$07 + $80 = $10000111, for A2M4 = 1
                                'DY/_DT Bit6 = 0 for using Day of month alarm
        ' Per MAXIM TechSupport, reset control register for desired Alarm
        ' after setting alarms.
            I2CWRITE SDA, SCL, RTCdevice, ContReg,[%00000101]
            PAUSE 10
        'Set the main Time
            I2CWRITE SDA, SCL, RTCdevice, SecReg,[sec,MINs,hr,day,date,mon,yr]
            PAUSE 10
        'Set the Alarm1 Time
            I2CWRITE SDA, SCL, RTCdevice, Alm1sec,[A1sec,A1MINs,A1hr,A1day]
            PAUSE 10
        'Set the Alarm2 Time  ' Alarm2 disabled for this application
            'I2CWrite SDA, SCL, RTCdevice, Alm2min,[A2MINs,A2hr,A2day]
            'PAUSE 10
        IF LCD_Flag = 1 THEN ' This code block for testing only
           'Test to see if Time set correctly by displaying on LCD
                I2CREAD SDA, SCL, RTCdevice, SecReg,[sec,MINs,hr,day,date,mon,yr]
                LCDOUT $fe,1 ' Clear Display
                LCDOUT $fe,Line1,"SetTime"
                LCDOUT $FE,Line2,HEX2 hr,":",HEX2 MINs,":",HEX2 sec
                PAUSE 3000    ' Delay so user can view display
            'Test if Alarm1 set correctly by displaying stored Alarm1 time
                I2CREAD SDA,SCL,RTCdevice,Alm1sec,[A1sec,A1MINs,A1hr,A1day]
                LCDOUT $fe,1 ' Clear Display
                LCDOUT $fe,Line1,"SetAlm1"
                LCDOUT $FE,Line2,HEX2 A1hr,":",HEX2 A1MINs,":",HEX2 A1sec
                PAUSE 3000    ' Delay so user can view display
        ENDIF
        ' Clear STATUS and set CONTROL registers
            I2CWRITE SDA, SCL, RTCdevice, StatusReg,[$00]
            PAUSE 10
            I2CWRITE SDA, SCL, RTCdevice, ContReg,[%00000101]
                          ' Starts oscillator, no interrupts enabled.
                          ' WORKS as intended!!
            PAUSE 10
RETURN
Temperature:        ' Subroutine to measure temperature from LM34
'===========
' This subroutine is an adaptation from below reference to 10-bit A/D
'****************************************************************
'*  Name    : LM34.BAS                                          *
'*  Author  : Bruce Reynolds                                    *
'*  Date    : 10/23/2001                                        *
'*  Version : 1.0                                               *
'*  Notes   :  8-bit A/D temperature conversion with National   *
'*          ;  LM34CAZ analog temperature sensor.               *
'*          :  See http://www.rentron.com/PicBasic/LM34.htm     *
'****************************************************************
    ' Setup registers for A/D input per Sec 21.0 of datasheet
    '1. Configure the A/D module:
      '  Configure analog pins, voltage reference AND I/O (ADCON1)
          ADCON1 = %00000011 'Vdd as ref; all analog except AN12 (RB0)
      '  SELECT A/D INPUT channel (ADCON0)
          ADCON0 = %001011000  'Set AN11 (PORTB.4) as A/D input but disabled
      '  SELECT A/D acquisition time & A/D conversion clock (ADCON2)
          ADCON2 = %10110101  'Right justified;TAD=16;A/D clock=Fosc/16
      '  Turn ON A/D module (ADCON0)
        ADCON0.0 = 1        'Enable the A/D module
    '2. Configure A/D INTERRUPT (IF desired):
        '  CLEAR ADIF BIT
        '  Set ADIE BIT
        '  Set GIE BIT
    '3. WAIT the required acquisition time (IF required).
        'PAUSE 250          ' Wait approximately 1/4 seconds per loop
    '4. Start conversion-Set GO/DONE BIT (ADCON0 register)
        ADCON0.1 = 1
    '5. WAIT FOR A/D conversion TO complete, by either:
        '  Polling FOR the GO/DONE BIT TO be cleared
            'OR
        '  Waiting FOR the A/D INTERRUPT
    FOR sample = 1 TO 20    ' Take 20 samples
        ADCIN 11, value      ' Read channel 11 into temp variable
        'PAUSE 2
        ' or use alternative manual method to read A/D input
            'WHILE ADCON0.1 : WEND
            '6. READ A/D Result registers (ADRESH:ADRESL);
                'CLEAR BIT ADIF, if required.
               'value.HIGHBYTE = ADRESH
               'value.LOWBYTE = ADRESL
         samples = samples + value ' Accumulate 20 samples
        '7. FOR NEXT conversion, go TO STEP 1 or STEP 2, as
            'required. The A/D conversion time per BIT IS
            'defined as TAD. A minimum WAIT of 3 TAD IS
            'required before the NEXT acquisition starts.
        PAUSE 250          ' Wait approximately 1/4 seconds per loop
    NEXT sample
    value = samples/20      ' Determine average of 20 measurements
    'ADC Math for 10-bit A/D when using Vdd as referenc
        'ADC Resolution = 4.99 V/1024 COUNT = 4.912109 mV/COUNT
        '=> LM34DZ OUTPUT voltage (mV) = 2 x COUNT
        'Temperature ( F) = LM34 OUTPUT voltage in mV/10
        '=> Temperature ( F) = 4.912109 x COUNT/10 = 0.4912109*COUNT
        ' Therefore temp = 0.4912109*Count or approx. = Count/2.036
    ' To avoid floating point math, multiply by 1000 and then DIV32
    value  = 1000 * value
    temp = DIV32 2036
    'temp = value
    samples = 0             ' Clear old sample accumulator for next cycle
    ADCON1 = 255            ' Go to all digital ops
RETURN
Email:
      EMAIL_SET:
        SEROUT2 TX,6,["AT+iSBJ:iChip ADC TEMPERATURE",$d,$a] 'Permanently sets Email header s Subject field
        SERIN2 RX ,6,2500,EMAIL_SET,[WAIT("I/OK")]
        SEROUT2 TX,6,["AT+iTOA:[email protected]",$d,$a]  'Permanently sets Email addressee
        SERIN2 RX ,6,2500,EMAIL_SET,[WAIT("I/OK")]
        SEROUT2 TX,6,["AT+ito:jellis00",$d,$a]      'Permanently sets Email header s =To:  description
        SERIN2 RX ,6,2500,EMAIL_SET,[WAIT("I/OK")]
        SEROUT2 TX,6,["AT+iREA:[email protected]",$d,$a] 'Permanently sets the RETURN EMAIL Address
        SERIN2 RX ,6,2500,EMAIL_SET,[WAIT("I/OK")]
        SEROUT2 TX,6,["AT+iFRM:iChip",$d,$a]        'Permanently sets Email header =From: description.
        SERIN2 RX ,6,2500,EMAIL_SET,[WAIT("I/OK")]
        SEROUT2 TX,6,["AT+iSMTP:mail.comcast.net",$d,$a]'Sets the SMTP Server Name or IP.
        SERIN2 RX ,6,2500,EMAIL_SET,[WAIT("I/OK")]
        SEROUT2 TX,6,["AT+iSMA=1",$d,$a]            'Permanently sets SMTP authentication method
        SERIN2 RX ,6,2500,EMAIL_SET,[WAIT("I/OK")]
        SEROUT2 TX,6,["AT+iSMP:Kevin111",$d,$a]       'Permanently sets authenticated SMTP login
        SERIN2 RX ,6,2500,EMAIL_SET,[WAIT("I/OK")]
        SEROUT2 TX,6,["AT+iSMU:[email protected]",$d,$a]'Permanently sets Authenticated SMTP login User Name.
        SERIN2 RX ,6,2500,EMAIL_SET,[WAIT("I/OK")]
      ' Put code here to email low temperature warning via WiFi
        SEROUT2 TX,6,["AT+iEMA:",$d,$a]  'Defines a plain text e-mail body
        SEROUT2 TX,6,["HI, THIS IS FROM 206 N Veterans.",$d,$a]
        SEROUT2 TX,6,["SENT EVERY 2 minutes.",$d,$a]
        SEROUT2 TX,6,["THE TEMPERATURE IS.",$d,$a]
        SEROUT2 TX,6,[DEC temp," F Inside.",$d,$a]
        SEROUT2 TX,6,["GO TO:",$d,$a]
        SEROUT2 TX,6,["http://www.lodestarassoc.com/ichip/ichip.html",$d,$a]
        SEROUT2 TX,6,["FOR A ONE MINUTE UPDATE.",$d,$a]
        'After successfully sending the e-mail, IF the stay online flag (!) IS specified.
               SEROUT2 TX,6,[$d,$a,".",$d,$a]
               SERIN2 RX,6,[WAIT("I/ONLINE")]
        PAUSE 1000
        'SEROUT2 PORTC.0,16468,["EMAIL SENT ",$d,$a]  'test not used
        WRITE 5,$01       ' Write 1 to EEPROM as test if Email sent
        HIGH PORTC.5      ' blink green led as test to show email sent
        PAUSE 1000
        LOW PORTC.5
RETURN
DIP:
      'IF (!ADR[1]) AND )!ADR[1]) AND (!ADR[2]) AND (!ADR[3]) THEN
        HIGH PORTC.6      'blink red led as test if not connected
        PAUSE 1000
        LOW PORTC.6
      'ENDIF
      'SEROUT2 PORTC.0,16468,[" CONNECTED ",$d,$a] ' test not used
      'SEROUT2 PORTC.0,16468,["ADR - " ,DEC3 ADR[0],".",DEC3 ADR[1],".",DEC3 ADR[2],".",DEC3 ADR[3],$d,$a]
      PAUSE 1000
  RETURN
  BROKE:
        SEROUT2 PORTC.0,16468,["NOT WORKING",$d,$a] 'Commented out for now
        WRITE 16, "B"     'Record in EEPROM as test if Broke
        ' Blink LED_RED 2X times to indicate iChip not working
           FOR I = 0 TO 1
               HIGH LED_RED
               PAUSE 500
               LOW LED_RED
               PAUSE 500
           NEXT
        PAUSE 500
        GOTO BOOT
' ----------------------[ END OF SUBROUTINES ]--------------------------
JumpPoint:
    'Setup before starting Main Program
    IF LCD_Flag = 1 THEN        ' only if LCD installed....
       GOSUB InitializeDisplay  ' initialize LCD before starting mainloop
    ENDIF
    IF LCD_Flag = 1 THEN ' This code block FOR TESTING ONLY with LCD
        LCDOut $fe,1' Clear Display
        LCDOUT $fe,Line1,"Entered"
        LCDOUT $fe,Line2,"JumpPoint"
        Pause 2000
    ENDIF
    
    'GOSUB SetTimeAndDate   'RTC presumably already set. Uncomment if not &
                            'set the current time and alarm settings in the
                            'SetTimeAndDate subroutine.
    'Initialize epoch indexes at time of powerup/reset
        I2CREAD SDA, SCL, RTCdevice, SecReg,[sec,MINs,hr,day,date,mon,yr]
        PAUSE 20
        ' Write start date/time/Alarm1 to EEPROM at startup
          WRITE 7, mon
          WRITE 8, date
          WRITE 9, yr
          WRITE 11, hr
          WRITE 12, MINs
          WRITE 13, sec
          WRITE 14, A1hr
          WRITE 15, A1MINs
        Pause 20
        J = 0                ' Initialize EEPROM logging index
    ' Setup test for low voltage condition that causes Vdd to go below 3.3 vdc
      ' Setup registers for HLVD module to support low voltage interrupt
          ' Setup the interrupt and an ISR in your code and enable the interrupt
          ' where you want to test for low voltage
    'Initialize LEDs to off
        LOW LED_GRN
        LOW LED_RED
    ' Enable the interrupts that are to be active during main loop.
        ext_pwr = 1            ' Turn on external power to LM34 & SRF02
        logflag = 0            ' Clear flag before main loop
@    INT_ENABLE   RBC_INT      ; enable RB Port Change interrupts
@    INT_ENABLE   LVD_INT      ; enable LVD interrupt
HIGH ext_pwr                   ' Turn external power on to SRF02 & LM34
'--------------------[ Begin Main Program Loop ]-------------------------
;@ StartSize(MainLoop)    ; FOR TEST ONLY
mainloop:
    ' Blink Green LED 1x at start of Main Loop
        HIGH LED_GRN
        PAUSE 250
        LOW LED_GRN
        PAUSE 250
    ' Read current time/date
        I2CREAD SDA, SCL, RTCdevice, SecReg,[sec,MINs,hr,day,date,mon,yr]
        PAUSE 20
    
    ' Display current Date/Time if LCD installed
        IF LCD_Flag = 1 THEN ' Display only if LCD installed
            LCDOUT $fe,1 ' Clear Display
            LCDOUT $fe,Line1,HEX2 mon,"/",HEX2 date,"/", HEX2 yr
            LCDOUT $FE,Line2,HEX2 hr,":",HEX2 MINs,":",HEX2 sec
        ENDIF
    ' Read temperature and send email if below/above threshold
        GOSUB Temperature
        IF (temp <= $28) THEN  ' If temp <= 40 deg F send email
           GOSUB Email
        ENDIF
        IF (temp >= $55) THEN   ' If temp >= 85 deg F send email
           GOSUB Email
        ENDIF
        ADCON1 = %00001110
    ' Write current temperature to EEPROM ....for test only..comment out
        WRITE 10, temp
    ' If low voltage exists during main loop a HLVD interrupt will take place
    ' here and LED_RED will blink 3x warning of low voltage.
        IF LowVolt = 1 THEN ' Blink LED_RED 5X to indicate Low Voltage detected
            FOR I = 0 TO 2
                HIGH LED_RED
                PAUSE 250
                LOW LED_RED
                PAUSE 250
                LowVolt=0     ' Reset LowVolt flag
            NEXT
        ENDIF
    ' Put code here for actions to take due to RTC Alarm1 interrupt
        IF logflag = 1 THEN   'RTC interrupt has occured..start ISR
            FOR I = 0 TO 1    ' Blink Green LED 2x due to ISR entry
                HIGH LED_GRN
                PAUSE 250
                LOW LED_GRN
                PAUSE 250
            NEXT
            IF J > 220 THEN  ' If logging space is full start over with logging
               J = 0     ' Reset logging index to start of logging space
                         ' Logging space covers time stamps for 220 interrupts
            ENDIF
        'Put code here to measure/log temperature from LM34 analog sensor
            GOSUB Temperature
            WRITE 16+J, hr    ' Log timestamp for temperature to EEPROM
            WRITE 17+J, MINs
            WRITE 18+J, sec
            WRITE 19+J, temp
            J = J+4
        ' Read temperature and send email if below/above threshold
          GOSUB Temperature
          IF (temp <= $28) THEN  ' If temp <= 40 deg F send emai
             GOSUB Email
          ENDIF
          IF (temp >=  $55) THEN   ' If temp >= 85 deg F send email
             GOSUB Email
          ENDIF
          ADCON1 = %00001110
        'If LCD installed reset to all digital ops before display of temperature
            IF LCD_Flag = 1 THEN
                TRISA = 0          ' Reset PortA to all outputs for LCD use
                TRISB = %00001100  ' RB2 & RB3 set as RTC Alarm1 & Alarm2 inputs
                            ' PORTB.2 is also an input from switch grounding
                ADCON0 = %00101100   ' A/D idle and disabled
                ADCON1 = %00101111   ' All Digital
                LCDOUT $fe,1 ' Clear Display
                LCDOUT $fe,Line1,"Temp:"  ' Display measured temperature
                LCDOUT $FE,Line2, DEC temp, " DEG F"
                PAUSE 1000
            ENDIF
        ' Clear RTC STATUS and set CONTROL registers for next RTC interrupt
            I2CWRITE SDA, SCL, RTCdevice, StatusReg,[$00]
            PAUSE 10
            I2CWRITE SDA, SCL, RTCdevice, ContReg,[%00000101]
            logflag = 0    ' clear flag after logging
        ENDIF
    GOTO mainloop
END
;@ EndSize(MainLoop)    ; Uncomment FOR TEST ONLY
'--------------------[ Begin Interrupt Handlers ]------------------------
Alarm:
        logflag = 1     ' Set flag to trigger logging in mainloop
    ' Resume Program where Interrupt happened
@ INT_RETURN
VoltLow:
        LowVolt = 1     ' Set low supply voltage warning flag
    ' Resume Program where Interrupt happened
@ INT_RETURN
'------------------{ End of Interrupt Handlers }-------------------------
End     ' Safety measure to insure program stops if reaches here
;@ EndSize(LOOKUP)       ; FOR TEST ONLY
;@ LibrarySize           ; FOR TEST ONLY
;@ UserSize              ; FOR TEST ONLY
;@ TotalSize             ; FOR TEST ONLY