Serial communication (3) Implementation of printf serial port output redirection and sending of one frame and one string of data

This article is the original csdn first release of the blogger. I hope it will be helpful to you after reading it. Please correct me if I have any shortcomings! Let’s communicate and learn together and make progress together!

>Personal homepage: @日月同光, a blog that coexists with me

>You are welcome to like the original blogger Sun and Moon Shine Together, and live with me? + follow + collect + comment?.

Column series: CSDN-MCU serial communication learning series

>My motto is: “Try your best and be the best version of yourself!

Please inform the blogger in advance when reprinting! ! !

Copyright statement: This article is an original article by the CSDN blogger “The sun and the moon shine together, live with me”, and is the only article on CSDN.

Table of Contents

1. Baud rate setting and initial value calculation

1.1 Review – Initial Value Formula

1.2 Calculate the initial value

1.3 Set baud rate

2. Initialization

2.1 Review – Query Method & Interruption Method

2.2 Initialization function

3. Send one frame of data

3.1 Principle

3.2 Send a frame of data function

3.3 Main program and result display 1

4. Send string

4.1 Principle

4.2Send string

4.3 Main program and result display 2

5. printf serial port output redirection

5.1printf function

5.2printf serial port output redirection function

5.3 Main program and result display 3

1. Baud rate setting and initial value calculation

1.1 Review-Initial Value Formula

fosc: crystal oscillator frequency, usually 11.0592Mhz=110592hz. In this design, the crystal oscillator frequency of the microcontroller is set to 11.0592Mhz.

SMOD: baud rate doubling bit. When SMOD=0, the baud rate remains unchanged; when SMOD=1, the baud rate doubles;

Baud rate: usually 9600bit/s, 19200bit/s, etc.

The initial value is the timing initial value TH1 and the timing reload value TL1.

1.2 Calculate initial value

The design baud rate of this article is 9600bit/s. The baud rate is 9600bit/s, the serial port working mode is 1, the initial value is 253, and the hexadecimal value is 0xFD.

1.3 Set baud rate

The baud rate of the virtual terminal, COMPIM, and virtual serial port is uniformly set to 9600bit/s.

2. Initialization

2.1 Review-Query Method &Interruption Method

Query method: When TI=0, it is ready to send; when TI=1, the sending is completed. Therefore, the function of while(!TI); is to wait for the sending to be completed, that is, before the sending is completed, TI=0, and the code execution will always execute the statement. After the sending is completed, TI is automatically set to 1, the statement will no longer be executed, and the next one will be executed. statement. After the transmission is completed, TI should be cleared to 0 so that it can be sent correctly next time.

void main()
{
UartInit();//Call the serial port initialization function
while(1)
{
SBUF = '0'; //Send one frame of data
while(!TI);//Waiting for the sending to be completed (TI will be automatically set to 1)
TI=0;//Set TI to 0
delay(1000);//Delay 1s
}
}

Interrupt method: Write an interrupt service function. When the transmission is completed, TI is automatically set to 1, so the interrupt service function is triggered. In the interrupt service function, clear TI to 0 so that it can be sent correctly next time. The serial port communication interrupt entry number is 4.

//Send data
void main()
{
UartInit();//Call the serial port initialization function
while(1)
{
SBUF=0x00;
delay(2000);//Delay 2s
}
}
//See Chapter 2 2.2 for initialization


//interrupt service function
void ES_timer() interrupt 4 //When TI is set to 1, it will enter the interrupt service function
{
if(TI)
{
TI=0;//TI software sets 0
}
}

2.2 initialization function

Query method initialization: During initialization, the register SCON (working mode, whether to communicate with multiple machines, and the number of data bits) must be set. REN generally needs to be received (set to 1). The initial value is the timing initial value TH1/timing reload value TL1. The working mode It is mode 1 (8-bit asynchronous reload), and the baud rate is variable. Because it is 8-bit data transmission, the timer T1 works in mode 2. The interrupt method settings are the same as above.

void UartInit() //[email protected]
{
SCON = 0x50; //8-bit data, variable baud rate
TMOD &= 0x0F; //Set timer mode
TMOD |= 0x20; //Set timer mode
TL1 = 0xFA; //Set the timing initial value
TH1 = 0xFA; //Set the timed reload value
ET1 = 0; //Disable timer interrupt
TR1 = 1; //Timer 1 starts timing
}

Interrupt method initialization: In addition to the same settings as the query method, you must also turn on the total interrupt EA=1 and the serial port interrupt switch ES=1.

void UartInit(void) //[email protected]
{
PCON & amp;= 0x7F; //The baud rate is not doubled
SCON = 0x50; //8-bit data, variable baud rate
TMOD &= 0x0F; //Set timer mode
TMOD |= 0x20; //Set timer mode
TL1 = 0xFD; //Set the timing initial value
TH1 = 0xFD; //Set the timed reload value
ET1 = 0; //Disable timer interrupt
ES=1;
EA=1;
TR1 = 1; //Timer 1 starts timing
}

3. Send one frame of data

3.1 Principle

The principle is the same as the query method. Step 1: Send data dat. Step 2: Wait for the sending to complete. Step 3: Clear TI to 0.

3.2 Send one frame of data function

void sendByte(unsigned char dat) //Send one frame of data function
{
SBUF=dat;
while(!TI);
TI=0;
}

3.3 Main program and result display 1

void main()
{
UartInit();//Call the serial port initialization function
while(1)
{
sendByte(0x88);
delay(2000);
}
}

result:

4. Send string

4.1 Principle

(1) Array: k constant, define array int a[k], a[k] stores k elements, the data element type is int, a[0]=a0, a[k-1]=a k-1 , there is a terminator ‘\0’ at the end. When the code sent to the character is equal to ‘\0’, it means that sending the string has been completed.

(2) Pointer:

Pointer variable: int *p= &a;p is a pointer variable, which stores the address of a, and the data type is int*.

The difference between * and & amp;: * is the dereference character, & amp; is the address character. p stores the address of a, so *p will find the value of a based on the address of a, that is, *p=a;

The array is essentially a pointer constant, using a subscript to represent the value of each element. You can use a pointer to replace the subscript, and the pointer represents the value of each element. a[] is an array, int *p=a; statement *p=a, at this time the pointer points to subscript 0 by default, that is, the value of *p is a[0], statement *p + +; OK Let the pointer change from pointing to subscript 0 to pointing to subscript 1, that is, *p=a[1] at this time.

4.2 Send String

void sendString(unsigned char *dat)//Send string function
{
while(*dat != '\0')
{
sendByte(*dat + + );
}
}

4.3 Main program and result display 2

void main()
{
UartInit();//Call the serial port initialization function
while(1)
{
\t\t
sendString("hellow world\r\
");
delay(2000);
}
}

result:

5. printf serial port output redirection

5.1printf function

The printf function is relatively unfamiliar to everyone. This is the library function in #include . The following figure is the format of the printf function:

It can be seen that different data types have different formats. The printf function sends a string/decimal number. You can save it and maybe use it in the future! ! !

5.2printf serial port output redirection function

char putchar(char c)
{
sendByte(c);
return c;
}

5.3 Main program and result display 3

void main()
{
unsigned char dat=88;
\t
char a = 1;
    int b = 12365;
    long c = 0x7FFFFFFF;

    unsigned char x = 'A';
    unsigned int y = 54321;
    unsigned long z = 0x4A6F6E00;

    float f = 10.0;
    float g = 22.95;

UartInit();//Call the serial port initialization function
while(1)
{
printf("hellow world\r\
");
printf("dat=%bu\r\
",dat);
printf ("char ? int %d long %ld\
",a,b,c);
        printf ("Uchar %bu Uint %u Ulong %lu\
",x,y,z);
        printf ("%f != %g\
", f, g);
delay(2000);
}
}

result:

unsigned char dat=88;

printf(“hellow world\r\
“);//Send to format hellow world
printf(“dat=%bu\r\
“,dat);//Send to format dat=88

Note: The printf function sends decimal numbers/strings.

char a = 1;
int b = 12365;
long c = 0x7FFFFFFF;

unsigned char x = ‘A’;
unsigned int y = 54321;
unsigned long z = 0x4A6F6E00;

float f = 10.0;
float g = 22.95;

printf (“char ? int %d long %ld\
“,a,b,c);

//The format is char 1 int 12365 long 21474 (convert c to decimal)
printf (“Uchar %bu Uint %u Ulong %lu\
“,x,y,z);

//The format is Uchar 65 Uint 54321 Ulong 1248816640
printf (“%f != %g\
“, f, g);//The format is 10.000000!=22.95

The next article will focus on the reception of one frame of data. Dear readers, please stay tuned, the next article will be more exciting! ! !

If you don’t study for a day, you will have no good ideas. My name is no free food. If you like me, you can support me. The blogger’s name is @日月同光, symbiotic with me.

https://blog.csdn.net/LIN___IT?spm=1000.2115.3001.5343icon-default.png?t=N7T8https://blog.csdn.net/LIN___IT?spm=1000.2115.3001.5343

syntaxbug.com © 2021 All Rights Reserved.