📄 main.c
字号:
/*******************************************************************************
// GY-51 LSM303DLH IIC测试程序
// 使用单片机STM32F103C8T6
// 晶振:8.00M
// 显示:pc串口助手 波特率:115200
// 编译环境 Keil uVision4
// 时间:2011年9月1日
// QQ:531389319
// 与模块连接 GPIOB6->SCL GPIOB7->SDA
// 使用 STM32F103C8T6串口1
*******************************************************************************/
#include "stm32f10x_lib.h"
#include <math.h> //Keil library
//#include <stdio.h> //Keil library
GPIO_InitTypeDef GPIO_InitStructure;
ErrorStatus HSEStartUpStatus;
#define uchar unsigned char
#define uint unsigned int
//磁场内部寄存器***********************************
/* Magnetometer registers */#define CRA_REG_M 0x00 /* Configuration register A */#define CRB_REG_M 0x01 /* Configuration register B */#define MR_REG_M 0x02 /* Mode register *//* resume state index */#define RES_CRA_REG_M 0 /* Configuration register A */#define RES_CRB_REG_M 1 /* Configuration register B */#define RES_MR_REG_M 2 /* Mode register *//* Output register start address*/#define OUT_X_M 0x03/* Magnetic Sensor Operation Mode */#define NORMAL_MODE 0x00#define POS_BIAS 0x01#define NEG_BIAS 0x02#define CC_MODE 0x00#define SC_MODE 0x01#define SLEEP_MODE 0x03
//加速度内部寄存器***********************************
#define AXISDATA_REG 0x28#define WHOAMI_LSM303DLH_ACC 0x32 /* Expctd content for WAI *//* CONTROL REGISTERS */#define WHO_AM_I 0x0F /* WhoAmI register */#define CTRL_REG1 0x20 /* */#define CTRL_REG2 0x21 /* */#define CTRL_REG3 0x22 /* */#define CTRL_REG4 0x23 /* */#define CTRL_REG5 0x24 /* */#define INT_CFG1 0x30 /* interrupt 1 config */#define INT_SRC1 0x31 /* interrupt 1 source */#define INT_THS1 0x32 /* interrupt 1 threshold */#define INT_DUR1 0x33 /* interrupt 1 duration */#define INT_CFG2 0x34 /* interrupt 2 config */#define INT_SRC2 0x35 /* interrupt 2 source */#define INT_THS2 0x36 /* interrupt 2 threshold */#define INT_DUR2 0x37 /* interrupt 2 duration */
#define M_SlaveAddress 0x3C
#define A_SlaveAddress 0x30
unsigned char TX_DATA[5]; //串口发送数组
unsigned char BUF[8]; //接收数据缓存区
char test=0; //I2C使用
short M_x,M_y,M_z,A_x,A_y,A_z; //磁场与加速度的原始值
float angle; //磁场计算的角度
//************************************++++++++++++++++++++++++++++++++
/*模拟IIC端口输出输入定义*/
#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 RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void USART1_Configuration(void);
void WWDG_Configuration(void);
void Delay(u32 nTime);
void Delayms(vu32 m);
/* 变量定义 ----------------------------------------------*/
/*******************************/
void DATA_printf(uchar *s,short temp_data)
{
if(temp_data<0){
temp_data=-temp_data;
*s='-';
}
else *s=' ';
*++s =temp_data/1000+0x30;
temp_data=temp_data%1000; //取余运算
*++s =temp_data/100+0x30;
temp_data=temp_data%100; //取余运算
*++s =temp_data/10+0x30;
temp_data=temp_data%10; //取余运算
*++s =temp_data+0x30;
}
/*******************************************************************************
* Function Name : I2C_GPIO_Config
* Description : Configration Simulation IIC GPIO
* Input : None
* Output : None
* Return : None
****************************************************************************** */
void I2C_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure I2C1 pins: SCL and SDA */
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);
}
/*******************************************************************************
* Function Name : I2C_delay
* Description : Simulation IIC Timing series delay
* Input : None
* Output : None
* Return : None
****************************************************************************** */
void I2C_delay(void)
{
u8 i=30; //这里可以优化速度 ,经测试最低到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; //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_WriteLSM303D(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();
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();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -