串口介绍就不用说了,几乎每款单片机都会有一个或者多个串口。
串口调试值得注意的几个地方:
1.首先确保串口调试软件可用
2.波特率数据位停止位设置、显示方式设置等要匹配
103RBT6上的串口有3个,我这里用的是USART1,占用的IO口是PA9和PA10。
实现功能:采用中断接收,查询发送方式,单片机不断发送回PC机传过来的一个数据。
值得注意的地方就是,TXD要配置成复用输出,RXD要配置成浮空输入。具体原因我也懒得去追究了,直接用就是了。
下面是main.c文件
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
//* Private variables ---------------------------------------------------------*/
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
uint16_t RxBuffer;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
void rcc_cfg()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA,ENABLE);
}
void gpio_cfg()
{
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
void usart_cfg()
{
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1,&USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
}
void nvic_cfg()
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void delay()
{
u16 i,j;
for(i=0;i<1000;i++)
for(j=0;j<500;j++)
;
}
/**
* @brief Main program.
* @param None
* @retval None
*/
int main(void)
{
/*!< At this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32f10x_xx.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32f10x.c file
*/
/* Add your application code here
*/
rcc_cfg();
gpio_cfg();
usart_cfg();
nvic_cfg();
/* Infinite loop */
while (1)
{
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET); // wait until USART_FLAG_TXE=RESET
USART_SendData(USART1,RxBuffer);
delay();
}
}
下面是stm32f10x_it.c文件,也就是中断函数
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_it.h"
/** @addtogroup STM32F10x_StdPeriph_Template
* @{
*/
/* Private variables ---------------------------------------------------------*/
extern uint16_t RxBuffer;
/**
* @brief This function handles USART1 global interrupt request.
* @param None
* @retval None
*/
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
/* Read one byte from the receive data register */
RxBuffer = USART_ReceiveData(USART1);
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
}
}
红色部分指示外部变量的用法。
那个自定义delay函数是使单片机发数据不至于太快,看起来不太爽。
实在是挺简单,按照这个模式配置就行,注释都懒得做了。
我纯粹只寻求实现想要的功能而已,至于USART结构体的成员表达的意义,如有个XX_Clock_InitTypeDef也没有去深究了,反正能用就行吧。没办法,人越来越懒了
用户553565 2013-9-10 09:44
DiracFatCat 2013-9-9 17:37
用户377235 2013-9-9 09:14
用户423038 2012-11-29 09:53
用户429514 2012-11-28 22:06
用户610833 2012-10-30 15:45