CC2530的ADC采集外部电压


初窥ZIGBEE

要在zigbee的组网中加入烟雾传感器的模块,所以需要用到cc2530的ADC对传感器输出的模拟信号进行采样。下面是自己对实现用CC2530的ADC采集外部电压的程序过程。

以下是ADC的配置:

 1 #include<iocc2530.h>
 2 #include"adc.h"
 3 #include"uart.h"
 4 #include<stdio.h>
 5 #include"led.h"
 6 
 7 #define VDD_REF 3.3
 8 float ADCValue = 0.0;//global ,传递电压值
 9 /**************************************************************
10 *function:ADInit()
11 *This funciton  is order to initialize ADC of CC2530
12 *单次采样,采用端口为P0.6
13 **************************************************************/
14 void ADInit(void)
15 { 
16   uint ADCREGValue = 0;
17   SET_IO_PORT_DIR(0,6,IO_IN);//设置P0..6为输入模式
18   ADC_ENABLE_CHANNEL(6);    //使能通道6作为ADC的采样通道
19   ADC_SINGLE_CONVERSION(ADC_REF_AVDD|ADC_12_BIT| ADC_AIN6);   //配置ADC参数,参考电压为AVDD5引脚电压,抽取率为512(12位分辨率)
20   ADC_SAMPLE_SINGLE();      //启动单次采样
21   
22    while(!(ADCCON1&0x80)) ;//等待AD转换完成
23    // while ( !ADCIF );  
24    /*获取结果,并转换为电压*/
25     ADCREGValue = ADCL>>4;//程序中设置的是12bit的精度,取低4位值
26     ADCREGValue |= ADCH<<4; //高八位值 
27     ADCValue = (float)(ADCREGValue/(float)2048)*3.3;//此处有疑问,本来是12bit的精度,除数应该是4096的,但是得用2048才能得到准确的值
28 }

以下是UART0的配置:

#include<iocc2530.h>
#include"uart.h"

void UARTInit(void)
{
  
  PERCFG = 0;       //配置UART0的IO位置为备用位置1
  P0SEL  = 0x3c;    //P0.2-PO.5设置为外设功能的端口
  P2DIR  &= ~(3<<6);//设置UART0为第一优先级,UART1为第二优先级,
  U0CSR  |= (1<<7); //select the mode as UART mode
  U0GCR  |= 0x09;    
  U0BAUD |= 59;     //19200
  UTX0IF = 1;       //clear the interrupt flag
  U0CSR |= (1<<6);  //enable receive bit
  IEN0 |= 0x84;
}
/*********************************************************
note: "length" is the length of one line
**********************************************************/
void UARTSend(char *data,int length)
{
  int i;
  for(i=0;i<length;i++)
  {
    U0DBUF = *data;
    data++;
    while(UTX0IF==0);   // complete receive
    UTX0IF = 0;         //clear the flag
  }
   U0DBUF =0x0A;        //carriage return
   while(UTX0IF==0);   // complete receive
    UTX0IF = 0;  
}

以下是主函数:

#include<iocc2530.h>
#include"adc.h"
#include"uart.h"
#include"led.h"
#include<stdio.h>
#include <string.h>

void delay(uint n);
void ClockInit(void);


void  main(void)
{
  char i = 0;
  char TempValue[10];
  float average ;
  char len;
  P1_0 = 0;
  ClockInit();
  UARTInit();
  SET_IO_PORT_DIR(1,0,IO_OUT); //设置LED,作为ADC采样进行的标志
  IO_FUNC_PORT_PIN(1, 0, IO_FUNC_GIO);//INIT_LED(); 
  IEN0 =IEN1=IEN2 =0;
 
  while(1)
  {
    average = 0.0;  
   for(i=0;i<64;i++)//取64次均值
   {
      ADInit();
      average +=ADCValue;
   }
    average /=64;
   LED();
   sprintf(TempValue,"%fV\r",(float)average);//将数值格式化为字符串
   len = strlen(TempValue);//字符串的长度
   UARTSend(TempValue,len);//向串口发送数据
   delay(20000);
  }

} 

/***********************************************************
初始化时钟参数
*************************************************************/
void ClockInit(void)
{
     CLKCONCMD = 0x28;           //时器计数时钟设定为1M Hz,  系统时钟设定为32 MHz 
    while(CLKCONSTA & 0x40);    //等晶振稳定
}
         

串口显示结果:

一开始的时候,读取出来的AD数值完全不对,最后发现是之前选取板子上的采样端口出现问题,换了P0.6之后,效果就好多了,估计就是由于端口复用的造成的影响。。弄好了基本的驱动,后面得开始将程序添加到ZIGBEE的模块中,实现组网。。

posted @ 2016-12-11 12:17  iczelion  阅读(7973)  评论(0编辑  收藏  举报