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
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.
Previous article:Microcontroller Exercise - Simulating a Phone Keypad
Next article:Microcontroller Exercise - DA Conversion
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications