UART基本介绍:
通用异步收发器UART他的功能非常强大
我们只使用UART的全双工异步通信功能,使用中断接收数据。
UART_RX:串行数据输入。
UART_TX:串行数据输出。
硬件支持:
连接串口(RS232)实现在超级终端里输入输出
软件支持:
超级终端,teraterm-4.75
1、配置UART,我们使用UART2
(1)设置波特率为115200,设置数据字段长为8字,使用1个停止位,无基偶校验,UART Clock disabled,打开发送和接收使能
以上基本设置使用一个函数进行封装设置:
函数内部实现:
设置数据字段长为8字:UART2->CR1 |= 0x00;
使用1个停止位: UART2->CR3 |= 0x00;
使用基数位校验:UART2->CR1 |= 0x00;
UART Clock disabled、打开发送和接收使能等等
(2)打开接收中断,当接收发生或者溢出发生时候,产生接收中断
(3)UART使能
UART2->CR1 &= (uint8_t)(~0x02);
(4)全局中断使能
enableInterrupts();
总体UART配置函数实现如下代码所示:
1 static void UART2_Config(void)
2 {
3 /* EVAL COM (UART) configuration -----------------------------------------*/
4 /* USART configured as follow:
5 - BaudRate = 115200 baud
6 - Word Length = 8 Bits
7 - One Stop Bit
8 - Odd parity
9 - Receive and transmit enabled
10 - UART Clock disabled
11 */
12 UART2_Init((uint32_t)115200, UART2_WORDLENGTH_8D,UART2_STOPBITS_1, UART2_PARITY_NO,
13 UART2_SYNCMODE_CLOCK_DISABLE, UART2_MODE_TXRX_ENABLE);
14
15 /* Enable the UART Receive interrupt: this interrupt is generated when the UART
16 receive data register is not empty */
17 UART2_ITConfig(UART2_IT_RXNE_OR, ENABLE);
18
19 /* Enable the UART Transmit complete interrupt: this interrupt is generated
20 when the UART transmit Shift Register is empty */
21 UART2_ITConfig(UART2_IT_TXE, ENABLE);
22
23 /* Enable UART */
24 UART2_Cmd(ENABLE);
25
26 /* Enable general interrupts */
27 enableInterrupts();
28 }
UART2_Config
2、UART输出功能
如果直接使用C语言的printf函数,只会在编译器的Terminal-I/O中输出,不会在我们想要的超级终端里面输出,所以需要对输出函数做重定向;
实现每次想要输出的时候,将信息打印到超级终端中,故重定向putchar (int c)函数,在函数内使用UART的传送数据功能就可以了,即将要输出的信息写入UART的数据寄存器
1 #define PUTCHAR_PROTOTYPE int putchar (int c)
2 ...
3 /**
4 * @brief Retargets the C library printf function to the UART.
5 * @param c Character to send
6 * @retval char Character sent
7 */
8 PUTCHAR_PROTOTYPE
9 {
10 /* Write a character to the UART2 */
11 UART2_SendData8(c);
12 /* Loop until the end of transmission */
13 while (UART2_GetFlagStatus(UART2_FLAG_TXE) == RESET);
14
15 return (c);
16 }
3、UART输入功能
输入功能实际上是字符串处理过程的实现,在超级终端中输入内容实际上是在UART的数据寄存器里写内容,所我们只需要去数据寄存器里面读取并处理字符串即可;
处理函数功能
首先我们得定义一个支持终端回显的函数uart_GetStr,其中功能包括:
(1)当有我们在终端里敲键盘的时候会立马有正确的内容显示;
(2)当按下特殊按键的时候会有正确的反应;比如backspace会删除一个字符;return会表示输入完毕进入发送;
(3)对于其他特殊案件处理不了应当屏蔽;比如不想实现delete功能,删除刚刚读入的delete字符,并不会回显;
函数实现:
uart_GetStr传入的第一个参数是指向接收数据数组的指针,第二个参数表示是否允许回显;
几个有用变量:
__IO uint8_t ReciveBuff = 0; //save the current char
uint8_t RxBuffer[32] = {0}; //save the input string
__IO uint8_t RxCounter = 0; //the length of valid string
所以,RxBuffer就是uart_GetStr函数的第一个参数,在uart_GetStr函数内部会对每一个字符进行处理,正确的字符才放入RxBuffer中;
//====================================================================================
2 //Function Name | dbg_GetStr
3 //Description | Get string via UART port.
4 //Input | *p_recv_buff : pointer to receive data buffer
5 // | b_echo_on : echo back on or off
6 //Output | detect terminal(0x0d character) :TRUE or FALSE
7 //Remark |
8 //====================================================================================
9 uint8_t uart_GetStr(uint8_t *p_recv_buff, bool b_echo_on)
10 {
11 uint8_t b_end = 0;
12 int i;
13 static uint8_t len = 0;
14 static uint8_t pos = 0;
15 static uint8_t esc_seq = 0;
16 uint8_t c;
17
18 // Get a character.
19 if((c = ReciveBuff) == 0){
20 return 0;
21 }
22 // echo back
23 if(b_echo_on){
24 printf("%c",c); //show the input
25 }
26
27 //Check
28 switch(esc_seq){
29 // Normal
30 case 0:
31 // Return(Terminate)
32 if(c == 0x0d){
33 p_recv_buff[len] = 0;
34 len = pos = 0; // clear
35 if(b_echo_on){
36 printf("n");
37 }
38 b_end = 1;
39 }
40 // Back Space
41 else if(c == 0x08){
42 if(len){
43 if(b_echo_on){
44 printf(" ");
45 printf("%c",0x08); // BS
46 }
47 len--; // update length info.
48 pos--; // update pos info
49 }
50 }
51 // ESC
52 else if(c == 0x1b){
53 esc_seq = 1;
54 }
55 // DEL
56 else if(c == 0x7f){
57 if(len){
58 DeleteChar(pos, len, &p_recv_buff[0]);
59 len --; // update length info
60 if(b_echo_on){
61 printf("%s",&p_recv_buff[pos]);
62 printf(" ");
63 printf("%c",0x08); // BS
64 // move cursor to character end.
65 for(i = 0; i < len - pos; i++){
66 printf("%c",0x1b); // ESC
67 printf("%c",'[');
68 printf("%c",'D');
69 }
70 }
71 }
72 }
73 // Other
74 else{
75 p_recv_buff[pos] = c;
76 len++; // update length info
77 pos++; // update pos info
78 }
79 break;
80 // ESC SEQ -> 1st
81 case 1:
82 if(c == '['){
83 esc_seq = 2; // Next seq.
84 }
85 else{
86 esc_seq = 0; // not support(to normal)
87 }
88
关键字:STM8S Universal asynchronous receiver transmitter UART 编辑:什么鱼 引用地址:http://news.eeworld.com.cn/mcu/ic480319.html 本网站转载的所有的文章、图片、音频视频文件等资料的版权归版权所有人所有,本站采用的非本站原创文章及图片等内容无法一一联系确认版权者。如果本网所选内容的文章作者及编辑认为其作品不宜公开自由传播,或不应无偿使用,请及时通过电子邮件或电话通知我们,以迅速采取适当措施,避免给双方造成不必要的经济损失。
上一篇:STM8S——Analog/digital converter (ADC)
下一篇:M41T11-RTC(实时时钟)
- 关注eeworld公众号
快捷获取更多信息
- 关注eeworld服务号
享受更多官方福利
推荐阅读


