📄 iic.c
字号:
//***************************************************************************************************
//Chinese Academy of Sciences, Institute of Automation
//File Name: main.c
//Description: IIC test
//Author: JuGuang,Lee
//***************************************************************************************************
#include "../inc/iic.h"
#include "../inc/board.h"
#include "../inc/utils.h"
void Delay1(int CNT)
{
int i;
for(i=0;i<CNT;i++);
}
int IicRead(int addr, int size, int *buf)
{
int error;
error = DrvIicRead(addr, size, buf);
return error;
}
int IicWrite(int addr, int size, int *pdata)
{
int PageBuf[PAGE_SIZE];
int page, no_of_page, error=0, i, remain_byte;
no_of_page = size/PAGE_SIZE;
remain_byte = size % PAGE_SIZE;
if (no_of_page)
{
for(page=0; page<no_of_page; page++)
{
for(i=0; i<PAGE_SIZE; i++)
PageBuf[i] = *pdata++;
DrvIicWrite(addr, PAGE_SIZE, PageBuf);
addr += PAGE_SIZE;
}
}
if (remain_byte)
{
for(i=0; i<remain_byte; i++)
PageBuf[i] = *pdata++;
DrvIicWrite(addr, remain_byte, PageBuf);
}
return error;
}
int IicInit()
{
int error = 0;
// reset the ICC
IICCON = IICRESET;
// clock setup, ICC CLK = SysClk/(16 x (prescaker + 1) + 3), 100KHz at 50MHz
IICPS = 0x0000001E;
return error;
}
int DrvIicRead(int addr, int size, int *pdata)
{
int error = 0, i;
// reset the IIC
IicInit();
// wait for not busy
while(IICCON & BUSY);
// Send Start: START | ACK
IICCON = START | IIC_ACK;
// send control byte (write cmd): read block 0
IICBUF = IIC_DEV_0 | S_WRITE ;
while(!(IICCON & BF));
// send lsb address
IICBUF = (int)(addr & 0xFF);
while(!(IICCON & BF));
// send re-start and control byte (read cmd)
IICCON = RESTART;
IICCON = START | IIC_ACK;
IICBUF = IIC_DEV_0 | S_READ ;
while(!(IICCON & BF));
// Generate ACK for multi read
IICCON = IIC_ACK;
while(!(IICCON & BF));
// Rx Data
for(i=0; i<size; i++)
{
*pdata++ = IICBUF;
while(!(IICCON & BF));
}
// Send NO ACK
IICCON = NOACK;
while(!(IICCON & BF));
// Send Stop
IICCON = STOP;
return error;
}
int DrvIicWrite(int address, int size,int *pdata)
{
int i;
// reset the ICC
IicInit();
// wait for not busy
while(IICCON & BUSY);
// Send Start: START | ACK
IICCON = START | IIC_ACK;
// send control byte (write cmd): read block 0
IICBUF = IIC_DEV_0 | S_WRITE;
while(!(IICCON & BF));
// send lsb address
IICBUF = (int)(address & 0xFF);
while(!(IICCON & BF));
// Write data
for(i=0; i<size; i++)
{
IICBUF = *pdata++;
while(!(IICCON & BF));
}
// STOP IIC Controller
IICCON = STOP;
// 5 ms write cycle
Delay1(100000);
return 0;
}
void Iic_Test(void)
{
int i;
int size=0;
char temp;
int a[100];
IicInit();
puts("\n 请输入要测试的数据 \n");
while((temp=getch())!=ENTER_KEY)
{
a[size]=(int)temp;
printf("\n a%i=%i \n",size,a[size]);;
size++;
}
//for(i=0;i<size;i++)printf("\n b%d=%d \n",i,a[i]);
i=IicWrite(0x0,size,a);
printf("\n 接受数字大小: %d \n",size);
puts("\n 请按ENTER键,进行读数据操作 \n");
i=DrvIicRead(0x0,size,a);
puts("读出的数据为:\n");
for(i=0;i<size;i++)printf("\n a%i=%i \n",i,a[i]);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -