c_cpp STC15 ADC查询模式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp STC15 ADC查询模式相关的知识,希望对你有一定的参考价值。

// From STC Datasheet

//suppose the frequency of test chip is 18.432MHz
#include "reg51.h"
#include "intrins.h"

#define   FOSC 18432000L
#define   BAUD 9600

typedef unsigned char   BYTE;
typedef unsigned int   WORD;

#define   URMD  0   
//0: select T2 as UART1 baud-rate generator
//1: select T1 as UART1 baud-rate generator(T1 as 16-bit auto-relaod timer/counter)
//2: select T1 as UART1 baud-rate generator (T1 as 8-bit auto-relaod timer/counter)

sfr   T2H   =   0xd6; 
sfr   T2L   =   0xd7; 
sfr   AUXR  =   0x8e;       //Auxiliary register 

/*Declare SFR associated with the ADC */
sfr   ADC_CONTR  = 0xBC;    //ADC control register
sfr   ADC_RES    = 0xBD;    //ADC hight 8-bit result register
sfr   ADC_LOW2    = 0xBE;    //ADC low 2-bit result register
sfr   P1ASF    = 0x9D;    //P1 secondary function control register

/*Define ADC operation const for ADC_CONTR*/
#define   ADC_POWER     0x80     //ADC power control bit
#define   ADC_FLAG      0x10     //ADC complete flag
#define   ADC_START     0x08     //ADC start control bit
#define   ADC_SPEEDLL   0x00     //540 clocks
#define   ADC_SPEEDL    0x20     //360 clocks
#define   ADC_SPEEDH    0x40     //180 clocks
#define   ADC_SPEEDHH   0x60     //90 clocks

void InitUart();
void InitADC();
void SendData(BYTE dat);
BYTE GetADCResult(BYTE ch);
void Delay(WORD n);
void ShowResult(BYTE ch);

void main()
{
 InitUart();      //Init UART, use to show ADC result
 InitADC();      //Init ADC sfr
 while (1)
  {
   ShowResult(0);     //Show Channel0   
   ShowResult(1);     //Show Channel1   
   ShowResult(2);     //Show Channel2   
   ShowResult(3);     //Show Channel3  
   ShowResult(4);     //Show Channel4   
   ShowResult(5);     //Show Channel5
   ShowResult(6);     //Show Channel6   
   ShowResult(7);     //Show Channel7
  }
}
/*----------------------------------------------------------
Send ADC result to UART
-------------------------------------------------------------*/
void ShowResult(BYTE ch)
{
SendData(ch);     //Show Channel NO.
SendData(GetADCResult(ch));    //Show ADC high 8-bit result
//if you want show 10-bit result, uncomment next line
// SendData(ADC_LOW2);    //Show ADC low 2-bit result
}

/*-----------------------------------------------------------
Get ADC result
--------------------------------------------------------------*/
BYTE GetADCResult(BYTE ch)
{
  ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ch | ADC_START;
  _nop_();     //Must wait before inquiry
  _nop_();
  _nop_();
  _nop_();
  while (!(ADC_CONTR & ADC_FLAG));  //Wait complete flag
  ADC_CONTR &= ~ADC_FLAG;    //Close ADC
  return ADC_RES;     //Return ADC result
}

/*--------------------------------
Initial UART
----------------------------*/
void InitUart()
{
  SCON   =   0x5a;    //set UART1 as 8-bit UART with variable baud-rate
  #if URMD == 0
  T2L   =   0xd8;    //Set the preload value
  T2H   =   0xff;    //115200 bps(65536-18432000/4/115200)
  AUXR   =   0x14;    //T2 in 1T mode, and run T2
  AUXR   |=  0x01;    //select T2 as UART1 baud rate generator
  #elif   URMD   ==   1
  AUXR   =   0x40;    //T1 in 1T mode
  TMOD   =   0x00;    //T1 in mode 0 (16-bit auto-reload timer/counter)
  TL1   =   0xd8;    //Set the preload value
  TH1   =   0xff;    //115200 bps(65536-18432000/4/115200)
  TR1   =   1;    //run T1
  #else
  TMOD   =   0x20;    //T1 in mode 2 (8-bit auto-reload timer/counter)
  AUXR   =   0x40;    //T1 in 1T mode
  TH1 =  TL1 =   0xfb;    //115200 bps(256 - 18432000/32/115200)
  TR1   =   1;
  #endif
}
/*---------------------------------------------------------------
Initial ADC sfr
-----------------------------------------------------------------*/
void InitADC()
  {
  P1ASF = 0xff;     //Open 8 channels ADC function
  ADC_RES = 0;     //Clear previous result
  ADC_CONTR = ADC_POWER | ADC_SPEEDLL;
  Delay(2);     //ADC power-on and delay
}
/*------------------------------------------------------
Send one byte data to PC
Input: dat (UART data)
Output:
----------------------------------------------------------*/
void SendData(BYTE dat)
{
  while (!TI);    //Wait for the previous data is sent
  TI = 0;    //Clear TI flag
  SBUF = dat;    //Send current data
}
/*--------------------------------------------------------
Software delay function
----------------------------------------------------------*/
void Delay(WORD n)
{
  WORD x;
  while (n--)
  {   
    x = 5000;   
    while (x--);
  }
}

以上是关于c_cpp STC15 ADC查询模式的主要内容,如果未能解决你的问题,请参考以下文章

STC15比较器做ADC程序示例

STC15系列P1.3做ADC-使用内部基准计算外部电压并串口打印

STC单片机LCD1602四线驱动模式显示ADC采样电压

STC单片机LCD1602四线驱动模式显示ADC采样电压和温度

STC单片机通过ADC外部输入调节PWM占空比输出并串口打印当前脉冲值

STC15系列PWM相关功能寄存器介绍