Building an I2C monitor ... but i think it is not Slave....


Results 1 to 9 of 9

Threaded View

  1. #7
    Join Date
    Feb 2010
    Posts
    30


    Did you find this post helpful? Yes | No

    Default Re: Building an I2C monitor ... but i think it is not Slave....

    Code:
    ASM
     __CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_ON & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
     __CONFIG _CONFIG2, _WRT_OFF & _PLLEN_ON & _STVREN_OFF & _BORV_19 & _LVP_OFF
    
    ENDASM
     
      DEFINE OSC 32
      
        TX  VAR PORTA.0 
        SCL VAR PORTA.1 
        SDA VAR PORTA.2 
    
        SCLB VAR PORTAB.1
        SDAB VAR PORTAB.2
        SCLF VAR IOCAF.1
        SDAF VAR IOCAF.2
                
        BIT_COUNTER VAR BYTE
        STOP_COUNTER VAR BYTE
        BYTE_READY VAR BIT
        BYTEINT VAR BYTE
        BYTEINT2 VAR BYTE
        BYTEINB VAR BYTE
        BYTEIN VAR BYTE[30]  
        PORTAB VAR BYTE            
        BYTE_COUNTER VAR BYTE
        BYTES_RECEIVED VAR BYTE
        STOPFLAG VAR BIT
    
        OSCCON = %01110000         
        IOCAN = %00000100
        IOCAP = %00000110 
        
        ON INTERRUPT GoTo INT   ' Declare interrupt handler routine
     
     START:    
        PAUSE_1000
     
        OPTION_REG.7 = 1         ' DISABLE PORTB PULLUPS
           
        PORTA = %00000110        ' SWAP PORTA.0 /PORTA.2 FOR SMALL PCB
        TRISA = %00001110        ' SWAP PORTA.0 /PORTA.2 FOR SMALL PCB
        ANSELA = $00             ' MAKE ALL INPUTS DIGITAL I/O PINS
        WPUA = $00
       
        BIT_COUNTER = 0
        BYTE_COUNTER = 0
        
        INTCON = %10001000        ' Enable GLOBAL AND IOCHANGE interrupts  
        
    MAIN_LOOP:
          
        IF STOPFLAG = 1 THEN   
        '/ Here we have the received bytes in the array BYTEIN(x)
        ' The size of the array is in the variable BYTES_RECEIVED.
        ' You have to process them  and then ...
        BYTE_COUNTER = 0 ' in order to get the next bytes
        ENDIF
      
        GOTO MAIN_LOOP
        
        
       
    '============================================================================
    '================ BIT BANGING interrupt handler  ============================
    '============================================================================
    
    Disable                                ' Don't check for interrupts in this section
    
    INT:                                
        PORTAB = PORTA                  'Buffer the STATUS OF THE PORT
        
        '------------------------- ignore errory databits ------------------------
        'using the I2CWRITE command of PBP i got an errory SDA pulse ...
        'using the next line i ignore this error-y line ...
        'for other uses you may ommit it .
    
        IF SCLB = 0 THEN IOCAF = $00 : RESUME ' IGNORE WHEN CLK = 0 
    
        '-----------------------receive data bit -----------------------------------    
            
        if SCLF = 1 THEN                'CLK_FLAG SET : IT IS DATA !
           
        '----------------------- store byte ----------------------------------------
        
         IF BIT_COUNTER.3 = 1 THEN       ' IF WE GET 8 BITS RESET BITCOUNTER
          BIT_COUNTER.3 = 0              
       
          BYTEIN(BYTE_COUNTER) = BYTEINT ' AND STORE THE BYTE
          BYTE_COUNTER = BYTE_COUNTER + 1
       
          IOCAF.1 = 0                     ' CLEAR INT ON CHANGE FLAGS
          RESUME 
         ENDIF  
         
        ' ------------------------- receive bit ------------------------------------     
        
         BYTEINT = (BYTEINT << 1)                
         BYTEINT.0 = PORTAB.2
         BIT_COUNTER = BIT_COUNTER + 1 
             
         IOCAF.1 = 0                     ' CLEAR INT ON CHANGE FLAGS
         RESUME
        ENDIF  
      
        ' --------------------start bit-----------------------------------------------
        IF SDAB = 0 THEN 
          BIT_COUNTER = 0 
          IOCAF.2 = 0                     ' CLEAR INT ON CHANGE FLAGS
          RESUME
         ENDIF
        '---------------------stop bit ----------------------------------------------
                
          STOPFLAG = 1 
          BYTES_RECEIVED =  BYTE_COUNTER
          'BYTE_COUNTER = 0               'nevermind BYTECOUNTER WILL RESET AT THE NEXT START BIT ANYWAY
          IOCAF.2 = 0                     ' CLEAR INT ON CHANGE FLAGS
          RESUME  
    ENABLE
    
    End
    Last edited by mackrackit; - 14th March 2012 at 05:07.

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