Add serial port printing information & Problems in use of serial port tools & printk Transformation

Source: Internet
Author: User

Serial Port printing information can be added to any code to facilitate debugging. You do not have to have a printf statement. This can be implemented in a simple way. First, you need to complete the serial port register initialization, including the input and output enabling, data bit and stop bit, baud rate, interrupt priority, and so on.

Typedef unsigned char uchar;

(1) Output a single ASCII character. Because the characters are also expressed in ASCII code

Void writebyte (uchar input) // outputs a single uchar
{
Tdata_urtdat2 = input; // Value Loss sending register
While (txend_ursts2! = 1); // query the sending end flag
}

(2) output character array

Void uu_send (uchar * P, uchar num) // The first address of the P array, with the num Length
{
Uchar I;
For (I = 0; I <num; I ++)
{
Writebyte (P [I]);

}
}

(3) Output string

Void rs232_writestr (uchar * tpstr) // The first address of the tpstr string
{
Uchar I;
For (I = 0; I <60; I ++)
{
If (tpstr [I] = '/0') // end character
{
Break; // calculate the string length
}

}
Uu_send (tpstr, I );
}

(4) Output 8 BCD data. (BCD: 0-> 0000,... 9-> 1001)

Void rs232_writehex81 (uchar tbinnum) // convert the BCD code parameters into ASCII characters for output.

{
Uchar I, gpbtmpbuf [8];
For (I = 0; I <2; I ++)
{
Gpbtmpbuf [I] = (tbinnum & 0xf0 );

Gpbtmpbuf [I] >>= 4;
If (gpbtmpbuf [I]> 9)
{
Gpbtmpbuf [I]-= 10;
Gpbtmpbuf [I] + = 'a ';
}
Else
Gpbtmpbuf [I] + = '0 ';
Tbinnum <= 4;
}
Uu_send (gpbtmpbuf, 2 );
}

(5) output the print statement and a single variable. For example: send_asc ("J2 =", J );
Void send_asc (uchar * STR, uchar num)

{
Rs232_writestr (STR );
Rs232_writehex81 (Num );
Rs232_writestr ("/N"); // line feed
}

(6) output the print statement and multiple variables. For example: JS [2] = {2, 3}; send_buf ("J1 + J2", JS, 2 );

Void send_buf (uchar * STR, uchar * Buf, uchar num) // output a series of array data
{
Uchar I;
Rs232_writestr (STR );
For (I = 0; I <num; I ++)
Rs232_writehex81 (BUF [I]);
Rs232_writestr ("/N ");
}

(7) output 16 BCD data. For example, send_dsa ("value is", 0xf332 );

Void send_dsa (uchar * STR, uint16 num) // output 16-Bit Data
{
Rs232_writestr (STR );
Rs232_writehex81 (uchar) (Num> 8 ));
Rs232_writehex81 (uchar) num );
Rs232_writestr ("/N ");
}

(8) output 32-bit BCD data. For example: send_long ("value is", 0xe4336f9)

Void send_long (uchar * STR, u32 num) // output 32-Bit Data
{
Rs232_writestr (STR );
Rs232_writehex81 (uchar) (Num> 24 ));
Rs232_writehex81 (uchar) (Num> 16 ));
Rs232_writehex81 (uchar) (Num> 8 ));
Rs232_writehex81 (uchar) num );
Rs232_writestr ("/N ");
}

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

Problems encountered when using the serial port Tool

(1) In the use of securecrt, the serial port can only output data that cannot be input. For example, you can only print the trace and cannot enter the command. One possible reason is that you need to turn off the streaming control of RTS/CTS.

(2) If the input of the serial port tool is valid, there should be a low level when pressing the button. Another note: rxd, txd, And Gnd are required, and the first two are high by default. If the hardware of the tool is not fully connected, even if you press the button, there will be no low level online.

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

Printk is the default trace printing statement in Linux. It can be transformed to output more accurate debugging information, including row numbers and function names, the information is defined in the kernel. A simple example is as follows:

The general statement is printk ("zhangcheng: % d \ n \ r \ t", ACC [mma8452q_axis_x], ACC [mma8452q_axis_y], ACC [encoding]);

The transformed statement is printk ("% S <% d> % s (), zhangcheng: % d \ n \ r \ t" ,__ file __, __line __,__ func __, ACC [mma8452q_axis_x], ACC [mma8452q_axis_y], ACC [mma8452q_axis_z]);

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.