The experience of using array, strnpy function and Atoi function in DSP engineering

Source: Internet
Author: User

This is what I encountered when using DSP to implement a function, the program requires to get a character array within the specified position of the data.

1. How to declare an array:

There are several ways to declare an array:

    CharCh[4] = {'a','b','C',};//mode 1 {} also can not fill in the data    Charch2[]="Multi";//Mode 2 This way of declaring the most stable in the development of DSP engineering    Char*ch3 ="Micro";//Way 3 Pointer way    Char*ch4[3] = {"i" " Love""," China"};//Mode 4

At that time I used in the DSP program is the first way to accept the Scia serial data sent over

Char rxabuffer[] = {""};

But the results show that the data is problematic, in using CCS software to look at the memory address of the rxabuffer array data found that the data is not just 16 bits, but more than a few, and the array at the end there is no ' s ' bit. At the time of doubt, we use Eclipse to define the array in this way, and after debugging, we find that there are ' s ' bits in the array. So the sense that the array is used under standard C and under engineering is somewhat different, and the reason for this is that it is above the end of the array. So the next time you debug the way 2 declaration method, and then in the DSP multiple debugging, and finally found that the data is correct, and the way 2 this way of declaring arrays in the DSP project is the most stable.

The advantage of Mode 2 is that since the length of the array is not initialized, the compiler will automatically calculate the length of the array based on the number of data in {}, and the key is that the compiler will automatically add a trailing ' s ' to the array. In this way, the length of the array is determined by the compiler, reducing human intervention and reducing the probability of program errors.

For mode 3 and Mode 4, tested on the DSP, the data in the Rxabuffer array is correct, but there is a problem using the strncpy function to intercept the specified position data within the Rxabuffer array.

strncpy (RXABUFFER_SELECTEPWM, Rxabuffer, 1);
strncpy (rxabuffer_rotation, Rxabuffer + 2, 1);
strncpy (rxabuffer_epwmfrequency, Rxabuffer + 4, 7);
strncpy (rxabuffer_occpuy_a, Rxabuffer + 12, 2);
strncpy (Rxabuffer_occpuy_b, Rxabuffer + 15, 2);

As shown in the program above, the first three arrays can be correctly intercepted in the Rxabuffer array of data, but rxabuffer_occpuy_a array I send the data in the array, into the rxabuffer_occpuy_a array memory address query, Finds that the data in the array is not the data at the specified location. Debug multiple times, also did not solve the problem.

To sum up, such as the use of arrays in the project, the small part of the recommended way to use the Method 2 declaration, this can not only ensure that the array has enough length space to store data, but also to ensure that the end of the array "

2. About the use of the strncpy function

The purpose of the strncpy function is to get the specified position within the target array, specify the length of the data, and copy the data into a new function. Its usage is

( ten); // Dest is the array to be stored, SRC is the target array for the data to be intercepted, and 10 is the length of the data in the SRC array.

If you want to intercept the data in the middle of the SRC array, you can use this method

5 ( ten); // src + 5 indicates that the data is copied from the 5th data after the SRC array.

Also note that when using the strncpy function, the target array src must have the end character '/', or it will go wrong. This is also why the above is recommended to use the method and the initialization of the array.

3. About the use of the Atoi function

This function is relatively simple to use and is used

INT_SELECTEPWM = Atoi (RXABUFFER_SELECTEPWM);

INT_SELECTEPWM is an int variable, RXABUFFER_SELECTEPWM is an array of type Char

The main guarantee is that there are no other characters in the RXABUFFER_SELECTEPWM array.

The experience of using array, strnpy function and Atoi function in DSP engineering

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.