I2C读写巨慢, 即使在400kbit/s下, 读写一个字节数据也要花费20多us, 这太慢了, 每读写一次设备的寄存器数据, 还要设备地址和寄存器地址, 又加了两个字节数据, 我就读了个传感器的两个字节数据而已, 动辄还要花费100us的阻塞时间, 这太浪费资源了
针对这个问题, 我利用硬件I2C及中断配合, 实现了这个没有任何阻塞MCU来读写I2C设备的方法, 效率大大提升
单片机采用stc32g8k64, 主频为40Mhz, 读写LDC1612电感涡流传感器为例来测试异步读写I2C数据
- /**
- * stc32g8k64 硬件i2c 实现 无阻塞异步执行
- * ldc1612 i2c异步读写测试
- */
- #include <STC32G.h>
- #include <stdio.h>
- #define FOSC 40000000UL // 主频40M
-
- void Delay1ms(void) //@40.000MHz
- {
- unsigned char data i, j;
-
- i = 39;
- j = 230;
- do
- {
- while (--j)
- ;
- } while (--i);
- }
- void delay(uint16 ms)
- {
- while (ms--)
- {
- Delay1ms();
- }
- }
-
- // ---------------------- uart1打印测试用 开始----------------------------
- bit busy;
- char wptr;
- char rptr;
- char buffer[16];
-
- void Uart1_Isr(void) interrupt 4
- {
- if (TI)
- {
- TI = 0;
- busy = 0;
- }
- if (RI)
- {
- RI = 0;
- buffer[wptr++] = SBUF;
- wptr &= 0x0f;
- }
- }
- void Uart1_Init(void) // 1000000bps@40.000MHz
- {
- SCON = 0x50; // 8位数据,可变波特率
- AUXR |= 0x40; // 定时器时钟1T模式
- AUXR &= 0xFE; // 串口1选择定时器1为波特率发生器
- TMOD &= 0x0F; // 设置定时器模式
- TL1 = 0xF6; // 设置定时初始值
- TH1 = 0xFF; // 设置定时初始值
- ET1 = 0; // 禁止定时器中断
- TR1 = 1; // 定时器1开始计时
- ES = 1; // 使能串口1中断
- }
- void UartSend(char dat)
- {
- while (busy)
- ;
- busy = 1;
- SBUF = dat;
- }
- void UartSendStr(char *p)
- {
- while (*p)
- {
- UartSend(*p++);
- }
- }
-
- char sendNumberCharArr[20];
- // 实现一个uint32转char的方法
- void numberToChar(uint32 number, char *buffer)
- {
- char temp[12]; // Maximum digits for uint32 + null terminator
- int i = 0;
- int j = 0;
-
- // Handle zero case
- if (number == 0)
- {
- buffer[0] = '0';
- buffer[1] = '\0';
- return;
- }
-
- // Extract digits in reverse order
- while (number > 0)
- {
- temp[i++] = (number % 10) + '0';
- number /= 10;
- }
-
- // Reverse the string to correct order
- while (i > 0)
- {
- buffer[j++] = temp[--i];
- }
- buffer[j] = '\0'; // Null terminate
- }
- // ---------------------- uart1打印测试用 结束----------------------------
-
- //------------------------------- I2C异步执行栈 开始 -----------------------------
- sbit ET4 = IE2 ^ 6;
- typedef void (*AsyncFunc)(void);
- AsyncFunc AsyncFuncNext_callback = NULL;
- // 延时执行下一个任务
- void AsyncFuncNext(AsyncFunc callback, uint16 usDelay)
- {
- uint16 totalTime = 0xffff - usDelay;
- T4T3M &= ~0x80;
- ET4 = 0;
- T4L = totalTime & 0xff; // 设置定时初始值
- T4H = totalTime >> 8; // 设置定时初始值
- AsyncFuncNext_callback = callback;
- T4T3M |= 0x80; // 定时器4开始计时
- ET4 = 1; // 使能中断
- }
- // 定时器用于异步执行
- void Timer4_Init(void) //@40.000MHz
- {
- // 1us计时一个数字
- TM4PS = 0x27; // 设置定时器时钟预分频
- T4T3M |= 0x20; // 定时器时钟1T模式
- T4L = 0x00; // 设置定时初始值
- T4H = 0x00; // 设置定时初始值
- T4T3M &= ~0x80; // 定时器4停止计时
- ET4 = 0;
- }
- // 定时器4用作异步执行定时器, 延时触发下一步的执行函数
- void Timer4_async_next_isr() interrupt 20
- {
- T4T3M &= ~0x80;
- ET4 = 0;
- if (AsyncFuncNext_callback != NULL)
- {
- AsyncFuncNext_callback();
- }
- }
- AsyncFunc I2C_Isr_callback = NULL; // I2C中断回调函数
- void I2C_Isr() interrupt 24 // I2C中断
- {
- if (I2CMSST & 0x40)
- {
- I2CMSST &= ~0x40; // 清中断标志
- if (I2C_Isr_callback != NULL)
- {
- I2C_Isr_callback();
- }
- }
- }
- //------------------------------- 异步执行栈 结束 -----------------------------
-
- //------------------------------- I2C寄存器操作 开始 --------------------------
- void Wait_I2C()
- {
- while (!(I2CMSST & 0x40))
- ;
- I2CMSST &= ~0x40;
- }
-
- uint8 i2c_readRegister16Async_addr = 0; // readRegister16Async 地址参数
- uint8 i2c_readRegister16Async_reg = 0; // readRegister16Async 寄存器参数
- uint8 i2c_readRegister16Async_step = 0; // readRegister16Async 异步执行步骤
- uint16 i2c_readRegister16Async_value = 0; // readRegister16Async 读取数据返回值
- AsyncFunc i2c_readRegister16Async_endCallback = NULL; // readRegister16Async 读取数据完成回调
- void i2c_readRegister16Async_timeout()
- {
- i2c_readRegister16Async_value = 0;
- i2c_readRegister16Async_step = 0;
- I2C_Isr_callback = NULL;
- I2CMSST = 0x00;
- if (i2c_readRegister16Async_endCallback != NULL)
- {
- i2c_readRegister16Async_endCallback();
- }
- }
- // I2C异步读取, 16位寄存器
- void i2c_readRegister16Async()
- {
- if (i2c_readRegister16Async_step == 1)
- {
- I2CTXD = i2c_readRegister16Async_addr << 1; // 发送i2c地址
- I2CMSCR = 0x89; // 中断使能, 发送起始信号+设备地址+写信号
- i2c_readRegister16Async_step++;
- AsyncFuncNext(i2c_readRegister16Async_timeout, 200);
- return;
- }
- if (i2c_readRegister16Async_step == 2)
- {
- I2CTXD = i2c_readRegister16Async_reg; // 发送读取寄存器地址
- I2CMSCR = 0x8A; // 中断使能, 发送数据命令+接收ACK命令
- i2c_readRegister16Async_step++;
- AsyncFuncNext(i2c_readRegister16Async_timeout, 200);
- return;
- }
- if (i2c_readRegister16Async_step == 3)
- {
- I2CTXD = i2c_readRegister16Async_addr << 1 | 0x01; // 发送i2c地址+读取数据
- I2CMSCR = 0x89; // 中断使能, 发送起始信号+设备地址+写信号
- i2c_readRegister16Async_step++;
- AsyncFuncNext(i2c_readRegister16Async_timeout, 200);
- return;
- }
- if (i2c_readRegister16Async_step == 4)
- {
- I2CMSST = 0x00; // 设置 ACK 信号
- I2CMSCR = 0x8B; // 中断使能, 接收数据命令+发送ACK(0)命令
- i2c_readRegister16Async_step++;
- AsyncFuncNext(i2c_readRegister16Async_timeout, 200);
- return;
- }
- if (i2c_readRegister16Async_step == 5)
- {
- i2c_readRegister16Async_value = I2CRXD;
- i2c_readRegister16Async_value <<= 8;
- I2CMSST = 0x01; // 设置 NAK 信号
- I2CMSCR = 0x8C; // 中断使能, 接收数据命令+发送NAK(1)命令
- i2c_readRegister16Async_step++;
- AsyncFuncNext(i2c_readRegister16Async_timeout, 200);
- return;
- }
- if (i2c_readRegister16Async_step == 6)
- {
- i2c_readRegister16Async_value |= I2CRXD;
- i2c_readRegister16Async_step++;
- I2CMSCR = 0x86; // 中断使能, 发送 STOP 命令
- AsyncFuncNext(i2c_readRegister16Async_timeout, 200);
- return;
- }
- if (i2c_readRegister16Async_step == 7)
- {
- I2C_Isr_callback = NULL;
- i2c_readRegister16Async_step = 0;
- AsyncFuncNext(i2c_readRegister16Async_endCallback, 20);
- return;
- }
- }
- // 使用异步来读取i2c数据
- void i2c_readRegister16Async_start(uint8 addr, uint8 reg, AsyncFunc readEndCallback)
- {
- i2c_readRegister16Async_addr = addr;
- i2c_readRegister16Async_reg = reg;
- i2c_readRegister16Async_value = 0;
- i2c_readRegister16Async_step = 1;
- I2C_Isr_callback = i2c_readRegister16Async;
- i2c_readRegister16Async_endCallback = readEndCallback;
- i2c_readRegister16Async();
- }
- // I2C同步读取, 16位寄存器
- uint16 i2c_readRegister16(uint8 addr, uint8 reg)
- {
- uint16 readDataValue = 0;
-
- I2CTXD = addr << 1; // 写数据到数据缓冲区
- I2CMSCR = 0x09; // 发送起始信号+设备地址+写信号
- Wait_I2C();
-
- I2CTXD = reg; // 写数据到数据缓冲区
- I2CMSCR = 0x0A; // 发送数据命令+接收ACK命令
- Wait_I2C();
-
- I2CTXD = addr << 1 | 0x01; // 发送i2c地址+读取数据
- I2CMSCR = 0x09; // 发送起始信号+设备地址+写信号
- Wait_I2C();
-
- I2CMSST = 0x00; // 设置 ACK 信号
- I2CMSCR = 0x0B; // 接收数据命令+发送ACK(0)命令
- Wait_I2C();
- readDataValue = I2CRXD;
- readDataValue <<= 8;
-
- I2CMSST = 0x01; // 设置 NAK 信号
- I2CMSCR = 0x0C; // 接收数据命令+发送NAK(1)命令
- Wait_I2C();
- readDataValue |= I2CRXD;
-
- I2CMSCR = 0x06; // 发送 STOP 命令
- Wait_I2C();
-
- return readDataValue;
- }
-
- uint8 i2c_writeRegister16Async_addr = 0; // writeRegister16Async 地址参数
- uint8 i2c_writeRegister16Async_reg = 0; // writeRegister16Async 寄存器参数
- uint8 i2c_writeRegister16Async_step = 0; // writeRegister16Async 异步执行步骤
- uint16 i2c_writeRegister16Async_value = 0; // writeRegister16Async 写入数据
- AsyncFunc i2c_writeRegister16Async_endCallback = NULL; // writeRegister16Async 读取数据完成回调
- // 异步写16位寄存器超时
- void i2c_writeRegister16Async_timeout()
- {
- i2c_writeRegister16Async_value = 0;
- i2c_writeRegister16Async_step = 0;
- I2C_Isr_callback = NULL;
- I2CMSST = 0x00;
- if (i2c_writeRegister16Async_endCallback != NULL)
- {
- i2c_writeRegister16Async_endCallback();
- }
- }
- // 异步写16位寄存器
- void i2c_writeRegister16Async()
- {
- if (i2c_writeRegister16Async_step == 1)
- {
- I2CTXD = i2c_writeRegister16Async_addr << 1; // 写数据到数据缓冲区
- I2CMSCR = 0x89; // 发送起始信号+设备地址+写信号
- i2c_writeRegister16Async_step++;
- AsyncFuncNext(i2c_writeRegister16Async_timeout, 200);
- return;
- }
- if (i2c_writeRegister16Async_step == 2)
- {
- I2CTXD = i2c_writeRegister16Async_reg; // 写数据到数据缓冲区
- I2CMSCR = 0x8A; // 发送数据命令+接收ACK命令
- i2c_writeRegister16Async_step++;
- AsyncFuncNext(i2c_writeRegister16Async_timeout, 200);
- return;
- }
- if (i2c_writeRegister16Async_step == 3)
- {
- I2CTXD = (i2c_writeRegister16Async_value >> 8) & 0xFF; // 写数据到数据缓冲区
- I2CMSCR = 0x8A; // 发送数据命令+接收ACK命令
- i2c_writeRegister16Async_step++;
- AsyncFuncNext(i2c_writeRegister16Async_timeout, 200);
- return;
- }
- if (i2c_writeRegister16Async_step == 4)
- {
- I2CTXD = i2c_writeRegister16Async_value & 0xFF; // 写数据到数据缓冲区
- I2CMSCR = 0x8A; // 发送数据命令+接收ACK命令
- i2c_writeRegister16Async_step++;
- AsyncFuncNext(i2c_writeRegister16Async_timeout, 200);
- return;
- }
- if (i2c_writeRegister16Async_step == 5)
- {
- I2CMSCR = 0x86; // 发送 STOP 命令
- i2c_writeRegister16Async_step++;
- AsyncFuncNext(i2c_writeRegister16Async_timeout, 200);
- return;
- }
- if (i2c_writeRegister16Async_step == 6)
- {
- I2C_Isr_callback = NULL;
- i2c_writeRegister16Async_step = 0;
- AsyncFuncNext(i2c_writeRegister16Async_endCallback, 20);
- return;
- }
- }
- // 使用异步来写入i2c数据
- void i2c_writeRegister16Async_start(uint8 addr, uint8 reg, uint16 value, AsyncFunc writeEndCallback)
- {
- i2c_writeRegister16Async_addr = addr;
- i2c_writeRegister16Async_reg = reg;
- i2c_writeRegister16Async_value = value;
- I2C_Isr_callback = i2c_writeRegister16Async;
- i2c_writeRegister16Async_endCallback = writeEndCallback;
- i2c_writeRegister16Async_step = 1;
- i2c_writeRegister16Async();
- }
- // 同步写16位寄存器
- void i2c_writeRegister16(uint8 addr, uint8 reg, uint16 value)
- {
- I2CTXD = addr << 1; // 写数据到数据缓冲区
- I2CMSCR = 0x09; // 发送起始信号+设备地址+写信号
- Wait_I2C();
- I2CTXD = reg; // 写数据到数据缓冲区
- I2CMSCR = 0x0A; // 发送数据命令+接收ACK命令
- Wait_I2C();
- I2CTXD = (value >> 8) & 0xFF; // 写数据到数据缓冲区
- I2CMSCR = 0x0A; // 发送数据命令+接收ACK命令
- Wait_I2C();
- I2CTXD = value & 0xFF; // 写数据到数据缓冲区
- I2CMSCR = 0x0A; // 发送数据命令+接收ACK命令
- Wait_I2C();
- I2CMSCR = 0x06; // 发送 STOP 命令
- Wait_I2C();
- }
- //------------------------------- I2C寄存器操作 结束 --------------------------
-
- //------------------------------- LDC1612操作 开始 ----------------------------
- #define SD_PIN P1_6 // SD引脚
- #define INTB_PIN P1_7 // 数据就绪中断
- #define CLKIN_PIN P1_3 // 外部时钟引脚
-
- // LDC1612 寄存器地址
- #define LDC1612_ADDR 0x2A // I2C地址 (ADDR接地时)
- #define REG_DATA_MSB_CH0 0x00 // 通道0数据高16位
- #define REG_DATA_LSB_CH0 0x01 // 通道0数据低16位
- #define REG_DATA_MSB_CH1 0x02 // 通道1数据高16位
- #define REG_DATA_LSB_CH1 0x03 // 通道1数据低16位
- #define REG_RCOUNT_CH0 0x08 // 通道0转换计数
- #define REG_RCOUNT_CH1 0x09 // 通道1转换计数
- #define REG_SETTLE_CNT_CH0 0x10 // 通道0稳定计数
- #define REG_SETTLE_CNT_CH1 0x11 // 通道1稳定计数
- #define REG_CLOCK_DIVIDERS_CH0 0x14 // 通道0时钟分频
- #define REG_CLOCK_DIVIDERS_CH1 0x15 // 通道1时钟分频
- #define REG_STATUS 0x18 // 状态寄存器
- #define REG_ERROR_CONFIG 0x19 // 错误配置
- #define REG_CONFIG 0x1A // 配置寄存器
- #define REG_MUX_CONFIG 0x1B // 多路复用配置
- #define REG_RESET_DEV 0x1C // 设备复位
- #define REG_DRIVE_CURRENT_CH0 0x1E // 通道0驱动电流
- #define REG_DRIVE_CURRENT_CH1 0x1F // 通道1驱动电流
-
- // 配置常量
- #define CONVERSION_TIME 0x0850 // 转换时间设置
- #define SETTLE_TIME 0x0400 // 稳定时间设置
- #define CLOCK_DIVIDER 0x1001 // 时钟分频设置
- #define DRIVE_CURRENT 0x1C00 // 驱动电流设置 (最大电流)
- // 全局变量
- uint32 ch0_value = 0;
- uint32 ch1_value = 0;
- uint8 LDC1612_read_ready = 0;
- uint8 LDC1612_data_ready = 0;
-
- uint16 LDC1612_readDataAsync_msb = 0;
- uint16 LDC1612_readDataAsync_lsb = 0;
- uint16 LDC1612_readDataAsync_step = 0;
- uint8 LDC1612_readDataAsync_isStart = 0;
- // 异步读取传感器数据
- void LDC1612_readDataAsync()
- {
- if (LDC1612_readDataAsync_step == 1)
- {
- LDC1612_readDataAsync_step++;
- AsyncFuncNext(LDC1612_readDataAsync, 100);
- i2c_readRegister16Async_start(LDC1612_ADDR, REG_STATUS, LDC1612_readDataAsync);
- return;
- }
- if (LDC1612_readDataAsync_step == 2)
- {
- LDC1612_readDataAsync_step++;
- i2c_readRegister16Async_start(LDC1612_ADDR, REG_DATA_MSB_CH0, LDC1612_readDataAsync);
- return;
- }
- if (LDC1612_readDataAsync_step == 3)
- {
- LDC1612_readDataAsync_step++;
- LDC1612_readDataAsync_msb = i2c_readRegister16Async_value;
- i2c_readRegister16Async_start(LDC1612_ADDR, REG_DATA_LSB_CH0, LDC1612_readDataAsync);
- return;
- }
- if (LDC1612_readDataAsync_step == 4)
- {
- LDC1612_readDataAsync_step = 0;
- LDC1612_readDataAsync_lsb = i2c_readRegister16Async_value;
- ch0_value = ((uint32)LDC1612_readDataAsync_msb << 16) | LDC1612_readDataAsync_lsb;
- ch0_value &= 0x0FFFFFFF; // 保留28位有效数据
- ch0_value >>= 12; // 转为16位数据
- LDC1612_data_ready = 1; // 读取数据完成
- return;
- }
- }
- // 同步读取传感器数据
- void LDC1612_readData()
- {
- uint16 msb = 0;
- uint16 lsb = 0;
- // 读取状态寄存器
- uint16 status = i2c_readRegister16(LDC1612_ADDR, REG_STATUS);
- // 检查错误标志
- if (status & 0x0008)
- {
- // Serial.println("Error: Amplitude too low!");
- }
- if (status & 0x0010)
- {
- // Serial.println("Error: Timeout occurred!");
- }
-
- // 读取通道0数据
- msb = i2c_readRegister16(LDC1612_ADDR, REG_DATA_MSB_CH0);
- lsb = i2c_readRegister16(LDC1612_ADDR, REG_DATA_LSB_CH0);
- ch0_value = ((uint32)msb << 16) | lsb;
- ch0_value &= 0x0FFFFFFF; // 保留28位有效数据
- ch0_value >>= 12;
- LDC1612_data_ready = 1; // 读取数据完成
- }
-
- uint8 LDC1612_writeInitReg_step = 0;
- // 异步方法写入寄存器值
- void LDC1612_writeInitRegAsync()
- {
- // 配置通道0
- if (LDC1612_writeInitReg_step == 1)
- {
- LDC1612_writeInitReg_step++;
- i2c_writeRegister16Async_start(LDC1612_ADDR, REG_RCOUNT_CH0, CONVERSION_TIME, LDC1612_writeInitRegAsync);
- return;
- }
- if (LDC1612_writeInitReg_step == 2)
- {
- LDC1612_writeInitReg_step++;
- i2c_writeRegister16Async_start(LDC1612_ADDR, REG_SETTLE_CNT_CH0, SETTLE_TIME, LDC1612_writeInitRegAsync);
- return;
- }
- if (LDC1612_writeInitReg_step == 3)
- {
- LDC1612_writeInitReg_step++;
- i2c_writeRegister16Async_start(LDC1612_ADDR, REG_CLOCK_DIVIDERS_CH0, CLOCK_DIVIDER, LDC1612_writeInitRegAsync);
- return;
- }
- if (LDC1612_writeInitReg_step == 4)
- {
- LDC1612_writeInitReg_step++;
- i2c_writeRegister16Async_start(LDC1612_ADDR, REG_DRIVE_CURRENT_CH0, DRIVE_CURRENT, LDC1612_writeInitRegAsync);
- return;
- }
- // 配置错误检测
- if (LDC1612_writeInitReg_step == 5)
- {
- LDC1612_writeInitReg_step++;
- // 启用数据输出错误检测
- i2c_writeRegister16Async_start(LDC1612_ADDR, REG_ERROR_CONFIG, 0x0001, LDC1612_writeInitRegAsync);
- return;
- }
- // 配置多路复用器 - 只启用通道0
- if (LDC1612_writeInitReg_step == 6)
- {
- LDC1612_writeInitReg_step++;
- // 只扫描通道0,去抖动计数=1
- i2c_writeRegister16Async_start(LDC1612_ADDR, REG_MUX_CONFIG, 0x0208, LDC1612_writeInitRegAsync);
- return;
- }
- // 配置主配置寄存器
- if (LDC1612_writeInitReg_step == 7)
- {
- LDC1612_writeInitReg_step++;
- // 启用传感器,单通道模式
- i2c_writeRegister16Async_start(LDC1612_ADDR, REG_CONFIG, 0x1401, LDC1612_writeInitRegAsync);
- return;
- }
- // 初始化完成
- if (LDC1612_writeInitReg_step == 8)
- {
- LDC1612_writeInitReg_step = 0;
- return;
- }
- }
- // 同步方法写入寄存器值
- void LDC1612_writeInitReg()
- {
- // 配置通道0
- i2c_writeRegister16(LDC1612_ADDR, REG_RCOUNT_CH0, CONVERSION_TIME);
- i2c_writeRegister16(LDC1612_ADDR, REG_SETTLE_CNT_CH0, SETTLE_TIME);
- i2c_writeRegister16(LDC1612_ADDR, REG_CLOCK_DIVIDERS_CH0, CLOCK_DIVIDER);
- i2c_writeRegister16(LDC1612_ADDR, REG_DRIVE_CURRENT_CH0, DRIVE_CURRENT);
-
- // 配置错误检测
- i2c_writeRegister16(LDC1612_ADDR, REG_ERROR_CONFIG, 0x0001); // 启用数据输出错误检测
-
- // 配置多路复用器 - 只启用通道0
- i2c_writeRegister16(LDC1612_ADDR, REG_MUX_CONFIG, 0x0208); // 只扫描通道0,去抖动计数=1
-
- // 配置主配置寄存器
- i2c_writeRegister16(LDC1612_ADDR, REG_CONFIG, 0x1401); // 启用传感器,单通道模式
- }
-
- // LDC1612 中断服务程序 (数据就绪)
- void LDC1612_ready_isr() interrupt 38
- {
- unsigned char intf;
- intf = P1INTF;
- if (intf)
- {
- P1INTF = 0x00;
- // P1.7 口中断
- if (intf & 0x80)
- {
- LDC1612_read_ready = 1;
- }
- }
- }
-
- void initI2C()
- {
- P_SW2 = 0x80; // 使能访问XFR
- P1PU |= 0x30; // P1.4和P1.5使能4K上拉电阻
- P1M1 |= 0x10;
- P1M0 |= 0x10; // P1.4(SDA)开漏输出
- P1M1 |= 0x20;
- P1M0 |= 0x20; // P1.5(SCL)开漏输出
-
- // I2C总线速度计算: SYSCLK / 2 / (MSSPEED * 2 + 4)
- // 对于400KHz: MSSPEED = (40M / 400K / 2 - 4) / 2 = 23 (0x17)
- I2CCFG = 0x80 | 0x40 | 0x17; // 使能I2C + 主机模式 + 400KHz速度
- I2CMSST = 0x00; // 清除状态寄存器
- IP2H |= 0x40; // 设置I2c中断的优先级为高
- IP2 |= 0x40; // 设置I2c中断的优先级为高
- }
-
- // LDC1612初始化
- void initLDC1612()
- {
- // p1.6 SD
- P1M1 &= ~0x40;
- P1M0 |= 0x40; // 推挽输出
- // p1.3 CLKIN_PIN
- P1M1 &= ~0x08;
- P1M0 |= 0x08; // 推挽输出
- // p1.7 INTB 数据准备好中断
- P1M1 |= 0x80;
- P1M0 &= ~0x80; // 高阻输入
-
- SD_PIN = 0; // 低电平使能ldc1612
- CLKIN_PIN = 0; // 禁用外部时钟引脚
- P1IM0 = 0x00; // 下降沿中断
- P1IM1 = 0x00;
- P1INTE = 0x80; // 使能 p1.7 口中断
-
- Timer4_Init();
- initI2C();
- delay(50);
-
- // 同步方法写入寄存器
- // LDC1612_writeInitReg();
- // 异步方式写入寄存器
- LDC1612_writeInitReg_step = 1;
- LDC1612_writeInitRegAsync();
- }
- //------------------------------- LDC1612操作 结束 ----------------------------
-
- void main(void)
- {
- P_SW2 = 0x80; // 使能访问XFR
- WTST = 0; // 设置程序指令延时参数,赋值为0可将CPU执行指令的速度设置为最快
- CKCON = 0; // 提高访问XRAM速度
-
- Uart1_Init(); // 初始化串口 1M波特率
- EA = 1; // 允许总中断
- initLDC1612();
- UartSendStr("setup\n");
-
- while (1)
- {
- if (LDC1612_read_ready == 1)
- {
- LDC1612_read_ready = 0;
- // 同步读取传感器数据
- // LDC1612_readData();
-
- // i2c无阻塞异步读取传感器数据
- LDC1612_readDataAsync_step = 1;
- LDC1612_readDataAsync();
- }
-
- if (LDC1612_data_ready == 1)
- {
- LDC1612_data_ready = 0;
- UartSendStr("LDC1612 data:");
- numberToChar(ch0_value, sendNumberCharArr);
- UartSendStr(sendNumberCharArr);
- UartSendStr("\n");
- }
- }
- }
复制代码
|