极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11449|回复: 0

[求助]关于用CC2530,mpu6050无线传输数据的一些问题

[复制链接]
发表于 2015-12-15 11:15:34 | 显示全部楼层 |阅读模式
我想用CC2530开发板实现mpu6050数据的读取,并且通过修改Basic RF的无线点灯实验,实现数据的无线传输。。。然而出现了谜之错误……在开始的阶段传输数据比较正常,但是mpu6050的运动并不怎么能引起数据的改变……然后我把发射板的电源拔掉了……居然接收端的串口还可以输出数据!!!!已经纠结好多天了,求大神指点一下,我基本上只改了app_switch和app-light两个函数。
/***********************************************************************************
  Filename: light_switch.c

  Description:  This application function either as a light or a
  switch toggling the ligh. The role of the
  application is chosen in the menu with the joystick at initialisation.

  Push S1 to enter the menu. Choose either switch or
  light and confirm choice with S1.
  Joystick Up: Sends data from switch to light

***********************************************************************************/

/***********************************************************************************
* INCLUDES
*/
#include <hal_lcd.h>
#include <hal_led.h>
#include <hal_joystick.h>
#include <hal_assert.h>
#include <hal_board.h>
#include <hal_int.h>
#include <string.h>
#include "hal_mcu.h"
#include "hal_button.h"
#include "hal_rf.h"
#include "util_lcd.h"
#include "basic_rf.h"
#include "IIC.h"
#include "MPU6050.h"


/***********************************************************************************
* CONSTANTS
*/
// Application parameters
#define RF_CHANNEL                25      // 2.4 GHz RF channel

// BasicRF address definitions
#define PAN_ID                0x2007
#define SWITCH_ADDR           0x2520
#define LIGHT_ADDR            0xBEEF
#define APP_PAYLOAD_LENGTH        64
#define LIGHT_TOGGLE_CMD          0

// Application states
#define IDLE                      0
#define SEND_CMD                  1

// Application role
#define NONE                      0
#define SWITCH                    1
#define LIGHT                     2
#define APP_MODES                 2

/***********************************************************************************
* LOCAL VARIABLES
*/
static uint8 pTxData[APP_PAYLOAD_LENGTH];
static uint8 pRxData[APP_PAYLOAD_LENGTH];
static basicRfCfg_t basicRfConfig;

// Mode menu
static menuItem_t pMenuItems[] =
{
#ifdef ASSY_EXP4618_CC2420
  // Using Softbaugh 7-seg display
  " L S    ", SWITCH,
  " LIGHT  ", LIGHT
#else
  // SRF04EB and SRF05EB
  "Switch",   SWITCH,
  "Light",    LIGHT
#endif
};

static menu_t pMenu =
{
  pMenuItems,
  N_ITEMS(pMenuItems)
};


#ifdef SECURITY_CCM
// Security key
static uint8 key[]= {
    0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
    0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
};
#endif

/***********************************************************************************
* LOCAL FUNCTIONS
*/
static void appLight();
static void appSwitch();
static uint8 appSelectMode(void);
void InitUart0(void)
{
    PERCFG = 0x00;           //外设控制寄存器 USART 0的IO位置:0为P0口位置1
    P0SEL = 0x0c;            //P0_2,P0_3用作串口(外设功能)
    P2DIR &= ~0xC0;          //P0优先作为UART0
   
    U0CSR |= 0x80;           //设置为UART方式
    U0GCR |= 11;                                       
    U0BAUD |= 216;           //波特率设为115200
    UTX0IF = 0;              //UART0 TX中断标志初始置位0
    U0CSR |= 0x40;           //允许接收
    IEN0 |= 0x84;            //开总中断允许接收中断  
}

void Uart0SendString(char *Data, int len)
{
  uint i;
  for(i=0; i<len; i++)
  {
    U0DBUF = *Data++;
    while(UTX0IF == 0);
    UTX0IF = 0;
  }
}
void DelayMS(uint msec)
{
    uint i,j;
   
    for (i=0; i<msec; i++)
        for (j=0; j<535; j++);
}
void InitClockTo32M(void)
{   
    CLKCONCMD &= ~0x40;              //设置系统时钟源为 32MHZ晶振
    while(CLKCONSTA & 0x40);         //等待晶振稳定
    CLKCONCMD &= ~0x47;              //设置系统主时钟频率为 32MHZ
}

