Problem with I2C


Results 1 to 10 of 10

Threaded View

  1. #8
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959

    Default Ds1621

    I got a good laugh out of this one.
    When the code doesn't work, rewrite PBP's I2C commands ...

    Try this ...
    Code:
    DEFINE OSC 48
    CLEAR
    
    #config
        __CONFIG    _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
        __CONFIG    _CONFIG1H, _FOSC_HSPLL_HS_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
        __CONFIG    _CONFIG2L, _PWRT_OFF_2L & _BOR_ON_2L & _BORV_3_2L & _VREGEN_ON_2L
        __CONFIG    _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H
        __CONFIG    _CONFIG3H, _CCP2MX_ON_3H & _PBADEN_OFF_3H & _LPT1OSC_OFF_3H & _MCLRE_OFF_3H
        __CONFIG    _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _XINST_OFF_4L
    #endconfig
    
    ;----[LCD definitions]--------------------------------------------------------
    DEFINE LCD_DREG  PORTB                            ; LCD Data port
    DEFINE LCD_DBIT  4                                ; starting Data bit (0 or 4)
    DEFINE LCD_EREG  PORTB                            ; LCD Enable port
    DEFINE LCD_EBIT  2                                ;     Enable bit
    DEFINE LCD_RSREG PORTB                            ; LCD Register Select port
    DEFINE LCD_RSBIT 1                                ;     Register Select bit
    DEFINE LCD_BITS  4                                ; LCD bus size (4 or 8 bits)
    DEFINE LCD_LINES 2                                ; number of lines on LCD
    DEFINE LCD_COMMANDUS 2000                         ; Command delay time in us 
    DEFINE LCD_DATAUS 50                              ; Data delay time in us 
    
    SCL VAR PORTA.2                                   ; I2C Clock
    SDA VAR PORTA.3                                   ; I2C Data
    
    TempHigh    CON 28                                ; DS1621 High Temp Limit
    TempLow     CON 24                                ; DS1621 Low Temp Limit
    DS1621      CON $90                               ; DS1621 Slave Address
    ConfigReg   VAR BYTE                              
    TempC       VAR WORD
    
    ;----[Initialization]-----------------------------------------------------------
    CMCON = 7
    ADCON1 = 15
    
    I2CREAD SDA,SCL,DS1621,$AC,[ConfigReg]            ; Read the configuration
    IF ConfigReg.0 = 1 THEN                           ; if in Polling Mode?
        I2CWRITE SDA,SCL,DS1621,$AC,[0]               ; Set it to Continuous Mode
        GOSUB WaitForEE
    ENDIF
    
    I2CREAD SDA,SCL,DS1621,$A1,[TempC.LowByte]        ; Read the High Temp Limit
    IF TempC != TempHigh THEN                         ; if High limit is incorrect
        I2CWRITE SDA,SCL,DS1621,$A1,[TempHigh]        ;  Write High Limit
        GOSUB WaitForEE
    ENDIF
    
    I2CREAD SDA,SCL,DS1621,$A2,[TempC.LowByte]        ; Read the Low Temp Limit
    IF TempC != TempLow THEN                          ; if Low limit is incorrect
        I2CWRITE SDA,SCL,DS1621,$A2,[TempLow]         ;  Write Low Limit
        GOSUB WaitForEE
    ENDIF
    
    I2CWRITE SDA,SCL,DS1621,$EE,[0]                   ; Start Conversions
    
    ;----[Main Program Loop]--------------------------------------------------------
    Main:
        PAUSE 900
        I2CREAD SDA,SCL,DS1621,$AC,[ConfigReg]        ; Read the configuration
        LCDOUT $FE,$80,"CONFIG=",IBIN8 ConfigReg
                                                      ; Read the Temperature
        I2CREAD SDA,SCL,DS1621,$AA,[TempC.HighByte, TempC.LowByte]
        LCDOUT $FE,$C0,"  Temp="
        GOSUB ShowTemp
    
        I2CREAD SDA,SCL,DS1621,$A1,[TempC.HighByte, TempC.LowByte]
        LCDOUT $FE,$94,"    TH="
        GOSUB ShowTemp
        
        I2CREAD SDA,SCL,DS1621,$A2,[TempC.HighByte, TempC.LowByte]
        LCDOUT $FE,$D4,"    TL="
        GOSUB ShowTemp
        
    GOTO Main
    
    ;----[Show Temp on LCD]----------------------------------------------------------
    ShowTemp:
        LCDOUT DEC TempC.HighByte
        IF TempC.7 THEN
            LCDOUT ".5  "
        ELSE
            LCDOUT ".0  "
        ENDIF
    RETURN
    
    ;----[Wait for DS1621 EEPROM Write]----------------------------------------------
    WaitForEE:
        REPEAT                                        ; Wait for EEPROM write
            I2CREAD SDA,SCL,DS1621,$AC,[ConfigReg]
            LCDOUT $FE,$80,"CONFIG=",IBIN8 ConfigReg
        UNTIL ConfigReg.4 = 0
    RETURN
    Attached Files Attached Files
    Last edited by Darrel Taylor; - 6th May 2012 at 20:03.
    DT

Members who have read this thread : 0

You do not have permission to view the list of names.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts