⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 Bluetooth CSR I2C操作示例代码
💻 C
字号:
/****************************************************************************
FILE
    I2C_test.c - source file for I-squared C Test code
 
CONTAINS
    main - the entry point
 
DESCRIPTION
    All the code here is written to read from a ISC device assuming
    that device is a slave.  It is initially assumed that the device
    in question is a PCF8574A
*/

#include <stdio.h>

#include <old/i2c.h>
#include <old/i2ciox.h>
#include <old/i2crom.h>

#include <vm.h>

/* Choose what to test */

static int TEST_I2CIOX  = 1;
static int TEST_EEPROM  = 0;
static int TEST_PCF8574 = 0;

/*
    This code tries to read and write a few bytes from an external
    serial eeprom connected to the I2C bus.
*/

static void eeprom_test(void)
{
    int i;

    for(i=0; i<8; i++)
    {
        (void) I2cromWrite(i*4, 0x55aa);
        (void) I2cromWrite(i*4+2, i + 0xff00);
    }
    
    for(i=0; i<8*2; i++)
    {
        uint16 data = I2CromRead(i*2);
        printf("%4.4X ", data);
    }
    
    printf("\n");
    
    for(i=0; i<16; i++)
    {
        (void) I2cioxWrite(I2CromRead(i*2));
    }
}

/*
    This fully exercises a sixteen bit port expnder connected to the
    the I2C bus (a PCF8575).  The code to do this is in the IOX lib.
*/

static void ping_pong(void)
{
    uint16 m = 1;
    uint16 n = 1;
    for(;;)
    {
        int i;
        for(i=0; i<7; i++)
        {
            (void) I2cioxWrite(m|n);
            VmWait(40);
            if((m <<= 1) == 0)
                m = 1;
            if((n >>= 1) == 0)
                n = 0x8000;

            (void) I2cioxWrite(m|n);
            VmWait(40);
            if((n >>= 1) == 0)
                n = 0x8000;
        }
    }
}

/*
    These are a few test routines for a PCF8574 connected to the BC01.
    The PCF8574 is an eight bit port expander offering eight
    pseydo-bidirectional IO pins.  This means that a pin must be
    written as a one, before it can be read.  The 'dir' mask is used
    for this purpose, setting a bit high make it an input
*/

static const uint8 dir = 0xf0;

static uint8 PCF8574_read(uint8 a)
{
    uint8 val;
    I2cStart();
    if(!I2cWriteByte(0x71 | ((a & 0x7) << 1)))
    {
        I2cStop();
        printf("Failed to write address (1)!\n");
        return 0x100;
    }
    val = I2cReadByte();
    I2cWriteAck(0);
    I2cStop();
    return val & dir;
}

static void PCF8574_write(uint8 a, uint8 v)
{
    I2cStart();
    if(!I2cWriteByte(0x70 | ((a & 0x7) << 1)))
    {
        I2cStop();
        printf("Failed to write address (2)!\n");
        return;
    }
    if(!I2cWriteByte(v | dir))
    {
        I2cStop();
        printf("Failed to write value!\n");
        return;
    }
    I2cStop();
}

int main(void)
{
    printf("started i2c test...\n");

    I2cInit() ;

    for(;;)
    {
        if(TEST_EEPROM)
            eeprom_test();
        if(TEST_I2CIOX)
            ping_pong();
        if(TEST_PCF8574)
        {
            uint8 t;
            uint16 i;

            for(i=0; i<10000; i++)
            {
                t = PCF8574_read(7);
                PCF8574_write(7, t>>4);
            }
        }
    }
    /*lint -unreachable */
    return 0; 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -