Method for sending incremental data cyclically & converting BCD code and ASCII code & fgets description

Source: Internet
Author: User

In some test programs, you sometimes need to send data cyclically to view the feedback results to obtain the correct parameters. For example, when writing a data in I2C, if a debugging tool can adjust the write parameters to obtain an accurate register value, if there is no debugging tool, you can observe the feedback value by writing data in a loop to obtain the accurate value.

As follows:

Int cm3623_write_ps () {u8 Buf = 0x01, I; for (I = 0; I <128; I ++) {ret = mt6573_i2c_polling_write (0, 0xf0, Buf, 1 ); // The Buf value is assigned to the unsigned char * If (ret = 0) {printf ("cm3623_write_ps value is 0x % x \ r \ n", Buf );} BUF + = 0x02; mdelay (5000); // keep the result of the oscilloscope for 5 seconds. Msleep can be used in the kernel. This value must be short, otherwise it will easily cause I2C bus errors and system restart} printf ("cm3623_write_ps result is % s \ r \ n", (ret = 0 )? ("OK") :( "fail"); If (Ret> 0) {return-1;} return 0 ;}

========================================================== ========================================================== ========================================================== ====

When installing firmware for some devices, you need to use the version number as the basis for judging whether either side is the version number read from the chip, and the other side is the version number retrieved from the new firmware. For example, the read chip firmware is 0xf3, And the firmware corresponds to 0x0f and 0x03. The former is a BCD value, and the latter is two ASCII code values. In fact, they are the same version numbers. The process of converting ASCII to BCD value is as follows:

Static unsigned char ms6000_getlibver (void) {unsigned char a, B; If (ms6000ctpm_fw [156] <'A ') // The 156 element in the firmware array is the 'F' ASCII code A = (ms6000ctpm_fw [156]-'0 '); // process elsea from '0' to '9' = (ms6000ctpm_fw [156]-'A' + 10 ); // processing from 'A' to 'F' if (ms6000ctpm_fw [157] <'A ') /// the 157 element in the firmware array is '3' ASCII code B = (ms6000ctpm_fw [157]-'0 '); elseb = (ms6000ctpm_fw [157]-'A' + 10); Return (A <4) + B); // A <4 this must be enclosed in parentheses, because the shift operator has a lower priority than +}

The above 243 is returned, exactly the same as the 0xf3 value 243. The reason for separate 0-9 and A-F values is 0x46-0x30, which is not 16, but 0x16.

========================================================== ========================================================== ======================================

The fgets function is an I/O library function for Linux ansic that reads a row of characters at a time. The prototype is char * fgets (char * s, int N, file * stream ); it is used to read (n-1) strings from the stream file. After reading, a null ending character is written after the last character. For example, if the content of the SSS. I file is ubcdaag, run the following program:

int _tmain(int argc, _TCHAR* argv[]){char s[5];FILE *fp;fp = fopen("sss.i","r");if (fp == NULL){puts("open file fail.");}fgets(s,3,fp);printf("data from is %s \r\n",s);fclose(fp);return 1;}

The screen ECHO is UB. This is easy to understand. The problem is that when a variable attribute is read in Linux, the variable value is 0xf4 before, so the actual reading is based on its decimal 244 as a string; that is to say, to completely read this variable attribute, the second parameter of fgets is 4. If it is 3, only 24 is read.

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.