|
1. Connecting the upper and lower levels In wireless sensor networks, it is important to convert the analog values of the sensors into digital quantities for easy transmission and processing. ADC (Analog-to-Digital Converter) is used to complete this conversion. In the previous section, we introduced serial port communication between the CC2430 and a PC. CC2430 has a temperature sensor embedded inside, and this section will implement a simple experiment on on-chip temperature monitoring based on the previous section: use the ADC to convert the voltage value of the on-chip temperature sensor into a digital quantity, use the formula to calculate the temperature value, and then transmit the temperature value to the PC through the serial port and display it. 2. ADC single sampling(1) Introduction to the experimentThe ADC is used to convert the temperature value of the CC2430 on-chip temperature sensor, and the temperature value is sent to the PC through the serial port and displayed. (2) Program flow chart
(3) Experimental source code and analysis/*
Experimental description: The in-chip temperature acquisition experiment sends the data to the PC through serial port 0
*/
#include
#define led1 P1_0
#define led2 P1_1
#define led3 P1_2
#define led4 P1_3
/*32M晶振初始化
-------------------------------------------------------*/
voidxtal_init(void)
{ SLEEP &= ~0x04; //都上电
while(! (SLEEP &0x40)); //晶体振荡器开启且稳定 CLKCON &= ~0x47; Choose a 32MHz crystal oscillator SLEEP |=0x04;
}
/*LED灯初始化
-------------------------------------------------------*/
voidled_init(void)
{ P1SEL =0x00; P1 is the normal I/O port P1DIR |=0x0F; P1.0 P1.1 P1.2 P1.3 output
led1 =1; led2 =1; led3 =1; led4 =1;
}
/*UART0初始化
-------------------------------------------------------*/
void Uart0Init(unsigned charStopBits,unsigned charParity)
{ P0SEL |= 0x0C; //初始化UART0端口 PERCFG&= ~0x01; //选择UART0为可选位置一 U0CSR =0xC0; set to UART mode and enable the acceptor U0GCR =11; U0BAUD =216; //设置UART0波特率为115200bps U0UCR |= StopBits| Parity; //设置停止位与奇偶校验
}
/*UART0发送字符
-------------------------------------------------------*/
void Uart0Send(unsigned chardata)
{
while(U0CSR&0x01); //等待UART空闲时发送数据 U0DBUF = data;
}
/*UART0发送字符串
-------------------------------------------------------*/
voidUart0SendString(unsigned char*s)
{
while(*s !=0) Uart0Send(*s++);
}
/*UART0接收数据
-------------------------------------------------------*/
unsigned charUart0Receive(void)
{
unsigned chardata;
while(! (U0CSR&0x04));//查询是否收到数据,否则继续等待 data=U0DBUF;
returndata;
}
/*延时函数
-------------------------------------------------------*/
voidDelay(unsigned intn)
{
unsigned inti;
for(i=0; i<n; i++);
for(i=0; i<n; i++);
for(i=0; i<n; i++);
for(i=0; i<n; i++);
for(i=0; i<n; i++);
}
/*得到实际温度值
-------------------------------------------------------*/
floatgetTemperature(void)
{
unsigned int value;
ADCCON3 = (0x3E); //选择1.25V为参考电压;14位分辨率;对片内温度传感器采样
ADCCON1 |=0x30; //选择ADC的启动模式为手动 ADCCON1 |=0x40; Initiate AD conversions
while(! (ADCCON1 &0x80)); //等待ADC转化结束
value = ADCL >>2; value |= (ADCH <<6); //取得最终转化结果,存入value中
returnvalue*0.06229-311.43; //根据公式计算出温度值
}
/*主函数
-------------------------------------------------------*/
voidmain(void)
{
chari;
floatavgTemp;
unsigned charoutput[]="";
xtal_init(); led_init();
led1 =0;
Uart0Init(0x00, 0x00); //初始化串口:无奇偶校验,停止位为1位
Uart0SendString("Hello CC2430 - TempSensor!");
while(1) { led1 =0; avgTemp =0;
for(i =0; i <64; i++) { avgTemp += getTemperature(); avgTemp = avgTemp/2; //每采样1次,取1次平均值 }
output[0] = (unsigned char)(avgTemp)/10 + 48; //十位 output[1] = (unsigned char)(avgTemp)%10 + 48; //个位 output[2] ='.'; //小数点 output[3] = (unsigned char)(avgTemp*10)%10+48; //十分位 output[4] = (unsigned char)(avgTemp*100)%10+48; //百分位 output[5] =''; //字符串结束符
Uart0SendString(output); Uart0SendString("℃"); led1 =1; //LED熄灭,表示转换结束,
Delay(20000); Delay(20000); Delay(20000); Delay(20000); Delay(20000); Delay(20000); Delay(20000); Delay(20000); Delay(20000); Delay(20000); }
}
For the code content of serial port communication, please refer to the previous section, which will not be explained here~ ADCs generally involve 6 SFRs: | ADCCON1 | For ADC general control, including conversion end flag, ADC trigger method, random number generator | | ADCCON2 | Configuration for continuous ADC conversion (this experiment does not involve continuous ADC conversion and therefore does not use this SFR) | | ADCCON3 | Configuration for a single ADC conversion, including selection of reference voltage, resolution, conversion source | | ADCH[7:0] | The high level of the ADC conversion result, i.e. ADC [13:6] | | ADCL[7:2] | The low level of the ADC conversion result, i.e. ADC [5:0] | | ADCCFG | AIN0~AIN7 with P0.0~P0.7 as the ADC input (this SFR is not used because the on-chip temperature sensor is selected as the conversion source in this test and does not involve AIN0~AIN7) |
(Note: Please refer to the CC2430 Chinese manual for the above SFR details) Next, let's focus on itgetTempuraturefunction, which is the key to getting the temperature value: (1) First, configure the ADC for a single sampling: set ADCCON3=0x3E, select 1.25V as the system voltage, select 14-bit resolution, and select the CC2430 on-chip temperature sensor as the ADC conversion source (2) Then set ADCCON1 |= 0x30 to set the ADC trigger method to manual (i.e., when ADCCON.6=1, start the ADC transition) (3) Then let ADCCON1 |= 0x40 to start the ADC single conversion (4) Use the statement while(!( ADCCON1 &0x80)) Wait for the end of the ADC transition (5) The conversion result is stored in ADCH[7:0] (8 bits high), ADCH [7:2] (6 bits lower), and passed: value = ADCL >>2; value |= (ADCH <<6);
Save the conversion result in the value (6) Finally, use the formulatemperature= value*0.06229-311.43, calculate the temperature value and return CC2430 TipsYou must be puzzled by the last formula, why a one-time function? Why does it have a slope of 0.06229 and an intercept of 211.43? OK, here's the answer: This temperature sensor is located inside the CC2430 chip, so its description can certainly be found in its manual. Sure enough, I'm hereElectrical specificationsThe relevant content is found in this section, and the screenshot is as follows:
This table describes the temperature (°C) of the temperature sensor in relation to the output voltage (V). First look at the second red box:temperature coefficient。 "Coefficient"? Doesn't it feel a little? Then look at its unit: mV/°C, and you will suddenly realize that the relationship between temperature and voltage is linear~ That is: where V is the output voltage value, T is the temperature value, and 2.45 is the slope. The intercept b must be determined below. At first glance, we will find at the first red box that the voltage at 0°C is 743mV, so b is equal to 743? Otherwise, if you continue to look down, you will find that its absolute error reaches as much as 8°C! Then looking to the right, we will see that it already provides the most suitable intercept, i.e.: b=763, so there is the following formula: OK, now we already have the temperature sensorEnter temperature TandOutput voltage VThe next step is to find the ADCInput voltage VandOutput value N(i.e., the conversion result of 14 bits), and finally find the conversion formula of N and T. The conversion result N is 14 bits, and when N=11 1111 1111 1111 (binary), the output voltage should be the maximum value (i.e. reference voltage 1.25V). Therefore, we have the following proportional relationship:
(Note: Since the output of 14 bits is a binary complement, the 14th bit is the symbol bit.) So in absolute terms, the effective value is only 13 bits, so it is 2 to the power of 13) Combining the two formulas, the relationship between T and N can be derived: OVER~
Finally, a few words about why 64 cycles are required per sample. Because sensors are inevitably subject to interference or random errors when measuring temperature, the data obtained by sensors is sometimes exaggerated (for example, a sudden change of 10°C, and then an instant return to normal). But we know that temperature change is an integral process, and it is rare for a large jump to occur in an instant). Therefore, we use the average method to reduce such errors. (4) Experimental resultsFirst, open the serial port debugging tool, then download the program and start it, and the following screen will appear:
The temperature inside the film is about 14.5°C. The author feels the room temperature of the dormitory with his body, which is about a little more than 10°C. The inside of the chip needs to get some heat, so 14°C is basically normal~ This is the end of the experiment. 3. ConclusionThis article describes the implementation of ADC single-sampling. In the next section, we will introduce a data transfer modeDMA(direct memory access), that is, "direct memory access". Peripheral units such as ADC/UART/RF transceivers and memory devices can be exchanged directly under the control of the "DMA controller"Little CPU intervention is required, which greatly improves the overall efficiency of the system. Stay tuned! </n; i++); </n; i++); </n; i++); </n; i++); </n; i++);
|