ZigBee Development (7)-Basic Experiment ad

Source: Internet
Author: User

I. basic working principle of A/D Conversion

The process of converting the time-varying analog quantity into the number of pulsesDigitalizationAnd the key device for digitalization isADC.

ADC:Digital-to-analog converterConverts time and amplitude-continuous analog numbers into discrete numbers of time and amplitude. A/D conversion usually goes throughSampling,Persistence,QuantizationAndEncoding4 processes.

 

II. A/D conversion module of the cc2530

The ADC module of the standard supports up to 14-bit binary analog digital conversion with 12-bit valid data bits. It consists of a analog multi-channel converter and has eight configurable channels, and a reference voltage generator.

This ADC module has the following main features:

<1> select the extraction rate and set the resolution (7 ~ 12 digits ).

<2> eight independent input channels can receive single-ended or differential signals.

<3> the reference voltage can be set to internal single-ended, external single-ended, external differential, or avdd5.

<4> an interrupt request can be generated when the single-channel conversion ends.

<5> when the sequence conversion ends, the DMA trigger is triggered.

<6> The on-chip temperature sensor can be used as input.

<7> battery voltage measurement function.

 

Iii. Signal Input of the ADC Module

Port 0The pin can be configured as the ADC input, followedAin0 ~ Ain7:

<1> the input can be set to single-ended input or differential input.

<2> differential input: ain0 ~ Ain1, ain2 ~ Ain3, ain4 ~ Ain5, ain6 ~ Ain7.

<3> the output of the On-chip temperature sensor can also be used as an ADC input to measure the temperature of the chip.

<4> A voltage corresponding to avdd5/3 can be used as an ADC input for battery voltage monitoring.

<5> neither the negative voltage nor the voltage greater than the VDD can be used on these pins.

<6>Single-ended VoltageEnter ain0 ~ Ain7, with the channel number0 ~ 7FourDifferential InputIf yes, use the channel number.8 ~ 11Representation;Temperature SensorThe channel number of is14;Avdd5/3 voltageThe entered channel number is15.

 

Iv. ADC-related concepts

<1>Sequential ADC Conversion: Multi-Channel ADC conversion can be performed in sequence, and the results are transmitted to the memory through DMA, without any CPU involvement.

<2>Single-channel ADC Conversion: In programming, a single-channel ADC conversion is triggered by writing the adccon3 register. Once the register is written, the conversion starts immediately.

<3>Reference Voltage: Internal voltage, avdd5 pin, external voltage for ain7 input pin, or for ain6 ~ The differential voltage of the ain7 input pin.

<4>Conversion Result: The result of the number conversion is in the form of a 2's complement. For a single-ended server, the result is always positive. For differential configuration, the difference between two pins is converted, which can be a negative number. When adccon1.eoc is set to 1, the digital conversion result can be obtained, and the result will always reside in the MSB segment of the adch and adcl register combination.

<5>Interrupt request: When a single-channel conversion is triggered by writing adccon3, an interruption is generated, while a sequence conversion is completed without interruption. Each time a sequence is converted, the ADC generates a DMA trigger.

<6>Register: The ADC has two data registers:AdclAndAdchThree control registers:Adccon1,Adccon2,Adccon3To configure the ADC and return the conversion result.

 

 

[1] configure the apcfg register

When ADC is used, the port 0 pin must be configuredADC analog input. To configure a port 0 pin as an ADC input,Apcfg registerMust be set to 1. The default value of this register is 0, and port 0 is selected as a non-analog input, that is, a digital I/O port.

Note:: The settings of the apcfg register overwrite those of the p0sel.

[2] configure adccon3 register

Single-channel ADC conversion, only write control wordsAdccon3 registerYou can.

[3] ADC Initialization

This module selects the port function, sets its transmission direction, and sets the port as a simulated input.

[4] ADC data collection

FirstAdcifThe flag is 0, and thenAdccon3Register settings. Once the register is written, the conversion is enabled immediately.AdcifSet to 1. At this time, the conversion is complete and the data can be read.

 

