Problems encountered in the Project (1)

Source: Internet
Author: User

There is a small subject to be done during the summer vacation. Today is the second day of the claim. During the past two days, I encountered some problems. Here I will record these issues as my Special fortune. I will be very happy if I can help people who encounter the same problem.

The following are some of my first mistakes. Some problems are simple, but they are easy to get killed if you don't pay attention to them or have never been in touch with them before.

(1) When using stm32 for serial communication, there is a strange phenomenon: the first byte sent will be lost. My code was like this at the beginning:

Void uartmediasend (u8 * pbuffer)
{
While (* pbuffer ))
{
Usart1-> DR = * pbuffer ++;
While (usart_getflagstatus (usart1, usart_flag_tc) = 0); // wait until the sending is complete. If the sending is complete, set the hardware to 1.
}
}

When I carefully read the manual, I found that the TC and txe flags were set to 1 at the reset time, so that the first while loop was useless. This causes the first character to be overwritten before being output, resulting in actual loss. The solution is as follows:

Void uartmediasend (u8 * pbuffer)
{
Usart_getflagstatus (usart1, usart_flag_tc );
/* Small CPU defect: the serial port is configured. If direct sending is performed, 1st bytes cannot be sent.
The preceding statement solves the problem that 1st bytes cannot be correctly sent */
While (* pbuffer ))
{
Usart1-> DR = * pbuffer ++;
While (usart_getflagstatus (usart1, usart_flag_tc) = 0); // wait until the sending is complete. If the sending is complete, set the hardware to 1.
}
}

(2) A laser ranging sensor is used in the project. The output format is RS232. At that time, I did not know what was going on. I directly connected the Tx and Rx pins of the sensor to the RX and TX pins of the stm32. As you can imagine, debugging takes a long time, which is a problem. Suddenly, the output level of the microcontroller is TTL level, and their levels do not match. Then I added a maxcompute, and everything was OK.

(3) Speaking of this maxcompute, it was also chilling. Because maxcompute is a 5 V device, I directly connected it to the serial port pin of stm32. When the blind cat hits the dead mouse, the two pins are 5 V tolerant. Use max3232 decisively next time.

(4) Let's Talk About the laser sensor. After sending instructions to the sensor via the serial port, the sensor will return data from the serial port. However, I enable the serial port to receive the interrupt, but I did not write the serial port to receive the interrupt handler function. Then we found the program dead. My test program is to write a sending command, and then under this sending command is to light up a light, found that the light has not been on. Because the program receives the data, it needs to run to the receiving interrupt function. It cannot find this entry point and is dead. Therefore, it cannot execute the command for lighting.

(5) code for converting an ASC code into a 10-digit code in the program:

Void a2i (void)
{
U8 I, K;
2010temp = 0;
For (k = 1; k <row-2; k ++) // when the laser sensor continuously measures, the first group of data is incorrect.
{// So starting from k = 1
For (I = 0; I <4; I ++)
{
If (userdata. recbuf [k] [I]! = 0)
{
Temp = (temp <3) + (temp <1); // multiply by 10
Temp + = userdata. recbuf [k] [I] & 0x0f;
}
}
Userdata. recnum [k] = temp;
Temp = 0;
}
}

The premise of this program is that the format of the output data of my sensor is as follows, for example, 508mm. The data 5, 0, and 8 are all sent back using the hexadecimal ASC code. Coincidentally, for example, the ASC code of integer variable 4 is 34. Exactly the 4 behind. So there is this sentence in the program: userdata. recbuf [k] [I] & 0x0f;

(6) typedef Union
{
Unsigned char recbuf [60] [4];

Int recnum [60];

}
Userdata;
Userdata xdata userdata;

This uses a consortium. I didn't use it when I used to write a program. I just want to learn about it here.

The space of the consortium must be large enough to accommodate the "widest" member; all members reference the same location in the memory. When you want to store different things in the same location at different times, you can use Union.

The above code can reduce the memory occupied by the array declaration, otherwise int recnum [60]; it will occupy another memory.

The above is what we learned in the past two days. Some things may not be worth mentioning in front of the gods, and some things may not be right. If something is wrong, please give your advice.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.