case HAL_ADC_RESOLUTION_10: resbits = HAL_ADC_DEC_128; break;
case HAL_ADC_RESOLUTION_12: resbits = HAL_ADC_DEC_256; break;
case HAL_ADC_RESOLUTION_14: default:
resbits = HAL_ADC_DEC_512; break; }
/* read ADCL,ADCH to clear EOC */ tmp = ADCL; tmp = ADCH; /* Setup Sample */ adctemp = ADCCON3; adctemp &= ~(HAL_ADC_CHN_BITS | HAL_ADC_REF_BITS);
adctemp |= channel | resbits | HAL_ADC_REF_VOLT;/* writing to this register starts the extra conversion */ ADCCON3 = adctemp;
/* Wait for the conversion to be done */ while (!(ADCCON1 & HAL_ADC_EOC)); /* Disable channel after done conversion */ ADCCFG &= (adcChannel ^ 0xFF); /* Read the result */
reading = (int16) (ADCL);
reading |= (int16) (ADCH << 8); /* Treat small negative as 0 */ if (reading < 0) reading = 0;
switch (resolution) {
case HAL_ADC_RESOLUTION_8: reading >>= 8; break;
case HAL_ADC_RESOLUTION_10: reading >>= 6; break;
case HAL_ADC_RESOLUTION_12: reading >>= 4; break;
case HAL_ADC_RESOLUTION_14: default:
HAL_ADC_DEC_BITS |
break; } #else
// unused arguments (void) channel; (void) resolution; #endif
return ((uint16)reading); }
4.实验总结
通过串口助手,配置正确的串口号和波特率,填入测试的数据指令,点击发送就可以获取网关返回的读取到节点的数据。
实验三、气压传感器实验
1. 实验内容
1.了解气压采集的原理
2.学习 MPX5010 气压传感器从而掌握气压传感器的原理 3.掌握“ 传感器节点板” 模块的原理和使用方法。 2.实验步骤
同“实验二十四 光照传感器实验 实验步骤第一步至第四步”
第五步:获取和查看大气气压传感器数据用 USB 线连接上 PC 机跟网关,打开“ \\软件工具及驱动\\无线龙 ZigBee 演示软件 V1.11”目录下“无线龙 ZigBee 演示软件 V1.11.exe”软件。通过设备管理器查看对应设备的串口号是多少,如图所示为 COM3。在监控软件中选择“ COM 端口” COM3,波特率: 38400,点击“打开串口”。在 RSS 曲线部分中的下拉选择框中选择节点压力(如下图),鼠标左键单击选中要查看的节点模块。点击“开始”按钮,就可开始显示节点空气压力的曲线了(注:这时“开始”
按钮将变为“关闭”按钮)。为了使显示曲线效果明显,可以通过电扇吹气方式来达到明显效果。点击“关闭”按钮,则曲线停止,但曲线不会消失,这时“关闭”按钮将变为“开始”按钮,这时在点击“开始”按钮会弹出一格对话框,选择“是”则不清空曲线,继续在图上画曲线。选择“否”则清空曲线,再 在图上画曲线。 点击“保存”按钮,可将曲线图以 RSS 格式的数据保存在电脑上,点击“载入”按钮,可以载入保存了曲线数据的 RSS 文件,将曲线显示在曲线图上。 3.实验代码与分析
传感器采集的函数在 void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )中
if((RfRx.RXDATA.HeadCom[1] == 'P') && (RfRx.RXDATA.HeadCom[2] == 'R'))//压力传感器 {
if(SensorNum == 5)//压力传感器板 {
memcpy(RfTx.TxBuf, RfRx.RxBuf, 14); temp = ReadSensorAdc(0);
RfTx.TXDATA.DataBuf[0] = temp/100 + 0x30;
temp = temp0;
RfTx.TXDATA.DataBuf[1] = temp/10 + 0x30; RfTx.TXDATA.DataBuf[2] = temp + 0x30; RfHaveTxDara = 1; } }
//调用子函数 ReadSensorAdc 完成读取
//---------------------------------------------------------- //Read Sensor ADC value //读取 AD 值 //输入:通道名
//返回: 8 位 AD 值
//---------------------------------------------------------- INT8U ReadSensorAdc(INT8U channel) {
INT8U temp;
temp = HalAdcReadSen(channel,HAL_ADC_RESOLUTION_8); return temp; }
通过 ADC 读取大气压力传感器值:
temp = HalAdcReadSen(channel,HAL_ADC_RESOLUTION_8);/读取空气压力传感器值 channel ??气体压力传感器对应的 AD 通道
HAL_ADC_RESOLUTION_8 ??采集分辨率 8Bit
ADC 采 集 子 函 数 在 在 “Components\\hal\\target\\CC2530EB ” 目 录 下 的 “ hal_adc.c”文件
/******************************************************************************
******************** * @fn HalAdcRead *
* @brief Read the ADC based on given channel and resolution *
* @param channel - channel where ADC will be read * @param resolution - the resolution of the value *
* @return 16 bit value of the ADC in offset binary format.
* Note that the ADC is \
*******************************************************************************
*******************/
uint16 HalAdcRead (uint8 channel, uint8 resolution) int16 reading = 0;
#if (HAL_ADC == TRUE) uint8 i, resbits;
uint8 adctemp; volatile uint8 tmp; uint8 adcChannel = 1; /*
* If Analog input channel is AIN0..AIN7, make sure corresponing P0 I/O pin is enabled. The code
* does NOT disable the pin at the end of this function. I think it is better to leave the pin * enabled because the results will be more accurate. Because of the inherent capacitance on the
* pin, it takes time for the voltage on the pin to charge up to its steady-state level. If
* HalAdcRead() has to turn on the pin for every conversion, the results may show a lower voltage
* than actuality because the pin did not have time to fully charge. */
if (channel < 8) {
for (i=0; i < channel; i++) {
adcChannel <<= 1; } }
/* Enable channel */
ADCCFG |= adcChannel;
/* Convert resolution to decimation rate */ switch (resolution) {
case HAL_ADC_RESOLUTION_8: resbits = HAL_ADC_DEC_064; break;
case HAL_ADC_RESOLUTION_10: resbits = HAL_ADC_DEC_128; break;
case HAL_ADC_RESOLUTION_12: resbits = HAL_ADC_DEC_256; break;
case HAL_ADC_RESOLUTION_14: default:
resbits = HAL_ADC_DEC_512; break; }
/* read ADCL,ADCH to clear EOC */ tmp = ADCL; tmp = ADCH; /* Setup Sample */