/******************************************************************************
* [url=home.php?mod=space&uid=288409]@file[/url] main.c
* [url=home.php?mod=space&uid=895143]@version[/url] V1.00
* $Revision: 2 $
* $Date: 14/05/14 6:35p $
* [url=home.php?mod=space&uid=247401]@brief[/url] MPU9250 project for Nano100 series MCU
*
* @note
* Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include <stdio.h>
#include "Nano100Series.h"
#include "NuEdu-Basic01.h"
#include "CJMCU116.h"
#include "MPU9250.h"
uint8_t BUF[10];
int16_t T_X, T_Y, T_Z, T_T;
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
void SYS_Init(void)
{
/* Unlock protected registers */
SYS_UnlockReg();
/* Enable External XTAL (4~24 MHz) */
//CLK->PWRCTL |= CLK_PWRCTL_HXT_EN_Msk;
CLK->PWRCTL |= CLK_PWRCTL_HIRC_EN_Msk;
/* Waiting for 12MHz clock ready */
//while((CLK->CLKSTATUS & CLK_CLKSTATUS_HXT_STB_Msk) == 0){};
while ((CLK->CLKSTATUS & CLK_CLKSTATUS_HIRC_STB_Msk) == 0) {};
/* Switch PLL clock source to XTAL */
//CLK->PLLCTL &= CLK_PLLCTL_PLL_SRC_HXT;
CLK->PLLCTL &= CLK_PLLCTL_PLL_SRC_HIRC;
/* Set PLL to power down mode and PLL_STB bit in CLKSTATUS register will be cleared by hardware.*/
CLK->PLLCTL &= CLK_PLLCTL_PD_Msk;
/* Set PLL frequency 84 MHz */
CLK->PLLCTL |= 0x0218;
/* Waiting for clock ready */
while ((CLK->CLKSTATUS & CLK_CLKSTATUS_PLL_STB_Msk) == 0) {};
/* Set HCLK_N = 1 */
CLK->CLKDIV0 = (CLK->CLKDIV0 & ~CLK_CLKDIV0_HCLK_N_Msk) | 0x01;
/* Switch HCLK clock source to PLL */
CLK->CLKSEL0 = CLK_CLKSEL0_HCLK_S_PLL;
/* Update System Core Clock */
/* User can use SystemCoreClockUpdate() to calculate SystemCoreClock. */
SystemCoreClockUpdate();
/* Lock protected registers */
SYS_LockReg();
}
/*---------------------------------------------------------------------------------------------------------*/
/* Init UART1 */
/*---------------------------------------------------------------------------------------------------------*/
void UART1_Init(void)
{
/* Set PC.10 and PC.11 multi-function pins for UART1_RXD, UART1_TXD */
SYS->PC_H_MFP |= (SYS_PC_H_MFP_PC11_MFP_UART1_TX | SYS_PC_H_MFP_PC10_MFP_UART1_RX);
/* Enable UART1 IP clock */
CLK->APBCLK |= CLK_APBCLK_UART1_EN_Msk;
/* Set UART1 IP clock source */
CLK->CLKSEL1 |= CLK_CLKSEL1_UART_S_HXT;
/* Configure UART1 and set UART1 Baudrate */
UART1->BAUD = UART_BAUD_MODE0 | UART_BAUD_MODE0_DIVIDER(FREQ_12MHZ, 115200);
UART1->TLCTL = UART_WORD_LEN_8 | UART_PARITY_NONE | UART_STOP_BIT_1;
}
// Init MPU9250
void Init_MPU9250(void)
{
I2C0_Write(GYRO_ADDRESS, PWR_MGMT_1, 0x00);
I2C0_Write(GYRO_ADDRESS, SMPLRT_DIV, 0x07);
I2C0_Write(GYRO_ADDRESS, CONFIG, 0x06);
I2C0_Write(GYRO_ADDRESS, GYRO_CONFIG, 0x18);
I2C0_Write(GYRO_ADDRESS, ACCEL_CONFIG, 0x01);
}
/**
* @brief Obtaining Accelerometer values (RAW Data)
* @param none
* [url=home.php?mod=space&uid=266161]@return[/url] none
*
* [url=home.php?mod=space&uid=1543424]@Details[/url] Obtaining Accelerometer values
*/
void READ_MPU9250_ACCEL(void)
{
BUF[0] = I2C0_Read(ACCEL_ADDRESS, ACCEL_XOUT_L);
BUF[1] = I2C0_Read(ACCEL_ADDRESS, ACCEL_XOUT_H);
T_X = (BUF[1] << 8) | BUF[0];
T_X /= 164;
BUF[2] = I2C0_Read(ACCEL_ADDRESS, ACCEL_YOUT_L);
BUF[3] = I2C0_Read(ACCEL_ADDRESS, ACCEL_YOUT_H);
T_Y = (BUF[3] << 8) | BUF[2];
T_Y /= 164;
BUF[4] = I2C0_Read(ACCEL_ADDRESS, ACCEL_ZOUT_L);
BUF[5] = I2C0_Read(ACCEL_ADDRESS, ACCEL_ZOUT_H);
T_Z = (BUF[5] << 8) | BUF[4];
T_Z /= 164;
}
/**
* @brief Obtaining gyroscope values (RAW Data)
* @param none
* @return none
*
* @details Obtaining gyroscope values
*/
void READ_MPU9250_GYRO(void)
{
BUF[0] = I2C0_Read(GYRO_ADDRESS, GYRO_XOUT_L);
BUF[1] = I2C0_Read(GYRO_ADDRESS, GYRO_XOUT_H);
T_X = (BUF[1] << 8) | BUF[0];
T_X /= 16.4;
BUF[2] = I2C0_Read(GYRO_ADDRESS, GYRO_YOUT_L);
BUF[3] = I2C0_Read(GYRO_ADDRESS, GYRO_YOUT_H);
T_Y = (BUF[3] << 8) | BUF[2];
T_Y /= 16.4;
BUF[4] = I2C0_Read(GYRO_ADDRESS, GYRO_ZOUT_L);
BUF[5] = I2C0_Read(GYRO_ADDRESS, GYRO_ZOUT_H);
T_Z = (BUF[5] << 8) | BUF[4];
T_Z /= 16.4;
}
/**
* @brief Obtaining Magnetomet values (RAW Data)
* @param none
* @return none
*
* @details Obtaining Magnetomet values
*/
void READ_MPU9250_MAG(void)
{
I2C0_Write(GYRO_ADDRESS, 0x37, 0x02); //turn on Bypass Mode
CLK_SysTickDelay(10000);
I2C0_Write(MAG_ADDRESS, 0x0A, 0x01);
CLK_SysTickDelay(10000);
BUF[0] = I2C0_Read(MAG_ADDRESS, MAG_XOUT_L);
BUF[1] = I2C0_Read(MAG_ADDRESS, MAG_XOUT_H);
T_X = (BUF[1] << 8) | BUF[0];
BUF[2] = I2C0_Read(MAG_ADDRESS, MAG_YOUT_L);
BUF[3] = I2C0_Read(MAG_ADDRESS, MAG_YOUT_H);
T_Y = (BUF[3] << 8) | BUF[2];
BUF[4] = I2C0_Read(MAG_ADDRESS, MAG_ZOUT_L);
BUF[5] = I2C0_Read(MAG_ADDRESS, MAG_ZOUT_H);
T_Z = (BUF[5] << 8) | BUF[4];
}
/*---------------------------------------------------------------------------------------------------------*/
/* MAIN function */
/*---------------------------------------------------------------------------------------------------------*/
int main()
{
/* Initial system */
SYS_Init();
/* Initial UART1 to 115200-8n1 for print message */
UART1_Init();
printf("Hello World.\n");
printf("PLL Clock = %d Hz\n", CLK_GetPLLClockFreq());
printf("Core Clock = %d Hz\n\n", CLK_GetHCLKFreq());
printf("+-------------------------------------------------------+\n");
printf("| Nano100 Series I2C Sample Code with MPU9250 |\n");
printf("+-------------------------------------------------------+\n");
/* Initial I2C */
I2C0_Init(100);
Init_MPU9250(); //Initialization MPU9250
while (1)
{
READ_MPU9250_ACCEL(); // Gravity acceleration 0.01g
printf("ACCEL: X: %4d Y: %4d Z: %4d /0.01 g\n", T_X, T_Y, T_Z);
READ_MPU9250_GYRO(); //angular velocity 1dps
printf("GYRO : X: %4d Y: %4d Z: %4d /1 dps\n", T_X, T_Y, T_Z);
READ_MPU9250_MAG(); //Magnetometer
printf("MAG : X: %4d Y: %4d Z: %4d /0.6 uT\n\n\n", T_X, T_Y, T_Z);
CLK_SysTickDelay(300000);
CLK_SysTickDelay(300000);
CLK_SysTickDelay(300000);
}
}
/*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/