📄 _iicg.c
字号:
/*
//==Program Name:iic.c================================
//--Program Description:iic fuction--------
//--Author:Yanyou Song---E-mail:syyo@163.com-----------------
//--Some Program&Fuction May Have BUG------------------------
//--The Code Is Strictly In public,Forbiding Updata On Net---
//--Use This Code Caused All The Problem Is Irrespective The
Programmer,Aftermath By Yourself!!---------------------------
//--Copyright Yanyou Song,All rights reserved.---------------
//--Design Data:2006-09-11 ----------------------------------
//===========================================================
//===========程序名称:Main_entry.c=======================
//--程序目的:iic函数-----------------------------
//-----设计&编写者 :宋彦佑--电子邮件:syyo@163.com---------
//------------程序&函数有存在BUG的可能--------------------
//------此原码不公开!!不允许公布在因特网上!!------------
//--使用此代码引起的一切问题与设计&编写者无关,后果自负---
//------------作者保留此代码的所有权力--------------------
//----设计日期:-2006-now---------------------------------
//========================================================
//--------最后更改日期---------
//---the last revision time----
//2006-10-20
*/
#include "snds.h"
#include "std.h"
/*=====================================================================*/
/* IIC EEPROM ADDRESS MAP */
/* Case of Using 64Kbit(=8K bytes) IIC Serial EEPROM, */
/* The address range is 0x0 to 0x1fff */
/*=====================================================================*/
#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
/*=====================================================================*/
/* CONTROL REGISTERS */
/*=====================================================================*/
#define BF 0x01 //Buffer flag
#define IEN 0x02 //Interrupt enable
#define LRB 0x04 //Last bit received
//ACK not received
#define ACK 0x08 //Acknowlege enable
//Generate an ACK signal at 9th SCL
#define NOACK 0x00 //No more received data is required
//from the slave
#define START 0x10
#define STOP 0x20
#define RESTART 0x30
#define BUSY 0x40
#define IICRESET 0x80 //Reset the IIC-BUS controller
//*********************************************************
//* IIC SETUP ROUTINE *
//*********************************************************
void iicSetup(void)
{
IICCON = IICRESET;
IICPS = 0x00000025;
}
int iicread(uint16 ReadAddr)
{
char i;
unsigned char rdata;
unsigned int con_wait;
int error=0;
iicSetup();
con_wait=0;
while(IICCON&BUSY)
IICCON = START|ACK;
IICBUF=IIC_DEV_0|S_WRITE;
con_wait=0;
while(!(IICCON &BF))
IICBUF = ReadAddr>>8;
con_wait=0;
while(!(IICCON &BF))
IICBUF = ReadAddr&0xff;
con_wait=0;
while(!(IICCON &BF))
IICCON = RESTART;
IICCON = START|ACK;
IICBUF = IIC_DEV_0|S_READ;
con_wait=0;
while(!(IICCON &BF))
IICCON=ACK;
con_wait=0;
while(!(IICCON &BF))
rdata=IICBUF;
con_wait=0;
while(!(IICCON &BF))
IICCON = NOACK;
con_wait=0;
while(!(IICCON &BF))
IICCON = STOP;
return 0;
}
int iicwrite(unsigned int WriteAddr,unsigned char pdata)
{
char i;
unsigned int con_wait;
int error=0;
iicSetup();
while(IICCON&BUSY)
IICCON=START|ACK;
IICBUF=0xa0;
while(!(IICCON &BF))
IICBUF=WriteAddr>>8;
while(!(IICCON &BF))
IICBUF=WriteAddr&0x00ff;
while(!(IICCON &BF))
IICBUF=pdata;
while(!(IICCON &BF))
IICCON=STOP;
Delay(6000);
return error;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -