Menu ▾ ▴

24FC256 I2C running at 1 Mhz?

Jberg
2 days ago
14 hours ago
  • Jberg

    Jberg - 2 days ago

    Hello,

    Can this work, a 24FC256 EEPROM + PIC18F14K22 at 64 Mhz
    an the i2c set at 1 Mhz to read EEPROM records ?

    Here's what AI replied for the I2C setup.

    ' Configure High-Speed Hardware I2C Master Mode
    ' For PIC18F14K22: SCL is Pin 11 (RB6), SDA is Pin 10 (RB4)

    ' #define HI2C_BAUD_RATE 1000 ' Set I2C bus speed to Ultra-Fast Mode (1 MHz / 1000 kHz)
    ' #define HI2C_DATA PORTB.4
    ' #define HI2C_CLOCK PORTB.6
    HI2CMode Master

    Thanks for any advices

     
  • Anobium

    Anobium - 2 days ago

    I would look at the demos to check.

     
  • Jberg

    Jberg - 2 days ago

    Basicaly the project use Holtek HT9032D FSK, a 24FC256 EEPROM + PIC18F14K22 at 64 Mhz
    if the make sense then I'll setup the parts on PCB, and start experimenting

    AI suggestion: (GCBASIC Code with 24FC256 1 MHz I2C Integration)

    '---------------------------------------------------------------------
    ' GCBASIC Caller ID Whitelist via High-Speed 24FC256 EEPROM (PIC18F14K22)
    '---------------------------------------------------------------------
    #chip 18F14K22, 64        ' Run PIC at 64 MHz max internal clock
    #config FamilyOsc = IRC   ' Set internal oscillator block with 4x PLL
    
    ' Define Hardware Pins for 20-Pin Package
    #define HT_DOUT PORTB.5   ' Hardware UART RX Pin from HT9032D DOUT (Pin 13)
    #define RELAY_PIN PORTC.0 ' Output pin to trigger relay/gate lock (Pin 16)
    
    Dir HT_DOUT In
    Dir RELAY_PIN Out
    RELAY_PIN = 0             ' Default off
    
    ' Configure Hardware UART1 for HT9032D (1200 Baud, 8-N-1)
    #define USART_BAUD_RATE 1200
    #define USART_TX_BLOCKING
    #define USART_DELAY 0
    
    ' Configure High-Speed Hardware I2C Master Mode 
    ' For PIC18F14K22: SCL is Pin 11 (RB6), SDA is Pin 10 (RB4)
    #define HI2C_BAUD_RATE 1000 ' Set I2C bus speed to Ultra-Fast Mode (1 MHz / 1000 kHz)
    #define HI2C_DATA PORTB.4
    #define HI2C_CLOCK PORTB.6
    HI2CMode Master
    
    ' 24FC256 Hardware I2C Target Address (Assuming A0, A1, A2 pins are grounded)
    #define EEPROM_ADDRESS 160 ' 0xA0 in decimal representation
    
    ' Memory Sizing Limits
    #define TOTAL_RECORDS 100  ' Can expand up to 2978 records on a 24FC256
    #define RECORD_SIZE 11
    
    ' Global Buffers and Counters
    Dim IncomingBuffer As String * 11
    Dim CurrentByte As Byte
    Dim DigitCount As Byte
    DigitCount = 0
    
    ' Main Program Loop
    Do
      ' Check if hardware UART has received a byte from the HT9032D
      If USARTHasData Then
        HSerReceive CurrentByte
    
        ' Extract numerical phone digits ('0' to '9')
        If CurrentByte >= 48 Then
          If CurrentByte <= 57 Then
            If DigitCount < RECORD_SIZE Then
              Mid(IncomingBuffer, DigitCount + 1, 1) = Chr(CurrentByte)
              DigitCount = DigitCount + 1
            End If
          End If
        Else
          ' End of FSK packet detected (Non-numeric delimiter byte)
          If DigitCount >= 10 Then
    
            ' Perform the high-speed external I2C search loop
            If SearchWhitelist(IncomingBuffer) Then
              RELAY_PIN = 1   ' ACCESS GRANTED
              Wait 3 s
              RELAY_PIN = 0
            Else
              RELAY_PIN = 0   ' ACCESS DENIED
            End If
    
            ' Reset memory registers for next call
            IncomingBuffer = ""
            DigitCount = 0
          End If
        End If
      End If
    Loop
    
    '---------------------------------------------------------------------
    ' High-Level Function: Read an 11-Byte Record from the 24FC256 at 1 MHz
    '---------------------------------------------------------------------
    Sub GetStoredNumber(In RecordIndex As Word, Out NumberOut As String)
      Dim StartByteOffset As Word
      Dim AddressHigh As Byte
      Dim AddressLow As Byte
      Dim CharPosition As Byte
      Dim DataByte As Byte
    
      ' Calculate 16-bit address offsets cleanly without bitwise operators
      StartByteOffset = (RecordIndex - 1) * RECORD_SIZE
    
      ' Separate the 16-bit word address into high and low bytes natively using GCBASIC system helpers
      AddressHigh = StartByteOffset_H
      AddressLow = StartByteOffset_L
    
      NumberOut = "           " ' Clear buffer space
    
      ' Execute an I2C random read burst block sequentially
      HI2CStart
      HI2CSend EEPROM_ADDRESS
      HI2CSend AddressHigh
      HI2CSend AddressLow
    
      HI2CReStart
      HI2CSend EEPROM_ADDRESS + 1 ' Read Mode Command Flag
    
      For CharPosition = 1 To RECORD_SIZE
        ' Fetch byte and handle I2C Acknowledgement natively
        If CharPosition = RECORD_SIZE Then
          HI2CReceive DataByte, False ' Send NACK on final byte to close stream
        Else
          HI2CReceive DataByte, True  ' Send ACK to continue reading sequential bytes
        End If
        Mid(NumberOut, CharPosition, 1) = Chr(DataByte)
      Next
      HI2CStop
    End Sub
    
    '---------------------------------------------------------------------
    ' High-Level Function: Add / Append a New Phone Number into the 24FC256
    '---------------------------------------------------------------------
    Sub AddPhoneNumber(In NewNumber As String)
      Dim SearchIndex As Word
      Dim TargetIndex As Word
      Dim CheckStr As String * 11
      Dim WriteOffset As Word
      Dim AddressHigh As Byte
      Dim AddressLow As Byte
      Dim CharIndex As Byte
      Dim AsciiVal As Byte
    
      TargetIndex = 0
    
      ' 1. Find the first empty memory cell block
      For SearchIndex = 1 To TOTAL_RECORDS
        GetStoredNumber(SearchIndex, CheckStr)
    
        ' Fresh or erased external EEPROM memory addresses return 255
        If Asc(CheckStr) = 255 Then
          TargetIndex = SearchIndex
          Exit For
        End If
    
        ' Duplicate checking parameter
        If CheckStr = NewNumber Then
          Exit Sub
        End If
      Next
    
      ' 2. If a vacant slot exists, execute an optimized Page Write sequence
      If TargetIndex > 0 Then
        WriteOffset = (TargetIndex - 1) * RECORD_SIZE
        AddressHigh = WriteOffset_H
        AddressLow = WriteOffset_L
    
        HI2CStart
        HI2CSend EEPROM_ADDRESS
        HI2CSend AddressHigh
        HI2CSend AddressLow
    
        ' Stream the 11 characters directly into the 24FC256 internal page buffer
        For CharIndex = 1 To RECORD_SIZE
          AsciiVal = Asc(Mid(NewNumber, CharIndex, 1))
          HI2CSend AsciiVal
        Next
        HI2CStop
    
        ' The 24FC256 takes up to 5ms to process a page write internally. 
        ' This delay ensures the chip is completely done writing.
        Wait 5 ms 
      End If
    End Sub
    
    '---------------------------------------------------------------------
    ' High-Level Search Engine
    '---------------------------------------------------------------------
    Function SearchWhitelist(TargetNumber As String) As Bit
      Dim RecordIndex As Word
      Dim StoredNumber As String * 11
    
      SearchWhitelist = 0 ' Default fail condition
    
      For RecordIndex = 1 To TOTAL_RECORDS
        GetStoredNumber(RecordIndex, StoredNumber)
    
        If StoredNumber = TargetNumber Then
          SearchWhitelist = 1 ' Match detected
          Exit Function
        End If
      Next
    End Function
    
     
  • Anobium

    Anobium - 2 days ago

    It will not compile but give it a go. When you get an error.. and you will, find the line and press F1, then, do a search to help to resolve the error.

    I would instrument the code to send serial out to a terminal to see what is happening.

     
  • Jberg

    Jberg - 2 days ago

    I'll give it a try.
    Thanks, for the inside.

     
  • Jerry Messina

    Jerry Messina - 14 hours ago

    You'll likely have a hard time getting the I2C hardware in the K22 to actually provide 1MHz operation, especially if using resistor pullups... the risetimes are just too slow.

    You could try using active rise-time accelerators to speed things up, but even with those I could never get much more than 800KHz to work reliably with the K22. The MSSP samples the signals too quickly and generates false bus collisions.

     
  • Anobium

    Anobium - 14 hours ago

    The AI may be incorrect with the operating frequency of the bus. So, proove the circuitry first at a lower frequecy.

     

Log in to post a comment.