CC2530 ADC acquisition of external voltage

Source: Internet
Author: User


First Glimpse of ZigBee

To add a smoke sensor module to the ZigBee network, a cc2530 ADC is required to sample the analog signal output from the sensor. The following is a procedure for implementing an external voltage acquisition using an ADC of the CC2530.

The following is the configuration of the ADC:

1#include <iocc2530.h>2#include"adc.h"3#include"uart.h"4#include <stdio.h>5#include"led.h"6 7 #defineVdd_ref 3.38 floatAdcvalue =0.0;//Global, transmit voltage value9 /**************************************************************Ten *function:adinit () One *this Funciton is Order to initialize ADC of CC2530 A * Single sampling, with Port as P0.6 - **************************************************************/ - voidAdinit (void) the {  -   UINTAdcregvalue =0; -Set_io_port_dir (0,6, io_in);//set P0. 6 for Input mode -Adc_enable_channel (6);//Enable channel 6 as the sampling channel for the ADC +Adc_single_conversion (adc_ref_avdd| Adc_12_bit| ADC_AIN6);//Configure ADC parameters with a reference voltage of AVDD5 pin with a decimation rate of 512 (12-bit resolution) -Adc_sample_single ();//Start a single sample +    A     while(! (adccon1&0x80)) ;//wait for ad conversion to complete at    //While (!  ADCIF);  -    /*get the results and convert to voltage*/ -Adcregvalue = adcl>>4;//The 12bit accuracy is set in the program, with a low 4-bit value -Adcregvalue |= adch<<4;//high eight-bit value -Adcvalue = (float) (adcregvalue/(float)2048)*3.3;//here is the question, it is 12bit accuracy, the divisor should be 4096, but 2048 to get the exact value -}

The following is the configuration of the UART0:

#include <iocc2530.h>#include"uart.h"voidUartinit (void) {percfg=0;//Configure the IO location for the UART0 as an alternate location 1P0sel =0x3c;//p0.2-po.5 ports that are set to peripheral functionsP2dir &= ~ (3<<6);//set UART0 as the first priority, UART1 as the second priority,U0CSR |= (1<<7);//Select the mode as UART modeU0GCR |=0x09; U0baud|= -;//19200Utx0if =1;//clear the interrupt flagU0CSR |= (1<<6);//enable receive bitIEN0 |=0x84;}/*********************************************************note: "Length" is the length of one line**************** ******************************************/voidUartsend (Char*data,intlength) {  inti;  for(i=0; i<length;i++) {U0dbuf= *data; Data++;  while(utx0if==0);//Complete receiveUtx0if =0;//Clear the Flag} u0dbuf=0x0A;//Carriage return    while(utx0if==0);//Complete receiveUtx0if =0; }

Here is the main function:

#include <iocc2530.h>#include"adc.h"#include"uart.h"#include"led.h"#include<stdio.h>#include<string.h>voidDelayUINTn);voidClockinit (void);voidMainvoid){  Chari =0; Chartempvalue[Ten]; floataverage; CharLen; P1_0=0;  Clockinit ();  Uartinit (); Set_io_port_dir (1,0, io_out);//setting the LEDs as a flag for ADC samplingIo_func_port_pin (1,0, Io_func_gio);//init_led ();IEN0 =ien1=ien2 =0;  while(1) {Average=0.0;  for(i=0;i< -; i++)//take 64-time mean value{adinit (); Average+=Adcvalue; } Average/= -;   LED (); sprintf (Tempvalue,"%fv\r",(float) average);//format a numeric value as a stringLen = strlen (Tempvalue);//the length of the stringUartsend (Tempvalue,len);//sending data to the serial portDelay20000); }} /*********************************************************** initializing the clock parameter ***************************************** ********************/voidClockinit (void) {Clkconcmd=0x28;//timer count clock set to 1M Hz, system clock set to + MHz     while(Clkconsta &0x40);//Stable Crystal oscillator}         

Serial Port Display Result:

At the beginning, read out the ad value is completely wrong, and finally found that the sample port on the board before the problem, after changing the P0.6, the effect is much better, is estimated to be due to the impact of port multiplexing. The basic driver has been set up and the program has to be added to the ZigBee module to implement the network.

CC2530 ADC acquisition of external voltage

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.