This is a small experiment I did last semester, but I think the control MCU is more direct than the control arm, because some of the arm is better than the MCU encapsulation, so you are not very in-depth.
If you have learned MCU, it will be easier for you to learn arm, but arm is a little more complicated than MCU.
This code was removed from my previous blog, but I used Sina before. Now I think Sina is an entertainment blog, but blog garden, I think it is a real technical blog website. I hope the blog will stick to this purpose in the future. Another reason I like the blog garden is that there are not many advertisements, such as msdn. There are too many advertisements. I have also written this on msdn before, but now I think it is not pure, there are too many advertisements. I don't know why they started msdn. Making money is a problem.
The source code in this article is on my previous Sina Blog. I hope this will be a good guide for beginners of single chip microcomputer or arm. The complete source code is here.
First, let's talk about what this program mainly does. In fact, it is to control the dynamic display of two digital tubes, one ten digits and the other ten digits. From 0.0 to 9.9. By the way, I use 51 single-chip microcomputer, and the model is stc12c5a32s2.
Take a look at the main function this time:
1 void main (void)
2 {
3 int x = 0;
4 num = 0;
5 p0_0 = 0;
6 p0_1 = 0;
7 p0_2 = 0;
8 p0_3 = 0;
9 p0_4 = 0;
10
11 timer0_init ();
12
13 (;;){
14 if (x +++> 100) x = 0;
15 switch (X % 2)
16 {
17. Delay (100 );
18 case 0:
19 p0_4 = 0; // disable
20 P2 = leddata0 [behinddot]; // you can specify a number.
21 p0_3 = 1; // Enabled
22 break;
23 Case 1:
24 p0_3 = 0;
25 P2 = leddata1 [frontdot];
26 p0_4 = 1;
27 break;
28}
29}
30}
The program starts with the main function, so let's start with the main function and explain it separately:
X controls which digital display is enabled, and num controls the displayed data value. Because it ranges from 0.0 to 9.9, but the integer number does not have decimal places, the numbers of digits and digits are taken for num, consider the ten digits as the single-digit data to be displayed and the single-digit data as the very-digit data to be displayed. These two functions are only initialized to 0 in the main function.
Because I used a lot of digital tubes on the single-chip microcomputer board, so the p0_0 to p0_4 are initialized to 0, so that they are not able to make all.
Timer0_init () is the initialization of the timer. Since we explain it in the running order of the program, let's take a look at how it is initialized.
View code
1 void timer0_init()
2 {
3 TMOD = 0x01;
4 TH0 = 0xfc;
5 TL0 = 0x65;
6 EA = 1;
7 ET0 = 1;
8 TR0 = 1;
9 }
Now let's explain one line:
3: set the timer 0 to the Working Mode 1, the interruption only needs to be set once
4, 5: Set the initial count value, respectively set the high eight bits and the low eight bits
6. Total disconnection
7: The timer interrupt is 0. This single-chip microcomputer has two timers. I can use only one timer in this experiment.
8: start the timer 0. When the count is full, run the interrupted service program timer0 ()
In fact, these are very similar to the micro-computer interface. As long as you have learned the micro-computer interface, you should be familiar with these things.
Let's take a look at the interrupted service program:
View code
1 void timer0() interrupt 1
2 {
3 TH0 = 0xfc;
4 TL0 = 0x65;
5 xms++;
6 if(xms == 1000){
7 num++;
8 if(num > 99) num = 0;
9 FrontDot = num / 10;
10 BehindDot = num % 10;
11 xms = 0;
12 }
13 }
3, 4: These two rows are very important. After the first full count, the count starts from 0. The initial value of the number should be re-designed in the interrupt program, otherwise it will start from 0, leading to inaccurate timing.
5: XMS records the number of times it has been timed. The sum is the time of one second. Change the number in one second and start from 0 at 1000.
8: Start from 0 when the value is greater than 99
9, 10: 10 digits and one digit respectively
Next, let's look at the main function:
There is an endless loop. We used to write programs to avoid it. But embedded systems seem to advocate an endless loop. In fact, OS is an endless loop.
When the remainder of X is 0, p0_3 is displayed, p0_4 is not displayed, and X is 1, which is the opposite. Leddata0 and leddata1 are an array that controls the display data of digital tubes, as shown below:
Uint leddata0 [10] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f };
Uint leddata1 [10] = {0xbf, 0x86, 0xdb, 0xcf, 0xe6, 0xed, 0xfd, 0x87, 0xff, 0xef };
Delay () is a software latency function that can be adjusted by yourself. As long as it can be correctly displayed, no code will be posted here.
Well, the program structure is like this. Today is Wednesday. I wish you a pleasant job and study!