⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simplesensor.c

📁 CC2430中A_D转换程序.rar
💻 C
📖 第 1 页 / 共 2 页
字号:
 * @param       commandId - The command ID of the binding being confirmed.
 *              status - The status of the bind operation.
 *
 * @return      none
 */
void zb_BindConfirm( uint16 commandId, uint8 status )
{

  if ( ( status == ZB_SUCCESS ) && ( myAppState == APP_START ) )
  {
    myAppState = APP_BOUND;

    //Start reporting sensor values
     myApp_StartReporting();
     //UartTX_Send_String("find a collect11!\n");
  }
  else
  {
    // Continue to discover a collector
    osal_start_timer( MY_FIND_COLLECTOR_EVT, myBindRetryDelay );
	//UartTX_Send_String("Bind  a collect failed !\n");
  }
}
/******************************************************************************
 * @fn          zb_AllowBindConfirm
 *
 * @brief       Indicates when another device attempted to bind to this device
 *
 * @param
 *
 * @return      none
 */
void zb_AllowBindConfirm( uint16 source )
{
}
/******************************************************************************
 * @fn          zb_FindDeviceConfirm
 *
 * @brief       The zb_FindDeviceConfirm callback function is called by the
 *              ZigBee stack when a find device operation completes.
 *
 * @param       searchType - The type of search that was performed.
 *              searchKey - Value that the search was executed on.
 *              result - The result of the search.
 *
 * @return      none
 */
void zb_FindDeviceConfirm( uint8 searchType, uint8 *searchKey, uint8 *result )
{
}
/******************************************************************************
 * @fn          zb_ReceiveDataIndication
 *
 * @brief       The zb_ReceiveDataIndication callback function is called
 *              asynchronously by the ZigBee stack to notify the application
 *              when data is received from a peer device.
 *
 * @param       source - The short address of the peer device that sent the data
 *              command - The commandId associated with the data
 *              len - The number of bytes in the pData parameter
 *              pData - The data sent by the peer device
 *
 * @return      none
 */
