串口通信是单片机学习的一个最基本、最重要的功能之一。串口可以间接的当做调试接口使用,实现单片机与电脑之间的通信。还可以与一些模块(比如蓝牙、wifi)通信,也可以作为和其他单片机通信的工具。作为开发者,熟悉串口是必须的。
===========================================
STM8L的USART特性:
我们在使用串口的时候,主要关心:波特率、停止位、奇偶校验位。
再次就是怎样接收、发送数据。常用:阻塞式发送数据、中断方式接收数据。
==========================================
在单片机的调试过程中经常会要适时地输出变量的值,这时printf函数是一个非常好的选择;对于一般的单片机只有串口可以与外界进行通信,所以我们需要将printf函数重定向到串口。
如何实现重定向c库函数printf到USART1函数?
不管printf函数多么复杂,最终调用的是purchar函数,所以我们只要重新定义putchar函数即可。
==========================================
使用到的库如下:
外设在使用前,都必须使能对应的外设时钟。为了cpu的低功耗,板子上默认的外设都是关闭的,否则板子一上电所有的外设都启动了功耗相对就高了,因此我们在使用的时候,用到哪一个外设就开启使能哪一个外设的时钟。
usart.h:
#ifndef __USART_H
#define __USART_H
#include "stm8l15x.h"
#define BAUDRATE 115200
#define SIZE 128
typedef struct uart_rxbuf_s
{
uint8_t buf[SIZE];
uint32_t len;
}uart_rxbuf_t;
extern uart_rxbuf_t g_uart_rxbuf;
extern void uart_init(void);
extern void uart_send(char *buf, int len);
extern int uart_getchar(void);
#endif
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
usart.c:
void uart_init(void)
{
memset(&g_uart_rxbuf, 0, sizeof(g_uart_rxbuf));
CLK_PeripheralClockConfig(CLK_Peripheral_USART1, ENABLE);
GPIO_Init(GPIOC, GPIO_Pin_2, GPIO_Mode_Out_PP_High_Fast);
GPIO_Init(GPIOC, GPIO_Pin_3, GPIO_Mode_In_PU_No_IT);
USART_DeInit(USART1);
USART_Init(USART1, BAUDRATE, USART_WordLength_8b, USART_StopBits_1, USART_Parity_No,
(USART_Mode_TypeDef)(USART_Mode_Tx | USART_Mode_Rx));
enableInterrupts();
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
USART_SendData8(USART1, '\r');
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
int putchar(int c)
{
if('\n' == (char)c)
{
USART_SendData8(USART1, '\r');
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
USART_SendData8(USART1, c);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
return (c);
}
void uart_send(char *buf, int len)
{
int i;
for(i=0; i<len; i++)
{
USART1->DR = (unsigned char)buf[i];
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
}
int uart_getchar(void)
{
int c;
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
c = USART_ReceiveData8(USART1);
USART_SendData8(USART1, c);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
return (c);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
时钟配置:
sysclock.h:
#ifndef __SYSCLOCK_H
#define __SYSCLOCK_H
#include "stm8l15x.h"
void sys_clk_init();
#endif
sysclock.c:
#include "sysclock.h"
#include "stm8l15x_clk.h"
void sys_clk_init(void)
{
CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1);
CLK_SYSCLKSourceSwitchCmd(ENABLE);
CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSE);
while (CLK_GetSYSCLKSource() != CLK_SYSCLKSource_HSE)
{}
}
发送数据不需要中断,但是接收数据需要利用中断来处理,这样效率比较高,我们只需要填充stm8l15x_it.c里的代码。这个是中断程序的源文件,在编写应用的过程中,需要根据需求改动,以便在自己的程序中使用中断功能。
部分代码:
/**
* @brief USART1 RX / Timer5 Capture/Compare Interrupt routine.
* @param None
* @retval None
*/
INTERRUPT_HANDLER(USART1_RX_TIM5_CC_IRQHandler, 28)
{
if(g_uart_rxbuf.len>=sizeof(g_uart_rxbuf.buf))
{
USART_ReceiveData8(USART1);
return ;
}
g_uart_rxbuf.buf[g_uart_rxbuf.len++] = USART_ReceiveData8(USART1);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
main.c:
这里只做了简单的printf打印:
#include "stm8l15x.h"
#include "usart.h"
#include "sysclock.h"
#include "stdio.h"
int main( void )
{
sys_clk_init();
uart_init();
uart_send("abc", 3);
int i;
for(i=0; i<5; i++)
{
printf("\ntest,test\n");
}
printf("\ntest finshed!\n");
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
测试结果:
若有什么功能要在主函数的while(1)做的,可以再实现,这里只是测试一下printf,所以没有写while。
总结:
程序从main.c进入
1、引脚初始化;
2、时钟初始化;
3、配置芯片的UART模块;
4、若有串口数据进来,会触发中断函数,执行中断,把接收到的数据取出来,再通过发送函数发送出去;