CC2538之TinyOS例程实验:5-Adc_Temp片内温度读取实验 TinyOS如何直接使用C文件
2015-12-30 09:59
537 查看
使用过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文件:
#ifndef CC2538_TEMP_SENSOR_H_ #define CC2538_TEMP_SENSOR_H_ #define CONST 0.58134 //(VREF / 2047) = (1190 / 2047), VREF from Datasheet #define OFFSET_DATASHEET_25C 827 // 1422*CONST, from Datasheet #define TEMP_COEFF (CONST * 4.2) // From Datasheet #define OFFSET_0C (OFFSET_DATASHEET_25C - (25 * TEMP_COEFF)) uint16_t Get_CC2538_Temp(void); #endif
cc2538_temp_sensor.c文件:
#include <stdbool.h> #include <stdint.h> #include <stdio.h> #include "hw_memmap.h" #include "gpio.h" #include "hw_ioc.h" #include "ioc.h" #include "interrupt.h" #include "adc.h" #include "sys_ctrl.h" #include "hw_sys_ctrl.h" #include "systick.h" #include "hw_rfcore_xreg.h" #include "hw_cctest.h" #include "cc2538_temp_sensor.h" uint16_t Get_CC2538_Temp(void){ uint16_t ui16Dummy; // // Enable RF Core (needed to enable temp sensor) // SysCtrlPeripheralEnable(SYS_CTRL_PERIPH_RFC); // // Connect temp sensor to ADC // HWREG(CCTEST_TR0) |= CCTEST_TR0_ADCTM; // // Enable the temperature sensor // HWREG(RFCORE_XREG_ATEST) = 0x01; // // Configure ADC, Internal reference, 512 decimation rate (12bit) // SOCADCSingleConfigure(SOCADC_12_BIT, SOCADC_REF_INTERNAL); // // Trigger single conversion on internal temp sensor // SOCADCSingleStart(SOCADC_TEMP_SENS); // // Wait until conversion is completed // while(!SOCADCEndOfCOnversionGet()){ } // // Get data and shift down based on decimation rate // ui16Dummy = SOCADCDataGet() >> SOCADC_12_BIT_RSHIFT; return ui16Dummy; }2,编写TinyOS例程:Adc_TempAppC.nc,Adc_TempC.nc,makefile文件:
makefile文件:
COMPONENT=Adc_TempAppC CFLAGS += -DUSE_TIMER_HANDLER CFLAGS += -DNOT_USE_PRINTFC_BUT_USE_PRINT include $(MAKERULES)
Adc_TempAppC.nc文件
configuration Adc_TempAppC { } implementation { components MainC, Adc_TempC; components new TimerMilliC() as Timer1; Adc_TempC -> MainC.Boot; Adc_TempC.Timer1 -> Timer1; }
Adc_TempC.nc文件:
/******************************************************************* *实验附加----CC2538片内温度读取 *节点需求数1 *编译命令make cc2538cb ********************************************************************/ #include "Timer.h" #include "printf.h" #include "cc2538_temp_sensor.h" module Adc_TempC{ uses interface Timer<TMilli> as Timer1; uses interface Boot; } implementation{ task void time1_Task(); uint16_t ui16Dummy=0; /*************************************************** *启动事件 ****************************************************/ event void Boot.booted(){ /**开启一秒的周期性定时器(单位毫秒) Timer1**/ call Timer1.startPeriodic( 1000 ); } /*************************************************** *任务time1_Task ****************************************************/ task void time1_Task(){ double dOutputVoltage; ui16Dummy = Get_CC2538_Temp(); printf("ADC raw readout: %d\n", ui16Dummy); //温度计算 dOutputVoltage = ui16Dummy * CONST; dOutputVoltage = ((dOutputVoltage - OFFSET_0C) / TEMP_COEFF); ui16Dummy = dOutputVoltage * 10; printf("Temperature: %d", ui16Dummy/10); printf(".%d", ui16Dummy%10); printf(" C\n"); } /*************************************************** *Timer1定时时间到事件 ****************************************************/ event void Timer1.fired(){ /****提交time1_Task任务***/ post time1_Task(); } }
大家可以看到Adc_TempC.nc文件中
#include "cc2538_temp_sensor.h" ui16Dummy = Get_CC2538_Temp();
这些不就是直接使用了C文件了吗?nesC只是C的方言,相信即使没有nesC语言基础的朋友应该也能大概看懂代码干了什么;就是1秒采集一次片内温度然后串口打印
至此可以引申如果是需要给TinyOS引入其他的传感器呢,呵呵,其实就是拿来厂家提供的C源码直接
使用即可,不用去写成nesC;
相关文章推荐
- servlet回顾
- Base64算法与多版本加密技术
- 改善记忆的方法
- 启动图片
- 多个枚举的判断
- Swift-手势识别
- EasyUI datagrid detailview
- Debian 创始人在 Twitter 上宣称自杀!并已删除 Twitter 账号
- JS模板引擎-腾讯artTemplate 简洁语法例子
- 多渠道打包
- <LeetCode OJ> Merge Sorted Array【88】
- windows10桌面崩溃问题
- C#实现http协议下的多线程文件传输
- C# 条形码操作【源码下载】
- 图像卷积与滤波的一些知识点
- 分享PHP守护进程类
- 获取无线的名称
- 获取Java项目的绝对根地址
- 错误:java.net.SocketException: Unrecognized Windows Sockets error: 0: JVM_Bind
- iOS中block的定义和使用