注册 登录
电子工程世界-论坛 返回首页 EEWORLD首页 频道 EE大学堂 下载中心 Datasheet 专题
dan158185的个人空间 https://home.eeworld.com.cn/space-uid-349284.html [收藏] [复制] [分享] [RSS]
日志

CC2538之TinyOS例程实验:5-Adc_Temp片内温度读取实验 TinyOS如何直接使用C文件

已有 960 次阅读2016-1-5 16:07 |个人分类:CC2538之TinyOS例程| 如何

使用过CC2530的朋友肯定对TI的片内温度不陌生;下面带来2538的片内温度读取;先看实验结果:



代码部分:选择使用TinyOS来实现

此处的实验是向大家展示如何TinyOS如何直接使用C(h)文件,打消大家认为TiinyOS编程只能用nesC的误区
1,温度读取函数 tinyos-main-release_tinyos_2_1_2\tos\chips\cc2538\adc 下的cc2538_temp_sensor.c和cc2538_temp_sensor.h

cc2538_temp_sensor.h文件:

  1. #ifndef CC2538_TEMP_SENSOR_H_  
  2. #define CC2538_TEMP_SENSOR_H_  
  3.   
  4. #define CONST 0.58134 //(VREF / 2047) = (1190 / 2047), VREF from Datasheet  
  5. #define OFFSET_DATASHEET_25C 827 // 1422*CONST, from Datasheet   
  6. #define TEMP_COEFF (CONST * 4.2) // From Datasheet  
  7. #define OFFSET_0C (OFFSET_DATASHEET_25C - (25 * TEMP_COEFF))  
  8. uint16_t  Get_CC2538_Temp(void);  
  9. #endif   

cc2538_temp_sensor.c文件:

  1. #include <stdbool.h>  
  2. #include <stdint.h>  
  3. #include <stdio.h>  
  4. #include "hw_memmap.h"  
  5. #include "gpio.h"  
  6. #include "hw_ioc.h"  
  7. #include "ioc.h"  
  8. #include "interrupt.h"  
  9. #include "adc.h"  
  10. #include "sys_ctrl.h"  
  11. #include "hw_sys_ctrl.h"  
  12. #include "systick.h"  
  13. #include "hw_rfcore_xreg.h"  
  14. #include "hw_cctest.h"  
  15. #include "cc2538_temp_sensor.h"  
  16.   
  17. uint16_t  Get_CC2538_Temp(void){  
  18.     uint16_t ui16Dummy;  
  19.   
  20.     //  
  21.     // Enable RF Core (needed to enable temp sensor)  
  22.     //  
  23.     SysCtrlPeripheralEnable(SYS_CTRL_PERIPH_RFC);  
  24.     //  
  25.     // Connect temp sensor to ADC  
  26.     //  
  27.     HWREG(CCTEST_TR0) |= CCTEST_TR0_ADCTM;  
  28.   
  29.     //  
  30.     // Enable the temperature sensor   
  31.     //  
  32.     HWREG(RFCORE_XREG_ATEST) = 0x01;  
  33.       
  34.     //  
  35.     // Configure ADC, Internal reference, 512 decimation rate (12bit)  
  36.     //  
  37.     SOCADCSingleConfigure(SOCADC_12_BIT, SOCADC_REF_INTERNAL);  
  38.        
  39.    
  40.     //  
  41.     // Trigger single conversion on internal temp sensor  
  42.     //  
  43.     SOCADCSingleStart(SOCADC_TEMP_SENS);  
  44.           
  45.     //  
  46.     // Wait until conversion is completed  
  47.     //  
  48.     while(!SOCADCEndOfCOnversionGet()){  
  49.     }  
  50.   
  51.     //  
  52.     // Get data and shift down based on decimation rate  
  53.     //  
  54.     ui16Dummy = SOCADCDataGet() >> SOCADC_12_BIT_RSHIFT;      
  55.     return ui16Dummy;  
  56.                  
  57.   
  58. }  
2,编写TinyOS例程:Adc_TempAppC.nc,Adc_TempC.nc,makefile文件:


makefile文件:

  1. COMPONENT=Adc_TempAppC  
  2. CFLAGS += -DUSE_TIMER_HANDLER  
  3. CFLAGS += -DNOT_USE_PRINTFC_BUT_USE_PRINT  
  4. include $(MAKERULES)  


Adc_TempAppC.nc文件

  1. configuration Adc_TempAppC  
  2. {  
  3. }  
  4. implementation  
  5. {  
  6.   components MainC, Adc_TempC;   
  7.   components new TimerMilliC() as Timer1;  
  8.     
  9.   Adc_TempC -> MainC.Boot;  
  10.   Adc_TempC.Timer1 -> Timer1;  
  11. }  

Adc_TempC.nc文件:

  1.  /*******************************************************************  
  2.  *实验附加----CC2538片内温度读取  
  3.  *节点需求数1  
  4.  *编译命令make cc2538cb  
  5.  ********************************************************************/  
  6.   
  7. #include "Timer.h"  
  8. #include "printf.h"  
  9. #include "cc2538_temp_sensor.h"  
  10.   
  11. module Adc_TempC{  
  12.    uses interface Timer<TMilli> as Timer1;  
  13.    uses interface Boot;  
  14. }  
  15. implementation{  
  16.   task void time1_Task();  
  17.   uint16_t ui16Dummy=0;  
  18.     
  19.   /***************************************************  
  20.   *启动事件  
  21.   ****************************************************/  
  22.   event void Boot.booted(){  
  23.     /**开启一秒的周期性定时器(单位毫秒)  Timer1**/  
  24.     call Timer1.startPeriodic( 1000 );   
  25.   
  26.   }  
  27.   
  28.   /***************************************************  
  29.   *任务time1_Task  
  30.   ****************************************************/  
  31.   task void time1_Task(){     
  32.       double dOutputVoltage;  
  33.   
  34.         
  35.       ui16Dummy = Get_CC2538_Temp();  
  36.       printf("ADC raw readout: %d\n", ui16Dummy);  
  37.         
  38.       //温度计算  
  39.       dOutputVoltage = ui16Dummy * CONST;         
  40.       dOutputVoltage = ((dOutputVoltage - OFFSET_0C) / TEMP_COEFF);  
  41.       ui16Dummy = dOutputVoltage * 10;  
  42.         
  43.       printf("Temperature: %d", ui16Dummy/10);  
  44.       printf(".%d", ui16Dummy%10);  
  45.       printf(" C\n");  
  46.   }  
  47.     
  48.   /***************************************************  
  49.   *Timer1定时时间到事件  
  50.   ****************************************************/  
  51.   event void Timer1.fired(){  
  52.     /****提交time1_Task任务***/  
  53.     post time1_Task();  
  54.   }  
  55.     
  56. }  


大家可以看到Adc_TempC.nc文件中

  1. #include "cc2538_temp_sensor.h"  
  2. ui16Dummy = Get_CC2538_Temp();  

这些不就是直接使用了C文件了吗?nesC只是C的方言,相信即使没有nesC语言基础的朋友应该也能大概看懂代码干了什么;就是1秒采集一次片内温度然后串口打印


至此可以引申如果是需要给TinyOS引入其他的传感器呢,呵呵,其实就是拿来厂家提供的C源码直接

使用即可,不用去写成nesC;

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

热门文章