//主函数 //IO口时钟源和串口时钟源:AHB1-GPIOB,APB2-USART1 /** * @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_stm32f4xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f4xx.c file */ USART_Config();//USART配置 NVIC_Config();//NVIC配置
// STM_EVAL_COMInit(COM1, &USART_InitStructure);
/* Output a message on Hyperterminal using printf function */ // printf("
USART rintf Example: retarget the C library printf function to the USART
");
while (1) { } }
/** * @brief Retargets the C library printf function to the USART. * @param None * @retval None */ PUTCHAR_PROTOTYPE //int fputc(int ch, FILE *f) { /* Place your implementation of fputc here */ /* e.g. write a character to the USART */ USART_SendData(EVAL_COM1, (uint8_t) ch);
/* Loop until the end of transmission */ while (USART_GetFlagStatus(EVAL_COM1, 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
", file, line) */
---------------------------------
你好,我在用STM32F407调试串口程序,一直不能进入接收中断,
/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx.h"
#include "stm324xg_eval.h"
#include <stdio.h>
/** @addtogroup STM32F4xx_StdPeriph_Examples
* @{
*/
/** @addtogroup USART_Printf
* @{
*/
/* rivate typedef -----------------------------------------------------------*/
/* rivate define ------------------------------------------------------------*/
/* rivate macro -------------------------------------------------------------*/
/* rivate variables ---------------------------------------------------------*/
USART_InitTypeDef USART_InitStructure;
/* rivate function prototypes -----------------------------------------------*/
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define UTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define UTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
/* rivate functions ---------------------------------------------------------*/
//USART配置函数,IO口时钟源和串口时钟源:AHB1-GPIOB,APB2-USART1
void USART_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//USART_ClockInitTypeDef USART_ClockInitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //开启USART1时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); //开启GPIOB时钟
/* Connect Xx to USARTx_Tx*/
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1);//这相当于M3的开启复用时钟?只配置复用的引脚,
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1);//
/*配置GPIOB*/
GPIO_StructInit(&GPIO_InitStructure); //缺省值填入
/*配置GPIOB_Pin6为TX输出*/
/* Configure USART1 Tx (PB.06) as alternate function */
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF; //设置为复用,必须为AF,OUT不行
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
/*配置GPIOB_Pin7为RX输入*/
/* Configure USART1 Rx (PB.7) as alternate function */
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF; //这也必须为复用,与M3不同!
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB,&GPIO_InitStructure);
/* USARTx configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
/*IO引脚复用功能设置,与之前版本不同*/
/*配置USART1*/
// USART_InitTypeDef USART_InitStructure;
USART_StructInit(&USART_InitStructure);
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_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
//USART_ClockStructInit(&USART_ClockInitStruct); //之前没有填入缺省值,是不行的
//USART_ClockInit(USART1, &USART_ClockInitStruct);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //使能 USART1接收中断
USART_Cmd(USART1, ENABLE); //使能 USART1
//USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
//USART_ClearFlag(USART1,USART_FLAG_TC);
}
//NVIC配置函数
void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //嵌套优先级分组为1
NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;//嵌套通道为USART1_IRQn
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;//抢占优先级为0
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;//响应优先级为0
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; //通道中断使能
NVIC_Init(&NVIC_InitStructure);
}
//主函数
//IO口时钟源和串口时钟源:AHB1-GPIOB,APB2-USART1
/**
* @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_stm32f4xx.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32f4xx.c file
*/
USART_Config();//USART配置
NVIC_Config();//NVIC配置
// STM_EVAL_COMInit(COM1, &USART_InitStructure);
/* Output a message on Hyperterminal using printf function */
// printf(" USART rintf Example: retarget the C library printf function to the USART ");
while (1)
{
}
}
/**
* @brief Retargets the C library printf function to the USART.
* @param None
* @retval None
*/
PUTCHAR_PROTOTYPE
//int fputc(int ch, FILE *f)
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(EVAL_COM1, (uint8_t) ch);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(EVAL_COM1, 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 ", file, line) */
/* Infinite loop */
while (1)
{
}
}
//中断服务函数
void USART1_IRQHandler(void)
{
uint32_t rx_dat;
if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)//判断为接收中断
{
USART_ClearITPendingBit(USART1,USART_IT_RXNE); //清除中断标志
USART_ClearFlag(USART1,USART_FLAG_RXNE);
rx_dat=USART_ReceiveData(USART1);//接受数据
printf("iojwfo ");
USART_SendData(USART1,(uint8_t) rx_dat);//发送收到的数据
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{}
}
}
一周热门 更多>