void zb_ReceiveDataIndication( uint16 source, uint16 command, uint16 len, uint8 *pData  )
{

  uint8 buf[64];
  uint8 saddr[2];
  uint8 *pBuf;
  uint8 *ieeeAddr;
  uint16  time,tmpLen;
  int cmdlen;
  int i;

         if (command == ENDDEV_REQUEST_CMD_ID)
         	{
         	       if ( pData[0] == TIME_REQUEST )
         	       	{

                            // Received report from a sensor
                               time = (pData[1]*256+pData[2]);				  			
                               myTempOrignalPeriod=time*1000;
				    myTempReportPeriod=myTempOrignalPeriod;
                               ZDApp_Set_Reporttime( time );
                          /*     osal_memset (buf, 0, 32);                    // X0 = 0
                                tmpLen = (uint8)osal_strlen( (char*)"Temp Time Changing Request:  " );
                                pBuf = osal_memcpy( buf, (char*)"Temp Time Changing Request:  " , tmpLen );
                                *pBuf++=time+'0';
                                *pBuf++ = 's';
			       	*pBuf++ = '\r';
                                *pBuf++ = '\n';
                                *pBuf = '\0';
				UartTX_Send_String(buf);*/
				myApp_StopReporting();
				osal_start_timer( MY_REPORT_TEMP_EVT, 2000 );
         	       	}
                      if ( pData[0] == TEMP_REQUEST )
                      	{
                      	  TEMPE pt;
                           int j;
                          SetEVWriteDire;
                          EV=0;
                          for(j=0;j<10;j++)
                             MicroWait (50000);
                             osal_memset(buf,0,sizeof(buf));
	                      cmdlen=8+1+owb_nCntDevice*2+5;
	                      buf[0]=PRESTR_DATA;
                    	       buf[1]=TEMP_REPORT;
	                     buf[2]=owb_nCntDevice*2+1;// 2个字节的传感器数据和1个字节的温度数据
	                     buf[3]=owb_nCntDevice+1;    //传感器数据和温度数据
	                    // osal_memcpy(buf+4,NLME_GetExtAddr(),8);
	                    ieeeAddr = NLME_GetExtAddr();
                            osal_cpyExtAddr( buf+4, ieeeAddr );
                            buf[12]=myApp_ReadBattery();
				owbMesureConvertBegin();
                            for(i=0;i<owb_nCntDevice;i++){
                            owb_CacheDeviceROM=owb_DeviceROMAdd[i];
	                     owbReadMeasure(&pt,0x00);	
	                     buf[13+2*i]=pt.th;
	                     buf[14+2*i]=pt.tl;
	                 	}
	                     buf[cmdlen-1]=ENDSTR_DATA;	
                          // SendData(buf, 0x0000,cmdlen);
                           zb_SendDataRequest( 0xFFFE, ENDDEV_REPORT_CMD_ID, cmdlen, buf, 0, AF_ACK_REQUEST, 0 );
                      	}
			 if ( pData[0] == ADD_REQUEST )
                      	{
                      	// int j;
                       // uint8 ad[8]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
                         OWBROM owb_ROM;


                             // owbSearchRomAddress();
                              cmdlen=2+(owb_nCntDevice+1)*8+5;
	                      osal_memset(buf,0,sizeof(buf));
	                      buf[0]=PRESTR_DATA;
	                      buf[1]=ADD_REPORT;
	                      buf[2]=(owb_nCntDevice+1)*8+2;// 2个字节的短地址
	                      buf[3]=owb_nCntDevice+1;    //节点地址和传感器地址个数
	                      //osal_memcpy(buf+4,(void *)NLME_GetExtAddr(),8);
	                       ieeeAddr = NLME_GetExtAddr();
                             osal_cpyExtAddr( buf+4, ieeeAddr );
                             saddr[0]=(NLME_GetShortAddr()>>8)&0x00ff;
	                      saddr[1]=(NLME_GetShortAddr())&0x00ff;
	                      osal_memcpy(buf+12,saddr,2);


	                      for(i=0;i<owb_nCntDevice;i++){  //
                             owb_ROM=owb_DeviceROMAdd[i];
	                     osal_memcpy(buf+14+SENSOR_ADDRESS_LEN*i,(void*)&owb_ROM,SENSOR_ADDRESS_LEN);
                           //   osal_memcpy(buf+14+SENSOR_ADDRESS_LEN*i,ad,SENSOR_ADDRESS_LEN);
	               	}
	              	buf[cmdlen-1]=ENDSTR_DATA;	
          //    SendData(buf, 0x0000, cmdlen);
                        EV=1;
                           zb_SendDataRequest( 0xFFFE, ENDDEV_REPORT_CMD_ID, cmdlen, buf, 0, AF_ACK_REQUEST, 0 );}

         	}

}
/******************************************************************************
 * @fn          my_StartReporting
 *
 * @brief       Starts the process to periodically report sensor readings
 *
 * @param
 *
 * @return      none
 */

void myApp_StartReporting( void )
{
  osal_start_timer( MY_REPORT_TEMP_EVT, 2000 );
  osal_start_timer( MY_REPORT_ADD_EVT, myAddressCheckPeriod );
//  HalLedSet( HAL_LED_2, HAL_LED_MODE_ON );

 //升压模块
SetEVWriteDire;
EV=0;

}
/******************************************************************************
 * @fn          my_StopReporting
 *
 * @brief       Stops the process to periodically report sensor readings
 *
 * @param
 *
 * @return      none
 */
void myApp_StopReporting( void )
{
  osal_stop_timer( MY_REPORT_TEMP_EVT );
  osal_stop_timer( MY_REPORT_ADD_EVT );
  //HalLedSet( HAL_LED_1, HAL_LED_MODE_OFF );
}
/******************************************************************************
 * @fn          myApp_ReadBattery
 *
 * @brief       Reports battery sensor reading
 *
 * @param
 *
 * @return
 */
// ADC definitions for CC2430 from the hal_adc.c file
#define HAL_ADC_REF_125V    0x00    /* Internal 1.25V Reference */
#define HAL_ADC_DEC_064     0x00    /* Decimate by 64 : 8-bit resolution */
#define HAL_ADC_DEC_128     0x10    /* Decimate by 128 : 10-bit resolution */
#define HAL_ADC_DEC_512     0x30    /* Decimate by 512 : 14-bit resolution */
#define HAL_ADC_CHN_VDD3    0x0f    /* Input channel: VDD/3 */
#define HAL_ADC_CHN_P01    0x01    /* Input channel: P04 */
#define HAL_ADC_CHN_P04    0x04    /* Input channel: P04 */
#define HAL_ADC_CHN_TEMP    0x0e    /* Temperature sensor */

// Macro for setting/clearing a channel as input of the ADC
// macro will also start the conversion.
#define ADC_SINGLE_CONVERSION(settings) \
   do{ ADCCON3 = settings; }while(0)
#define ADC_ENABLE_CHANNEL(ch)   ADCCFG |=  (0x01<<ch)
#define ADC_DISABLE_CHANNEL(ch)  ADCCFG &= ~(0x01<<ch)
#define ADC_STOP() \
  do { ADCCON1 |= 0x30; } while (0)

// Macro for initiating a single sample in single-conversion mode (ADCCON1.STSEL = 11).
#define ADC_SAMPLE_SINGLE() \
  do { ADC_STOP(); ADCCON1 |= 0x40;  } while (0)
uint8 myApp_ReadBattery( void )
{

#if defined HAL_MCU_CC2430

   uint16 value;
//  float v;
  /* Clear ADC interrupt flag */
  ADCIF = 0;
{
	
  ADC_ENABLE_CHANNEL(4);

  ADC_SINGLE_CONVERSION(HAL_ADC_REF_125V | HAL_ADC_DEC_128 | HAL_ADC_CHN_P04);//HAL_ADC_CHN_VDD3

  ADC_SAMPLE_SINGLE();
  /* Wait for the conversion to finish */
  while ( !ADCIF );

  ADC_DISABLE_CHANNEL(4);
  /* Get the result */
  value = ADCL;
  value |= ((uint16) ADCH) << 8;
  value = value >> 6;   // divide first by 2^6
  value = value * 88.7; //37.5
  value = value >> 9;   // ...and later by 2^9...to prevent overflow during multiplication

  /*
   * value now contains measurement of Vdd/3
   * 0 indicates 0V and 32767 indicates 1.25V
   * voltage = (value*4*1.25)/32767 volts
   * we will multiply by this by 10 to allow units of 0.1 volts
   */


  //    v=value*1.25/8192;
  //    value=(uint16)v;
}

  return value;

#endif    // CC2430

#if defined HAL_MCU_MSP430

  uint16 value;

  ADC12CTL0 = ADC12ON+SHT0_2+REFON;         // Turn on and set up ADC12
  ADC12CTL1 = SHP;                          // Use sampling timer
  ADC12MCTL0 = SREF_1+INCH_11;                      // Vr+=Vref+

  ADC12CTL0 |= ENC | ADC12SC;                   // Start conversion
  while ((ADC12IFG & BIT0)==0);

  value = ADC12MEM0;

  /*
   * value now contains measurement of AVcc/2
   * value is in range 0 to 4095 indicating voltage from 0 to 1.5V
   * voltage = (value*2*1.5)/4095 volts
   * we will multiply by this by 10 to allow units of 0.1 volts
   */

  value = value >> 1;     // value is now in range of 0 to 2048
  value = value * 30;
  value = value >> 11;

  return ( value );

#endif // MSP430

#if defined HAL_MCU_AVR

  // If platform doesnt support a battery sensor, just return random value

  uint8 value;
  value = 20 + ( osal_rand() & 0x000F );
  return ( value );

#endif  // AVR

}
/******************************************************************************
 * @fn          myApp_ReadTemperature
 *
 * @brief       Reports temperature sensor reading
 *
 * @param
 *
 * @return
 */
uint8 myApp_ReadTemperature( void )
{

#if defined HAL_MCU_CC2430

  uint16 value;

  /* Clear ADC interrupt flag */
  ADCIF = 0;

  ADCCON3 = (HAL_ADC_REF_125V | HAL_ADC_DEC_512 | HAL_ADC_CHN_TEMP);

  /* Wait for the conversion to finish */
  while ( !ADCIF );

  /* Get the result */
  value = ADCL;
  value |= ((uint16) ADCH) << 8;

  /*
   * value ranges from 0 to 0x8000 indicating 0V and 1.25V
   * VOLTAGE_AT_TEMP_ZERO = 0.743 V = 19477
   * TEMP_COEFFICIENT = 0.0024 V/C = 62.9 /C
   * These parameters are typical values and need to be calibrated
   * See the datasheet for the appropriate chip for more details
   * also, the math below may not be very accurate
   */
#define VOLTAGE_AT_TEMP_ZERO      19477   // 0.743 V
#define TEMP_COEFFICIENT          64.2    // 0.0024 V/C

  // limit min temp to 0 C
  if ( value < VOLTAGE_AT_TEMP_ZERO )
    value = VOLTAGE_AT_TEMP_ZERO;

  value = value - VOLTAGE_AT_TEMP_ZERO;

  // limit max temp to 99 C
  if ( value > TEMP_COEFFICIENT * 99 )
    value = TEMP_COEFFICIENT * 99;

  return ( (uint8)(value/TEMP_COEFFICIENT) );

#endif  // CC2430

#if defined HAL_MCU_MSP430

  uint16 value;

  ADC12CTL0 = ADC12ON+SHT0_7+REFON;         // Turn on and set up ADC12
  ADC12CTL1 = SHP;                          // Use sampling timer
  ADC12MCTL0 = SREF_1+INCH_10;              // Vr+=Vref+

  ADC12CTL0 |= ENC | ADC12SC;               // Start conversion
  while ((ADC12IFG & BIT0)==0);

  value = ADC12MEM0;

  /*
   * value ranges from 0 to 0x0FFF indicating 0V and 1.5V
   * VOLTAGE_AT_TEMP_ZERO = 0.986 V = 2692
   * TEMP_COEFFICIENT = 0.00355 V/C = 9.69 /C
   * These parameters are typical values and need to be calibrated
   * See the datasheet for the appropriate chip for more details
   * also, the math below is not very accurate
   */

#define VOLTAGE_AT_TEMP_ZERO      2692      // 0.986 V
#define TEMP_COEFFICIENT          9.69      // 0.00355 V/C

  // limit min temp to 0 C
  if ( value < VOLTAGE_AT_TEMP_ZERO )
    value = VOLTAGE_AT_TEMP_ZERO;

  value = value - VOLTAGE_AT_TEMP_ZERO;

  // limit max temp to 99 C
  if ( value > TEMP_COEFFICIENT * 99 )
    value = TEMP_COEFFICIENT * 99;

  return ( (uint8)(value/TEMP_COEFFICIENT) );

#endif // MSP430

#if defined HAL_MCU_AVR

  // If platform doesnt support a temperature sensor, just return random value
  uint8 value;
  value = 20 + ( osal_rand() & 0x000F );
  return ( value );

#endif  // AVR

}

static void  ZDApp_Set_Reporttime( uint16 Reporttime )
{

  osal_nv_write( ZCD_NV_REPORT_TIME, 0, sizeof(Reporttime), (void *)&Reporttime);

}

/*
#pragma vector = ST_VECTOR
__interrupt  void ST_ISR(void)
 {
   STIF = 0;
 }
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -