×
单片机 > 单片机程序设计 > 详情

STM32初学笔记4之USART

发布时间:2020-08-27 发布时间:
|
=========================== 文件分割线 ===============================

/**
 ** 文件名称:BSP_ALIENTEK.H
 ** 功能描述:ALIENTEK开发板板级支持包声明
 ** 硬件平台:STM32F103RBT6 Development Board
 ** 编译环境:Keil uversion4 IDE
 ** 版本信息:v0.2
 ** 编写作者:唐华明
 ** 编写时间:2011.11.26
 ** 附加说明:无
 ** 修改记录:2011-11-26  将DS0Off() GPIO_SetBits(GPIOD, GPIO_Pin_8)修改为
 **                       DS0Off() GPIO_SetBits(GPIOA, GPIO_Pin_8)
 **           2011-11-27  增加串口1配置函数和输入输出函数声明。
 **/
#ifndef _BSP_ALIENTEK_H
#define _BSP_ALIENTEK_H
#include
/*
*****************************************************************************
模块名称:LED模块      
模块组成:DS0       DS1 
对应引脚:GPIOA8   GPIOD2 
*****************************************************************************
*/
#define DS0On()  GPIO_ResetBits(GPIOA, GPIO_Pin_8) //Turn on DS0
#define DS1On()  GPIO_ResetBits(GPIOD, GPIO_Pin_2) //Turn on DS1
#define DS0Off() GPIO_SetBits(GPIOA, GPIO_Pin_8)    //Turn off DS0
#define DS1Off() GPIO_SetBits(GPIOD, GPIO_Pin_2)    //Turn off DS1
#define DS0Sft() GPIOA->ODR ^= 0x0100;              //Shift DS0 status  0001 0000 0000
#define DS1Sft() GPIOD->ODR ^= 0x0004;              //Shift DS1 status  0000 0000 0100

/*用户函数声明*/
void BSP_Init(void);
void GPIO_Configuration(void);
void RCC_Configuration(void);
void NVIC_Configuration(void);
void InitLCD(void);
void USART1_Configuration(uint32_t baudrate);
int fputc(int ch, FILE *F);  //fputc函数重定向
int fgetc(FILE *f);       //fgetc函数重定向
#endif

=========================== 文件分割线 ===============================

/**
 ** 文件名称:BSP_ALIENTEK.C
 ** 功能描述:ALIENTEK开发板板级支持包函数定义
 ** 硬件平台:STM32F103RBT6 Development Board
 ** 编译环境:Keil uversion4 IDE
 ** 版本信息:v0.2
 ** 编写作者:唐华明
 ** 编写时间:2011.11.26
 ** 附加说明:无
 ** 修改记录:2011-11-27 增加按键外部中断配置语句
 **           2011-11-27 增加串口配置,其使用需要单独调用一个串口配置函数。
 **/
#include "stm32f10x.h"
#include "bsp_alientek.h"
/**
 ** 函数名称:void BSP_Init(void)
 ** 功能描述:板级支持包的初始化
 ** 输入参数:无
 ** 返回值  :无
 ** 版本信息:v0.0
 ** 编写作者:唐华明
 ** 编写时间:2011.11.26
 ** 附加说明:无
 ** 修改记录:无
 **/
void BSP_Init(void)
{
 RCC_Configuration();
 NVIC_Configuration();
 GPIO_Configuration();
 USART1_Configuration(115200); 
 InitLCD();
}
/**
 ** 函数名称:void GPIO_Configuration(void)
 ** 功能描述:通用IO口配置函数
 ** 输入参数:无
 ** 返回值  :无
 ** 版本信息:v0.2
 ** 编写作者:唐华明
 ** 编写时间:2011.11.26
 ** 附加说明:无
 ** 修改记录:2011-11-27 增加按键IO口配置和外部中断线的配置
 **           2011-11-27 增加USART1的引脚配置
 **/
