📄 main1.c
字号:
//***************************************************************************************************
//Chinese Academy of Sciences, Institute of Automation
//File Name: main.c
//Description: IIC.
//Author: chaisc97
//Date: 2003-10-20
//***************************************************************************************************
#include "s3c4510b_add.h"
#define PAGE_SIZE 8
#define IIC_DEV_0 0xa0 // IIC device's slave address
#define S_WRITE 0x00 // Write IIC data for slave
#define S_READ 0x01 // Read IIC data for slave
#define BF 0x01 // Buffer flag
#define IEN 0x02 // Interrupt enable
#define LRB 0x04 // Last bit received/ACK not received
#define ACK 0x08 // Ack enable/Generate an ACK signal at 9th SCL
#define NOACK 0x00
#define START 0x10
#define STOP 0x20
#define RESTART 0x30
#define BUSY 0x40
#define IICRESET 0x80
void Delay(int CNT)
{
int i;
for(i=0;i<CNT;i++)
{
}
}
int DrvEepromRead(int addr, int size, int *buf)
{
int error;
error = DrvIicRead(addr, size, buf);
return error;
}
int DrvEepromWrite(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;
}
static int DrvIicInit()
{
int error = 0;
// reset the ICC
IICCON = IICRESET;
// clock setup, ICC CLK = SysClk/(16 x (prescaker + 1) + 3), 100KHz at 50MHz
IICPS = 0x0000001E;
// Install Vector
// SysSetInterrupt(nIIC_INT, IICisr);
// Enable the INT, Enable_Int(nTIMER1_INT);
// Enable_Int(nIIC_INT);
return error;
}
int DrvIicRead(int addr, int size, int *pdata)
{
int error = 0, i;
// reset the ICC
DrvIicInit();
// wait for not busy
while(IICCON & BUSY);
// Send Start: START | ACK
IICCON = START | ACK;
// send control byte (write cmd): read block 0
IICBUF = IIC_DEV_0 | S_WRITE ;
while(!(IICCON & BF));
// send msb address
// IICBUF = (int)((addr>>8) & 0xFF);
// while(!(IICCON & 0x01));
// send lsb address
IICBUF = (int)(addr & 0xFF);
while(!(IICCON & BF));
// send re-start and control byte (read cmd)
IICCON = RESTART;
IICCON = START | ACK;
IICBUF = IIC_DEV_0 | S_READ ;
while(!(IICCON & BF));
// Generate ACK for multi read
IICCON = 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;
int block;
// reset the ICC
DrvIicInit();
// wait for not busy
while(IICCON & BUSY);
// Send Start: START | ACK
IICCON = START | ACK;
// send control byte (write cmd): read block 0
IICBUF = IIC_DEV_0 | S_WRITE;
while(!(IICCON & BF));
// send msb address
// IICBUF = (tU8)((address>>8) & 0xFF);
// while(!(IICCON & 0x01));
// 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
Delay(5000);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -