📄 wendu.txt
字号:
*
* @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 )
{
}
/******************************************************************************
* @fn my_StartReporting
*
* @brief Starts the process to periodically report sensor readings
*
* @param
*
* @return none
*/
void myApp_StartReporting( void )
{
osal_start_timerEx( sapi_TaskID, MY_REPORT_TEMP_EVT, myTempReportPeriod );
osal_start_timerEx( sapi_TaskID, MY_REPORT_BATT_EVT, myBatteryCheckPeriod );
HalLedSet( HAL_LED_1, HAL_LED_MODE_ON );
}
/******************************************************************************
* @fn my_StopReporting
*
* @brief Stops the process to periodically report sensor readings
*
* @param
*
* @return none
*/
void myApp_StopReporting( void )
{
osal_stop_timerEx( sapi_TaskID, MY_REPORT_TEMP_EVT );
osal_stop_timerEx( sapi_TaskID, MY_REPORT_BATT_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_TEMP 0x0e /* Temperature sensor */
uint8 myApp_ReadBattery( void )
{
#if defined HAL_MCU_CC2430
uint16 value;
/* Clear ADC interrupt flag */
ADCIF = 0;
ADCCON3 = (HAL_ADC_REF_125V | HAL_ADC_DEC_128 | HAL_ADC_CHN_VDD3);
/* Wait for the conversion to finish */
while ( !ADCIF );
/* Get the result */
value = ADCL;
value |= ((uint16) ADCH) << 8;
/*
* value now contains measurement of Vdd/3
* 0 indicates 0V and 32767 indicates 1.25V
* voltage = (value*3*1.25)/32767 volts
* we will multiply by this by 10 to allow units of 0.1 volts
*/
value = value >> 6; // divide first by 2^6
value = value * 37.5;
value = value >> 9; // ...and later by 2^9...to prevent overflow during multiplication
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
*/
uint16 myApp_ReadTemperature( void )
{
#if defined HAL_MCU_CC2430
uint16 value=readmain();
return value;
#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
}
//读温湿度数据
unsigned char chars_measure(unsigned char *p_value,unsigned char *p_checksum,unsigned char mode)
{
unsigned char error=0;
unsigned int i,j;
//*p_value=0;
//*(p_value+1)=0;
//*p_checksum=0;
s_transstart(); //传输开始
switch(mode)
{
case MEASURE_TEMP:
error+=s_write_byte(MEASURE_TEMP);break;
case MEASURE_HUMI:
error+=s_write_byte(MEASURE_HUMI);break;
default:break;
}
for(i=0;i<65535;i++)
for(j=0;j<20;j++)
if(DATA==0) break;
if(DATA) error+=1;
*(p_value)=s_read_byte(ACK);
*(p_value+1) = s_read_byte(ACK);
*p_checksum = s_read_byte(noACK);
return error;
}
//温湿度值标度变换及温度补偿
void calc_sth15(float *p_humidity,float *p_temperature)
{
const float c1=-4.0;
const float c2=0.0405;
const float c3=-0.0000028;
const float t1=-0.01;
const float t2=-0.00008;
float rh=*p_humidity;
float t=*p_temperature;
float rh_lin;
float trh_ture;
float t_c;
t_c=t*0.01-40;
rh_lin=c3*rh*rh+c2*rh+c1;
trh_ture=(t_c-25)*(t1+t2*rh)+rh_lin;
*p_temperature=t_c;
*p_humidity=trh_ture;
}
//从相对温度和湿度计算露点
/*char calc_dewpoint(float h,float t)
{
float logex,dew_point;
logex=0.66077+7.5*t/(237.3+t)+(log10(h)-2);
dew_point=(logex-0.66077)*237.3/(0.66077+7.5-logex);
return dew_point;
}*/
//传输启动函数
void s_transstart(void)
{
release_data_1();Delay_us(1);
set_sck_0();Delay_us(1);
set_sck_1();Delay_us(5);
set_data_0();Delay_us(1);
set_sck_0();Delay_us(1);
set_sck_1();Delay_us(5);
release_data_1();Delay_us(1);
set_sck_0();Delay_us(1);
}
//写命令函数
unsigned char s_write_byte(unsigned char cammand)
{
unsigned char i,error1=0;
for(i=0x80;i>0;i/=2)
{
if(cammand&i) {release_data_1();Delay_us(1);}
else {set_data_0();Delay_us(1);}
set_sck_1();Delay_us(5);
set_sck_0();Delay_us(1);
}
release_data_1();Delay_us(1);
set_sck_1();Delay_us(5);
error1=DATA;
set_sck_0();Delay_us(1);
return error1;
}
//读一个字节数据函数
unsigned char s_read_byte(unsigned char ack)
{
unsigned char i;
unsigned char value=0;
release_data_1();
for(i=0x80;i>0;i/=2)
{
set_sck_1();Delay_us(5);
if(DATA)
value=(value|i);
set_sck_0();Delay_us(1);
}
if(ack==0) {release_data_1();Delay_us(1);}
else
{
set_data_0();Delay_us(1);
set_sck_1();Delay_us(5);
set_sck_0();Delay_us(1);
release_data_1();Delay_us(1);
}
return value;
}
/*void Delay(unsigned char n)
{
unsigned char i;
unsigned int j;
for(i = 0; i < n; i++)
for(j = 1; j; j++)
;
}*/
void Delay_ms(unsigned int n)//ms延时函数
{
//unsigned char i;
unsigned int j;
unsigned int i;
for(i = 0; i <n; i++)
for(j = 0; j <1316; j++)
;
}
void Delay_us(unsigned int m)
{
unsigned int i;
unsigned int j;
for(i = 0; i <m; i++)
for(j = 0; j <2; j++)
;
}
uint16 readmain(void)
{
P0SEL &= 0x03; //P0 为普通 I/O 口
P1SEL &= ~0x02; //P1 为普通 I/O 口
// unsigned int result=0;
static unsigned char value1=0,checksum=0;
unsigned char err=1;
unsigned char *val,*check;
//static unsigned char sBuf[4];
//unsigned char *temp;
float *h,*t;
val=&value1;check=&checksum;
unsigned int temprature=0,humidity=0;
float hu=0,te=0;
h=&hu;t=&te;
P1DIR |= 0x02;
set_sck_output();
while(err)
{
value1=0;
err=chars_measure(val,check,MEASURE_TEMP);
temprature=(*val)*256+(*(val+1));
value1=0;
err=chars_measure(val,check,MEASURE_HUMI);
humidity=(*val)*256+(*(val+1));
te=(float)(temprature);
hu=(float)(humidity);
calc_sth15(h,t);
}
float number = te;
int whole = (int)number; //(int)永远是截取整数部分
float num=100*(number-((int)(number)));
unsigned int num1=(int)num;
unsigned char *high,*low;
high=(unsigned char *)(&whole);
low=(unsigned char *)(&num1);
*(low+1)=*high;
P1_1 = 0;
Delay_ms(500);
P1_1 = 1;
return (num1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -