Quote Originally Posted by jellis00 View Post
I will try to post the code in another posting.
Code:
  
Pause 10     ' This statement is placed at beginning of code for ICD use

'Configuration Fuses
    ASM ; 18F2550/4550, 8mhz crystal
       __CONFIG   _CONFIG1L, _PLLDIV_2_1L & _CPUDIV_OSC4_PLL6_1L & _USBDIV_2_1L
       __CONFIG   _CONFIG1H, _FOSC_HSPLL_HS_1H
       __CONFIG   _CONFIG2L, _PWRT_ON_2L & _BOR_OFF_2L & _VREGEN_ON_2L
       __CONFIG   _CONFIG2H, _WDT_OFF_2H & _WDTPS_512_2H
       __CONFIG   _CONFIG3H, _PBADEN_OFF_3H ; PortB resets as digital
       __CONFIG   _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L
    ENDASM

' Define Hardware / Set Registers     
    DEFINE OSC 16
    DEFINE I2C_SLOW 1   ' Set i2c to the standard speed
    DEFINE I2C_HOLD 1   ' Enable recieving i2c device to pause communication
    Include "Modedefs.Bas"  
    INCLUDE "ALLDIGITAL.pbp"  
    INCLUDE "DT_INTS-18.bas"    
    INCLUDE "ReEnterPBP-18.bas"

    TRISA = 0           ' PortA output connections used for LCD interface
    TRISB = %00001100   ' RB2 & RB3 set as RTC Alarm1 & Alarm2 inputs 
                        ' PORTB.2 is an input from switch grounding
                        ' PORTB.4 is used during test as a Green LED heartbeat
                        ' and as a USB connected indicator during ops 
    TRISC.1 = 0         ' PORTC.1 used during test as a RED LED output
    TRISC.2 = 0         ' PortC.2 is used for the LCD R/W connection
    
'Declare Variables, Aliases & Constants 
    day_cnt  VAR BYTE      ' Counter for day number during monthly epoch
    ext_pwr  VAR PORTC.0   ' Alias to control power to Ultrasonic sensors
    i        VAR Byte      ' Index in FOR-NEXT loops
    J        VAR BYTE      ' Index to EEPROM address for avg. range store
    LED_RED  VAr PortC.1   ' Red LED used to indicate battery is low
    LED_GRN  VAR PortB.4   ' Green LED used to indicate USB connected
    rng0     VAR w0.Byte0  ' LSB of range measurement when right justified
    rng1     VAR w0.byte1  ' MSB of range measurement when right justified 
    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 ops
    w0       var word      ' W0 is the word value of the range data 
               
' Constants
    RTCdevice CON $D0  ' Device write address = %11010000 for RTC
    srfdevice CON $E0  ' Device address for SRF02 ultrasonic RangeFinder
    Vthr      CON 46   ' Threshold (3.3v) triggers battery low voltage
                    
'SETUP FOR USING DS1337 Real Time Clock
         
'Initialize LEDs to off
    LOW LED_GRN
    LOW LED_RED
GOSUB InitializeDisplay  ' Initialize LCD before starting mainloop
GOSUB SetTimeAndDate     ' Set RTC time/date before starting mainloop  
        
' START SUBROUTINES
GOTO JumpPoint ' Jump over all subroutines

InitializeDisplay:
     ' Blink LED_GRN twice to indicate entered IntializeDisplay
           For i = 0 to 1
               HIGH LED_GRN
               Pause 500
               LOW LED_GRN
               PAUSE 500
           Next
     ' Saving characters by eliminating in post
Return

SetTimeAndDate:  ' Subroutine to set current time, date and alarm  

    ' Blink LED_GRN 3X times to indicate entered SetTimeAndDate
           For i = 0 to 2
               HIGH LED_GRN
               Pause 500
               LOW LED_GRN
               PAUSE 500
           Next
      ' Saving characters by eliminating in post 
Return
'  END OF SUBROUTINES 
            
JumpPoint:

'Initialize epoch indexes on powerup/reset
    day_cnt = 0
    WRITE 9, day_cnt     ' Store new epoch initialized day_count
    Pause 20
    J = 0                ' Initialize index at EEPROM start address 
    WRITE 10,J           ' Store new epoch initialized J index
    Pause 20

' SETUP FOR INTERRUPTS  
ASM
INT_LIST  macro    ; IntSource,         Label,  Type, ResetFlag?
        ;INT_Handler   USB_Handler
        INT_Handler   INT2_INT,        _Range,   PBP,  yes
    endm
    INT_CREATE                ; Creates the interrupt processor
ENDASM

' Per DT, DT_INTS already takes care of setting INTCON and RCON registers
' but this doesn't work unles INTCON2 set per below:
    INTCON2 = %10000000  ' Set INT2 for falling edge (Bit4-low)
                         ' on RTC's interrupt.
                         ' Disable all PortB pull-ups (Bit7-high)
    RTC_INT_FLG  VAR INTCON3.1 'INT2 interrupt flag from RTC..DT_INTS handles

@    INT_ENABLE   INT2_INT     ; enable external (INT) interrupts
LOW ext_pwr  ' Turn off power to SRF02 until I2C bus is needed again        

' Begin Main Program Loop
mainloop:
    PAUSE 100 ' Without this Pause, there's nothing to keep the WDT clear.
              ' HIGH, LOW & GOTO don't generate CLRWDT instuctions.
    HIGH LED_GRN     ' Blink LED_GRN during main loop
    
    ' Place code here to put MCU and Ultrasonic Ranger in SLEEP mode.
    ' while waiting for clock interrupt to wakeup from SLEEP mode.
    
    ' Awaiting interrupt from RTC
    
    PAUSE 1000
    LOW LED_GRN  ' Turn off LED_GRN as Heartbeat during mainloop
    Pause 900

Goto mainloop   ' Do it forever     

' Begin Interrupt Handler 
Range:
     ' Saving characters by eliminating in post
    ' Blink LED_RED 3X to indicate entered Range ISR
           For i = 0 to 2
               HIGH LED_RED
               Pause 500
               LOW LED_RED
               PAUSE 500
           Next
@ INT_RETURN

'End of Interrupt Handler