/***********************************************************************************
* @fn          appLight
*
* @brief       Application code for light application. Puts MCU in endless
*              loop waiting for user input from joystick.
*
* @param       basicRfConfig - file scope variable. Basic RF configuration data
*              pRxData - file scope variable. Pointer to buffer for RX data
*
* @return      none
*/
static void appLight()
{
  //  halLcdWriteLine(HAL_LCD_LINE_1, "Light");
   // halLcdWriteLine(HAL_LCD_LINE_2, "Ready");
   
#ifdef ASSY_EXP4618_CC2420
    halLcdClearLine(1);
    halLcdWriteSymbol(HAL_LCD_SYMBOL_RX, 1);
#endif

    // Initialize BasicRF
    basicRfConfig.myAddr = LIGHT_ADDR;
    if(basicRfInit(&basicRfConfig)==FAILED) {
      HAL_ASSERT(FALSE);
    }
    basicRfReceiveOn();
    InitUart0();
    // Main loop
    while (TRUE) {
        while(!basicRfPacketIsReady());

        if(basicRfReceive(pRxData, APP_PAYLOAD_LENGTH, NULL)>0) {
            //if(pRxData[0] == 'a') {
                halLedToggle(1);
                Uart0SendString(pRxData, strlen(pRxData));
                DelayMS(2000);
               
            //}
        }
    }
}


/***********************************************************************************
* @fn          appSwitch
*
* @brief       Application code for switch application. Puts MCU in
*              endless loop to wait for commands from from switch
*
* @param       basicRfConfig - file scope variable. Basic RF configuration data
*              pTxData - file scope variable. Pointer to buffer for TX
*              payload
*              appState - file scope variable. Holds application state
*
* @return      none
*/
static void appSwitch()
{
//   halLcdWriteLine(HAL_LCD_LINE_1, "Switch");
  //  halLcdWriteLine(HAL_LCD_LINE_2, "Joystick Push");
  //  halLcdWriteLine(HAL_LCD_LINE_3, "Send Command");
#ifdef ASSY_EXP4618_CC2420
    halLcdClearLine(1);
    halLcdWriteSymbol(HAL_LCD_SYMBOL_TX, 1);
#endif

    //pTxData[0] = LIGHT_TOGGLE_CMD;
    //pTxData[0]='a';
    // Initialize BasicRF
    basicRfConfig.myAddr = SWITCH_ADDR;
    if(basicRfInit(&basicRfConfig)==FAILED) {
      HAL_ASSERT(FALSE);
    }

    // Keep Receiver off when not needed to save power
    basicRfReceiveOff();
    int16_t ax, ay, az;
    float x,y,z;
    int16_t gx, gy, gz;
    float ggx,ggy,ggz;
    int16_t temperature;
    float f_temperature;  
    char str[9]="M0:";   
    unsigned char tempV = 0;
    InitClockTo32M();                      //设置系统时钟源
    IIC_Init();                     //IIC初始化
    char strTemp[64];
   
     while(1)
    {      
        tempV = MPU6050_GetDeviceID();
        //这个值如果打印为  [WHOE AM I]0x68, 那么表示已经正确辨认到mpu6050了
        sprintf(strTemp, "[WHOE AM I ?] chip ID = 0x%02x(if = 0x68, is OK)\n", tempV);  
        //pTxData[0]='a';
        for(int i=0;i<64;i++)
             pTxData[i]=strTemp[i];
        basicRfSendPacket(LIGHT_ADDR, pTxData, APP_PAYLOAD_LENGTH);

            // Put MCU to sleep. It will wake up on joystick interrupt
         halIntOff();
         halMcuSetLowPowerMode(HAL_MCU_LPM_3); // Will turn on global
            // interrupt enable
         halIntOn();
        
        if(tempV == MPU6050_ADDRESS_AD0_LOW)          //测试是否 mpu6050 已经读到
        {           
            MPU6050_Initialize();                   //初始化mpu6050
             while((tempV = MPU6050_GetDeviceID()) == MPU6050_ADDRESS_AD0_LOW)   //连续测试是否 mpu6050 已经读到
            {
                //读取mpu6050的实时数据           
                MPU6050_GetRawAccelGyro(&ax, &ay, &az, &temperature, &gx, &gy, &gz);
                //转换成温度单位为度的数据
                x=ax/16384.00;
                y=ay/16384.00;
                z=az/16384.00;
                ggx=gx/131.00;
                ggy=gy/131.00;
                ggz=gz/131.00;
                //x = -12;
                //x =1.2;
               // f_temperature = (float)temperature/340.0 + 36.53;               
                //sprintf(strTemp, "[%f %f %f] : [%f %f %f]\n", \
                    x, y, z, ggx, ggy, ggz);

               sprintf(strTemp, "[%f %f %f]: [%f %f %f]\n", \
           x, y, z, ggx, ggy, ggz);
                //pTxData[0]='a';
                 //sprintf(strTemp, "[%f %f %f] : [%7d = %.02fC] : [%f %f %f]\n", \
                    x, y, z, temperature, f_temperature, ggx, ggy, ggz);
                for(int i=0;i<64;i++)
                    pTxData[i]=strTemp[i];
                //串口打印该数据
                 basicRfSendPacket(LIGHT_ADDR, pTxData, APP_PAYLOAD_LENGTH);

                 // Put MCU to sleep. It will wake up on joystick interrupt
                 halIntOff();
                 halMcuSetLowPowerMode(HAL_MCU_LPM_3); // Will turn on global
                 // interrupt enable
                 halIntOn();
                 DelayMS(2000);                       //延时函数使用定时器方式               
            }            
        }
        else
        {
            //mpu6050连接失败
            sprintf(strTemp, "MPU6050_Initialize FAIL");
            //串口打印该数据
            Uart0SendString(strTemp, strlen(strTemp));               
        }
        
        DelayMS(2000);  

   
      //  if( halJoystickPushed() ) {  //bu qiujie tech
     /* if(halButtonPushed()==HAL_BUTTON_1){
              
            basicRfSendPacket(LIGHT_ADDR, pTxData, APP_PAYLOAD_LENGTH);

            // Put MCU to sleep. It will wake up on joystick interrupt
            halIntOff();
            halMcuSetLowPowerMode(HAL_MCU_LPM_3); // Will turn on global
            // interrupt enable
            halIntOn();*/

        }
   
}


