Single Chip Microcomputer, at first I still don't know what this thing is doing. until the winter vacation of the year before, I bought a 51 board and started my single chip microcomputer career. At the beginning, I still don't know how this thing and the program are integrated. Is it so easy to write a few lines of programs to control the single chip microcomputer? Silly, according to the information given, I started a program of LED lights first. Ah, this magical thing happened. The lamp lit one by one and it was quite beautiful.
Looking at the code, there are just a few lines of code. After talking about ACM, this program should be super simple. I am not very familiar with it at the beginning. So I began to understand it slowly. Below are various flow lights I wrote. The Code has been annotated and should be well understood. The specific circuit diagram is also below.
# Include <reg51.h> // This is the header file of 51 single-chip microcomputer. It mainly includes registers, such as P0 and P1. // These registers are 8-bit, void delay (unsigned int K); // This is the latency function defined by myself. Watch the light on and off, in fact, it depends on this function to determine the length of the void main (void) // main function, which is not required, anyone who has learned C knows {unsigned char I, temp; // defines the variables I, and temp, mainly because the 51 registers are 8 bits, it indicates the number between 0 and //, so it is defined by unsigned char. While (1) // in an endless loop, isn't it always a fl lamp? Therefore, this function must be running all the time, so it is to make it run cyclically and cyclically. Do not stop, or exit the main function. {// 8 flow lights from the D8-D1 one by one flashing temp = 0x01; // 0x01 with binary is 0000 0001, right? For (I = 0; I <8; I ++) {P2 = ~ Temp; // the inverse of temp is 1111 1110, and then assigned to P2, that is, P2 is connected to the LED, // each lamp is one of P2, this indicates that the P2 0-bit light is low, so the light is on/(the Board is low) delay (500 ); // This is a period of time, that is, let the first lamp shine for a Period of Time Temp <= 1; // temp moved one to the left, that is, 0000 0010, and then assign the value to P2. P2 // that is, P2 = 1111 1101, that is, the second light is on, and then you will know that this is slowly from // The first light to the eighth light, the light of a light is the light of a flow.} // 8 flow lights from the D1-D8 one by one flashing temp = 0x7f; // here temp is 0111 1111 for (I = 0; I <8; I ++) {P2 = temp; // P2 is 0111 1111, that is, 8th lamp is lit, and then the following is the same as the first lamp to the eighth lamp above, here I just want to reverse, from 8th to the First lamp delay (500); temp >>= 1; temp | = 0x80 ;} // 8 sequential flow lights from D8-D1 all light temp = 0xff; // temp = 1111 1111 for (I = 0; I <8; I ++) {P2 = temp; // P2 is 1111 1111 at the beginning, so all of them are extinguished. Delay (500); temp <= 1; // then temp = 1111 1110 1111, then the second light is lit, and then temp will become 1100, // is the first light and the second light are lit, and then slowly all the lights are to give him light} // 8 sequential flow lights from D1-D8 all light temp = 0x7f; // This is exactly the opposite of the above for (I = 0; I <8; I ++) {P2 = temp; delay (500); temp >>= 1 ;} p2 = 0xff; delay (1500); // the odd number lights up P2 = 0x55; // This P2 = 0101 0101, that is, those lights with 0 are on, the lights of 1 are off. Delay (1500); P2 = 0xff; delay (1500); // even light P2 = 0xaa; // P2 = 1010 1010 1500, same as the above delay ); p2 = 0xff; delay (1500); // All light up P2 = 0x00; // P2 = 0000 0000, all the other light up. Delay (1500); P2 = 0xff; delay (1500); // binary addition, which starts from 0 and increases from 0 to 15 P2 = 0xff; // P2 = 1111 1111 at the beginning. so they are all out of for (I = 0; I <15; I ++) {P2 --; // P2 --, that is, P2 = 1111 1110, the first light is on, and then subtract 1 from 1111 to 1101 ., this is the result of the 2 // hexadecimal addition. The actual experiment effect is obvious: delay (1000);} delay (1000); // The binary alkali method, which starts at 15, from 15 to 0 for (I = 0; I <15; I ++) {P2 ++; // This is similar to the preceding delay (1000 );} delay (1000) ;}} void delay (unsigned int K) {unsigned char J; For (; k --) for (j = 0; J <255; j ++); // view? Magic, isn't it a waste of time? If ACM contains this, you will be scolded by // teammates. This will increase the time complexity of my algorithm. In fact, this/is just a wonderful place. If you don't do anything, you just need to keep the lights in this status. // there is still a difference between single-chip microcomputer and program programming}
Okay. Now, you should understand the flow lamp? The main board is gone, so the effect has not been put up, but I believe that you like single-chip microcomputer. If you want to learn it, you will certainly buy one, and you will know it after testing everywhere. The Magical single-chip microcomputer world, life is beautiful for you. Hahaha, it will be easier to get started with single-chip microcomputer and then learn pic, FPGA, arm, etc. Therefore, learning this is the foundation. I have been going to bed, and there are still many tasks to complete tomorrow. Come on!