?? i2c_test.c
字號:
#define LPCEB2000_I
/*********************************************************************************************
* File: i2c_test.c
* Author: Embest w.h.xie
* Desc: test I2C bus read and write
* History:
*********************************************************************************************/
/*------------------------------------------------------------------------------------------*/
/* include files */
/*------------------------------------------------------------------------------------------*/
#include "target.h"
/*------------------------------------------------------------------------------------------*/
/* define */
/*------------------------------------------------------------------------------------------*/
#define FOSC 10000000
#define FCCLK 40000000
#define Fpclk 10000000
#define CAT24WC02 0xA0 // 24WC02 address
/*------------------------------------------------------------------------------------------*/
/* local function declare */
/*------------------------------------------------------------------------------------------*/
__irq void irq_i2c(void); // I2C interrupt
/*------------------------------------------------------------------------------------------*/
/* global variable define */
/*------------------------------------------------------------------------------------------*/
uint8 ucI2CAdd; //
uint8 ucI2CRamAdd; // RAM read and write address
uint8 ucI2CNum; // I2C read and write Num
uint8 *ucI2CBuf; // I2C work pointer
uint8 ucReadBuf[8]; // read data buf
uint8 ucWriteBuf[8]; // write data buf
uint8 ucI2CEnd; // end sign
uint8 ucI2CAddEn; // work enabled sign
/*********************************************************************************************
* name: time_dly
* func: display code
* para: dly --in, delay value
* ret: none
* modify:
* comment:
*********************************************************************************************/
void time_dly(uint32 dly)
{
uint32 i;
for(; dly>0; dly--)
for(i=0; i<500; i++);
}
/*********************************************************************************************
* name: sys_init
* func: init the sys
* para: none
* ret: none
* modify:
* comment:
*********************************************************************************************/
void sys_init()
{
// allow PLL
PLLCON = 1;
VPBDIV = PLL_VPBDIV;
PLLCFG = PLL_CFG;
PLLFEED = 0xaa;
PLLFEED = 0x55;
while((PLLSTAT & (1 << 10)) == 0);
PLLCON = 3;
PLLFEED = 0xaa;
PLLFEED = 0x55;
// set MAM 2 CCLK
MAMCR = 0;
MAMTIM = MAM_TIM;
MAMCR = 2;
// init all interrupt
VICIntEnClr = 0xffffffff;
VICVectAddr = 0;
VICIntSelect = 0;
}
/*********************************************************************************************
* 名稱: irq_i2c(void)
* 功能: I2C中斷服務程序
* 參數: 無
* 返回: 無
* 修改:
* 注釋:
*********************************************************************************************/
__irq void irq_i2c(void)
{ uint8 sta;
sta = I2C_I2STAT; // read state
switch(sta) {
case 0x08:
if (ucI2CAddEn == 1)
I2C_I2DAT = ucI2CAdd & 0xFE; // send slave address + read sign
else
I2C_I2DAT = ucI2CAdd; // send slave address
I2C_I2CONCLR = 0x28; // SI=0
break;
case 0x10:
I2C_I2DAT = ucI2CAdd; // reset, send slave address
I2C_I2CONCLR = 0x28; // SI=0
break;
case 0x18:
if (ucI2CAddEn == 0) {
if(ucI2CNum>0) {
I2C_I2DAT = *ucI2CBuf++; // send data
I2C_I2CONCLR = 0x28;
ucI2CNum--;
} else {
I2C_I2CONSET = 0x10; // send finish,
I2C_I2CONCLR = 0x28;
ucI2CEnd = 1; // set end sign
}
break;
}
if (ucI2CAddEn == 1) {
I2C_I2DAT = ucI2CRamAdd; // send work address
}
if (ucI2CAddEn == 2) {
I2C_I2DAT = ucI2CRamAdd; // send work address
ucI2CAddEn = 0; // clear sign
}
I2C_I2CONCLR = 0x28; // SI=0 , clear interrupt sign
break;
case 0x28:
if (ucI2CAddEn == 0) {
if (ucI2CNum>0) {
I2C_I2DAT = *ucI2CBuf++; // send work address
ucI2CNum--;
} else {
I2C_I2CONSET = 0x10; // send finish
ucI2CEnd = 1;
}
I2C_I2CONCLR = 0x28; // SI=0 , clear interrupt sign
}
if (ucI2CAddEn == 1) { // read appoint address
I2C_I2CONSET = 0x20;
I2C_I2CONCLR = 0x08;
ucI2CAddEn = 0; // appoint address dispose finish
}
break;
case 0x20:
case 0x30:
case 0x38: // bus error
I2C_I2CONCLR = 0x28;
ucI2CEnd = 0xFF;
break;
case 0x40: // already send sla+r and recive the ack
if(1==ucI2CNum) { // send no ack when last byte
I2C_I2CONCLR = 0x2C;
} else { // recive and send ack
I2C_I2CONSET = 0x04;
I2C_I2CONCLR = 0x28;
}
break;
case 0x50:
*ucI2CBuf++ = I2C_I2DAT; // read data
ucI2CNum--;
if (1==ucI2CNum) {
I2C_I2CONCLR = 0x2C;
} else {
I2C_I2CONSET = 0x04;
I2C_I2CONCLR = 0x28;
}
break;
case 0x58:
*ucI2CBuf++ = I2C_I2DAT; // read last byte data
I2C_I2CONSET = 0x10; // bus end
I2C_I2CONCLR = 0x28;
ucI2CEnd = 1;
break;
case 0x48:
I2C_I2CONCLR = 0x28; // bus error
ucI2CEnd = 0xFF;
break;
default:
break;
}
VICVectAddr = 0x00; // interrupt finish
}
/*********************************************************************************************
* name: i2c_init
* func: init the I2C bus
* para: none
* ret: none
* modify:
* comment:
*********************************************************************************************/
void i2c_init(void)
{
PINSEL0 = (PINSEL0&0xFFFFFF0F) | 0x50; // I2C open
I2C_I2SCLH = 200; // set I2C clock
I2C_I2SCLL = 200;
I2C_I2CONCLR = 0x2C;
I2C_I2CONSET = 0x40; // set Master Mode
// set I2C interrupt
VICIntSelect = 0x00000000;
VICVectCntl0 = 0x29;
VICVectAddr0 = (int)irq_i2c;
VICIntEnable = 0x0200;
}
/*********************************************************************************************
* name: I2C_sendstr
* func: I2C send str
* para: sla -- in, slave address
* add -- in, data address
* ret: none
* modify:
* comment:
*********************************************************************************************/
uint8 I2C_sendstr(uint8 sla, uint8 add)
{
// setup basic parameter
ucI2CAdd = sla;
ucI2CNum = 8;
ucI2CRamAdd = add;
ucI2CBuf = ucWriteBuf;
ucI2CEnd = 0;
ucI2CAddEn = 2;
// set master Mode, startup bus
I2C_I2CONCLR = 0x2C;
I2C_I2CONSET = 0x60;
// wait work finish
while(0==ucI2CEnd);
if(1==ucI2CEnd) return(1);
else return(0);
}
/*********************************************************************************************
* name: I2C_rcvstr
* func: I2C receive str
* para: sla -- in, slave address
* add -- in, data address
* ret: none
* modify:
* comment:
*********************************************************************************************/
uint8 I2C_rcvstr(uint8 sla, uint8 add)
{
// setup basic parameter
ucI2CAdd = sla + 1;
ucI2CNum = 8;
ucI2CRamAdd = add;
ucI2CBuf = ucReadBuf;
ucI2CEnd = 0;
ucI2CAddEn = 1;
// set master Mode, startup bus
I2C_I2CONCLR = 0x2C;
I2C_I2CONSET = 0x60;
// wait work finish
while(0==ucI2CEnd);
if(1==ucI2CEnd) return(1);
else return(0);
}
/*********************************************************************************************
* name: Main
* func: main fun
* para: none
* ret: none
* modify:
* comment:
*********************************************************************************************/
int main(void)
{
uint8 i;
// init GPIO
PINSEL0 = 0x00000000;
PINSEL1 = 0x00000000;
// init beep
IODIR0 = BEEP_CON;
IOSET0 = BEEP_CON;
for (i=0; i<8 ; i++) {
ucWriteBuf[i] = i * 11;
ucReadBuf[i] = i;
}
sys_init();
i2c_init();
I2C_sendstr(CAT24WC02, 0x00);
time_dly(100); // wait work finish
I2C_rcvstr(CAT24WC02, 0x00);
for (i=0; i<8; i++) { // compare
if (ucReadBuf[i] != ucWriteBuf[i]) break;
}
do{
if (i<8) { // error
IOCLR0 = BEEP_CON;
time_dly(100);
IOSET0 = BEEP_CON;
time_dly(100);
} else { // right
IOCLR0 = BEEP_CON;
time_dly(10000000);
IOSET0 = BEEP_CON;
time_dly(100);
}
} while(1);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -