I2C with 16F628A mystery


Closed Thread
Results 1 to 18 of 18

Hybrid View

  1. #1
    Join Date
    May 2009
    Location
    Montreal, QC, Canada
    Posts
    118


    Did you find this post helpful? Yes | No

    Default

    The chip is enclosed into a sealed device and the only thing exposed are the 4 pads for power and I2C. With trial and error... and trial and error over trial and error I managed to get what I need working. I know my program doesn't have much style but for now it does what it is supposed to do.

    The process of my program is as follow:
    When I press the switch I need to read address $04 and if it is > 5 it will flash the OK LED the number of the value found and then quit. If it is not greater than 5 then I want to set values $01,$01,$01,$3c,$3c,$3c,$3c,$3c,$3c,$08,$08,$08,$2 1,$21,$21,$FF all over.
    I don't understand why I just write once and the chip gets updated all over but it works.

    This device is hooked up to another device that will update the chip daily by setting address $04, $05, $06 to -1 each day.

    I am definitely going to order some chips and test on breadboard until I get a good understanding of this.
    In the mean time this is my working code:
    Code:
    @ __config _INTOSC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_ON & _LVP_OFF & _CP_OFF
    clear
    DEFINE OSC 4
    TRISB = %00001000           
    CMCON = 7                   ; Disable comparator
    OPTION_REG = 0
    
    
    SWSTAT      var byte
    Dat         VAR byte
    I           var byte
    I2CAddr     Var word
    I2CDevice   var byte
    D1          var PORTA.2     ; Cartrige white wire
    D2          VAR PORTA.3     ; Cartrage blue wire
    Activate    var PORTB.2     ; select between reset=1 normal=0
    GO          VAR PORTB.3     ; Start switch
    OK          VAR PORTB.4     ; Green LED
    NOTOK       var PORTB.5     ; Red LED
    
    activate = 0                ; Normal operation
    ok = 0                      ; Green LED off
    notok = 0                   ; Red LED off
    
    goto swstatus
    
    Start:
    
    ;--------make sure reset is needed-----------
    activate = 1                ; get ready to reset
    swstat = 0                  ; reset switch debounce
    pause 50                    ; give time or relay contact
    
    i2cdevice = $A1
    i2caddr = 3
    i2cread d1,d2,i2cdevice,i2caddr.lowbyte,[dat]
    pause 10
    
    if dat > 5 then
        for I = 1 to dat
            ok=1: pause 500:ok = 0:pause 500
        next
        activate = 0                    ;------Resume normal operation
        goto SwStatus
    else
    
        ok = 1: pause 3000: ok = 0  ; Green LED on for 3 seconds
        pause 5000
        I2CAddr = $0
        I2CDevice = $A0
    
        i2cwrite d1,d2,i2cdevice,i2caddr.lowbyte,[$01]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr.lowbyte,[$01]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr.lowbyte,[$01]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr.lowbyte,[$3c]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr.lowbyte,[$3c]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr.lowbyte,[$3c]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr.lowbyte,[$3c]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr.lowbyte,[$3c]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr.lowbyte,[$3c]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr.lowbyte,[$08]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr.lowbyte,[$08]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr.lowbyte,[$08]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr.lowbyte,[$21]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr.lowbyte,[$21]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr.lowbyte,[$21]:i2caddr = i2caddr + 1:pause 10
        i2cwrite d1,d2,i2cdevice,i2caddr.lowbyte,[$ff]:pause 10
    
    ;---------------------make sure data was written-------------------------
        i2cdevice = $A1
        i2caddr = 4
        i2cread d1,d2,i2cdevice,i2caddr.lowbyte,[dat]
        pause 10
        activate = 0                ;------Resume normal operation
        if dat = $3c then           ;------all is good, flash green LED
            for I = 1 to 10
                toggle ok
            pause 500
            next
            ok = 0
        else                        ;------Something wrong, flash red LED
            for I = 1 to 10
                toggle notok
                pause 500   
            next
            notok = 0
        endif
    endif
    
    SwStatus:
    if go = 0 then debounce
    swstat = 0
    goto SWstatus
    
    debounce:
    swstat = swstat + 1
    pause 10
    if swstat = 150 then start
    goto swstatus
    
    END
    Thank you for your help.

    Mike

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    May 2009
    Location
    Montreal, QC, Canada
    Posts
    118


    Did you find this post helpful? Yes | No

    Default

    Hi Dave,

    I went a little too fast in my project, when I thought my circuit was good I built the PCB and dismantled my breadboard and its only after I saw this.

    But thanks for the info

    Mike

Members who have read this thread : 0

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

Posting Permissions

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