过完年了,该走的亲戚也都去过了,今天终于忙完了,有时间继续来学了。今天看的是串口通信。
STM32的通用同步异步收发器(USART),它支持同步单工、双工通信和半双工单线通信。它也支持LIN(局部互连网),智能卡协议和IrDA(红外数据组织)SIR ENDEC规范,以及调制解调器(CTS/RTS)操作。它还允许多处理器通信。可用于DMA方式和中断方式。
主要特性和参数如下:
全双工,异步通信
NRZ标准格式
分数波特率发生器系统:发送和接收共用的可编程波特率,最高到4.5Mbits/s
可编程数据字长度(8位或9位)、停止位(1或2个停止位)
LIN主发送同步断开符的能力以及LIN从检测断开符的能力 -当USART硬件配置成LIN时,生成13位断开符,检测10/11位断开符
发送方为同步传输提供时钟
IRDA SIR 编码器解码器:在正常模式下支持3/16位的持续时间
智能卡模拟功能
单线半双工通信
单独的发送器和接收器使能位
检测标志:接收缓冲器满 、发送缓冲器空 、传输结束标志
校验控制:发送校验位 、 对接收数据进行校验
4个错误检测标志:溢出错误 、 噪音错误 、帧错误 、校验错误
10个带标志的中断源、CTS改变 、
LIN断开符检测 、发送数据寄存器、发送完成 、接收数据寄存器、
检测到总线为空、溢出错误 、帧错误 、噪音错误 、校验错误
多处理器通信: 如果地址不匹配,则进入静默模式
两种唤醒接收器的方式:地址位(MSB)、空闲总线
下面是一些函数;
配置USART1的GPIO口
/*************************************************
* Function Name : USART1_GPIO_Configuration
* Description : Configures the usart1 GPIO ports.
* Input : None
* Output : None
* Return : None
***************************************************/
void USART1_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// Configure USART1_Tx as alternate function push-pull
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Configure USART1_Rx as input floating
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
配置串口参数
/***********************************************
* Function Name : USART1_Configuration
* Description : Configures the usart1
* Input : None
* Output : None
* Return : None
*************************************************/
void UART1_Configuration(void)
{USART_InitTypeDef USART_InitStructure;
/* USART1 configured as follow:
- BaudRate = 9600 baud
- Word Length = 8 Bits
- One Stop Bit
- Even parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
- USART Clock disabled
- USART CPOL: Clock is active low
- USART CPHA: Data is captured on the second edge
-USART LastBit: The clock pulse of the last data bit is not output tothe
SCLK pin*/
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_Even;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_Clock = USART_Clock_Disable;
USART_InitStructure.USART_CPOL = USART_CPOL_Low;
USART_InitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_InitStructure.USART_LastBit = USART_LastBit_Disable;
/* Configure the USART1*/
USART_Init(USART1, &USART_InitStructure);
/* Enable USART1 Receive and Transmit interrupts */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//接收中断
/* Enable the USART1 */
USART_Cmd(USART1, ENABLE);
}
这里使用的是USART使用中断发送数据,则需要修改stm32f10x_it.c 中的串口中断函数,并且修改void NVIC_Configuration函数。之后串口就可以正常工作了。
在给的例程里面进行了修改,可是连接上电脑的时候总出问题,家里电脑上就一个串口,笔记本上也没有串口,也不知道怎么调了。再接着看看了。。。