/************************************ Program description: the temperature information is sent to the upper computer through the serial port through the internal ad control. Some chips have a large error and need to be calibrated. When you touch the chip, the temperature increases significantly. **************************************/# Include <iocc2530.h> // # include "inituart_timer.h" // set the path in Option # include <stdio. h> # define uint unsigned int # define uchar unsigned char/* ======================== ur0 initialization Function ======================= */void usart_init (void) {percfg & = ~ 0x01; // position 1 P0 port p0sel | = 0x0c; // p0_2, p0_3 for serial port, second function p2dir & = ~ 0xc0; // P0 takes priority as uart0, priority u0csr | = 0x80; // u0ucr in UART mode | = 0x0c; u0gcr | = 11; // u0gcr and u0baud are used with u0baud | = 216; // The baud rate is set to 115200 u0csr | = 0x40; // allows receiving Ea = 1; // enable the total interrupt urx0ie = 1; // enable the serial port to receive the interrupt utx0if = 0; // uart0 TX interrupt mark the initial position urx0if = 0; // uart0 RX interrupt flag initial position} void usart_send_byte (uchar c) {u0dbuf = C; while (! Utx0if); // wait until the sending is completed. The flag location is 1 utx0if = 0;} void usart_send_string (uchar * data, uint Len) {While (Len --) {usart_send_byte (* Data ++ );}} /*************************************** ************************ Temperature Sensor initialization function *********** **************************************** * ************/void inittempsensor (void) {Ea = 0; clkconcmd & = ~ 0x40; // set the system clock source to a 32 MHz crystal oscillator while (clkconsta & 0x40); // wait for the crystal oscillator to stabilize to 32 m clkconcmd & = ~ 0x47; // set the system master clock frequency to // disable_all_interrupts (); // disable all interrupts // initclock (); // set the system master clock to 32 m // tr0 = 0x01; // set '1' to connectthe temperature sensorto thesoc_adc. // atest = 0x01; // enablesthe Temperature Sensor }/********************************* * ******************************** function for reading the ad value of the temperature sensor *** **************************************** * *******************/float gettemperature (void) {uint value; adccon3 = (0x3e ); // Select 1.25 V as the reference voltage; 12-bit resolution; sample adccon1 for the on-chip Temperature Sensor | = 0x30; // select the ADC startup mode as manual adccon1 | = 0x40; // start AD conversion while (! (Adccon1 & 0x80); // wait until the AD conversion is complete. value = adcl> 4; // The value of the low 4-bit adcl register is invalid. | = (uint) adch) <4); Return (value-1367.5)/4.5-4; // calculate the actual temperature based on the ad value. The chip and // Manual are incorrect, the temperature coefficient should be 4.5/℃ // For temperature correction. Here, the temperature is reduced by 4 ℃ (different chips are corrected according to specific conditions )} /*************************************** * ************************ main function ************* **************************************** * **********/void main (void) {uchar I; uchar tempvalue [6]; float avgtemp; usart_init (); // initialize the serial port inittempsensor (); // initialize the ADC while (1) {avgtemp = 0; for (I = 0; I <64; I ++) {avgtemp + = gettemperature (); avgtemp = avgtemp/2; // after adding 2}/*** after each time, convert the temperature to the ASCII code and send the ***/tempvalue [0] = (unsigned char) (avgtemp) /10 + 48; // ten-digit tempvalue [1] = (unsigned char) (avgtemp) % 10 + 48; // one-digit tempvalue [2] = '. '; // decimal point tempvalue [3] = (unsigned char) (avgtemp * 10) % 10 + 48; // very bit tempvalue [4] = (unsigned char) (avgtemp * 100) % 10 + 48; // percentile tempvalue [5] = '\ 0'; // string Terminator usart_send_string (tempvalue, 6 ); // delayms (2000); // use 32 m crystal oscillator, so here 2000 is approximately equal to 1 s }}

 

ZigBee Development (7)-Basic Experiment ad

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.