mirror of
https://github.com/ycHepth/KalmanFilter.git
synced 2026-03-13 10:23:28 +08:00
233 lines
5.6 KiB
C
233 lines
5.6 KiB
C
#include"stm32f1ox.h"
|
|
|
|
/*模拟IIC端口IO定义*/
|
|
#define SCL_H GPIOB->BSRR = GPIO_Pin_6
|
|
#define SCL_L GPIOB->BRR = GPIO_Pin_6
|
|
|
|
#define SDA_H GPIOB->BSRR = GPIO_Pin_7
|
|
#define SDA_L GPIOB->BRR = GPIO_Pin_7
|
|
|
|
//#define SCL_read GPIOB->IDR & GPIO_Pin_6
|
|
#define SDA_read GPIOB->IDR & GPIO_Pin_7
|
|
|
|
void I2C_GPIO_Config(void)
|
|
{
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
|
|
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
|
|
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
|
}
|
|
|
|
void I2C_delay(void)
|
|
{
|
|
u8 i=30; //the min of i is 5
|
|
while(i)
|
|
{
|
|
i--;
|
|
}
|
|
}
|
|
|
|
void delay5ms(void)
|
|
{
|
|
int i=5000;
|
|
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; //if SDA is 0 , then BUS is busy
|
|
SDA_L;
|
|
I2C_delay();
|
|
if(SDA_read) return FALSE; //if SDA is 1 , then BUS occur error
|
|
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) //return 1 - Ack ;return 0 - NoAck
|
|
{
|
|
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) //MSB
|
|
{
|
|
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;
|
|
}
|
|
|
|
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); //ALTER
|
|
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);//ALTER
|
|
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;
|
|
}
|