STC12C5201AD chip AD application summary

Publisher:upsilon30Latest update time:2015-04-30 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
STC12C5201AD chip application summary

1. Internally contains 8-channel AD conversion

2. First declare the registers related to AD conversion

Sfr ADC_CONTR =0XBC; //AD conversion control register address

Sfr ADC_RES=0XBD; //AD conversion result storage register

Sfr P1ASF=0x9d; //P1 port analog function control register

                //Which position is 1, P1.0-P1.7 is set to AD conversion function

3. To make the program more flexible, each bit of ADC_CONTR can be set individually.

#define ADC_POWER 0X80; //All ADC power control, bit 7

#define ADC_FLAG 0X10; //AD conversion completed flag, bit 4,

#define ADC_START 0X08; //AD conversion start flag, bit 3,

#define ADC_SPEEDLL 0X00; //AD conversion speed control bit, bit 5 and 6

                               //420 clocks

#define    ADC_SPEEDL   0x20;//280 clocks

#define    ADC_SPEEDH  0x40;//140 clocks

#define    ADC_SPEEDHH  0x60;//70 clocks

4. Initialize ADC

Void initADC(void)

{

 P1ASF=0X01; //Set which P1 port to use for ADC

             //0X01  P1.0

             //0X02 P1.1

             //0X03 P1.0 and P1.1

             //0XFF all 8 ports

 ADC_RES=0; //result register cleared

 ADC_CONTR = ADC_POWER | ADC_SPEEDLL // Turn on the ADC power and set the conversion rate

 Delay(2); //Software delay 2ms

}

5. Read the ADC result

Uchar getADCresult(uchar ch) //ch is the value of the channel to be read, 0-7

{

ADC_CONTR=ADC_POWER|ADC_SPEEDLL |ADC_START|ch;

             //Set the ADC control register, turn on the power , set the conversion rate, turn on the start, and set the conversion channel

 _nop_(); //After the statement to set the ADC_CONTR control register is executed, it will take 4 CPU clock delays.

        //Its value can be set into the ADC_CONTR control register

 _nop_();

 _nop_();

 _nop_();

 while(!(ADC_CONTR & ADC_FLAG));//wait complete flag

 

 ADC_CONTR&=~ADC_FLAG;//close ADC

 

 return ADC_RES;//return ADC result

}

The result of AD conversion is 8-bit data, which is stored in the ADC_RES register.

Its value is: ADC_RES[0:7]=256 X Vin/Vcc

In order to use the converted values ​​to compile other control programs and debug, the converted data can be sent through the serial port and then monitored using the serial port debugging tool . This involves the compilation of the serial port communication program.

Compilation of serial communication program (only send but not receive):

1. To improve the adaptability of the program, you can first define the communication baud rate and crystal oscillator frequency.

#define BAUD 9600 //9600 baud rate

#define FOSC 11059200L //11.0592M crystal

2. Serial port initialization

Void initUART()

{

 SCON=0X5A; //8-bit data, no check bit

 TMOD=0X20; //Set timer 1 to work in 8-bit auto-reload mode

 TH1=TL1=-(FOSC/12/32/BAUD); //T1 initial value

 TR1=1; //Start timer 1

}

Of course, this program can also be automatically generated using the baud rate calculator provided by STC, as follows:

void UartInit(void)         clock is Fosc/12, i.e. 12T

AUXR &= 0xfe; //Serial port 1 selects timer 1 as the baud rate generator

TMOD &= 0x0f; // Clear timer 1 mode bit

TMOD |= 0x20; //Set timer 1 to 8-bit auto-reload mode

TL1 = 0xFD; //Set the initial timing value

TH1 = 0xFD; //Set the timer reload value

ET1 = 0; //Disable timer 1 interrupt

TR1 = 1; //Start timer 1

}

But be careful to declare the special auxiliary register AUXR

Sfr AUXR=0X8E;

3. Send data

void senddata(fly data)

{

 While(!TI); //Wait for the previous data to be sent

 TI=0; //TI flag cleared

 SBUF=dat; //Send current data

}

In this way, you can see the data in the serial port debugging software as long as you call the send data function.

Of course, you can also use 74HC595 and digital tube to make a display circuit for monitoring . This circuit will be summarized later.

Reference address:STC12C5201AD chip AD application summary

Previous article:DS18B20-64 bit serial code reading program
Next article:Application summary of PWM function of STC12C5201AD microcontroller

Latest Microcontroller Articles
  • 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)
    Since development under LINUX is still quite troublesome, is there a more convenient and simple development method under WINDOWS? The answer is yes. Of course, it is not a development tool like ADS, because it ...
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
  • Learn ARM development(18)
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号