In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail the principles and key points of atmega128a single-chip microcomputer programming and how to solve the data dislocation received by serial ports. The content of the article is of high quality, so the editor will share it for you to make a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
As the elite of 8-bit single-chip microcomputer, atmega128a is really an ideal master chip for low-end and middle-end product research and development. Personally, I think it is suitable for this level of product development. Because its main frequency can only be within 16MHz. Compared with other mcu products that are often hundreds of megahertz, it is really not high-end. For 8-bit single-chip microcomputer, it has 128k program storage space. There is also a 4k ROM,4k epprom for permanent storage of data and a variety of serial development interfaces. These are his strengths. Our software can run well on 3 major operating systems to meet your same needs in different systems.
Below, I will combine my project to illustrate several usage and experimental phenomena for your reference.
In this project, timers are used to send data at least once a second. The serial port zero exchanges data with the sensor, and uses the liquid crystal display to display the received sensor data, which is also collected once a second. The serial port is used to send data at a fixed time, and the transmission frequency depends on whether the timer is the lowest second here. When the project is initialized, the timer is initialized, the serial port is zero and the serial port is one, and then the global interrupt is turned on. This is all done in initialization. There is a strange phenomenon when receiving data through serial port, which often occurs in my previous projects. It's still the same here. The strange phenomenon is that the serial port receives 8 bytes of data, but sometimes data dislocation occurs. For example, send 0102030405060708, after receiving, it becomes 0801020304050607. Isn't that weird? The main reason for the dislocation of serial port receiving data is that the frequency of sending data is too fast, which is less than the processing time of serial port interrupt program.
The serial port responded to the new serial port interrupt before it was finished. Some friends may say that I can just empty the array of serial buffers. But try it. It won't work. Through the experiment, I have come to the conclusion that every byte of the data sent by the serial port will produce an interrupt, and after each interrupt is generated, it will be put into the serial port receiving buffer of the single chip microcomputer, which is not the udr register. What I see is that the register UDR reads the data of the buffer received by the serial port of the single-chip microcomputer. The serial port receiving buffer should be inside the single-chip microcomputer. When the data is sent, all the data first enters the serial port receiving buffer of the single-chip microcomputer, and then reads the data from the buffer to the register udr, then we read the data received by the serial port when we read the register udr0 or udr1 in the interrupt program. How can we prevent it? What about the misalignment of data reception? That is to clear the internal serial port of the single-chip microcomputer to receive the buffer data. How to do it? It's simple. Only need to re-initialize the serial port, so that the serial buffer data of the single-chip microcomputer will be emptied, that is, the misplaced data will be ignored to continue to receive.
Initialization of timers and serial ports. How to set the value of various registers? Here I recommend a tool. Iccavr. After setting the CPU, the gadget can set the timer timing time, frequency division and serial port baud rate to register. It's very convenient.
For interrupts that are not used in the program, do not set the interrupt flag to write 1. When I initialize in the serial port, I set the serial port send interrupt to one. The result is a CPU reset. If you can't detect whether the CPU is reset, you need to design a led indicator on your development board. When the program is initialized, the LED is lit. Turn off the indicator light when the program enters the main loop. This way, if you have a CPU run exception while debugging. The running status of the program can be judged by the indicator light.
On the atmega128a single-chip microcomputer programming to talk about the principle and key points and how to solve the serial port receiving data dislocation to share here, I hope the above content can be of some help to everyone, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.