void Print_Str(unsigned char *s) { while (*s != '\0') { Send_Byte(*s++); // delay_ms(5); } }
时间: 2024-04-27 18:21:06 浏览: 12
这段代码定义了一个函数`Print_Str`,用于向串口打印一个字符串。
函数的参数是一个指向unsigned char类型的指针s,它指向要打印的字符串的首地址。
函数实现的过程是先进入一个while循环,判断指针所指向的字符是否为'\0',如果不是则说明字符串还没有打印完,需要继续打印。在循环中,每次先调用`Send_Byte`函数将指针所指向的字符发送到串口,然后将指针向后移动一位,指向下一个字符。
当整个字符串打印完成后,函数会自动返回,等待下一次调用。
相关问题
void init_uart() { SCON = 0x50; TMOD = 0x20; TH1 = 0xfd; TL1 = 0xfd; TR1 = 1; EA = 1; ES = 1; } void send_byte(unsigned char dat) { SBUF = dat; while(!TI); TI = 0; } void send_string(char *str) { while(*str != '\0') { send_byte(*str++); } }含义
这段代码是定义了三个函数,分别为:
1. `init_uart()`:初始化串口通信的相关寄存器,包括设置波特率、设置串口工作方式等。
2. `send_byte(unsigned char dat)`:发送一个字节的数据到串口,使用的是SBUF寄存器进行发送。在发送完毕之前,使用while循环等待TI(Transmit Interrupt)标志位被置位,表示数据已经发送完毕。然后将TI标志位清零,以便下一次发送。
3. `send_string(char *str)`:发送一个字符串到串口,使用的是`send_byte`函数。该函数使用了一个指向字符串的指针`str`作为参数,然后在while循环中依次发送字符串中的每个字符,直到遇到字符串结束符'\0'为止。
这三个函数一起实现了在单片机上进行串口通信的功能,可以通过调用`init_uart`函数初始化串口,然后使用`send_string`函数发送字符串到串口,实现与其他设备的通信。
void buffer_print(char* buff int len) { int i = 0; for(i = 0;i < len; i++) { if(i != 0 && i%8 ==0) printf("\n"); printf("%02X",(unsigned char)bufff[i]); } }
这段代码中定义了一个函数 `buffer_print()`,用于打印一个缓冲区的内容。函数的参数包括:缓冲区的起始地址 `buff`、缓冲区的长度 `len`。
函数的实现逻辑是,遍历缓冲区中的每一个字节,每输出8个字节就换行,然后以十六进制的格式输出每一个字节的值。具体说明见代码注释:
```c
void buffer_print(char* buff, int len) {
int i = 0;
for (i = 0; i < len; i++) {
// 如果已经输出了8个字节,则换行
if (i != 0 && i % 8 == 0)
printf("\n");
// 以十六进制的格式输出每一个字节的值
printf("%02X", (unsigned char) buff[i]);
}
}
```
在调用函数时,需要传入缓冲区的起始地址和长度,示例代码如下:
```c
#include <stdio.h>
void buffer_print(char* buff, int len);
int main() {
char buff[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10};
buffer_print(buff, sizeof(buff));
return 0;
}
```
运行结果为:
```
0102030405060708
090A0B0C0D0E0F10
```
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)