while(i) { i--; } }
/*******************************************************************************
* Function Name : I2C_Start
* Description : Master Start Simulation IIC Communication * Input : None * Output : None
* Return : Wheather Start
****************************************************************************** */
bool I2C_Start(void) { SDA_H; SCL_H; I2C_delay(); if(!SDA_read)return FALSE; //SDA线为低电平则总线忙,退出 SDA_L; I2C_delay(); if(SDA_read) return FALSE; //SDA线为高电平则总线出错,退出 SDA_L; I2C_delay(); return TRUE; }
/*******************************************************************************
* Function Name : I2C_Stop
* Description : Master Stop Simulation IIC Communication * Input : None * Output : None * Return : None
****************************************************************************** */
void I2C_Stop(void) { SCL_L; I2C_delay(); SDA_L; I2C_delay(); SCL_H; I2C_delay();
SDA_H; I2C_delay(); }
/*******************************************************************************
* Function Name : I2C_Ack
* Description : Master Send Acknowledge Single * Input : None * Output : None * Return : None
****************************************************************************** */
void I2C_Ack(void) { SCL_L; I2C_delay(); SDA_L; I2C_delay(); SCL_H; I2C_delay(); SCL_L; I2C_delay() }
/*******************************************************************************
* Function Name : I2C_NoAck
* Description : Master Send No Acknowledge Single * Input : None * Output : None * Return : None
****************************************************************************** */
void I2C_NoAck(void) { SCL_L; I2C_delay(); SDA_H; I2C_delay(); SCL_H; I2C_delay(); SCL_L; I2C_delay(); }
/******************************************************************************
*
* Function Name : I2C_WaitAck
* Description : Master Reserive Slave Acknowledge Single * Input : None * Output : None
* Return : Wheather Reserive Slave Acknowledge Single
****************************************************************************** */
bool I2C_WaitAck(void) //返回为:=1有ACK,=0无ACK { SCL_L; I2C_delay(); SDA_H; I2C_delay(); SCL_H; I2C_delay(); if(SDA_read) {
SCL_L; I2C_delay();
return FALSE; } SCL_L; I2C_delay(); return TRUE; }
/*******************************************************************************
* Function Name : I2C_SendByte
* Description : Master Send a Byte to Slave * Input : Will Send Date * Output : None * Return : None
****************************************************************************** */
void I2C_SendByte(u8 SendByte) //数据从高位到低位// {
u8 i=8; while(i--) {
SCL_L;
I2C_delay(); if(SendByte&0x80) SDA_H;
else
SDA_L; SendByte<<=1; I2C_delay(); SCL_H;
I2C_delay(); }
SCL_L; }
/*******************************************************************************
* Function Name : I2C_RadeByte
* Description : Master Reserive a Byte From Slave * Input : None * Output : None
* Return : Date From Slave
****************************************************************************** */
unsigned char I2C_RadeByte(void) //数据从高位到低位// {
u8 i=8;
u8 ReceiveByte=0;
SDA_H; while(i--) {
ReceiveByte<<=1; SCL_L;
I2C_delay(); SCL_H;
I2C_delay(); if(SDA_read) {
ReceiveByte|=0x01; } }
SCL_L;
return ReceiveByte; }
//ZRX
//单字节写入*******************************************
bool Single_Write(unsigned char SlaveAddress,unsigned char REG_Address,unsigned char REG_data) //void
{
if(!I2C_Start())return FALSE;
I2C_SendByte(SlaveAddress); //发送设备地址+写信号//I2C_SendByte(((REG_Address & 0x0700) >>7) | SlaveAddress & 0xFFFE);//设置高起始地址+器件地址 if(!I2C_WaitAck()){I2C_Stop(); return FALSE;}
I2C_SendByte(REG_Address ); //设置低起始地址 I2C_WaitAck();
I2C_SendByte(REG_data); I2C_WaitAck(); I2C_Stop(); delay5ms(); return TRUE; }
//单字节读取*****************************************
unsigned char Single_Read(unsigned char SlaveAddress,unsigned char REG_Address) { unsigned char REG_data; if(!I2C_Start())return FALSE;
I2C_SendByte(SlaveAddress); //I2C_SendByte(((REG_Address & 0x0700) >>7) | REG_Address & 0xFFFE);//设置高起始地址+器件地址
if(!I2C_WaitAck()){I2C_Stop();test=1; return FALSE;}
I2C_SendByte((u8) REG_Address); //设置低起始地址 I2C_WaitAck(); I2C_Start();
I2C_SendByte(SlaveAddress+1); I2C_WaitAck();
REG_data= I2C_RadeByte(); I2C_NoAck(); I2C_Stop(); //return TRUE; return REG_data; } /*
********************************************************************************
** 函数名称 : RCC_Configuration(void) ** 函数功能 : 时钟初始化 ** 输 入 : 无 ** 输 出 : 无 ** 返 回 : 无
void RCC_Configuration(void)
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库陀螺仪介绍及编程(2)在线全文阅读。
相关推荐: