51 MCU AD conversion program

Publisher:点亮未来Latest update time:2015-08-10 Source: 21ic Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
  1. void Read_init (unsigned char CHA){
  2. unsigned char AD_FIN=0; //Store A/D conversion flag
  3. CHA &= 0x07; //Select one of the 8 interfaces of ADC (0000 0111 clears the high 5 bits)
  4. ADC_CONTR = 0x40; //ADC conversion speed (0XX0 0000 where XX controls the speed, please set according to the data sheet)
  5. _nop_();
  6. ADC_CONTR |= CHA; //Select the current A/D channel
  7. _nop_();
  8. ADC_CONTR |= 0x80; //Start A/D power supply
  9. DELAY_MS(1); //Let the input voltage reach stability (1ms is enough)
  10. }
  11. unsigned char Read (void) {
  12. unsigned char AD_FIN=0; //Store A/D conversion flag
  13. ADC_CONTR |= 0x08; //Start A/D conversion (0000 1000 makes ADCS = 1)
  14. _nop_();
  15. _nop_();
  16. _nop_();
  17. _nop_();
  18. while (AD_FIN ==0){ //Wait for A/D conversion to end
  19. AD_FIN = (ADC_CONTR & 0x10); //0001 0000 Test whether A/D conversion is finished
  20. }
  21. ADC_CONTR &= 0xE7; //1111 0111 clear ADC_FLAG bit, turn off A/D conversion,
  22. return (ADC_DATA); //Return A/D conversion result (8 bits)
  23. }

Above - 8-bit ADC program module (applicable to STC12C2052AD series)
C CodeFavorite Code
  1. #include //MCU header file
  2. #include //51 basic operations (including _nop_ empty function)
  3. void DELAY_MS (unsigned int a){
  4. unsigned int i;
  5. while( a-- != 0){
  6. for(i = 0; i < 600; i++);
  7. }
  8. }
  9. void UART_init (void) {
  10. //EA = 1; //Enable general interrupts (if interrupts are not used, they can be //masked)
  11. //ES = 1; //Enable UART serial port interrupt
  12. TMOD = 0x20; //Timer T/C1 working mode 2
  13. SCON = 0x50; //Serial port working mode 1, serial port reception is allowed (serial port reception is prohibited when SCON = 0x40 )
  14. TH1 = 0xF3; //Set the high 8 bits of the timer initial value
  15. TL1 = 0xF3; //Set the initial value of the timer to the lower 8 bits
  16. PCON = 0x80; //Baud rate multiplication (shield the baud rate of this sentence to 2400)
  17. TR1 = 1; //Timer starts
  18. }
  19. void UART_T (unsigned char UART_data){ //Define the serial port sending data variable
  20. SBUF = UART_data; //Send the received data back
  21. while(TI == 0); //Check the send interrupt flag
  22. TI = 0; //Set the transmit interrupt flag to 0 (cleared by software)
  23. }
  24. void Read_init (unsigned char CHA){
  25. unsigned char AD_FIN=0; //Store A/D conversion flag
  26. CHA &= 0x07; //Select one of the 8 interfaces of ADC (0000 0111 clears the high 5 bits)
  27. ADC_CONTR = 0x40; //ADC conversion speed (0XX0 0000 where XX controls the speed, please set according to the data sheet)
  28. _nop_();
  29. ADC_CONTR |= CHA; //Select the current A/D channel
  30. _nop_();
  31. ADC_CONTR |= 0x80; //Start A/D power supply
  32. DELAY_MS(1); //Let the input voltage reach stability (1ms is enough)
  33. }
  34. unsigned char Read (void) {
  35. unsigned char AD_FIN=0; //Store A/D conversion flag
  36. ADC_CONTR |= 0x08; //Start A/D conversion (0000 1000 makes ADCS = 1)
  37. _nop_();
  38. _nop_();
  39. _nop_();
  40. _nop_();
  41. while (AD_FIN ==0){ //Wait for A/D conversion to end
  42. AD_FIN = (ADC_CONTR & 0x10); //0001 0000 Test whether A/D conversion is finished
  43. }
  44. ADC_CONTR &= 0xE7; //1111 0111 clear ADC_FLAG bit, turn off A/D conversion,
  45. return (ADC_DATA); //Return A/D conversion result (8 bits)
  46. }
  47. void main (void) {
  48. unsigned char R;
  49. UART_init(); //Serial port initialization program
  50. Read_init(0); //ADC initialization
  51. P1M0 = 0x01; //P1.7~.0: 0000 0001 (high impedance) //Note: When changing the ADC channel, the corresponding IO interface must be changed to high impedance input at the same time.
  52. P1M1 = 0x00; //P1.7~.0: 0000 0000 (forced push)
  53. while(1){
  54. R = Read ();
  55. UART_T (R); //Serial port secretary, send ADC reading value to serial port for display
  56. }
  57. }

Above - 8-bit ADC application example (applicable to STC12C2052AD series)
C CodeFavorite Code
  1. void Read_init (unsigned char CHA){
  2. unsigned char AD_FIN=0; //Store A/D conversion flag
  3. CHA &= 0x07; //Select one of the 8 interfaces of ADC (0000 0111 clears the high 5 bits)
  4. ADC_CONTR = 0x40; //ADC conversion speed (0XX0 0000 where XX controls the speed, please set according to the data sheet)
  5. _nop_();
  6. ADC_CONTR |= CHA; //Select the current A/D channel
  7. _nop_();
  8. ADC_CONTR |= 0x80; //Start A/D power supply
  9. DELAY_MS(1); //Let the input voltage reach stability (1ms is enough)
  10. }
  11. unsigned int ADC_Read (void) {
  12. unsigned char AD_FIN=0; //Store A/D conversion flag
  13. ADC_CONTR |= 0x08; //Start A/D conversion (0000 1000 makes ADCS = 1)
  14. _nop_();
  15. _nop_();
  16. _nop_();
  17. _nop_();
  18. while (AD_FIN ==0){ //Wait for A/D conversion to end
  19. AD_FIN = (ADC_CONTR & 0x10); //0001 0000 Test whether A/D conversion is finished
  20. }
  21. ADC_CONTR &= 0xE7; //1111 0111 clear ADC_FLAG bit, turn off A/D conversion,
  22. return (ADC_RES*4+ADC_RESL); //Return the A/D conversion result (the high 8 bits of the 10-bit ADC data are in ADC_RES, and the low 2 bits are in ADC_RESL)
  23. }

Above - 10-bit ADC program module (for STC12C5A60S2 series)
C CodeFavorite Code
  1. #include //MCU header file
  2. #include //51 basic operations (including _nop_ empty function)
  3. void DELAY_MS (unsigned int a){
  4. unsigned int i;
  5. while( a-- != 0){
  6. for(i = 0; i < 600; i++);
  7. }
  8. }
  9. void UART_init (void) {
  10. //EA = 1; //Enable general interrupts (if interrupts are not used, they can be //masked)
  11. //ES = 1; //Enable UART serial port interrupt
  12. TMOD = 0x20; //Timer T/C1 working mode 2
  13. SCON = 0x50; //Serial port working mode 1, serial port reception is allowed (serial port reception is prohibited when SCON = 0x40 )
  14. TH1 = 0xF3; //Set the high 8 bits of the timer initial value
  15. TL1 = 0xF3; //Set the initial value of the timer to the lower 8 bits
  16. PCON = 0x80; //Baud rate multiplication (shield the baud rate of this sentence to 2400)
  17. TR1 = 1; //Timer starts
  18. }
  19. void UART_T (unsigned char UART_data){ //Define the serial port sending data variable
  20. SBUF = UART_data; //Send the received data back
  21. while(TI == 0); //Check the send interrupt flag
  22. TI = 0; //Set the transmit interrupt flag to 0 (cleared by software)
  23. }
  24. void Read_init (unsigned char CHA){
  25. unsigned char AD_FIN=0; //Store A/D conversion flag
  26. CHA &= 0x07; //Select one of the 8 interfaces of ADC (0000 0111 clears the high 5 bits)
  27. ADC_CONTR = 0x40; //ADC conversion speed (0XX0 0000 where XX controls the speed, please set according to the data sheet)
  28. _nop_();
  29. ADC_CONTR |= CHA; //Select the current A/D channel
  30. _nop_();
  31. ADC_CONTR |= 0x80; //Start A/D power supply
  32. DELAY_MS(1); //Let the input voltage reach stability (1ms is enough)
  33. }
  34. void ADC_Read (void) {
  35. unsigned char AD_FIN=0; //Store A/D conversion flag
  36. ADC_CONTR |= 0x08; //Start A/D conversion (0000 1000 makes ADCS = 1)
  37. _nop_();
  38. _nop_();
  39. _nop_();
  40. _nop_();
  41. while (AD_FIN ==0){ //Wait for A/D conversion to end
  42. AD_FIN = (ADC_CONTR & 0x10); //0001 0000 Test whether A/D conversion is finished
  43. }
  44. ADC_CONTR &= 0xE7; //1111 0111 clear ADC_FLAG bit, turn off A/D conversion,
  45. }
  46. void main (void) {
  47. UART_init(); //Serial port initialization program
  48. Read_init(0); //ADC initialization
  49. P1M1 = 0x01; //P1.7~P1.0: 0000 0001 (high impedance) //Note: When changing the ADC channel, the corresponding IO interface must be changed to high impedance input at the same time.
  50. P1M0 = 0x00; //P1.7~P1.0: 0000 0000 (forced)
  51. while(1){
  52. ADC_Read (); //Call ADC conversion program
  53. UART_T (ADC_RES); //Serial port secretary, send the high 8 bits of the ADC read value to the serial port 0000 0000
  54. UART_T (ADC_RESL); //Serial port secretary, send the lower 2 bits of the ADC read value to the serial port XXXX XX00
  55. }
  56. }
Reference address:51 MCU AD conversion program

Previous article:What is a stack? An example of using the 51 MCU stack pointer SP
Next article:51 MCU timer/counter, interrupt

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号