请大家帮我提下意见,看看哪里可能出问题
程序如下:
#include "stm32f10x.h"
#include <math.h> //Keil library
GPIO_InitTypeDef GPIO_InitStructure;
ErrorStatus HSEStartUpStatus;
#define uchar unsigned char
#define uint unsigned int
// ?¨??MPU6050???????·
//****************************************
#define SMPLRT_DIV 0x19 //?????????ù????????????0x07(125Hz)
#define CONFIG 0x1A //???¨???¨??????????????0x06(5Hz)
#define GYRO_CONFIG 0x1B //??????×??ì?°????·??§??????????0x18(??×??ì??2000deg/s)
#define ACCEL_CONFIG 0x1C //??????×??ì??????·??§?°???¨???¨??????????????0x01(??×??ì??2G??5Hz)
#define ACCEL_XOUT_H 0x3B
#define ACCEL_XOUT_L 0x3C
#define ACCEL_YOUT_H 0x3D
#define ACCEL_YOUT_L 0x3E
#define ACCEL_ZOUT_H 0x3F
#define ACCEL_ZOUT_L 0x40
#define TEMP_OUT_H 0x41
#define TEMP_OUT_L 0x42
#define GYRO_XOUT_H 0x43
#define GYRO_XOUT_L 0x44
#define GYRO_YOUT_H 0x45
#define GYRO_YOUT_L 0x46
#define GYRO_ZOUT_H 0x47
#define GYRO_ZOUT_L 0x48
#define PWR_MGMT_1 0x6B //???????í??????????0x00(????????)
#define WHO_AM_I 0x75 //IIC???·?????÷(????????0x68??????)
//****************************
#define MPU6050_Addr 0xD0 //?¨???÷????IIC×????????????·,?ù??ALT ADDRESS???·????????????
unsigned char TX_DATA[4]; //????????????
unsigned char BUF[10]; //??????????????
char test=0; //IIC????
short T_X,T_Y,T_Z,T_T; //X,Y,Z?á??????
//************************************
/*????IIC?????????????¨??*/
#define SCL_H GPIOB->BSRR = GPIO_Pin_8
#define SCL_L GPIOB->BRR = GPIO_Pin_8
#define SDA_H GPIOB->BSRR = GPIO_Pin_9
#define SDA_L GPIOB->BRR = GPIO_Pin_9
#define SCL_read GPIOB->IDR & GPIO_Pin_8
#define SDA_read GPIOB->IDR & GPIO_Pin_9
/* ?????ê?÷ -----------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void USART3_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/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;
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
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_9;
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_Write(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) | 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)
{
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
/* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG and AFIO clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOF , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG | RCC_APB2Periph_AFIO , ENABLE);
}
/*
********************************************************************************
** ???????? ?? GPIO_Configuration(void)
** ???????? ?? ??????????
** ?? ?? ?? ??
** ?? ?? ?? ??
** ·? ?? ?? ??
********************************************************************************
*/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD, ENABLE );
RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART3, ENABLE );
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // ????????9
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // ???????ì????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // ×???????????50MHz
GPIO_Init(GPIOB, &GPIO_InitStructure); // ????A????
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //????????10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //????????
GPIO_Init(GPIOB, &GPIO_InitStructure); //????A????
}
/*
********************************************************************************
** ???????? ?? USART1_Configuration(void)
** ???????? ?? ?®??1??????
** ?? ?? ?? ??
** ?? ?? ?? ??
** ·? ?? ?? ??
********************************************************************************
*/
void USART3_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE );
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE );
USART_ClockInitStructure.USART_Clock = USART_Clock_Disable; // ?±????????????
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low; // ?±????????
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge; // ?±????????±???????????????
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable; // ×??ó???????????±??????????SCLK????
/* Configure the USART1 synchronous paramters */
USART_ClockInit(USART3, &USART_ClockInitStructure); // ?±?????????????è??
USART_InitStructure.USART_BaudRate = 115200; // ?¨????????115200
USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 8??????
USART_InitStructure.USART_StopBits = USART_StopBits_1; // ?????á??????1????????
USART_InitStructure.USART_Parity = USART_Parity_No ; // ?????§??
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // ?????÷?????§??
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // ·???????+????????
/* Configure USART1 basic and asynchronous paramters */
USART_Init(USART3, &USART_InitStructure);
/* Enable USART1 */
USART_ClearFlag(USART3, USART_IT_RXNE); //???????????????????????ó?????ú?ú????
USART_ITConfig(USART3,USART_IT_RXNE, ENABLE); //????USART1??????
USART_Cmd(USART3, ENABLE); //USART1×???????????
}
/*
********************************************************************************
** ???????? ?? NVIC_Configuration(void)
** ???????? ?? ??????????
** ?? ?? ?? ??
** ?? ?? ?? ??
** ·? ?? ?? ??
********************************************************************************
*/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = WWDG_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_Init(&NVIC_InitStructure);
}
/*
********************************************************************************
** ???????? ?? WWDG_Configuration(void)
** ???????? ?? ?????·??????
** ?? ?? ?? ??
** ?? ?? ?? ??
** ·? ?? ?? ??
********************************************************************************
*/
void WWDG_Configuration(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, ENABLE);
WWDG_SetPrescaler(WWDG_Prescaler_8); // WWDG clock counter = (PCLK1/4096)/8 = 244 Hz (~4 ms)
WWDG_SetWindowValue(0x41); // Set Window value to 0x41
WWDG_Enable(0x50); // Enable WWDG and set counter value to 0x7F, WWDG timeout = ~4 ms * 64 = 262 ms
WWDG_ClearFlag(); // Clear EWI flag
WWDG_EnableIT(); // Enable EW interrupt
}
/*
********************************************************************************
** ???????? ?? Delay(vu32 nCount)
** ???????? ?? ???±????
** ?? ?? ?? ??
** ?? ?? ?? ??
** ·? ?? ?? ??
********************************************************************************
*/
void Delay(vu32 nCount)
{
for(; nCount != 0; nCount--);
}
/*
********************************************************************************
** ???????? ?? void Delayms(vu32 m)
** ???????? ?? ?¤???±???? m=1,???±1ms
** ?? ?? ?? ??
** ?? ?? ?? ??
** ·? ?? ?? ??
********************************************************************************
*/
void Delayms(vu32 m)
{
u32 i;
for(; m != 0; m--)
for (i=0; i<50000; i++);
}
/*
********************************************************************************
** ???????? ?? WWDG_IRQHandler(void)
** ???????? ?? ?°???á?°????????
** ?? ?? ?? ??
** ?? ?? ?? ??
** ·? ?? ?? ??
********************************************************************************
*/
void WWDG_IRQHandler(void)
{
/* Update WWDG counter */
WWDG_SetCounter(0x50);
/* Clear EWI flag */
WWDG_ClearFlag();
}
//************************************************
void USART3_SendData(uchar SendData)
{
USART_SendData(USART3, SendData);
Delayms(1);
}
//??????MPU6050???ù???è????????pdf????????************************
void Init_MPU6050(void)
{
/*
Single_Write(MPU6050_Addr,PWR_M, 0x80); //
Single_Write(MPU6050_Addr,SMPL, 0x07); //
Single_Write(MPU6050_Addr,DLPF, 0x1E); //??2000??
Single_Write(MPU6050_Addr,INT_C, 0x00 ); //
Single_Write(MPU6050_Addr,PWR_M, 0x00); //
*/
Single_Write(MPU6050_Addr,PWR_MGMT_1 , 0x00); //????????×???
Single_Write(MPU6050_Addr,SMPLRT_DIV , 0x07);
Single_Write(MPU6050_Addr,CONFIG , 0x06);
Single_Write(MPU6050_Addr,GYRO_CONFIG , 0x18);
Single_Write(MPU6050_Addr,ACCEL_CONFIG, 0x01);
}
//******????MPU6050????****************************************
void READ_MPU6050(void)
{
BUF[0]=Single_Read(MPU6050_Addr,ACCEL_XOUT_L);
BUF[1]=Single_Read(MPU6050_Addr,ACCEL_XOUT_H);
T_X= (BUF[1]<<8)|BUF[0];
T_X/=16.4; //????????X?á????
BUF[2]=Single_Read(MPU6050_Addr,ACCEL_YOUT_L);
BUF[3]=Single_Read(MPU6050_Addr,ACCEL_YOUT_H);
T_Y= (BUF[3]<<8)|BUF[2];
T_Y/=16.4; //????????Y?á????
BUF[4]=Single_Read(MPU6050_Addr,ACCEL_ZOUT_L);
BUF[5]=Single_Read(MPU6050_Addr,ACCEL_ZOUT_H);
T_Z= (BUF[5]<<8)|BUF[4];
T_Z/=16.4; //????????Z?á????
// BUF[6]=Single_Read(MPU6050_Addr,TEMP_OUT_L);
// BUF[7]=Single_Read(MPU6050_Addr,TEMP_OUT_H);
// T_T=(BUF[7]<<8)|BUF[6];
// T_T = 35+ ((double) (T_T + 13200)) / 280;// ??????????????
}
//********?®??·???????***************************************
void Send_data(uchar axis)
{uchar i;
USART3_SendData(axis);
USART3_SendData(':');
for(i=0;i<4;i++)USART3_SendData(TX_DATA
);
USART3_SendData(' ');
USART3_SendData(' ');
}
/*
********************************************************************************
** ???????? ?? main(void)
** ???????? ?? ?÷????
** ?? ?? ?? ??
** ?? ?? ?? ??
** ·? ?? ?? ??
********************************************************************************
*/
int main(void)
{
RCC_Configuration(); //????RCC
GPIO_Configuration(); //????GPIO
USART3_Configuration(); //?????®??
I2C_GPIO_Config(); //????IIC????????
Delayms(10); //???±
Init_MPU6050(); //??????MPU6050
while(1)
{
READ_MPU6050(); //????MPU6050????
DATA_printf(TX_DATA,T_X);//×???X?á????????×é
Send_data('X'); //·???X?á??
DATA_printf(TX_DATA,T_Y);//×???Y?á????????×é
Send_data('Y'); //·???Y?á??
DATA_printf(TX_DATA,T_Z);//×???Z?á????????×é
Send_data('Z'); //·???Z?á??
// DATA_printf(TX_DATA,T_T);//×???????????????×é
// Send_data('T'); //·???????????
USART3_SendData(0X0D); //????
USART3_SendData(0X0A); //????
Delayms(5); //???±
}
}
/*************?á??***************/
请大家帮我提下意见,看看哪里可能出问题
楼主的可以了吗?
一周热门 更多>