/***********************************************************************************
* @fn          main
*
* @brief       This is the main entry of the "Light Switch" application.
*              After the application modes are chosen the switch can
*              send toggle commands to a light device.
*
* @param       basicRfConfig - file scope variable. Basic RF configuration
*              data
*              appState - file scope variable. Holds application state
*
* @return      none
*/
void main(void)
{
    uint8 appMode = NONE;

    // Config basicRF
    basicRfConfig.panId = PAN_ID;
    basicRfConfig.channel = RF_CHANNEL;
    basicRfConfig.ackRequest = TRUE;
#ifdef SECURITY_CCM
    basicRfConfig.securityKey = key;
#endif

    // Initalise board peripherals
    halBoardInit();
  //  halJoystickInit();//BY QIUJIE

    // Initalise hal_rf
    if(halRfInit()==FAILED) {
      HAL_ASSERT(FALSE);
    }

    // Indicate that device is powered
    halLedSet(1);

    // Print Logo and splash screen on LCD
   // utilPrintLogo("Light Switch");

    // Wait for user to press S1 to enter menu
  //  while (halButtonPushed()!=HAL_BUTTON_1);
  //  halMcuWaitMs(350);
  //  halLcdClear();

    // Set application role
   // appMode = appSelectMode();
   // halLcdClear();
    // appMode =  SWITCH;
    // Transmitter application
  //  if(appMode == SWITCH) {
        // No return from here
   
   
    //注:函数appSwitch()和appLight()只能打开一个
   
    //作为开关板打开此函数(appSwitch)
       appSwitch();
   
    //被点灯的板打开此函数(appLight)
       //appLight();
        
        
        
  //  }
    // Receiver application
  //  else if(appMode == LIGHT) {
        // No return from here
   
  //  }
    // Role is undefined. This code should not be reached
  //  HAL_ASSERT(FALSE);
}


/****************************************************************************************
* @fn          appSelectMode
*
* @brief       Select application mode
*
* @param       none
*
* @return      uint8 - Application mode chosen
*/
static uint8 appSelectMode(void)
{
    halLcdWriteLine(1, "Device Mode: ");

    return utilMenuSelect(&pMenu);
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-18 19:23 , Processed in 0.047877 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表