陀螺仪介绍及编程(2)

2020-06-07 14:31

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)


陀螺仪介绍及编程(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:《药用植物学与生药学》实验指导书绍兴文理学院元培学院

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: