📄 main.cpp
字号:
/*--------------------------------------------------------------------------
LPCARM之I2C中断读写CAT1025 C++程序祥解
本例程可以做为通用串行EEPROM读写或SMBus总线操作基础参考(祥见SMBus程序祥解)。
菜地公告:引用本文必须注明出处!!!
菜农HotPower 2007.3.11 于西安大雁塔菜地 http://HotPower.21ic.org/
---------------------------------------------------------------------------*/
#include "LPC213XDEF.h"
#include "main.h"
#include <string.h>
#include <stdio.h>
__noinit__ UartObj Uart;
__noinit__ I2cObj I2c;
/*-----------------------------------
串口0中断服务程序
-----------------------------------*/
extern "C" void IRQ_UART0 (void) __irq
{
Uart.UartExec();
VIC->VectAddr = 0x00; /* 通知VIC中断处理结束 */
}
extern "C" void IRQ_I2C0 (void) __irq
{
I2c.Exec();
VIC->VectAddr = 0x00; /* 通知VIC中断处理结束 */
}
int main (void) {
unsigned int i, j, error = 0;
char str[32];
unsigned char state, ptr[256];
Uart.puts("LPCARM之I2C中断读写CAT1025 C++程序 HotPower\n");
Uart.Wait();
for(i = 0;i < 16; i ++) {
for(j = 0;j < 16; j ++) {
I2c.TxBuffer[j] = i* 16 + j;
}
state = I2c.WriteBlock(i * 16, 16);
switch (state) {
case I2C_BUS_OK:
// Uart.puts("页写成功\n");
break;
case I2C_BUS_ERROR:
error ++;
sprintf(str, "%02d页写失败 state = %02X Count = %d\n", i, I2c.state, I2c.Count);
Uart.puts(str);
break;
}
}
if (error == 0) {
Uart.puts("全部16个页写成功!!!\n");
}
for(i = 0;i < 16; i ++) {
state = I2c.ReadBlock(i * 16, 16);
switch (state) {
case I2C_BUS_OK:
// Uart.puts("页读成功\n");
for(j = 0;j < 16; j ++) {
ptr[i * 16 + j] = I2c.RxBuffer[j];
}
break;
case I2C_BUS_ERROR:
sprintf(str, "%02d页读失败 state = %02X Count = %d\n", i, I2c.state, I2c.Count);
Uart.puts(str);
break;
default:
sprintf(str, "state = %02X Count = %d\n", I2c.state, I2c.Count);
Uart.puts(str);
}
}
Uart.Wait();
for(i = 0;i < 16; i ++) {
sprintf(str, "0x%04X: ", i * 16);
Uart.puts(str);
for(j = 0;j < 16; j ++) {
sprintf(str, "%02X ", ptr[i * 16 + j]);
Uart.puts(str);
}
Uart.puts("\n");
Uart.Wait();//等待串口数据发送结束才能操作IAP
}
while(1) {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -