| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4032 人关注过本帖
标题:我想问一下ADC转换后是二进制的数据处理问题
只看楼主 加入收藏
镖叔
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2012-10-4
结帖率:75%
收藏
已结贴  问题点数:20 回复次数:1 
我想问一下ADC转换后是二进制的数据处理问题
/***************************************************************
功能:实现ADC0采样芯片温度,通过LCD显示,并通过串口0(J13)发送到PC机
     注:试验时把ADC0的工作基准VREF0(J7_5和J7_6或J7_2和J7_6)和LCD电源跳线(J18_1和J18_2)联接好!
作者:ZDP
时间:2005-11-30
版本:V1.0
用外部基准:
                 J7
    NC            1    2    内部VREF
    外部VREF    3    4    内部DAC工作基准输入
    外部VREF    5---6    内部ADC0工作基准输入
    外部VREF    7    8    内部ADC1工作基准输入
或用内部基准:
                 J7
    NC            1    2-    内部VREF
    外部VREF    3    4 |    内部DAC工作基准输入
    外部VREF    5    6-    内部ADC0工作基准输入
    外部VREF    7    8    内部ADC1工作基准输入
***************************************************************/
#include <c8051f020.h>                 // SFR declarations
#include <stdio.h>
#include <INTRINS.H>
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F02x
//-----------------------------------------------------------------------------

sfr16 DP       = 0x82;                 // data pointer
sfr16 TMR3RL   = 0x92;                 // Timer3 reload value
sfr16 TMR3     = 0x94;                 // Timer3 counter
sfr16 ADC0     = 0xbe;                 // ADC0 data
sfr16 ADC0GT   = 0xc4;                 // ADC0 greater than window
sfr16 ADC0LT   = 0xc6;                 // ADC0 less than window
sfr16 RCAP2    = 0xca;                 // Timer2 capture/reload
sfr16 T2       = 0xcc;                 // Timer2
sfr16 RCAP4    = 0xe4;                 // Timer4 capture/reload
sfr16 T4       = 0xf4;                 // Timer4
sfr16 DAC0     = 0xd2;                 // DAC0 data
sfr16 DAC1     = 0xd5;                 // DAC1 data

#define BAUDRATE     115200            // Baud rate of UART in bps
#define SYSCLK       22118400          // SYSCLK frequency in Hz
#define SAMPLE_RATE  50000             // Sample frequency in Hz
#define INT_DEC      256               // integrate and decimate ratio
#define    AMX0SL_AIN   8                   // 0=AIN0....7=AIN7,8=TEMP

void SYSCLK_Init (void);
void PORT_Init (void);
void UART0_Init (void);
void ADC0_Init (void);
void Timer3_Init (int counts);
void ADC0_ISR (void);
void LCD_Init(void);

unsigned char xdata NCDdata[6]={0x30,0x30,0x30,0x2e,0x30,0x30};
long result;
unsigned char *lcdpoint;        //指向 lcddata数组的指针
unsigned char lcd_data_count;   //要显示的数据个数

void main (void) {
   long temperature,x;
   int temp_int, temp_frac;
   char data1;

   WDTCN = 0xde;                       // disable watchdog timer
   WDTCN = 0xad;

   SYSCLK_Init ();                     // initialize oscillator
   PORT_Init ();                       // initialize crossbar and GPIO
   UART0_Init ();                      // initialize UART0
   Timer3_Init (SYSCLK/SAMPLE_RATE);   // initialize Timer3 to overflow at
                                       // sample rate

   ADC0_Init ();                       // init ADC

    AD0EN = 1;                         // enable ADC

    EA = 1;  
    while(result==0);                    //等于0,侧等待                     
    while (1) {
      EA = 0;                          // 关中断
      temperature = result;
      EA = 1;                          //开中断
      //转换为实际温度数据
      temperature = temperature - 42380;
      temperature = (temperature * 100L) / 156;
      temp_int = temperature / 100;
      temp_frac = temperature - (temp_int * 100);
      printf ("Temperature is %+02d.%02d\n", temp_int, temp_frac);//把温度数据通过串口发送至PC机
      //在LCD上显示温度
      //LCD显示数据处理

      NCDdata[0]=temp_int/100+0x30;NCDdata[1]=(temp_int%100)/10+0x30;NCDdata[2]=(temp_int%100)%10+0x30;
      NCDdata[4]=temp_frac/10+0x30;NCDdata[5]=temp_frac%10+0x30;


      LCD_Init();                             //LCD初始化
      P2  = 0xA0;                             //准备送数据
      lcdpoint=&NCDdata;                    //取地址
      for(lcd_data_count=6;lcd_data_count>0;lcd_data_count--)
      {
              data1=*lcdpoint;                //读出数据               
            P3 = data1;                     //写数据到端口
            P2 = 0X20;
            P2 = 0XA0;                        //控制LCD   
            lcdpoint++;
            for(x=0;x<0x5000;x++);
      }
      for(data1=0;data1<0x6;data1++)
      {
            for(x=0;x<0xffff;x++)
              {_nop_();}
       }
    }
}

//-----------------------------------------------------------------------------
// SYSCLK配置
//-----------------------------------------------------------------------------
// 配置系统时钟使用外部晶振22.1184MHz

void SYSCLK_Init (void)
{
   int i;                              // delay counter

   OSCXCN = 0x67;                      // start external oscillator with
                                       // 22.1184MHz crystal
   for (i=0; i < 256; i++) ;           // XTLVLD blanking interval (>1ms)

   while (!(OSCXCN & 0x80)) ;          // Wait for crystal osc. to settle

   OSCICN = 0x88;                      // select external oscillator as SYSCLK
                                       // source and enable missing clock
                                       // detector
}

//-----------------------------------------------------------------------------
// PORT配置
//-----------------------------------------------------------------------------
void PORT_Init (void)
{
   XBR0    = 0x04;                     // Enable UART0
   XBR1    = 0x00;
   XBR2    = 0x40;                     // Enable crossbar and weak pull-ups
   P0MDOUT |= 0x01;                    // enable TX0 as a push-pull output
   P2MDOUT  = 0xe0;                    // P2口设为推挽方式
   P3MDOUT  = 0xff;                    // P2口设为推挽方式
}

//-----------------------------------------------------------------------------
// UART0配置
//-----------------------------------------------------------------------------
// Configure the UART0 using Timer1, for <baudrate> and 8-N-1.
void UART0_Init (void)
{
   SCON0   = 0x50;                     // SCON0: mode 1, 8-bit UART, enable RX
   TMOD    = 0x20;                     // TMOD: timer 1, mode 2, 8-bit reload
   TH1    = -(SYSCLK/BAUDRATE/16);     // set Timer1 reload value for baudrate
   TR1    = 1;                         // start Timer1
   CKCON |= 0x10;                      // Timer1 uses SYSCLK as time base
   PCON  |= 0x80;                      // SMOD00 = 1
   TI0    = 1;                         // Indicate TX0 ready
}

//-----------------------------------------------------------------------------
// ADC0配置,T3定时启动ADC
//-----------------------------------------------------------------------------
void ADC0_Init (void)
{
   ADC0CN = 0x05;                      // ADC0 T3定时采样,左对齐
   REF0CN = 0x07;                      // 启用内部基准源
   AMX0SL = AMX0SL_AIN;                // 选择采样输入源
   ADC0CF = (SYSCLK/2500000) << 3;     // ADC conversion clock = 2.5MHz
   ADC0CF |= 0x01;                     // PGA gain = 2

   EIE2 |= 0x02;                       // 启用 ADC 中断
}

//-----------------------------------------------------------------------------
// Timer3配置,T3定时启动ADC
//-----------------------------------------------------------------------------
void Timer3_Init (int counts)
{
   TMR3CN = 0x02;
   TMR3RL  = -counts;
   TMR3    = 0xffff;
   EIE2   &= ~0x01;
   TMR3CN |= 0x04;
}

//-----------------------------------------------------------------------------
// ADC0采样中断
//-----------------------------------------------------------------------------
void ADC0_ISR (void) interrupt 15
{
   static unsigned int_dec=INT_DEC;   
   static long accumulator=0L;                     

   AD0INT = 0;                           // 清 ADC 中断标志位


    accumulator += ADC0;               // 累加ADC采样数据
   int_dec--;                          // 指针减1

   if (int_dec == 0) {                 // 累加完了吗?
      int_dec = INT_DEC;               // 指针复位
      result = accumulator >> 8;
      accumulator = 0L;                // 累加和变量清0
   }
}
//LCD初始化
void LCD_Init(void)
{unsigned long x;
   P2 = 0X80;
   for(x=0;x<1000;x++);
   //P7 = 0x30;                /*一行显示*/
   P3 = 0x38;                /*两行显示*/
   P2 = 0X00;//0x08;
   P2 = 0X80;//0x09;
   for(x=0;x<1000;x++);
   P3 = 0x0e;
   P2 = 0x00;
   P2 = 0x80;
   for(x=0;x<1000;x++);
   P3=  0x06;
   P2 = 0x00;
   P2 = 0x80;
   for(x=0;x<5000;x++);
   P3 = 0x01;
   P2 = 0x00;
   P2 = 0x80;
   for(x=0;x<5000;x++);
}
上面中 temperature = temperature - 42380;
      temperature = (temperature * 100L) / 156;时将ADC数据转化为十进制吗,我想知道其中的原理
搜索更多相关主题的帖子: 工作 二进制 
2012-10-23 09:25
于祥
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:蒙面侠
威 望:5
帖 子:1047
专家分:4132
注 册:2011-4-24
收藏
得分:20 
这个的看所给的ADC的资料吧?

最基础的往往是你最容易忽略的!
2012-10-25 21:44
快速回复:我想问一下ADC转换后是二进制的数据处理问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017979 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved