USART提供了一种灵活的方法来与使用工业标准NRZ异步串行数据格式的外部设备之间进行全双工数据交换。USART利用分数波特率发生器提供宽范围的波特率选择。本身USART比较复杂,但是在这里我只是用来调试,也就是把数据从设备发到主机,所以我们暂且把这一部分实现,我们把更多的时间转到USB通信,因为这是趋势。下面给出一个USART的具体的例子。
/**
******************************************************************************
* @file Project/Template/main.c
* @author MCD Application Team
* @version V3.0.0
* @date 04/06/2009
* @brief Main program body
******************************************************************************
* @copy
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>© COPYRIGHT 2009 STMicroelectronics</center></h2>
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "stm32f10x_conf.h"
#include<string.h>
/** @addtogroup Template_Project
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
char *SendData;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
void RCC_Config()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);
}
void GPIO_Config()
{
GPIO_InitTypeDef GPIO_InitStructure;
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);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
void NVIC_Config()
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART_Config()
{
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
USART_StructInit(&USART_InitStructure);//使用默认初始设置
USART_ClockStructInit(&USART_ClockInitStructure);//使用默认的初始设置
USART_Init(USART1,&USART_InitStructure);
USART_ClockInit(USART1,&USART_ClockInitStructure);
USART_Cmd(USART1,ENABLE);
//接收和发送中断使能打开
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_ITConfig(USART1,USART_IT_TXE,ENABLE);
}
void USART1_IRQHandler()
{
if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET){
USART_ITConfig(USART1,USART_IT_RXNE,DISABLE);
}
if(USART_GetITStatus(USART1,USART_IT_TXE)!=RESET){
USART_SendData(USART1,*(SendData++));
if(*(SendData)=='\0')
USART_ITConfig(USART1,USART_IT_TXE,DISABLE);
}
}
void print(char *p)
{
SendData=p;
USART_ITConfig(USART1,USART_IT_TXE,ENABLE);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
/**
* @brief Main program.
* @param None
* @retval : None
*/
int main(void)
{
/* Setup STM32 system (clock, PLL and Flash configuration) */
SystemInit();
/* Add your application code here
*/
RCC_Config();
GPIO_Config();
NVIC_Config();
USART_Config();
/* Infinite loop */
while (1)
{
print("Hello\r\n");
// while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
}
#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
/**
* @}
*/
/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
附件是整个工程:
文章评论(0条评论)
登录后参与讨论