MCU Exercise - AD Conversion

Publisher:柳絮轻风Latest update time:2016-12-29 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Continuing like yesterday, today I also used the TX-B development board to refer to the experimental requirements and source code in the application of analog-to-digital conversion ADC0804, and improved some codes myself.

Connection circuit diagram of ADC0804 and single-chip microcomputer:


Connection circuit diagram of 6-digit digital tube and single-chip microcomputer


The principle of dynamic scanning display of digital tube: Take the six-digit digital tube display 123456 as an example as follows: first let the first digital tube display 1, and the rest are all dark. 1 is about a few milliseconds, and then extinguished. Immediately afterwards, let the second digital tube display 2, and the rest are all dark. 2 is also bright for a few milliseconds. In this way, the sixth digital tube is lit, and then it comes back to display 1. This cycle continues at a very fast speed. Since the visual retention time of the human eye is about 20 milliseconds, it is not felt that there are dark digital tubes. What we see is that six digital tubes are displaying at the same time, and the value is 123456. If we slow down this process a little bit, we see that the first digital tube displays 1, then moves to the second and then displays 2. That is to say, only one digital tube is bright at any time.

ADC0804: ADC0804 is an 8-bit full MOS medium-speed A/D converter. It is a successive approximation A/D converter with a three-state data output latch on the chip, which can be directly interfaced with a single-chip microcomputer. Single-channel input, the conversion time is about 100us. The conversion timing of ADC0804 is: when CS=0, A/D conversion is allowed. When WR changes from low to high, A/D starts to convert, and a total of 66-73 clock cycles are required for one conversion. When CS and WR are valid at the same time, A/D conversion is started, and the INTR signal (low level is valid) is generated at the end of the conversion, which can be used for query or interrupt signal. The data results can be read under the control of CS and RD. The INTR signal is not used in this experiment.

Source code:


ADC0804

  1#include

  2//Read the analog quantity converted into digital quantity by ADC0804 and display the corresponding value

  3#include //To use the _nop_() function, delay a no-operation time

  4

  5#define uchar unsigned char

  6

  7unsigned char j, k;

  8//Delay function, for example, if i=10, the delay is about 10ms.

  9void delay(unsigned char i)

 10{

 11    for(j = i; j > 0; j--)

 12    {

 13        for(k = 125; k > 0; k--);

 14    }

 15}

 16

 17unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,

 18 0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //0-F digital tube coding (common cathode)

 19

 20//Latch end of the latch that controls the digital tube segment selection. Latch after the falling edge, that is, first set dula = 1, then set dula = 0 to complete the latch

 21sbit dula=P2^6;  

 22//Latch end of the latch that controls the digital tube bit selection process is similar to dula

 23sbit temperature=P2^7;

 24sbit adrd = P3^7; //A rising edge pulse, turn on ADC reading

 25sbit adwr = P3^6; //A rising edge pulse, start ADC

 26uchar ds[] = {0, 0, 0}; //Store the conversion result

 27sbit adcs = P0^7; //ADC chip select, low level is effective

 28

 29//datas[0] hundreds digit, datas[1] tens digit, datas[2] ones digit

 30void display(uchar datas[]) 

 31{

 32 uchar count;

 33    for(count = 0; count < 3; count++)

 34    {

 35 //Chip select

 36 temperature = 0; 

 37 P0 = ((0xfe << count) | (0xfe >> (8 - count))) & 0x7f; //Select the (count + 1)th digital tube

 38 wela = 1; //Open the latch and give it a falling edge

 39 temperature = 0;

 40 //Segment selection

 41 games = 0;

 42 P0 = table[datas[count]]; //Display numbers

 43 dula = 1; //Open the latch and give it a falling edge

 44 games = 0;

 45 delay(5); //delay 5ms, light on for 5ms

 46

 47 // Clear the segment first, turn off the digital tube, remove the influence on the next bit, and remove the ghosting of the high bit on the low bit

 48 //If you want to know the effect, you can remove this code

 49 //Because the digital tube is a common cathode, all the codes for extinguishing are: 00H

 50 games = 0;

 51 P0 = 0x00; //Display numbers

 52 dula = 1; //Open the latch and give it a falling edge

 53 games = 0;

 54    }

 55}

 56

 57//Start ADC

 58void ADStart()

 59{

 60 adcs = 0; //Select ADC

 61    _nop_();

 62 adwr = 0;

 63    _nop_();

 64 adwr = 1; //A rising edge pulse

 65    _nop_();

 66    adcs = 1;

 67}

 68

 69 //Read ADC

 70uchar ADRead()

 71{

 72 fly tmp;

 73 adcs = 0; //Select ADC

 74    _nop_();

 75 adrd = 0; //Ready to read

 76    _nop_();

 77    _nop_();  

 78 tmp = P1; //Read ADC data

 79    adrd = 1;

 80    _nop_();

 81    adcs = 1;

 82    return tmp;

 83}

 84

 85void main()

 86{

 87    uchar advalue, icount;

 88    

 89    while(1)

 90    {

 91 //Start AD conversion

 92        ADStart();

 93 //Read AD

 94        advalue = ADRead();

 95        

 96 ds[0] = advalue / 100; //hundreds digit

 97 ds[1] = (advalue % 100) / 10; // tens digit

 98 ds[2] = advalue % 10; //unit digit

 99 //Loop display fifty times, then sample the voltage, leaving enough time for ADC0804 to complete the analog-to-digital conversion

100        for(icount = 0; icount < 50; icount++)

101        {

102            display(ds);

103        }

104    }

105}


Especially for the digital tube display function display()

 1 //datas[0] hundreds digit, datas[1] tens digit, datas[2] ones digit2
 void display(uchar datas[]) 
 3 {
 4     uchar count;
 5     for(count = 0; count < 3; count++)
 6     {
 7         //Chip select
 8         wela = 0; 
 9         P0 = ((0xfe << count) | (0xfe >> (8 - count))) & 0x7f; //Select the (count + 1)th digital tube
10         wela = 1; //Open the latch and give it a falling edge
11         wela = 0;
12         //Segment select
13         dula = 0;
14         P0 = table[datas[count]]; //Display number
15         dula = 1; //Open the latch and give it a falling edge
16         dula = 0;
17         delay(5); //Delay 5ms, i.e. light 5ms
18
19         //Clear segment first, Make the digital tube turn off, remove the influence on the next bit, and remove the ghosting of the high bit on the low bit
20         //If you want to know the influence effect, you can remove this code by yourself
21         //Because the digital tube is common cathode, all the codes for turning off are: 00H
22         dula = 0;
23         P0 = 0x00; //Display number
24         dula = 1; //Open the latch and give it a falling edge
25         dula = 0;
26     }
27 }


If the 4 lines of code that turn off the digital number are removed, the display effect will be as follows:


The ideal effect should be as follows:


This is because the latch of 74HC573, the result of the previous bit is latched, and after the chip is selected, it is also displayed on the selected bit.
As long as the latched result is cleared before the chip is selected, this effect can be eliminated.


Reference address:MCU Exercise - AD Conversion

Previous article:Microcontroller Exercise - Simulating a Phone Keypad
Next article:Microcontroller Exercise - DA Conversion

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
Guess you like

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号