Skip to content

hqnicolas/CH341A-I2C

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 

Repository files navigation

CH341A I2C Programming Guide

ch341

"CH341A MiniProgrammer"

  • A practical guide to using the CH341A MiniProgrammer for I2C communication, with signal behavior analysis.

Setup Requirements

CH341PAR.ZIP (Mirror)

Files Needed

  • CH341DLL.H (Header file)
  • CH341DLL.LIB (Linker library)
  • CH341DLL.DLL (Runtime DLL)

Initialization Code

#include "CH341DLL.H"

int main() {
  // Open the first CH341 device
  if ((int)CH341OpenDevice(0) < 0) {
    // Handle error
  }

  // Optional: Set I2C speed (0=20kHz, 1=100kHz, 2=400kHz, 3=750kHz)
  CH341SetStream(0, 1); 

  // ... Perform I2C operations ...

  CH341CloseDevice(0); // Cleanup
  return 0;
}

I2C Functions & Signal Behavior

1. CH341WriteI2C

Usage: Writes 1 byte to a device register.
Example:

UCHAR deviceAddr = 0x86 >> 1; // Right-shift 7-bit address
UCHAR regAddr = 0x22;
UCHAR data = 0x13;
CH341WriteI2C(0, deviceAddr, regAddr, data);

Bus Sequence:

  1. Start condition
  2. Write device address (LSB=0 for write)
  3. Write register address byte (0x22)
  4. Write data byte (0x13)
  5. Stop condition

I2C Write Command


2. CH341ReadI2C

Usage: Reads 1 byte from a device register.
Example:

UCHAR deviceAddr = 0x86 >> 1; // Right-shift 7-bit address
UCHAR regAddr = 0xD4;
UCHAR receivedData;
CH341ReadI2C(0, deviceAddr, regAddr, &receivedData);

Bus Sequence:

  1. Start condition
  2. Write device address (LSB=0)
  3. Write register address byte (0xD4)
  4. Repeated start
  5. Write device address (LSB=1 for read)
  6. Read data byte
  7. Stop condition

I2C Read Command


3. CH341StreamI2C

Usage: Flexible multi-byte read/write operations.

Write-Only Mode

UCHAR writeBuffer[] = {0x86, 0x22, 0x13}; // Device address + data
CH341StreamI2C(0, 3, writeBuffer, 0, NULL);

Bus Sequence:

  1. Start condition
  2. Write all bytes sequentially (0x86, 0x22, 0x13)
  3. Stop condition

I2C Stream Write


Read-Only Mode (Not Recommended)

UCHAR readBuffer[2];
CH341StreamI2C(0, 0, NULL, 2, readBuffer); // No address sent!

Bus Sequence:

  1. Start condition
  2. Clock pulses (no address/data)
  3. Stop condition

I2C Stream Read


Write-Then-Read Mode

UCHAR writeBuffer[] = {0x86}; // Device address (write mode)
UCHAR readBuffer[2];
CH341StreamI2C(0, 1, writeBuffer, 2, readBuffer);

Bus Sequence:

  1. Start condition
  2. Write address byte (0x86 with LSB=0)
  3. Repeated start
  4. Write address byte (0x87 with LSB=1)
  5. Read 2 bytes
  6. Stop condition

I2C Stream Write Then Read


Register Read Example

UCHAR writeBuffer[] = {0x86, 0xD4}; // Device address + register
UCHAR readBuffer[1];
CH341StreamI2C(0, 2, writeBuffer, 1, readBuffer);

Bus Sequence:

  1. Start condition
  2. Write address (0x86) + register (0xD4)
  3. Repeated start
  4. Write address (0x87 with LSB=1)
  5. Read 1 byte
  6. Stop condition

Key Notes

  1. Address Handling: Always right-shift 7-bit addresses (no LSB for R/W)
  2. Clock Speeds: Use CH341SetStream() for 20/100/400/750kHz
  3. DLL Dependency: Must be in executable directory or System32
  4. Signal Verification: Use logic analyzer to validate complex transactions

About

CH341A I2C Programming (Windows API)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published