📄 i2ctest.c
字号:
/****************************************************************************
* 文件名:I2CTEST.C
* 功能:使用硬件I2C对ZLG7290进行操作,利用中断方式操作。
* 说明:将跳线器JP5短接。
****************************************************************************/
#include "config.h"
#define ZLG7290 0x70 /* 定义器件地址 */
#define Glitter_COM 0x70
/****************************************************************************
* 名称:I2C_Init()
* 功能:主模式I2C初始化,包括初始化其中断为向量IRQ中断。
* 入口参数:fi2c 初始化I2C总线速率,最大值为400K
* 出口参数:无
****************************************************************************/
void I2C_Init(uint32 fi2c)
{ if(fi2c>400000) fi2c = 400000;
PINSEL0 = (PINSEL0&0xFFFFFF0F) | 0x50; // 设置I2C控制口有效
I2SCLH = (Fpclk/fi2c + 1) / 2; // 设置I2C时钟为fi2c
I2SCLL = (Fpclk/fi2c) / 2;
I2CONCLR = 0x2C;
I2CONSET = 0x40; // 使能主I2C
/* 设置I2C中断允许 */
VICIntSelect = 0x00000000; // 设置所有通道为IRQ中断
VICVectCntl0 = 0x29; // I2C通道分配到IRQ slot 0,即优先级最高
VICVectAddr0 = (int)IRQ_I2C; // 设置I2C中断向量地址
VICIntEnable = 0x0200; // 使能I2C中断
}
/****************************************************************************
* 名称:DelayNS()
* 功能:长软件延时
* 入口参数:dly 延时参数,值越大,延时越久
* 出口参数:无
****************************************************************************/
void DelayNS(uint32 dly)
{ uint32 i;
for(; dly>0; dly--)
for(i=0; i<5000; i++);
}
/****************************************************************************
* 名称:main()
* 功能:对ZLG7290进行操作
* 说明:在STARTUP.S文件中使能IRQ中断(清零CPSR中的I位);
* 在CONFIG.H文件中包含I2CINT.H、ZLG7290.H。
****************************************************************************/
int main(void)
{ uint8 disp_buf[8];
uint8 key;
uint8 i;
PINSEL0 = 0x00000000; // 设置管脚连接,使用I2C口
PINSEL1 = 0x00000000;
DelayNS(10);
I2C_Init(30000); // I2C配置及端口初始化
/* 进行全闪测试 */
for(i=0; i<8; i++) disp_buf[i] = 0xC8;
ZLG7290_SendBuf(disp_buf,8);
DelayNS(150);
/* 显示"8 7 6 5 4 3 2 1" */
for(i=0; i<8; i++) disp_buf[i] = i+1;
ZLG7290_SendBuf(disp_buf,8);
DelayNS(150);
/* 显示"LPC2210F" */
disp_buf[7] = 0x14;
disp_buf[6] = 0x16;
disp_buf[5] = 0x0c;
disp_buf[4] = 0x02;
disp_buf[3] = 0x02;
disp_buf[2] = 0x01;
disp_buf[1] = 0x00;
disp_buf[0] = 0x0F;
ZLG7290_SendBuf(disp_buf,8);
/* 读取按键,设置键值对应的显示位闪烁 */
while(1)
{ DelayNS(1);
key = 0;
IRcvStr(ZLG7290, 0x01, disp_buf, 2);
if(0==disp_buf[1])
{ key = disp_buf[0];
}
switch(key)
{ case 1:
case 9:
ZLG7290_SendCmd(Glitter_COM, 0x01);
break;
case 2:
case 10:
ZLG7290_SendCmd(Glitter_COM, 0x02);
break;
case 3:
case 11:
ZLG7290_SendCmd(Glitter_COM, 0x04);
break;
case 4:
case 12:
ZLG7290_SendCmd(Glitter_COM, 0x08);
break;
case 5:
case 13:
ZLG7290_SendCmd(Glitter_COM, 0x10);
break;
case 6:
case 14:
ZLG7290_SendCmd(Glitter_COM, 0x20);
break;
case 7:
case 15:
ZLG7290_SendCmd(Glitter_COM, 0x40);
break;
case 8:
case 16:
ZLG7290_SendCmd(Glitter_COM, 0x80);
break;
default:
break;
}
}
return(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -