STC12C5620AD series A/D conversion C language program

Publisher:书卷气息Latest update time:2016-03-23 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include "STC12C5620AD.H"

//---------------------------------------------------------------------
typedef    unsigned char   INT8U;
typedef    unsigned int    INT16U;
//---------------------------------------------------------------------
//The following selects the ADC conversion rate. Only one of them can be selected.
                       //          SPEED1 SPEED0 The time required for A/D conversion
#define AD_SPEED   0x60 //0110,0000          270 clock cycles are converted once.
                       //                         When the CPU operating frequency is 27MHz,
                       //                         The A/D conversion speed is about 100KHz
//#define AD_SPEED   0x40 //0100,0000          540 clock cycles are converted once
. //#define AD_SPEED   0x20 //0010,0000          810 clock cycles are converted once.
//#define AD_SPEED   0x00 //0000,0000          1080 clock cycles for conversion
//---------------------------------------------------------------------
void initiate_RS232 (void);         // Serial port initialization
void Send_Byte(INT8U one_byte);     // Send a byte
INT16U get_AD_result(INT8U channel);
void delay(INT8U delay_time);       // Delay function
//---------------------------------------------------------------------
void main()
{
   INT16U ADC_10bit;

   initiate_RS232();               //Baud rate = 9600

   ADC_CONTR |= 0x80;              //1000,0000 Turn on the A/D conversion power
   while(1)
   {
       Send_Byte(0xAA);
       Send_Byte(0xAA);

       ADC_10bit = get_AD_result(2); //P1.2 is the current A/D channel
       Send_Byte(ADC_10bit >> 8); //Send the high 8-bit measurement result
       delay(0x1);

       ADC_10bit = get_AD_result(3);//P1.3 is the current channel of A/D, measure and send the resultSend_Byte
       (ADC_10bit >> 8); //Send the high 8 bits of measurement resultdelay
      
       (0x30);                //Delay
   }
}
//---------------------------------------------------------------------
void delay(INT8U delay_time)        // Delay function
{
   INT16U n;
   while(delay_time--)
   {
       n = 6000;
       while(--n);
   }
}
//---------------------------------------------------------------------
void initiate_RS232 (void)          // Serial port initialization
{
   ES = 0;                         // Disable serial port interruptTMOD
   = 0x20;                    // Set T1 as baud rate generatorSCON
   = 0x50;                    // 0101,0000 8-bit data bit, no parity checkTH1
   = 0xFB;                     // 18.432MHz crystal, baud rate = 9600
   TL1 = 0xFB;
   RI = 0;
   TR1 = 1;
   ES = 1;                         // Enable serial port interrupt
}
//---------------------------------------------------------------------
void Send_Byte(INT8U one_byte)      // Send one byte
{
   TI = 0;                         // Clear the serial port send interrupt flag
   SBUF = one_byte;
   while (!TI);
   TI = 0;                         // Clear the serial port send interrupt flag
}
//---------------------------------------------------------------------
INT16U get_AD_result(INT8U channel)
{
   ADC_DATA   = 0;

   channel &= 0x07;                //0000,0111 clear high 5 bits
   ADC_CONTR = AD_SPEED;
   ADC_CONTR = 0xE0;               //1110,0000 clear ADC_FLAG, ADC_START and low 3 bits
   ADC_CONTR |= channel;           //select A/D current channel
   delay(1);                       //let input voltage reach stability
   ADC_CONTR |= 0x08;              //0000,1000 set ADCS = 1, start A/D conversion,
   while (1)                       //wait for A/D conversion to end
   {
       if (ADC_CONTR & 0x10)       //0001,0000 test whether A/D conversion is finished or not
       { break; }
   }
   ADC_CONTR &= 0xE7;              //1111,0111 clear ADC_FLAG, turn off A/D conversion,

   return (ADC_DATA<<8 | ADC_LOW2); //Return A/D 10-bit conversion result

Reference address:STC12C5620AD series A/D conversion C language program

Previous article:Reading ID number of STC series microcontroller
Next article:Serial communication between PC and MCU

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号