#include "math.h"
#include "stdio.h"
#include "main.h"
#include "stm32f4xx_hal.h"
#include "math.h"
#define SlaveAddress 0x3c
#ifdef __GNUC__
/* With GCC, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
/* USER CODE BEGIN Includes */
//******************************顯示在LCD的資料取位********************************//
/* 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(&huart2, (uint8_t*)& ch, 1,10 );
return ch;
}
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
static void MX_USART2_UART_Init(void);
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE END PFP */
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
*
* @retval None
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* 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();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_I2C1_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
uint16_t x, y, z;
double angle;
uint8_t r_data, data[7], s = 0x00;
//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(&hi2c1, 0x3C, 0x00, 8, &s, 1, 10);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
uint16_t address = 0x00;
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(&hi2c1, 0x3C, 0x00, 8, &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 = -(0xFFFF - x + 1);
if(z>32768)
z = -(0xFFFF - z + 1);
if(y>32768)
y = -(0xFFFF - y + 1);
if(x<0) //這里的資料樓主讀出來x偶爾有負整數,而y永遠是負整數
x=-x;
angle= (atan2((double)y,(double)x) * (180 / 3.14159265) + 180);
HAL_Delay(10);
printf("角度為:%.3lf \n", angle);
HAL_Delay(50);
return angle;
uj5u.com熱心網友回復:
程式運行沒有問題,是地址出錯了嗎,devadress是指slave的地址嗎,那memadress又指啥東西,stm401re指南針uj5u.com熱心網友回復:
自己慢慢學吧兄弟uj5u.com熱心網友回復:
程式 太多了, 一時看不出問題, 建議你多列印幾個 變數, 看看 其他的資料有沒變化, 跟角度有關的變數都列印出來。如果都沒變化,就證明這個芯片有可能沒有作業轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/48963.html
標籤:單片機/工控
