配置USART
void USART1_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVICinitStucture;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
/*
* USART1_TX -> PA9 , USART1_RX -> PA10
*/
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_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
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_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //接收中断
NVICinitStucture.NVIC_IRQChannel =USART1_IRQn;//中断通道,选择串口中断
NVICinitStucture.NVIC_IRQChannelPreemptionPriority=1;//配置中断优先级
NVICinitStucture.NVIC_IRQChannelCmd =ENABLE;//使能打开
NVICinitStucture.NVIC_IRQChannelSubPriority =0;//配置中断子优先级
NVIC_Init(&NVICinitStucture);//串口结构体初始化
USART_Cmd(USART1, ENABLE);
}
注意:若要使用中断组来设置中断优先级,则需要使用NVIC_Config(),如果只需要实现串口通信,可以把NVIC_Config()注释掉,使用USART_ITConfig(USART1, USART_IT_TXE, ENABLE)。两个函数不能同时使用,否则会出现错误
发送字符串
void USART_sendstring(USART_TypeDef* USARTx, char *String)
{
int i = 0;
USART_ClearFlag(USARTx,USART_FLAG_TC); //清空传输完成标志
while(String[i] != '\0') //字符串结束符
{
USART_SendData(USARTx,String[i]); //每次发送字符串的一个字符
while(USART_GetFlagStatus(USARTx,USART_FLAG_TC) == 0); //等待数据发送成功
i++;
}
}
/*
USART_ClearFlag可选的USART_FLAG
USART_FLAG_CTS: CTS 更改标志(不适用于 UART4 和 UART5)。
USART_FLAG_LBD: LIN中断检测标志
USART_FLAG_TC:传输完成标志
USART_FLAG_RXNE: 接收数据寄存器不为空标志
*/
在stm32f10x_it.c里编写中断函数,中断函数名要对应startup_stm32f10x_hd.s里定义的中断入口名称,例如使用USRAT1对应的中断函数名就为USART1_IRQHandler
void USART1_IRQHandler()
{
unsigned char ReceivedData;
if (USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)
{
ReceivedData = USART_ReceiveData( USART1 );
USART_SendData(USART1,ReceivedData);
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
}
在首尾加入如下代码即可使用printf发送数据
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
/*
********************************************************
void main()
{
}
********************************************************
*/
/**
* @brief Retargets the C library printf function to the USART.
* @param None
* @retval None
*/
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(USART1, (uint8_t) ch);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{}
return ch;
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
奉上main.c
void Delay (uint32_t nCount)
{
for(; nCount != 0; nCount--);
}
int main()
{
char wstr[] ="成功了";
char success[]="完成了";
char i=0;
NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4);
USART1_Configuration();
while(i<5)
{
USART_sendstring(USART1,wstr);
Delay (0xffffff);
USART_sendstring(USART1,success);
Delay (0xffffff);
i++;
}
}