Home > other >  Why do the compass output Angle has been unchanged, the man help me answer the card a few days
Why do the compass output Angle has been unchanged, the man help me answer the card a few days

Time:09-18

# include "math. H"
# include "stdio.h"
# include "is the main. H"
# include "stm32f4xx_hal. H"
# include "math. H"
# define SlaveAddress 0 x3c
# ifdef __GNUC__
/* With GCC, small printf (option LD would - & gt; Libraries - & gt; Small print f
Set to 'Yes') calls __io_putchar () */
# define PUTCHAR_PROTOTYPE int __io_putchar int (ch)
# the else
# define PUTCHAR_PROTOTYPE int fputc (int ch, FILE * f)
# endif

/* the USER CODE BEGIN Includes */
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * is displayed in the LCD data take a * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//

/* the USER CODE END Includes */
/* Private variables -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - */
I2C_HandleTypeDef hi2c1;

UART_HandleTypeDef huart2;

USER CODE BEGIN PV/* */
/* Private variables -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - */
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. Write a character to the EVAL_COM1 and Loop until the end of transmission */
//HAL_StatusTypeDef HAL_UART_Transmit (UART_HandleTypeDef * huart, uint8_t * pData, uint16_t Size, uint32_t Timeout)
HAL_UART_Transmit (& amp; Huart2, (uint8_t *) & amp; Ch. 1, 10);

return ch;
}
USER CODE END PV/* */

/* Private function as -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - */
Void SystemClock_Config (void);
The static void MX_GPIO_Init (void);
The static void MX_I2C1_Init (void);
The static void MX_USART2_UART_Init (void);

/* the USER CODE BEGIN PFP */
/* Private function as -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - */

/* the USER CODE END PFP */

USER CODE BEGIN 0/* */

/* the USER CODE END 0 */

/* *
* @ brief, The application entry point.
*
* @ retval None
*/
Int main (void)
{
USER CODE BEGIN 1/* */

/* 1 */USER CODE END

/* MCU Configuration -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - */

/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init ();

USER CODE BEGIN Init/* */

USER CODE END Init/* */

/* Configure the system clock */
SystemClock_Config ();

/* the USER CODE BEGIN SysInit */

/* the USER CODE END SysInit */

/* the Initialize all configured peripherals */
MX_GPIO_Init ();
MX_I2C1_Init ();
MX_USART2_UART_Init ();
/* the USER CODE BEGIN 2 */
Uint16_t x, y, z;
Double Angle;

Uint8_t r_data, data [7], s=0 x00;
//Mem_Write (I2C_HandleTypeDef * hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t * pData, uint16_t Size, uint32_t Timeout)
HAL_I2C_Mem_Write (& amp; X3c hi2c1, 0, 0 x00, 8, & amp; S, 1, 10);

/* 2 */USER CODE END

Infinite loop/* */
/* the USER CODE BEGIN WHILE */
While (1)
{
Uint16_t address=0 x00;
HAL_Delay (50);
for (int i=0; i <6; I++)
{
//Mem_Read (I2C_HandleTypeDef * hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t * pData, uint16_t Size, uint32_t Timeout)
HAL_I2C_Mem_Read (& amp; X3c hi2c1, 0, 0 x00, 8, & amp; R_data, 1, 10);
Data [I]=r_data;
Address=address + 1;
HAL_Delay (50);
}
X=data [0];
X=x<8;
X=x | data [1];
Y=data [2];
Y=y<8;
Y=y | data [3].
Z=data [4];
Z=z<8;
Z=z | data [5];
If (x> 32768)
X=- (0 XFFFF - x + 1);
If (z> 32768)
Z=- (0 XFFFF - z + 1);
If (y> 32768)
Y=- (0 XFFFF - y + 1);
If (x<0)//the data here the original poster read the occasional negative integer x, and y is always negative integer
X=- x;
Angle=(atan2 ((double), y (double) x) * (180/3.14159265) + 180);
HAL_Delay (10);
Printf (" Angle is: % 3 lf \ n ", Angle).
HAL_Delay (50);
The return Angle;

CodePudding user response:

Program is running without problem, is that an error in the address, devadress refers to the address of the slave, the memadress refers to what things again, stm401re compass

CodePudding user response:

Slowly to learn by yourself

CodePudding user response:

Program too much, he couldn't see a problem, suggest you print a few more variables, see no change, other data is related to the Angle of the variables are printed, if don't change, just to prove this chip may not work
  • Related