void GPIO_Configuration(void)
{
 GPIO_InitTypeDef GPIO_InitStructure;
 EXTI_InitTypeDef EXTI_InitStructure;
 
 /* Configure IO connected to PA8 and PD2 *********************/ 
 GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_2;
   GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
   GPIO_Init(GPIOD, &GPIO_InitStructure);

 GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_8;
 GPIO_Init(GPIOA, &GPIO_InitStructure);
 /* Configure KEY IO connected to PA13 and PA15 ***************/
 GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_13 | GPIO_Pin_15;
   GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IPU;      //上拉输入
   //当GPIO口设为输入模式使,输出驱动电路与端口断开,此时输出速度
 //配置无意义,不用配置。
   GPIO_Init(GPIOA, &GPIO_InitStructure);
 /* Configure PA13 and PA15 as EXTI Line *********************/
 GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource13);
 GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource15);
 EXTI_ClearITPendingBit(EXTI_Line13);
 EXTI_ClearITPendingBit(EXTI_Line15);
 EXTI_InitStructure.EXTI_Line = EXTI_Line13 | EXTI_Line15;
 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
 EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
 EXTI_InitStructure.EXTI_LineCmd = ENABLE;
 EXTI_Init(&EXTI_InitStructure); 
 /*Configure USART1(PA9 and PA10) ***************************/
 //define USART1 Tx(PA9) as AF_PP
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
 GPIO_Init(GPIOA, &GPIO_InitStructure);
 //define USART Rx(PA10) as IN_FLOATING
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
 GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/**
 ** 函数名称:void RCC_Configuration(void)
 ** 功能描述:系统时钟配置函数
 ** 输入参数:无
 ** 返回值  :无
 ** 版本信息:v0.1
 ** 编写作者:唐华明
 ** 编写时间:2011.11.26
 ** 附加说明:无
 ** 修改记录:2011-11-27 开启串口1的时钟
 **/
void RCC_Configuration(void)
{
 SystemInit();

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOD, ENABLE);
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); 
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA 
//                          |RCC_APB2Periph_GPIOB  | RCC_APB2Periph_GPIOC
//                          |RCC_APB2Periph_GPIOD  | RCC_APB2Periph_GPIOE
//        |RCC_APB2Periph_ADC1   | RCC_APB2Periph_AFIO 
//                          |RCC_APB2Periph_SPI1, ENABLE );
//    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ALL ,ENABLE );
//    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4 | RCC_APB1Periph_USART3
//                        |RCC_APB1Periph_TIM2, ENABLE );
// RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
}
/**
 ** 函数名称:void NVIC_Configuration(void)
 ** 功能描述:嵌入向量中断控制器配置函数
 ** 输入参数:无
 ** 返回值  :无
 ** 版本信息:v0.1
 ** 编写作者:唐华明
 ** 编写时间:2011.11.24
 ** 附加说明:无
 ** 修改记录:2011-11-27 增加按键外部中断的中断向量配置。
 **/
void NVIC_Configuration(void)
{
 NVIC_InitTypeDef NVIC_InitStructure;
   /* Configure the NVIC Preemption Priority Bits ********/  
   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
 /* Configure the EXTI_Line15_10 ***********************/
 NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
 NVIC_Init(&NVIC_InitStructure);

 /*Set the Vector Table ********************************/
 #ifdef  VECT_TAB_RAM  
   /* Set the Vector Table base location at 0x20000000 */ 
   NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); 
 #else  /* VECT_TAB_FLASH  */
   /* Set the Vector Table base location at 0x08000000 */ 
   NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);   
 #endif
}
/**
 ** 函数名称:void InitLCD(void)
 ** 功能描述:金牛开发板上的LCD的初始化设置。
 ** 输入参数:无
 ** 返回值  :无
 ** 版本信息:v0.0
 ** 编写作者:唐华明
 ** 编写时间:2011.11.24
 ** 附加说明:初始化状态可以根据用户的需要做修改。
 ** 修改记录:无
 **/
void InitLCD(void) 
{
   /* LCD Module init */
//   GLCD_init();
//   GLCD_clear(White);
//   GLCD_setTextColor(Blue);
//   GLCD_displayStringLn(Line1, "     GoldBull");
//   GLCD_setTextColor(Red);
//   GLCD_displayStringLn(Line2, "   GPIO example");
}

/**
 ** 函数名称:void USART1_Configuration(uint_32 baudrate)
 ** 功能描述:串口1的配置函数
 ** 输入参数:baudrate:待配置的波特率
 ** 返回值  :无
 ** 版本信息:v0.0
 ** 编写作者:唐华明
 ** 编写时间:2011.11.27
 ** 附加说明:无
 ** 修改记录:无
 **/
void USART1_Configuration(uint32_t baudrate)
{
 USART_InitTypeDef USART_InitStructure;
 /*Configure the structure of USART_InitTypeDef ********************************/
    USART_InitStructure.USART_BaudRate = baudrate;
 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_Tx | USART_Mode_Rx;
 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
 /*Initial USART1 *************************************************************/
 USART_Init(USART1, &USART_InitStructure);
 /*Enable USART1 **************************************************************/
 USART_Cmd(USART1, ENABLE);
 /*Configure the interrupt of USART1 ******************************************/
 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
 USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
}

/**
 ** 函数名称:int fputc(int ch, FILE *F)
 ** 功能描述:将C库函数printf重定向到串口
 ** 输入参数:
 ** 返回值  :
 ** 版本信息:v0.0
 ** 编写作者:唐华明
 ** 编写时间:2011-11-27
 ** 附加说明:无
 ** 修改记录:无
 **/
int fputc(int ch, FILE *F)
{
 /* 写入一个字符到串口 */
 USART_SendData(USART1, (uint8_t)ch);
 /* 等待发送完成 */
 while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
 {}
 return ch;
}
/**
 ** 函数名称:int fgetc(FILE *F)
 ** 功能描述:将C库函数scanf重定向到串口
 ** 输入参数:
 ** 返回值  :
 ** 版本信息:v0.0
 ** 编写作者:唐华明
 ** 编写时间:2011-11-27
 ** 附加说明:该功能还不能实现。
 ** 修改记录:无
 **/
int fgetc(FILE *f)
{
 /* 等待接收一个字符完成 */
 while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
 {}
 /* 从串口读取一个字符并返回 */
 return (USART_ReceiveData(USART1));
}

=========================== 文件分割线 ===============================

/******************************************************************************/
/*                 STM32F10x Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_stm32f10x_xx.s).                                            */
/******************************************************************************/
void EXTI15_10_IRQHandler(void)
{
 if (EXTI_GetITStatus(EXTI_Line13) != RESET)
 {
  EXTI_ClearITPendingBit(EXTI_Line13);
  DS0Sft();
  printf("%d  KEY0 is pressed once!\r\n", count++);
 }
 if (EXTI_GetITStatus(EXTI_Line15) != RESET)
 {
  EXTI_ClearITPendingBit(EXTI_Line15);
  DS1Sft();
  printf("%d  KEY1 is pressed once!\r\n", count++);
 }
}

=========================== 文件分割线 ===============================

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
// unsigned char temp=0;
   /*!< 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
     */     
 BSP_Init();
   /* Add your application code here
     */

 printf("This is a test for USART1 and EXTI use.\r\n");
   /* Infinite loop */
   while (1)
   {
  printf("Please input a charactor\r\n");
  delay_ms(1000);
//  scanf("%c", &temp);//not success!
//  delay_ms(1000);
//  printf("You had entered a charactor:%c\r\n", temp);
 }
}

=========================== 文件分割线 ===============================




『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
ARM 汇编的必知必会