stm32中可以定义2个一样名字的函数吗,比如另个NVIC_Configuration()

我现在看到的一个程序里面他一共定义了2个这个函数,一个是在main函数所在的源文件中,另一个里面的他前面加了static,加static的那个函数是不是只在那个源文件中才能被调用,而且是从定义它的地方开始,?
我可以2个都加static吗?这个函数设计到外设的优先级,定义2个函数,如果他们的优先级设置是一样的,主优先级和此优先级都是0,那么如果他们同时中断会不会有问题

1个回答

对,static 有限定作用域在该源文件的作用。
中断应该不会有问题,这和两个不同名函数是一样的,虽然这两个函数同名,但他们地址是不同的。
只要编译通过了,编译后就按2个不同函数处理了。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
GD32调用NVIC_SystemReset跳转不到IAP有什么解决的办法吗?

我的项目碰到一台机跳转不到IAP的问题, 利用的是__set_FAULTMASK(1); // close all IT NVIC_SystemReset(); // reset 我开始怀疑是不是这台机器的mcu坏了,但试着读取出来的bin文件与我下载进去的bin文件是一样的,希望大家可以提供下建议,在这先谢谢各位大侠了。

stm32延时函数可行和不可行

# 1. 可行的延时 #MeLED.c ``` include "MELED.h" void LED_TIM_Config(void) { TIM_TimeBaseInitTypeDef LED_TIM_Struct; TIM_OCInitTypeDef TIM_OCInitConfig; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); TIM_DeInit(TIM4); //GPIO_PinRemapConfig(GPIO_Remap_TIM4, ENABLE); TIM_OCInitConfig.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitConfig.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitConfig.TIM_OCPolarity = TIM_OCPolarity_Low; LED_TIM_Struct.TIM_Prescaler = (36000 - 1); LED_TIM_Struct.TIM_Period = 2 - 1; LED_TIM_Struct.TIM_ClockDivision = TIM_CKD_DIV1; LED_TIM_Struct.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM4, &LED_TIM_Struct); TIM_ClearFlag(TIM4, TIM_FLAG_Update); TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE); TIM_OC1PreloadConfig(TIM4, ENABLE); TIM_Cmd(TIM4, ENABLE); } ``` ``` void LED_NVIC_Config(void) { NVIC_InitTypeDef LED_NVIC_Struct; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); LED_NVIC_Struct.NVIC_IRQChannel = TIM4_IRQn; LED_NVIC_Struct.NVIC_IRQChannelPreemptionPriority = 0;//抢占优先级 LED_NVIC_Struct.NVIC_IRQChannelSubPriority = 0;//响应优先级 LED_NVIC_Struct.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&LED_NVIC_Struct); } ``` int i = 0;//注意该变量是定义在MeLED.c文件中的 ``` void TIM4_IRQHandler(void) { if(TIM_GetITStatus(TIM4,TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(TIM4, TIM_IT_Update); i++; } } ``` ``` void My_Systick_Delay(uint32_t nTime) { i = 0; while(nTime != i); } ``` #MeLED.h ``` #ifndef __MELED_H #define __MELED_H #include "stm32f10x.h" #include "led.h" #include "Oper_System_Delay.h" void MELED_Init(void); void MELED_PWM_Init(void); void LED_TIM_Config(void); void LED_NVIC_Config(void); void TIM4_IRQHandler(void); void My_Systick_Delay(uint32_t nTime); #endif ``` #main.c ``` #include "stm32f10x.h" #include "usart.h" #include "led.h" #include "RTC_Time.h" #include <stdio.h> #include "MeLED.h" #include "Delay.h" ``` ``` int main(void) { while(1) { My_Systick_Delay(1000); printf("Delay over\n"); } } ``` #2.不可行的延时(我想知道为什么不可行?) #Delay.c ``` #include "Delay.h" void My_Systick_Delay(uint32_t nTime) { Attain_delay = 0; while(nTime != Attain_delay);//程序会不停的在这里循环 } ``` #Delay.h ``` #ifndef __Delay_H #define __Delay_H #include "stm32f10x.h" #include "stm32f10x_tim.h" #include "stm32f10x_it.h" #include "core_cm3.h" #include "Oper_System_Delay.h" #endif ``` #Oper_System_Delay.h ``` #ifndef __Oper_System_Delay_H #define __Oper_System_Delay_H #include "stm32f10x.h" #include "stm32f10x_tim.h" #include "stm32f10x_it.h" static uint32_t Attain_delay; //void Oper_System_Delay(); #endif ``` #MeLED.c ``` void TIM4_IRQHandler(void) { if(TIM_GetITStatus(TIM4,TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(TIM4, TIM_IT_Update); Attain_delay++; } } ``` 所以第2点有人知道为什么不可行吗?最好可以提供第1和第2的差别在哪?

STM32串口如何实现一个发数据,另外一个接收数据

在keil5在编程环境下,想要实现的功能是:利用中断,让串口四接收数据,串口一发送数据 部分代码如下: int main(void) { //初始化USART1和UART4 配置模式为 115200 8-N-1,通过USART1发送数据,通过UART4接收数据 Debug_USART1_Config(); Debug_UART4_Config(); while(1) { } } 、、、、、、、、、中断函数如下、、、、、、、、、 相关宏定义 #define DEBUG_R_USART_IRQHandler UART4_IRQHandler #define DEBUG_R_USART_IRQ UART4_IRQn //串口四的中断程序:用于接收数据 void DEBUG_R_USART_IRQHandler(void) { if(USART_GetITStatus(UART4,USART_IT_RXNE)!=RESET) { USART_ClearFlag(UART4,USART_IT_RXNE); // USART_ClearITPendingBit(UART4,USART_IT_RXNE); //清除中断标志 ucTemp = USART_ReceiveData(UART4); } } //串口一的中断程序:用于发送数据 void DEBUG_T_USART_IRQHandler(void) { if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET) { USART_ClearFlag(USART1,USART_IT_RXNE); //清除标志 // USART_ClearITPendingBit(USART1,USART_IT_RXNE); //清除中断预处理位 USART_SendData(USART1,ucTemp); } } 、、、、、、、、中断函数的配置、、、、、、、、、 //接收串口引脚定义 /*******************************************************/ #define DEBUG_USART UART4 #define DEBUG_USART_CLK RCC_APB1Periph_UART4 #define DEBUG_USART_BAUDRATE 115200 //串口波特率 #define DEBUG_USART_RX_GPIO_PORT GPIOA #define DEBUG_USART_RX_GPIO_CLK RCC_AHB1Periph_GPIOA #define DEBUG_USART_RX_PIN GPIO_Pin_1 #define DEBUG_USART_RX_AF GPIO_AF_UART4 #define DEBUG_USART_RX_SOURCE GPIO_PinSource1 #define DEBUG_USART_TX_GPIO_PORT GPIOA #define DEBUG_USART_TX_GPIO_CLK RCC_AHB1Periph_GPIOA #define DEBUG_USART_TX_PIN GPIO_Pin_0 #define DEBUG_USART_TX_AF GPIO_AF_UART4 #define DEBUG_USART_TX_SOURCE GPIO_PinSource0 #define DEBUG_R_USART_IRQHandler UART4_IRQHandler #define DEBUG_R_USART_IRQ UART4_IRQn /************************************************************/ //发送串口引脚定义 /*******************************************************/ #define DEBUG_T_USART USART1 #define DEBUG_T_USART_CLK RCC_APB2Periph_USART1 #define DEBUG_T_USART_BAUDRATE 115200 //串口波特率 #define DEBUG_T_USART_RX_GPIO_PORT GPIOA #define DEBUG_T_USART_RX_GPIO_CLK RCC_AHB1Periph_GPIOA #define DEBUG_T_USART_RX_PIN GPIO_Pin_10 #define DEBUG_T_USART_RX_AF GPIO_AF_USART1 #define DEBUG_T_USART_RX_SOURCE GPIO_PinSource10 #define DEBUG_T_USART_TX_GPIO_PORT GPIOA #define DEBUG_T_USART_TX_GPIO_CLK RCC_AHB1Periph_GPIOA #define DEBUG_T_USART_TX_PIN GPIO_Pin_9 #define DEBUG_T_USART_TX_AF GPIO_AF_USART1 #define DEBUG_T_USART_TX_SOURCE GPIO_PinSource9 #define DEBUG_T_USART_IRQHandler USART1_IRQHandler #define DEBUG_T_USART_IRQ USART1_IRQn /************************************************************/ static void NVIC_Configuration(void) //串口四的中断参数的配置 { NVIC_InitTypeDef NVIC_InitStructure; /* 嵌套向量中断控制器组选择 */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); /* 配置UART4为中断源 */ NVIC_InitStructure.NVIC_IRQChannel = DEBUG_R_USART_IRQ; /* 抢断优先级为1 */ NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; /* 子优先级为1 */ NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; /* 使能中断 */ NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; /* 初始化配置NVIC */ NVIC_Init(&NVIC_InitStructure); } static void T_NVIC_Configuration(void) //串口一的中断参数的配置 { NVIC_InitTypeDef NVIC_InitStructure; /* 嵌套向量中断控制器组选择 */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); /* 配置USART1为中断源 */ NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; /* 抢断优先级为1 */ NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; /* 子优先级为1 */ NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; /* 使能中断 */ NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; /* 初始化配置NVIC */ NVIC_Init(&NVIC_InitStructure); } 、、、、、、、、有关串口的配置、、、、、、、、 void Debug_UART4_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; RCC_AHB1PeriphClockCmd(DEBUG_USART_RX_GPIO_CLK|DEBUG_USART_TX_GPIO_CLK,ENABLE); /* 使能 USART 时钟 */ RCC_APB1PeriphClockCmd(DEBUG_USART_CLK, ENABLE); /* GPIO初始化 */ GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /* 配置Tx引脚为复用功能 */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Pin = DEBUG_USART_TX_PIN ; GPIO_Init(DEBUG_USART_TX_GPIO_PORT, &GPIO_InitStructure); /* 配置Rx引脚为复用功能 */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Pin = DEBUG_USART_RX_PIN; GPIO_Init(DEBUG_USART_RX_GPIO_PORT, &GPIO_InitStructure); /* 连接 PXx 到 USARTx_Tx*/ GPIO_PinAFConfig(DEBUG_USART_RX_GPIO_PORT,DEBUG_USART_RX_SOURCE,DEBUG_USART_RX_AF); /* 连接 PXx 到 USARTx__Rx*/ GPIO_PinAFConfig(DEBUG_USART_TX_GPIO_PORT,DEBUG_USART_TX_SOURCE,DEBUG_USART_TX_AF); /* 配置串DEBUG_USART 模式 */ /* 波特率设置:DEBUG_USART_BAUDRATE */ USART_InitStructure.USART_BaudRate = DEBUG_USART_BAUDRATE; /* 字长(数据位+校验位):8 */ USART_InitStructure.USART_WordLength = USART_WordLength_8b; /* 停止位:1个停止位 */ USART_InitStructure.USART_StopBits = USART_StopBits_1; /* 校验位选择:不使用校验 */ USART_InitStructure.USART_Parity = USART_Parity_No; /* 硬件流控制:不使用硬件流 */ USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; /* USART模式控制:同时使能接收和发送 */ USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; /* 完成USART初始化配置 */ USART_Init(DEBUG_USART, &USART_InitStructure); /* 嵌套向量中断控制器NVIC配置 */ NVIC_Configuration(); /* 使能串口接收中断 */ USART_ITConfig(DEBUG_USART, USART_IT_RXNE, ENABLE); //使能了接收中断,那么ORE中断也同时被开启了。 /* 使能串口 */ USART_Cmd(DEBUG_USART, ENABLE); } void Debug_USART1_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); /* 使能 USART 时钟 */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); /* GPIO初始化 */ GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /* 配置Tx引脚为复用功能 */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Pin = DEBUG_USART_TX_PIN ; GPIO_Init(DEBUG_T_USART_TX_GPIO_PORT, &GPIO_InitStructure); /* 配置Rx引脚为复用功能 */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Pin = DEBUG_T_USART_RX_PIN; GPIO_Init(DEBUG_T_USART_RX_GPIO_PORT, &GPIO_InitStructure); /* 连接 PXx 到 USARTx_Tx*/ GPIO_PinAFConfig(DEBUG_T_USART_RX_GPIO_PORT,DEBUG_T_USART_RX_SOURCE,DEBUG_T_USART_RX_AF); /* 连接 PXx 到 USARTx__Rx*/ GPIO_PinAFConfig(DEBUG_T_USART_TX_GPIO_PORT,DEBUG_T_USART_TX_SOURCE,DEBUG_T_USART_TX_AF); /* 配置串DEBUG_USART 模式 */ /* 波特率设置:DEBUG_USART_BAUDRATE */ USART_InitStructure.USART_BaudRate = DEBUG_T_USART_BAUDRATE; /* 字长(数据位+校验位):8 */ USART_InitStructure.USART_WordLength = USART_WordLength_8b; /* 停止位:1个停止位 */ USART_InitStructure.USART_StopBits = USART_StopBits_1; /* 校验位选择:不使用校验 */ USART_InitStructure.USART_Parity = USART_Parity_No; /* 硬件流控制:不使用硬件流 */ USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; /* USART模式控制:同时使能接收和发送 */ USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; /* 完成USART初始化配置 */ USART_Init(DEBUG_T_USART, &USART_InitStructure); /* 嵌套向量中断控制器NVIC配置 */ T_NVIC_Configuration(); /* 使能串口接收中断 */ USART_ITConfig(DEBUG_T_USART, USART_IT_RXNE, ENABLE); /* 使能串口 */ USART_Cmd(DEBUG_T_USART, ENABLE); } 未解决:串口一和串口四都可以单独收发,但是我想要实现一个串口收,另外一个串口发的功能,,,,,,,,求助啊,,,,,,试了好多可能性了

为什么stm32f4的中断跳到startup_stm32f40_41xxx.s库中后就跳不出来了

为什么我的中断函数跳到startup_stm32f40_41xxx.s库中后就跳不出来了。![![图片说明](https://img-ask.csdn.net/upload/201807/17/1531813979_881771.png)图片说明](https://img-ask.csdn.net/upload/201807/17/1531813971_83556.png) debug到中断初始化之后就动不了了,停止之后发现在startup_stm32f40_41xxx.s中 初始化代码: TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE); ///ʹÄÜTIM3ʱÖÓ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitStructure.NVIC_IRQChannel=TIM1_UP_TIM10_IRQn; //¶¨Ê±Æ÷3ÖÐ¶Ï NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x00; //ÇÀÕ¼ÓÅÏȼ¶1 NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x00; //×ÓÓÅÏȼ¶3 NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_Init(&NVIC_InitStructure); TIM_TimeBaseInitStructure.TIM_Period = 10000-1; //×Ô¶¯ÖØ×°ÔØÖµ TIM_TimeBaseInitStructure.TIM_Prescaler= 8400-1; //¶¨Ê±Æ÷·ÖƵ TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //ÏòÉϼÆÊýģʽ TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; TIM_TimeBaseInitStructure.TIM_RepetitionCounter=0; TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStructure);//³õʼ»¯TIM3 //TIM_ClearFlag(TIM1,TIM_FLAG_Update); TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE); //ÔÊÐí¶¨Ê±Æ÷3¸üÐÂÖÐ¶Ï TIM_Cmd(TIM1,ENABLE);

stm32 PVD掉电检测,进不了中断

芯片是stm32f103C8T6,PVD掉电检测,中断服务函数是通过串口发送数据。可是并没有发送,不知道是没有进入中断,还是进入了中断,但电压值过低,串口发送数据失败。求大神看看,写了好久没解决问题。代码如下: void PVD_Init(void) { SystemInit(); EXTI_InitTypeDef EXTI_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE);//PWRʹÄÜ /* Configure EXTI Line16(PVD Output) to generate an interrupt on rising and falling edges */ EXTI_ClearITPendingBit(EXTI_Line16); EXTI_InitStructure.EXTI_Line = EXTI_Line16; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); } void PVD_NVIC(void) { NVIC_InitTypeDef NVIC_InitStructure; /* Configure one bit for preemption priority */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); /* Enable the PVD Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = PVD_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } void PVD_IRQHandler(void) { if(EXTI_GetITStatus(EXTI_Line16) != RESET) { GPIO_SetBits(GPIOA,GPIO_Pin_1); start();//串口发送数据 EXTI_ClearITPendingBit(EXTI_Line16); } } int main() { ..... PWR_PVDLevelConfig(PWR_PVDLevel_2V9); PWR_PVDCmd(ENABLE); PVD_Init(); PVD_NVIC(); .... while(1) { ; } }

CM4 NVIC优先级的问题

![图片说明](https://img-ask.csdn.net/upload/201701/12/1484205223_967871.jpg) ![图片说明](https://img-ask.csdn.net/upload/201701/12/1484205213_559156.jpg) CM4的资料就给这么点说明,也百度过好多贴子,不明白该怎么操作,才能设置不同的优先级,还有就是怎么分主优先级和子优先级。 我理解是往NVIC->IP里写一个值(值是范围是0~7),值越大,该优先级就越小,不知道对不对 我所参考的源代码是MQX4.1的代码,文件名为nvic.c ``` /*! * \brief Initialize a specific interrupt in the cortex core nvic * * \param[in] irq Interrupt number * \param[in] prior Interrupt priority * \param[in] enable enable the interrupt now? * * \return uint32_t MQX_OK or error code */ _mqx_uint _nvic_int_init ( // [IN] Interrupt number _mqx_uint irq, // [IN] Interrupt priority _mqx_uint prior, // [IN] enable the interrupt now? bool enable ) { VCORTEX_NVIC_STRUCT_PTR nvic = (VCORTEX_NVIC_STRUCT_PTR)&(((CORTEX_SCS_STRUCT_PTR)CORTEX_PRI_PERIPH_IN_BASE)->NVIC); _mqx_uint ext_irq_no = irq - 16; // check priority value, must be below maximal enabled/set value if (prior >= (1 << CORTEX_PRIOR_IMPL)) { return MQX_INVALID_PARAMETER; } if (irq >= PSP_INT_FIRST_INTERNAL && irq <= PSP_INT_LAST_INTERNAL) { nvic->PRIORITY[ext_irq_no >> 2] = (nvic->PRIORITY[ext_irq_no >> 2] & ~(0xff << ((ext_irq_no & 3) * 8))) | (((prior << CORTEX_PRIOR_SHIFT) & CORTEX_PRIOR_MASK) << ((ext_irq_no & 3) * 8)); if (enable) _nvic_int_enable(irq); else _nvic_int_disable(irq); } else return MQX_INVALID_PARAMETER; return MQX_OK; } /*! * \brief Enable interrupt on cortex core NVIC * * \param[in] irq Interrupt number * * \return uint32_t MQX_OK or error code */ _mqx_uint _nvic_int_enable ( // [IN] Interrupt number _mqx_uint irq ) { VCORTEX_NVIC_STRUCT_PTR nvic = (VCORTEX_NVIC_STRUCT_PTR)&(((CORTEX_SCS_STRUCT_PTR)CORTEX_PRI_PERIPH_IN_BASE)->NVIC); uint32_t ext_irq_no = irq - 16; if (ext_irq_no >= PSP_INT_FIRST_INTERNAL && ext_irq_no <= PSP_INT_LAST_INTERNAL) { nvic->ENABLE[ext_irq_no >> 5] = 1 << (ext_irq_no & 0x1f); } else return MQX_INVALID_PARAMETER; return MQX_OK; } /*! * \brief Disable interrupt on cortex core NVIC * * \param[in] irq Interrupt number * * \return uint32_t MQX_OK or error code */ _mqx_uint _nvic_int_disable ( // [IN] Interrupt number _mqx_uint irq ) { VCORTEX_NVIC_STRUCT_PTR nvic = (VCORTEX_NVIC_STRUCT_PTR)&(((CORTEX_SCS_STRUCT_PTR)CORTEX_PRI_PERIPH_IN_BASE)->NVIC); uint32_t ext_irq_no = irq - 16; if (ext_irq_no >= PSP_INT_FIRST_INTERNAL && ext_irq_no <= PSP_INT_LAST_INTERNAL) { nvic->DISABLE[ext_irq_no >> 5] = 1 << (ext_irq_no & 0x1f); } else return MQX_INVALID_PARAMETER; return MQX_OK; } ``` ``` ```

STM32 的 USART 使能USART_IT_RXNE进不了中断

# STM32 的 USART 使能USART_IT_RXNE进不了中断,USART_IT_TXE就能进 void USART_Configure() { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE ); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 |GPIO_Pin_10; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1); USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx |USART_Mode_Tx; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_Init(USART1,&USART_InitStructure); USART_Cmd(USART1,ENABLE); USART_ClearFlag(USART1,USART_FLAG_RXNE); USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//USART_IT_TXE /* 中断*/ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//优先级分布 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; NVIC_Init(&NVIC_InitStructure); } void USART1_IRQHandler(void) { u8 res; if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET) { USART_ClearFlag(USART1,USART_FLAG_RXNE); USART_ClearITPendingBit(USART1,USART_IT_RXNE); res = USART_ReceiveData(USART1); USART_SendData(USART1,res); } while (1) { USART_SendData(USART1,'C'); while(RESET == USART_GetFlagStatus(USART1,USART_FLAG_TXE)); } } // int main(void) { SystemInit(); USART_Configure(); delay(1000); while (1) { USART_SendData(USART1,'a'); while(RESET == USART_GetFlagStatus(USART1,USART_FLAG_TXE)); delay(1000); } }

stm32f103c8t6串口 usart1只能发送不能接受

这几天一直在搞毕设,需要通过串口用电脑控制单片机,用的是usart1,调了好几天,死活就是只能发送数据,不能接受数据,下面是我的配置函数和串口中断处理函数,求指导!!! //串口引脚初始化 void Init_Usart(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1,ENABLE); 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_Configuration(uint32_t BaudRate) { USART_InitTypeDef USART_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); 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_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); USART_Cmd(USART1, ENABLE); } //串口中断处理函数 void USART1_IRQHandler(void) { uint8_t ReceiveDate; if(!(USART_GetITStatus(USART1,USART_IT_RXNE))) { USART_ClearITPendingBit(USART1,USART_IT_RXNE); ReceiveDate=USART_ReceiveData(USART1); printf("%d",(char *)ReceiveDate); printf("\n\r"); } } //串口中断NVIC配置函数 void Init_NVIC(void) { NVIC_InitTypeDef NVIC_InitStructure; #ifdef VECT_TAB_RAM NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); #else NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); #endif NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); } 调了好几天了,实在是高不出来了,求大神指导!!!!!!!跪谢!!

stm32f407 DMA接收、发送问题

进行串口收发时,可以发送数据,但是DMA无法读取数据,配置看了好几遍,不知道哪里出现了问题,特意贴个代码,希望大家帮忙看看 #include "stm32f4xx.h" #include "myconst.h" #include "myfunction.h" #include <stdio.h> #include "eeprom.h" extern uint8_t uploaddata[10]; extern uint8_t usart1_dma_received[55]; extern uint8_t usart1_dma_flag; //Óë´«¸ÐÐÅÏ¢°åͨÐŵĴ®¿Ú£ºPA9---TX; PA10---RX //½ÓÊÕ´«¸Ð°å·¢À´µÄ59¸ö×Ö½Ú£º3A+A3+±ÚÕÏ2¸ö×Ö½Ú+µçÁ¿2¸ö×Ö½Ú+¹ßµ¼12¸ö×Ö½Ú+GPS39¸ö×Ö½Ú+Éî¶È2¸ö×Ö½Ú //Ïò´«¸Ð°å·¢ËÍË®ÖÊÐÅÏ¢£º10¸ö×Ö½Ú£ºA4+phÖµ2¸ö×Ö½Ú+µçµ¼ÂÊ3¸ö×Ö½Ú+ζÈ2¸ö×Ö½Ú+0A+0D; void USART1_Configuration(void) { USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; DMA_InitTypeDef DMA_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1); 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_ClearFlag(USART1, USART_FLAG_TC); USART_Cmd(USART1, ENABLE); /*DMA·¢ËÍÅäÖÃ*/ DMA_DeInit(DMA2_Stream7); DMA_StructInit( &DMA_InitStructure); DMA_InitStructure.DMA_Channel = DMA_Channel_4; DMA_InitStructure.DMA_PeripheralBaseAddr =USART1_DR_Base; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)0; DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; DMA_InitStructure.DMA_BufferSize = 8; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA2_Stream7, &DMA_InitStructure); DMA_ITConfig(DMA2_Stream7, DMA_IT_TC, ENABLE); USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE); DMA_ClearITPendingBit(DMA2_Stream7, DMA_IT_TCIF7); /*DMA ½ÓÊÕÊý¾ÝÉèÖÃ*/ DMA_DeInit(DMA2_Stream5); DMA_StructInit(&DMA_InitStructure); DMA_InitStructure.DMA_Channel = DMA_Channel_4; //Ñ¡ÔñChannel_5 DMA_InitStructure.DMA_PeripheralBaseAddr =USART1_DR_Base; //Êý¾Ý´«ÊäµÄÍâÉèÊ×µØÖ·£¬Ïê½â¼ûÉÏ DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)(&usart1_dma_received[0]); //×Ô¼º¶¨Òå´ý·¢ËÍÊý×éµÄÊ×µØÖ·£¬ÒªÇ¿ÖÆת»»Îª32λ DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; //Êý¾Ý´«Êä·½ÏòÑ¡ÔñΪÄÚ´æ<-ÍâÉè DMA_InitStructure.DMA_BufferSize =8; //´«ÊäÊý¾Ý´óСΪ8£¬µ¥Î»ÓÉÒÔÏÂÈ·¶¨£¬´óСҪÅäºÏ¶¨ÒåµÄÊý×éÀàÐͺÍÍâÉèÊý¾ÝÀàÐÍ DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //ÍâÉèµØÖ·¼Ä´æÆ÷×Ô¶¯Ôö¼Ó½ûÖ¹£¬ÒòΪÕâÀïÖ»Óõ½ÁËDRÊý¾Ý¼Ä´æÆ÷ DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //ÄÚ´æµØÖ·×ÔÔöÔÊÐí£¬ÒòΪҪ¶ÁÈ¡Ò»¸öÊý×é DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //ÍâÉèµÄÊý¾Ý´óС£¬ÒòΪUSART6_DRÊý¾Ý¼Ä´æÆ÷Ϊ8Ϊ£¬¹ÊÑ¡Byte DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //ÕâÀïҲѡByte DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //DMA´«ÊäģʽΪNormal£¬Èç¹ûΪCircular,½«»áÑ­»·´«Êä DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; //ÓÅÏȼ¶ÎªHigh DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA2_Stream5, &DMA_InitStructure); //DMA_ITConfig(DMA2_Stream5, DMA_IT_TC, ENABLE); DMA_ClearITPendingBit(DMA2_Stream5, DMA_IT_TCIF5); USART_DMACmd(USART1,USART_DMAReq_Rx, ENABLE); DMA_Cmd(DMA2_Stream5, ENABLE); } /*dma ½ÓÊÕÖжÏ*/ void DMA2_Stream5_IRQHandler(void) { if (DMA_GetITStatus(DMA2_Stream5, DMA_IT_TCIF5) != RESET) { DMA_Cmd(DMA2_Stream5, DISABLE); DMA_ClearITPendingBit(DMA2_Stream5, DMA_IT_TCIF5); USART_DMACmd(USART1, USART_DMAReq_Rx, DISABLE); usart1_dma_flag=1; } } //USART1 DMA ·¢ËÍÖÐ¶Ï void DMA2_Stream7_IRQHandler(void) { if (DMA_GetITStatus(DMA2_Stream7, DMA_IT_TCIF7) != RESET) //·¢ËÍÍê³É { DMA_ClearITPendingBit(DMA2_Stream7, DMA_IT_TCIF7); USART_DMACmd(USART1, USART_DMAReq_Tx, DISABLE); DMA_Cmd(DMA2_Stream7, DISABLE); } } void UASRT1_DMA_Senddata(u32 par,u16 ndtr) { DMA_Cmd(DMA2_Stream7, DISABLE); DMA2_Stream7->M0AR = (uint32_t)(par); DMA2_Stream7->NDTR = ndtr; DMA_Cmd(DMA2_Stream7, ENABLE); USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE); } void USART1_IRQHandler(void) { if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //ÅжÏΪ½ÓÊÕÖÐ¶Ï { // uint8_t Received; // float angle_temp=0; // Received = USART_ReceiveData(USART1); // USART_SendData(USART1,Received); // usart1_dma_flag=1; USART_ClearITPendingBit(USART1,USART_IT_RXNE); } } int main(void) { while(1) if(usart1_dma_flag==1)//Êý¾Ý½ÓÊÜ { //......... DMA_Cmd(DMA2_Stream5, ENABLE); USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE); } } void NVIC_Config() { NVIC_InitTypeDef NVIC_InitStructure; /* Enable the USARTx Interrupt */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); ////// NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); ////// NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); ////// NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //ǶÌ×ÓÅÏȼ¶·Ö×éΪ 1 NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream5_IRQn; //ǶÌ×ͨµÀΪDMA2_Stream7_IRQn NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //ÇÀÕ¼ÓÅÏȼ¶Îª 1 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //ÏìÓ¦ÓÅÏȼ¶Îª 0 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ͨµÀÖжÏʹÄÜ NVIC_Init(&NVIC_InitStructure); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //ǶÌ×ÓÅÏȼ¶·Ö×éΪ 1 NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream7_IRQn; //ǶÌ×ͨµÀΪDMA2_Stream7_IRQn NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //ÇÀÕ¼ÓÅÏȼ¶Îª 1 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4; //ÏìÓ¦ÓÅÏȼ¶Îª 0 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ͨµÀÖжÏʹÄÜ NVIC_Init(&NVIC_InitStructure); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); ////// NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 5; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //ǶÌ×ÓÅÏȼ¶·Ö×éΪ 1 NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream1_IRQn; //ǶÌ×ͨµÀΪDMA2_Stream7_IRQn NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //ÇÀÕ¼ÓÅÏȼ¶Îª 1 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 6; //ÏìÓ¦ÓÅÏȼ¶Îª 0 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ͨµÀÖжÏʹÄÜ NVIC_Init(&NVIC_InitStructure); // // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); // ÇÀռʽÓÅÏȼ¶±ð // NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;//Ö¸¶¨ÖжÏÔ´ // NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // NVIC_InitStructure.NVIC_IRQChannelSubPriority = 7;// Ö¸¶¨ÏìÓ¦ÓÅÏȼ¶±ð1 // NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // NVIC_Init(&NVIC_InitStructure); // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); // ÇÀռʽÓÅÏȼ¶±ð NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_TIM10_IRQn;//Ö¸¶¨ÖжÏÔ´ NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 7;// Ö¸¶¨ÏìÓ¦ÓÅÏȼ¶±ð1 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); // ÇÀռʽÓÅÏȼ¶±ð NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }

stm32串口发送可以进入中断但没有数据显示

程序功能是:若接收到数据0x55,则发送接收到的数据(0x55)。 但现在问题是:通过串口助手发送0x55,已经进入接收中断接受到数据,然后通过SendData函数发送数据,能进入发送中断,但是串口助手却没有显示0x55(好像是数据并没有发送出去) /*头文件*/ #ifndef __HEAD_H__ #define __HEAD_H__ #include <misc.h> #include <stm32f10x_usart.h> #include <stm32f10x_gpio.h> #include <STM32F10x_rcc.h> #include <stdint.h> #include "stm32f10x_flash.h" //#include <stm32100e_eval.h> //#include <stm32_eval.h> void NVIC_Configuration(void); void RCC_Configuration(void); void Usart_Initial(void); void gpio_Init(void); void Delayms(uint16_t ms); extern uint16_t rece; #endif /*函数*/ #include "head.h" void NVIC_Configuration(void){ //ÖжÏÓÅÏȼ¶ÉèÖà NVIC_InitTypeDef NVIC_InitStruc; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); NVIC_InitStruc.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStruc.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStruc.NVIC_IRQChannelSubPriority = 0; NVIC_InitStruc.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStruc); } void RCC_Configuration(void){ ErrorStatus HSEStartUpStatus; RCC_DeInit(); RCC_HSEConfig(RCC_HSE_ON); HSEStartUpStatus = RCC_WaitForHSEStartUp(); if(RCC_WaitForHSEStartUp() == SUCCESS) { FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); FLASH_SetLatency(FLASH_Latency_2); RCC_HCLKConfig(RCC_SYSCLK_Div1); RCC_PCLK2Config(RCC_HCLK_Div1); RCC_PCLK1Config(RCC_HCLK_Div2); RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); RCC_PLLCmd(ENABLE); while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY==RESET)) {} RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); while(RCC_GetSYSCLKSource()!=0x08){} } RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE); } void Usart_Initial(void){ USART_InitTypeDef USART_InitStructure; USART_ClockInitTypeDef USART_ClockStructure; USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_WordLength = USART_WordLength_8b; // USART_ClockStructure.USART_Clock = USART_Clock_Disable; // USART_ClockStructure.USART_CPHA = USART_CPHA_2Edge; // USART_ClockStructure.USART_CPOL = USART_CPOL_Low; // USART_ClockStructure.USART_LastBit = USART_LastBit_Disable; USART_ClockStructInit(&USART_ClockStructure); USART_Init(USART1,&USART_InitStructure); USART_ClockInit(USART1,&USART_ClockStructure); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); // USART_ITConfig(USART1,USART_IT_TXE,ENABLE); // USART_ITConfig(USART1, USART_IT_RXNE,ENABLE); // USART_WakeUpConfig(USART1, USART_WakeUp_IdleLine); USART_Cmd(USART1,ENABLE); } void gpio_Init(void){ GPIO_InitTypeDef GPIO_InitSTA; GPIO_InitTypeDef GPIO_InitSTB; /* GPIO_PinLockConfig(GPIOB,GPIO_Pin_12);*/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOB,ENABLE); GPIO_InitSTA.GPIO_Mode = GPIO_Mode_Out_PP; //TxD1 GPIO_InitSTA.GPIO_Pin = GPIO_Pin_9; GPIO_InitSTA.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitSTA); GPIO_InitSTA.GPIO_Mode = GPIO_Mode_IN_FLOATING; //RxD1 GPIO_InitSTA.GPIO_Pin = GPIO_Pin_10; // GPIO_InitSTA.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitSTA); GPIO_InitSTB.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitSTB.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13; GPIO_InitSTB.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB,&GPIO_InitSTB); GPIO_SetBits(GPIOA,GPIO_Pin_9 | GPIO_Pin_10); GPIO_SetBits(GPIOB,GPIO_Pin_12 | GPIO_Pin_13); } void Delayms(uint16_t ms){ uint16_t i = 0; uint16_t j = 0; for(j = 0;j < ms;j ++){ for(i = 0;i < 12000;i ++){ } } } /*主程序,串口usart1中断函数*/ #include "head.h" #include "stdio.h" #include "stm32f10x_it.h" uint16_t rece = 0; int main(void){ NVIC_Configuration(); RCC_Configuration(); Usart_Initial(); gpio_Init(); USART_ITConfig(USART1,USART_IT_TXE,ENABLE); USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); GPIO_WriteBit(GPIOB,GPIO_Pin_12 | GPIO_Pin_13,Bit_RESET); while(1){ // GPIO_WriteBit(GPIOB,GPIO_Pin_12 | GPIO_Pin_13,Bit_SET); // Delayms(1000); GPIO_WriteBit(GPIOB,GPIO_Pin_12 | GPIO_Pin_13,Bit_RESET); Delayms(1000); // USART_SendData(USART1,0x55); // Delayms(1000); // while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET){}; } } void USART1_IRQHandler(void) { /* if(USART_GetITStatus(USART1, USART_IT_PE) != RESET) { USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); rece = USART_ReceiveData(USART1); } */ if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET) { GPIO_WriteBit(GPIOB,GPIO_Pin_12,Bit_SET); //若发送0x55,引脚电平会变 // USART_SendData(USART1, 0xff); // while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET){}; USART_ClearFlag(USART1,USART_FLAG_TC); USART_ITConfig(USART1, USART_IT_TXE, DISABLE); } if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { /* Disable USART1 RXNE Interrupt */ rece = USART_ReceiveData(USART1); USART_ClearFlag(USART1,USART_IT_RXNE); // USART_ITConfig(USART1, USART_IT_RXNE, DISABLE); if(rece == 0x55){ // GPIO_WriteBit(GPIOB,GPIO_Pin_12,Bit_SET); USART_ITConfig(USART1,USART_IT_TXE,ENABLE); USART_SendData(USART1,rece); while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET){}; // printf("hello"); } } }

STM32F407 UART5没反应,波形也不对

#include "usart.h" #include "stm32f4xx.h" #define RS485tX GPIO_SetBits(GPIOE,GPIO_Pin_1);//1 拉高 #define RS485rX GPIO_ResetBits(GPIOE, GPIO_Pin_1);//0 拉低 void uart_init(void) { USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE); GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_UART5); //UART5_RX GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_UART5); //UART5_TX GPIO_PinAFConfig(GPIOE, GPIO_PinSource1, GPIO_AF_UART5); //uart5_DE GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_Init(GPIOE, &GPIO_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(UART5, &USART_InitStructure); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_Cmd(UART5, ENABLE); USART_ITConfig(UART5, USART_IT_RXNE, ENABLE); } static void delay(unsigned int dl) { unsigned int i,y; for(i = 0; i < 500; i++) { for(y = 0; y < dl; y++); } } void UART5_IRQHandler(void) { uint8_t x,y,z; uint16_t checksum1,checksum2; if(USART_GetITStatus(UART5, USART_IT_RXNE) != RESET) { x=USART_ReceiveData(UART5); USART_SendData(UART5,x); } } void uartsend(int ch) { RS_485tX; delay(100); while(RESET == USART_GetFlagStatus(UART5,USART_FLAG_TXE)); USART_SendData(UART5, (uint8_t) ch); RS485rX; delay(100); } 用的是STM32F407的片子,现在接受和发送数据都没有,这是为什么呢?

STM32为什么用串口接收数据不全,时常丢失?

``` ```#include "stm32f10x.h" #include "usart.h" #include "delay.h" void My_USART1_Init(void) { GPIO_InitTypeDef GPIO_InitStrue; USART_InitTypeDef USART_InitStrue; NVIC_InitTypeDef NVIC_InitStrue; //①使能GPIOA和串口1 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); //②初始化GPIOA两个引脚 GPIO_InitStrue.GPIO_Mode=GPIO_Mode_AF_PP; GPIO_InitStrue.GPIO_Pin=GPIO_Pin_9; GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz; GPIO_Init(GPIOA,&GPIO_InitStrue); GPIO_InitStrue.GPIO_Mode=GPIO_Mode_IN_FLOATING; GPIO_InitStrue.GPIO_Pin=GPIO_Pin_10; GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz; GPIO_Init(GPIOA,&GPIO_InitStrue); //③串口初始化 //波特率 USART_InitStrue.USART_BaudRate=115200; //硬件控制 USART_InitStrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None; USART_InitStrue.USART_Mode=USART_Mode_Rx|USART_Mode_Tx; //奇偶验证 USART_InitStrue.USART_Parity=USART_Parity_No; //停止位 USART_InitStrue.USART_StopBits=USART_StopBits_1; //字长 USART_InitStrue.USART_WordLength=USART_WordLength_8b; USART_Init(USART1,&USART_InitStrue); //串口使能 USART_Cmd(USART1,ENABLE);//③ USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//开启接受中断 USART_ITConfig(USART1,USART_IT_ORE,ENABLE);//开启溢出中断 //初始化中断 NVIC_InitStrue.NVIC_IRQChannel=USART1_IRQn; NVIC_InitStrue.NVIC_IRQChannelCmd=ENABLE; NVIC_InitStrue.NVIC_IRQChannelPreemptionPriority=1; NVIC_InitStrue.NVIC_IRQChannelSubPriority=1; NVIC_Init(&NVIC_InitStrue); } u8 usart_buf[4][USART_REC_LEN];//接收缓冲,最大USART_REC_LEN个字节 static volatile u16 count=0;//接收状态标记 static volatile int p=0; void USART1_IRQHandler(void) { if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)//开启接收中断 { USART_ClearITPendingBit(USART1,USART_IT_RXNE);//清除标志中断位 p = (count / USART_REC_LEN) % 4; usart_buf[p][count % USART_REC_LEN] = USART_ReceiveData(USART1); count++; } } int main(void) { //要使用中断,先要中断分组,一般在主函数的开头 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); My_USART1_Init(); delay_init(); while(1){ if(count > 0){ if(count % 256 == 0){ for(int i = 0; i < 256; i++){ USART_SendData(USART1,usart_buf[p][i]); while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET); } printf("\r\n\r\n"); delay_ms(100); } } }; } ![图片说明](https://img-ask.csdn.net/upload/201910/31/1572495626_268037.png)

STM32 SPI2使用DMA接收,DMA接收完成后不进DMA中断???

//Configuration SPI2 of CCPU2MCU RCC_AHB1PeriphClockCmd(PROTOCOL_SPI_PORT_CLK,ENABLE); //使能GPIO B口的时钟 RCC_APB1PeriphClockCmd(PROTOCOL_SPI_CLK, ENABLE); //使能SPI2 时钟 GPIO_PinAFConfig(PROTOCOL_SPI_CS_PORT, PROTOCOL_SPI_CS_PIN_SRC, GPIO_AF_SPI2); GPIO_PinAFConfig(PROTOCOL_SPI_SCK_PORT, PROTOCOL_SPI_SCK_PIN_SRC, GPIO_AF_SPI2); GPIO_PinAFConfig(PROTOCOL_SPI_MISO_PORT, PROTOCOL_SPI_MISO_PIN_SRC, GPIO_AF_SPI2); GPIO_PinAFConfig(PROTOCOL_SPI_MOSI_PORT, PROTOCOL_SPI_MOSI_PIN_SRC, GPIO_AF_SPI2); GPIO_InitStructure.GPIO_Pin = PROTOCOL_SPI_CS_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(PROTOCOL_SPI_CS_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = PROTOCOL_SPI_SCK_PIN; GPIO_Init(PROTOCOL_SPI_SCK_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = PROTOCOL_SPI_MOSI_PIN; GPIO_Init(PROTOCOL_SPI_MOSI_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Pin = PROTOCOL_SPI_MISO_PIN; GPIO_Init(PROTOCOL_SPI_MISO_PORT, &GPIO_InitStructure); SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Slave; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Hard; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 0x07; SPI_Init(SPI2, &SPI_InitStructure); SPI_Cmd(SPI2, ENABLE); // Configuration DMA1 DMA_InitTypeDef DMA_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE); DMA_DeInit(DMA1_Stream3); //SPI2 Rx DMA_InitStructure.DMA_Channel = DMA_Channel_0; DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&(SPI2->DR)); DMA_InitStructure.DMA_Memory0BaseAddr = (u32)(&USART_RX_DMA_Buf[0]); DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; DMA_InitStructure.DMA_BufferSize = 16384+6; //32768+6; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA1_Stream3, &DMA_InitStructure); SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Rx, ENABLE); DMA_ClearITPendingBit(DMA1_Stream3,DMA_IT_TCIF3); DMA_ITConfig(DMA1_Stream3,DMA_IT_TC,ENABLE); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannel = ENABLE; NVIC_Init(&NVIC_InitStructure); DMA_Cmd(DMA1_Stream3, ENABLE); // DMA Interrupt void DMA1_Stream3_IRQHandler(void) { unsigned int len; len = 0; if(DMA_GetITStatus(DMA1_Stream3, DMA_IT_TCIF3) != RESET) { DMA_ClearITPendingBit(DMA1_Stream3,DMA_IT_TCIF3); DMA_Cmd(DMA1_Stream3,DISABLE); spiRxFlag = 1; DMA_SetCurrDataCounter(DMA1_Stream3, 16384+6); DMA_Cmd(DMA1_Stream3, ENABLE); } }

STM32F103C8T6芯片写的三路超声波读距离数据跳变问题

大家好,请大家帮我看看这个问题,我使用STM32F103C8T6这款芯片,参考网上的程序改写了一个使用定时器TIM4的三个通道分别读三个超声波距离的代码,但是三个超声波的数据均不准确,显示的数据完全超出正常范围(使用单个通道测试单个超声波的数据获取基本上正确可用)。相关程序如下: 一 定时器配置 void Ultrasonic_TIM4_Init(u16 arr, u16 psc) { TIM_ICInitTypeDef TIM_ICInitStruct; GPIO_InitTypeDef GPIO_InitStruct; NVIC_InitTypeDef NVIC_InitStruct; TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct; // RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInitStruct.TIM_Period = arr; TIM_TimeBaseInitStruct.TIM_Prescaler = psc; TIM_TimeBaseInit(TIM4, &TIM_TimeBaseInitStruct); //TIM4 CH1 INIT TIM_ICInitStruct.TIM_Channel = TIM_Channel_1; TIM_ICInitStruct.TIM_ICFilter = 0x00; TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Rising; //³õʌ׎̬ÉèÖÃΪÉÏÉýÑز¶»ñ TIM_ICInitStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStruct.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInit(TIM4, &TIM_ICInitStruct); //TIM4 CH2 INIT TIM_ICInitStruct.TIM_Channel = TIM_Channel_2; TIM_ICInitStruct.TIM_ICFilter = 0x00; TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM_ICInitStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStruct.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInit(TIM4, &TIM_ICInitStruct); //TIM4 CH3 INIT TIM_ICInitStruct.TIM_Channel = TIM_Channel_3; TIM_ICInitStruct.TIM_ICFilter = 0x00; TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM_ICInitStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStruct.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInit(TIM4, &TIM_ICInitStruct); //CH1---PB6 CH2---PB7 CH3---PB8 GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPD; GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStruct); GPIO_ResetBits(GPIOB,GPIO_Pin_6); //TRIG PIN INIT PB0 PB1 PB2 GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStruct); GPIO_ResetBits(GPIOB, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2); // NVIC_InitStruct.NVIC_IRQChannel = TIM4_IRQn; NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0; NVIC_Init(&NVIC_InitStruct); // TIM_ITConfig(TIM4, TIM_IT_Update | TIM_IT_CC1, ENABLE); TIM_ITConfig(TIM4, TIM_IT_Update | TIM_IT_CC2, ENABLE); TIM_ITConfig(TIM4, TIM_IT_Update | TIM_IT_CC3, ENABLE); TIM_Cmd(TIM4, ENABLE); // Working_Light = 1; //code debug } 二 定时器中断函数 void TIM4_IRQHandler(void) { //CH1 if((TIM4CH1_CAPTURE_STA & 0x80) == 0) { if(TIM_GetITStatus(TIM4, TIM_IT_Update) == 1) { if(TIM4CH1_CAPTURE_STA & 0x40) { if((TIM4CH1_CAPTURE_STA & 0x3F) == 0x3F) { TIM4CH1_CAPTURE_STA |= 0x80; TIM4CH1_CAPTURE_VAL = 0xFFFF; // Working_Light = 0; //code debug } else { TIM4CH1_CAPTURE_STA++; // Indicator_Light_TIM2 = 0; //code debug } } } if(TIM_GetITStatus(TIM4, TIM_IT_CC1) == 1) { if(TIM4CH1_CAPTURE_STA & 0x40) { TIM4CH1_CAPTURE_STA |= 0x80; TIM4CH1_CAPTURE_VAL = TIM_GetCapture1(TIM4); TIM_OC1PolarityConfig(TIM4, TIM_ICPolarity_Rising); // Indicator_Light_TIM2 = 1; //code debug } else { TIM4CH1_CAPTURE_STA = 0; TIM4CH1_CAPTURE_VAL = 0; TIM4CH1_CAPTURE_STA |= 0x40; TIM_SetCounter(TIM4, 0); TIM_OC1PolarityConfig(TIM4, TIM_ICPolarity_Falling); // Working_Light = 1; //code debug } } } //CH2 if((TIM4CH2_CAPTURE_STA & 0x80) == 0)//»¹ÎŽ³É¹Š²¶»ñ { if(TIM_GetITStatus(TIM4, TIM_IT_Update) == 1) //žüÐÂÖжÏÔŽ ÖжϷ¢Éú£š¶šÊ±Æ÷ÖжϷ¢Éú£© { //CH2 if(TIM4CH2_CAPTURE_STA & 0x40) //ÒÑŸ­²¶»ñµœžßµçÆœÁË { if((TIM4CH2_CAPTURE_STA & 0x3F) == 0x3F) //žßµçƜ׎̬ { TIM4CH2_CAPTURE_STA |= 0x80; //±êŒÇ³É¹Š²¶»ñÁËÒ»ŽÎ TIM4CH2_CAPTURE_VAL = 0xFFFF; // Working_Light = 0; //code debug } else { TIM4CH2_CAPTURE_STA++; Indicator_Light_TIM2 = 0; //code debug } } } if(TIM_GetITStatus(TIM4, TIM_IT_CC2) == 1){ //ÍšµÀ2·¢Éú²¶»ñÊÂŒþ if(TIM4CH2_CAPTURE_STA & 0x40) // ²¶»ñµœÏÂœµÑØ { TIM4CH2_CAPTURE_STA |= 0x80; //±êŒÇ³É¹Š²¶»ñµœÒ»ŽÎÉÏÉýÑØ TIM4CH2_CAPTURE_VAL = TIM_GetCapture2(TIM4); TIM_OC2PolarityConfig(TIM4, TIM_ICPolarity_Rising);//ÉèÖÃΪÉÏÉýÑز¶»ñ Indicator_Light_TIM2 = 1; //code debug } else { TIM4CH2_CAPTURE_STA = 0; //Çå¿Õ TIM4CH2_CAPTURE_VAL = 0; TIM4CH2_CAPTURE_STA |= 0x40; //±êŒÇ²¶»ñµœÁËÉÏÉýÑØ TIM_SetCounter(TIM4, 0); TIM_OC2PolarityConfig(TIM4, TIM_ICPolarity_Falling); //ÉèÖÃΪÏÂœµÑز¶»ñ // Working_Light = 1; //code debug } } } //CH3 if((TIM4CH3_CAPTURE_STA & 0x80) == 0) { if(TIM_GetITStatus(TIM4, TIM_IT_Update) == 1) { if(TIM4CH3_CAPTURE_STA & 0x40) { if((TIM4CH3_CAPTURE_STA & 0x3F) == 0x3F) { TIM4CH3_CAPTURE_STA |= 0x80; TIM4CH3_CAPTURE_VAL = 0xFFFF; // Working_Light = 0; //code debug } else { TIM4CH3_CAPTURE_STA++; // Indicator_Light_TIM2 = 0; //code debug } } } if(TIM_GetITStatus(TIM4, TIM_IT_CC3) == 1) { if(TIM4CH3_CAPTURE_STA & 0x40) { TIM4CH3_CAPTURE_STA |= 0x80; TIM4CH3_CAPTURE_VAL = TIM_GetCapture3(TIM4); TIM_OC3PolarityConfig(TIM4, TIM_ICPolarity_Rising); // Indicator_Light_TIM2 = 1; //code debug } else { TIM4CH3_CAPTURE_STA = 0; TIM4CH3_CAPTURE_VAL = 0; TIM4CH3_CAPTURE_STA |= 0x40; TIM_SetCounter(TIM4, 0); TIM_OC3PolarityConfig(TIM4, TIM_ICPolarity_Falling); // Working_Light = 1; //code debug } } } // TIM_SetCounter(TIM4, 0); TIM_ClearITPendingBit(TIM4, TIM_IT_Update | TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3); //Çå³ýÖÐ¶Ï } 三 计算距离的函数 通道1 u16 Ultrasonic_Value() { u32 temp; GPIO_SetBits(GPIOB, GPIO_Pin_0); delay_us(20); GPIO_ResetBits(GPIOB, GPIO_Pin_0); // Ultrasonic_TIM4_Init(arr, psc); delay_ms(1); while(TIM4CH1_CAPTURE_STA&0x80) { temp=TIM4CH1_CAPTURE_STA&0X3F; temp*=65536;//Òç³öʱŒä×ÜºÍ temp+=TIM4CH1_CAPTURE_VAL;//µÃµœ×ܵĞߵçƜʱŒä //value = ~~ (cm) value = 340*temp/2/10000; TIM4CH1_CAPTURE_STA = 0; } return value; } 通道2 u16 Ultrasonic_Value2() { u32 temp; GPIO_SetBits(GPIOB, GPIO_Pin_1); delay_us(20); GPIO_ResetBits(GPIOB, GPIO_Pin_1); // delay_ms(1); while(TIM4CH2_CAPTURE_STA&0x80) { temp=TIM4CH2_CAPTURE_STA&0X3F; temp*=65536;//Òç³öʱŒä×ÜºÍ temp+=TIM4CH2_CAPTURE_VAL;//µÃµœ×ܵĞߵçƜʱŒä TIM4CH2_CAPTURE_STA &= 0x3f;//Çå3y±ê֟λ //value = ~~ (cm) value2 = 340*temp/2/10000; TIM4CH2_CAPTURE_STA = 0; } return value2; } 通道3 u16 Ultrasonic_Value3() { u32 temp; GPIO_SetBits(GPIOB, GPIO_Pin_2); delay_us(20); GPIO_ResetBits(GPIOB, GPIO_Pin_2); // delay_ms(1); while(TIM4CH3_CAPTURE_STA&0x80) { temp=TIM4CH3_CAPTURE_STA&0X3F; temp*=65536;//Òç³öʱŒä×ÜºÍ temp+=TIM4CH3_CAPTURE_VAL;//µÃµœ×ܵĞߵçƜʱŒä TIM4CH3_CAPTURE_STA &= 0x3f;//Çå3y±ê֟λ //value = ~~ (cm) value3 = 340*temp/2/10000; TIM4CH3_CAPTURE_STA = 0; } return value3; } 四 主函数 (主函数里我就把主要相关的代码贴出了) NVIC_Configuration(); Ultrasonic_TIM4_Init(0XFFFF,72-1); while(1){ switch (count){ case 0: Ultrasonic_Value(); count = 1; break; case 1: Ultrasonic_Value2(); count = 2; break; case 2: Ultrasonic_Value3(); count = 0; break; } 最后,我是通过Nokia5110来显示三个距离数据的,上面的代码已经确认定时器配置等没有问题(部分代码是在写帖子的时候改回来的,我现在将代码改为一个定时器负责一个超声波数据读取都不能达到要求,我想一个通道获取一路数据应该是正常的解决方式),因为已经能够通过各个通道读取到数据,但就是数据跳变的很厉害,完全不是正确数据。网络上面参考了很多方法,有的说是因为两个通道以上读取数据的时候,定时器就不能够清零,有的说定时器预装值不对等等。我能够使用stm32但是并不精通,所以这个问题困扰了我好久,本来要解决的demo也脱了很久了。在这里请各位帮我看看到底是什么原因导致了三个通道获取三个超声波数据会出现数据乱窜的问题。谢谢。

使用stm32f1通过串口接收上位机信息产生中断来控制电机,但是中断打不开,想请大佬看看是为什么?

主要思路是在串口通信助手中发送“1”,然后串口接收中断打开,在串口通信助手中打印“中断开始”的信息。但是这句“中断开始”信息没有显示,想请大佬看看shi'wei'shen'm usart.c ``` #include "stm32f10x.h" #include "usart.h" //已检查 无误 static void NVIC_Config(void) { NVIC_InitTypeDef NVIC_InitStructure; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn ; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_Init(&NVIC_InitStructure); } //串口初始化函数已检查 无误 void USART1_Init(void) { USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; //看io口和usart的时钟 //RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA,ENABLE); 函数用错了 //RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); //初始化串口USART1输入输出的io口 //输出 推挽复用输出 PA9 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //初始化PA9 GPIO_Init( GPIOA , &GPIO_InitStructure ); //输出 浮空输入 PA10 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //初始化PA10 GPIO_Init( GPIOA , &GPIO_InitStructure ); //初始化串口 USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Parity = USART_Parity_No; USART_Init(USART1,&USART_InitStructure); //串口中断优先级配置 NVIC_Config(); //串口接收中断 因为是接受上位机消息后驱动电机转动,所以使用接受中断 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //串口使能 USART_Cmd( USART1,ENABLE); } /* 使用串口发送一个字节 */ void USART_SendByte(USART_TypeDef* pUSARTx,uint16_t Data) { USART_SendData( pUSARTx, Data); while( USART_GetFlagStatus( pUSARTx,USART_FLAG_TXE) == RESET); } /* 发送字符串(来自野火教程) */ void USART_SendStr( USART_TypeDef* pUSARTx , uint8_t *str) { uint8_t i=0; do { USART_SendByte( pUSARTx, *(str+i)); i++; }while( *(str+i) != '\0' ); while( USART_GetFlagStatus( pUSARTx,USART_FLAG_TC) == RESET); } ///重定向c库函数printf到串口,重定向后可使用printf函数 int fputc(int ch, FILE *f) { /* 发送一个字节数据到串口 */ USART_SendData(USART1, (uint8_t) ch); /* 等待发送完毕 */ while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); return (ch); } ///重定向c库函数scanf到串口,重写向后可使用scanf、getchar等函数 int fgetc(FILE *f) { /* 等待串口输入数据 */ while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET); return (int)USART_ReceiveData(USART1); } ``` usart.h ``` #ifndef __USART_H #define __USART_H #include "stm32f10x.h" #include <stdio.h> void USART1_Init(void); void Usart_SendByte(USART_TypeDef* pUSARTx, uint8_t data); void Usart_SendStr(USART_TypeDef* pUSARTx, uint8_t *str); #endif /* __USART_H */ ``` main.c ``` #include "stm32f10x.h" #include "moto.h" #include "usart.h" int main(void) { USART1_Init(); USART_SendStr( USART1 , "000\n"); //USART1_Init(); while(1){} } ``` 中断服务函数 ``` #include "stm32f10x_it.h" #include "usart.h" u8 Start_Flag=1; //电机启动/锁定标志 u16 Pluse_High=10; //脉冲高电平中断次数 1->10us u16 Pluse_Period=200;//脉冲周期中断次数 (转速) //脉冲中断服务函数------------------------------------------------------------ u32 TimeCount=0;//进中断计数 u16 a=0; void SysTick_Handler(void) { } void USART_IT_Handler(void) { //USART_SendStr( USART1 , "test\n"); if(USART_GetITStatus( USART1 ,USART_IT_RXNE)!=RESET) { printf("中断开始\n");//这句信息在串口通信助手中不显示 while(1) { if( USART_ReceiveData(USART1) == 1 ); break; } GPIO_SetBits(GPIOE,GPIO_Pin_5); while(a>1000)//让方向信号的输出略大于脉冲信号 {a++;} DJ_Init(); while(1){TIM4_Int_Init(10-1,84-1);} } } ```

stm32F4DISCOVERY串口通信问题

void RCC_Configuration(void) { RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); } void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; /* Configure USART Tx and Rx as alternate function push-pull */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Pin =GPIO_Pin_9; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOD,GPIO_PinSource8,GPIO_AF_USART3); GPIO_PinAFConfig(GPIOD,GPIO_PinSource9,GPIO_AF_USART3); } void USART_Configuration(void) { USART_InitTypeDef USART_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(USART3,&USART_InitStructure); /* Configure USART1 basic and asynchronous paramters */ //USART_ITConfig(USART3,USART_IT_RXNE,ENABLE); USART_ITConfig(USART3,USART_IT_TXE,ENABLE); USART_Cmd(USART3, ENABLE); /* Enable USART1 */ USART_ClearITPendingBit(USART3,USART_IT_TC); } void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure;//????? NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } int main(void) { RCC_Configuration(); GPIO_Configuration(); NVIC_Configuration(); USART_Configuration(); while(1); } volatile char StringLoop[] = "The quick brown fox jumps over the lazy dog"; void USART3_IRQHandler(void) { static int tx_index = 0; // static int rx_index = 0; if (USART_GetITStatus(USART3, USART_IT_TXE) != RESET) // Transmit the string in a loop { USART_ClearITPendingBit(USART3, USART_IT_TXE); USART_SendData(USART3, StringLoop[tx_index++]); if (tx_index >= (sizeof(StringLoop) - 1)) tx_index = 0; } } 就是这个程序为什么会不停的发送数据,PC接收端一直在接收,不是清除了标志位吗,想问问大家啊

STM32F4超声波串口打印不出来,大神求救

STM32F4超声波模块测距,想串口助手显示距离打印不出来,新手求救,不仅感谢。程序如下: cs.c #include "cs.h" #include "stm32f4xx.h" #include "delay.h" #include "usart.h" /*¼Ç¼¶¨Ê±Æ÷Òç³ö´ÎÊý*/ uint overcount=0; /*ÉèÖÃÖжÏÓÅÏȼ¶*/ void NVIC_Config(void) { NVIC_InitTypeDef NVIC_InitStructer; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitStructer.NVIC_IRQChannelPreemptionPriority=0; NVIC_InitStructer.NVIC_IRQChannelSubPriority=0; NVIC_InitStructer.NVIC_IRQChannel=TIM2_IRQn; NVIC_InitStructer.NVIC_IRQChannelCmd=ENABLE; NVIC_Init(&NVIC_InitStructer); } /*³õʼ»¯Ä£¿éµÄGPIOÒÔ¼°³õʼ»¯¶¨Ê±Æ÷TIM2*/ void CH_SR04_Init(void) { GPIO_InitTypeDef GPIO_InitStructer; TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructer; RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOB, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); /*TRIG´¥·¢ÐźÅ*/ GPIO_InitStructer.GPIO_Speed=GPIO_Speed_50MHz; GPIO_InitStructer.GPIO_Mode=GPIO_Mode_OUT; GPIO_InitStructer.GPIO_Pin=GPIO_Pin_8; GPIO_Init(GPIOB, &GPIO_InitStructer); /*ECOH»ØÏìÐźÅ*/ GPIO_InitStructer.GPIO_Mode=GPIO_Mode_IN; GPIO_InitStructer.GPIO_Pin=GPIO_Pin_9; GPIO_Init(GPIOB, & GPIO_InitStructer); /*¶¨Ê±Æ÷TIM2³õʼ»¯*/ TIM_DeInit(TIM2); TIM_TimeBaseInitStructer.TIM_Period=999;//¶¨Ê±ÖÜÆÚ1000 TIM_TimeBaseInitStructer.TIM_Prescaler=71; //·ÖƵϵÊý72 TIM_TimeBaseInitStructer.TIM_ClockDivision=TIM_CKD_DIV1; TIM_TimeBaseInitStructer.TIM_CounterMode=TIM_CounterMode_Up; //TIM_TimeBaseInitStructer.TIM_RepetitionCounter TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructer); TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);//¿ªÆô¸üÐÂÖÐ¶Ï NVIC_Config(); TIM_Cmd(TIM2,DISABLE);//¹Ø±Õ¶¨Ê±Æ÷ʹÄÜ } float Senor_Using(void) { float length=0,sum=0; u16 tim; uint i=0; /*²â5´ËÊý¾Ý¼ÆËãÒ»´Îƽ¾ùÖµ*/ while(i!=5) { PBout(8)=1; //À­¸ßÐźţ¬×÷Ϊ´¥·¢ÐźŠdelay_us(20); //¸ßµçƽÐźų¬¹ý10us PBout(8)=0; /*µÈ´ý»ØÏìÐźÅ*/ while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_9)==RESET); TIM_Cmd(TIM2,ENABLE);//»ØÏìÐźŵ½À´£¬¿ªÆô¶¨Ê±Æ÷¼ÆÊý i+=1; //ÿ´ÎÊÕµ½Ò»´Î»ØÏìÐźÅ+1,ÊÕµ½5´Î¾Í¼ÆËã¾ùÖµ while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_9)==SET);//»ØÏìÐźÅÏûʧ TIM_Cmd(TIM2,DISABLE);//¹Ø±Õ¶¨Ê±Æ÷ tim=TIM_GetCounter(TIM2);//»ñÈ¡¼ÆTIM2Êý¼Ä´æÆ÷ÖеļÆÊýÖµ£¬Ò»±ß¼ÆËã»ØÏìÐźŠlength=(tim+overcount*1000)/58.0;//ͨ¹ý»ØÏìÐźżÆËã¾àÀë sum=length+sum; TIM2->CNT=0; //½«TIM2¼ÆÊý¼Ä´æÆ÷µÄ¼ÆÊýÖµÇåÁã overcount=0; //ÖжÏÒç³ö´ÎÊýÇåÁã delay_ms(100); } length=sum/5; return length;//¾àÀë×÷Ϊº¯Êý·µ»ØÖµ } void TIM2_IRQHandler(void) { if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=RESET) { TIM_ClearITPendingBit(TIM2,TIM_IT_Update);//Çå³ýÖжϱêÖ¾ overcount++; } } 主函数 #include "stm32f4xx.h" #include "usart.h" #include "delay.h" #include "cs.h" float ChangeDistance(int cout1) { float distance=0; printf("cou1=%d\n",cout1); distance=cout1/58.0; return distance; } int main(void) { NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//ÉèÖÃϵͳÖжÏÓÅÏȼ¶·Ö×é2 delay_init(168); //ÑÓʱ³õʼ»¯ uart_init(115200); //´®¿Ú³õʼ»¯²¨ÌØÂÊΪ115200 // int count=0; // float distance=0; CH_SR04_Init(); printf("Senor_Using\n"); }

STM32 串口收发数据出错

#include "stm32f10x.h" #include <stdio.h> /************************************************* 函数: void RCC_Configuration(void) 功能: 复位和时钟控制 配置 参数: 无 返回: 无 **************************************************/ void RCC_Configuration(void) { ErrorStatus HSEStartUpStatus; //定义外部高速晶体启动状态枚举变量 RCC_DeInit(); //复位RCC外部设备寄存器到默认值 RCC_HSEConfig(RCC_HSE_ON); //打开外部高速晶振 HSEStartUpStatus = RCC_WaitForHSEStartUp(); //等待外部高速时钟准备好 if(HSEStartUpStatus == SUCCESS) //外部高速时钟已经准别好 { FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //开启FLASH预读缓冲功能,加速FLASH的读取。所有程序中必须的用法.位置:RCC初始化子函数里面,时钟起振之后 FLASH_SetLatency(FLASH_Latency_2); //flash操作的延时 RCC_HCLKConfig(RCC_SYSCLK_Div1); //配置AHB(HCLK)时钟等于==SYSCLK RCC_PCLK2Config(RCC_HCLK_Div1); //配置APB2(PCLK2)钟==AHB时钟 RCC_PCLK1Config(RCC_HCLK_Div2); //配置APB1(PCLK1)钟==AHB1/2时钟 RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); //配置PLL时钟 == 外部高速晶体时钟 * 9 = 72MHz RCC_PLLCmd(ENABLE); //使能PLL时钟 while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) //等待PLL时钟就绪 { } RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //配置系统时钟 = PLL时钟 while(RCC_GetSYSCLKSource() != 0x08) //检查PLL时钟是否作为系统时钟 { } } } /******************************************************************************* * Function Name : NVIC_Configuration * Description : Configures NVIC and Vector Table base location. * Input : None * Output : None * Return : None *******************************************************************************/ void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; /* Set the Vector Table base location at 0x08000000 */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); /* Configure the NVIC Preemption Priority Bits */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); /* Enable the USART1 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; //通道设置为串口3中断 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //中断响应优先级0 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打开中断 NVIC_Init(&NVIC_InitStructure); //初始化 } /******************************************************************************* 函数名:USART3_Configuration 输 入: 输 出: 功能说明: 初始化串口硬件设备,启用中断 配置步骤: (1)打开GPIO和USART3的时钟 (2)设置USART3两个管脚GPIO模式 (3)配置USART3数据格式、波特率等参数 (4)使能USART3接收中断功能 (5)最后使能USART3功能 */ void USART3_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; /* 第1步:打开GPIO和USART部件的时钟 */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); /* 第2步:将USART Tx的GPIO配置为推挽复用模式 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); /* 第3步:将USART Rx的GPIO配置为浮空输入模式*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &GPIO_InitStructure); /* 第4步:配置USART3参数*/ 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(USART3, &USART_InitStructure); /* 若接收数据寄存器满,则产生中断 */ USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); /* 第5步:使能 USART3, 配置完毕 */ USART_Cmd(USART3, ENABLE); /* 如下语句解决第1个字节无法正确发送出去的问题 */ USART_ClearFlag(USART3, USART_FLAG_TC); // 清标志 } /*******************************************************************/ /* */ /* STM32向串口3发送1字节 */ /* */ /* */ /*******************************************************************/ void Uart3_PutChar(u8 ch) { USART_SendData(USART3, (u8) ch); while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET); } /*******************************************************************/ /* */ /* STM32在串口3接收1字节 */ /* 说明:串口3接收中断 */ /* */ /*******************************************************************/ void USART3_IRQHandler(void) { u8 dat; if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //若接收数据寄存器满 { dat = USART_ReceiveData(USART3); Uart3_PutChar(dat); } } /************************************************* 函数: int main(void) 功能: main主函数 参数: 无 返回: 无 **************************************************/ int main(void) { u8 abc[]={0x11,0x12,0x13,0x14}; u8 i; RCC_Configuration(); NVIC_Configuration(); USART3_Configuration(); for(i=0;i<4;i++) {Uart3_PutChar(abc[i]);} while(1); }

STM32F103CB使用MAX485接收不到

PC向主机串口1发送的数据,由串口2经MAX485转发,从机接收数据并通过MAX485到串口2,最后控制继电器。目前发现串口1发送485发送的数据正常,从机用示波器测量MAX485和串口2处有信号,但是从机芯片无法接收。主机从机发送正常,单独使用串口2发送到串口1也正常。 串口2是PA2和PA3,使用PA4做MAX485控制引脚 #define RS485_TX GPIO_SetBits(GPIOA,GPIO_Pin_4) //485发送使能(在使用时注意有响应延时) #define RS485_RX GPIO_ResetBits(GPIOA,GPIO_Pin_4) //485接收使能(在使用时注意有响应延时) #define USART2_RX_EN 1 //0:不接收;1:接收 #define USART_REC_LEN 200 //定义最大接收字节数 200 u16 USART_RX_STA; // 串口1接收状态标志 u8 USART_RX_BUF[USART_REC_LEN]; //串口1接收缓冲 /** //功能:RS485串口2初始化 //参数:bound:波特率 //返回值:None **/ void RS485_Init(u32 bound) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); /*************************************GPIO Config****************************************/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; 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_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStructure); #if USART2_RX_EN /*************************************USART Config*****************************************/ USART_InitStructure.USART_BaudRate = bound; 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(USART2,&USART_InitStructure); USART_Cmd(USART1, ENABLE); //使能串口外设 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //使能串口接收中断 USART_ClearFlag(USART1, USART_FLAG_TC); //软件清除发送完成标志位 /**************************************NVIC Config*****************************************/ NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStructure.NVIC_IRQChannelSubPriority =2; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); #endif RS485_RX; //485默认接收 } //********************串口2中断服务函数**********// void USART2_IRQHandler() { u16 temp; if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //接收中断,必须以0x0d,0x0a结尾 { temp = USART_ReceiveData(USART2); //读取接收到的数据 if((USART_RX_STA & 0x8000) == 0) //接收未完成 { if(USART_RX_STA & 0x4000) //如果接收到了0x0d { if(temp != 0x0a) USART_RX_STA = 0; //接受错误,重新开始接收 else { USART_RX_STA |= 0x8000; //接收完成 } } else //未收到0x0d { if(temp == 0x0d) USART_RX_STA |= 0X4000; else { USART_RX_BUF[USART_RX_STA & 0X3FFF] = temp; USART_RX_STA++; if(USART_RX_STA > (USART_REC_LEN - 1)) USART_RX_STA = 0; //接收数据错误,重新开始接收 } } } }else if(USART_GetFlagStatus(USART2,USART_FLAG_IDLE)!=RESET) { USART_ClearFlag(USART2,USART_FLAG_IDLE); } } int main() { u16 t = 0; u8 length; u16 times=0; SysTick_Init(); Delay_us(100); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC优先级分组2:2位抢占优先级,2位响应优先级 UART1_Init(115200); UART2_Init(9600); RS485_Init(9600); printf("\r\n01 finsh\r\n"); printf("\r\n等待接收\r\n"); while(1) { if(USART_RX_STA & 0x8000) { RS485_RX; Delay_ms(100); length = USART_RX_STA&0X3fff; //获取数据长 printf("\r\nyour massage is:\r\n"); for(t=0;t<length;t++) { Delay_us(1000); SendByte(USART1,USART_RX_BUF[t]);//自己写的发送函数,正常 } printf("\r\n发送完成\r\n"); RS485_RX; USART_RX_STA=0; }else { times++; if(times % 5000 == 0) { printf("\r\nlab\r\n"); printf("ALIENTEK\r\n\r\n"); } if(times % 200 == 0) printf("\r\n waitting \r\n"); if(times % 30 == 0) printf("\r\nsystem runing\n");//提示系统正在运行 Delay_ms(10); } } }

YOLOv3目标检测实战:训练自己的数据集

YOLOv3目标检测实战:训练自己的数据集

150讲轻松搞定Python网络爬虫

150讲轻松搞定Python网络爬虫

实用主义学Python(小白也容易上手的Python实用案例)

实用主义学Python(小白也容易上手的Python实用案例)

我说我不会算法,阿里把我挂了。

不说了,字节跳动也反手把我挂了。

立方体线框模型透视投影 (计算机图形学实验)

计算机图形学实验 立方体线框模型透视投影 的可执行文件,亲测可运行,若需报告可以联系我,期待和各位交流

2019 AI开发者大会

2019 AI开发者大会

组成原理课程设计(实现机器数的真值还原等功能)

实现机器数的真值还原(定点小数)、定点小数的单符号位补码加减运算、定点小数的补码乘法运算和浮点数的加减运算。

C/C++跨平台研发从基础到高阶实战系列套餐

一 专题从基础的C语言核心到c++ 和stl完成基础强化; 二 再到数据结构,设计模式完成专业计算机技能强化; 三 通过跨平台网络编程,linux编程,qt界面编程,mfc编程,windows编程,c++与lua联合编程来完成应用强化 四 最后通过基于ffmpeg的音视频播放器,直播推流,屏幕录像,

MFC一站式终极全套课程包

该套餐共包含从C小白到C++到MFC的全部课程,整套学下来绝对成为一名C++大牛!!!

软件测试2小时入门

软件测试2小时入门

三个项目玩转深度学习(附1G源码)

三个项目玩转深度学习(附1G源码)

计算机图形学-球的光照模型课程设计

计算机图形学-球的光照模型,有代码完美运行,有课程设计书

Linux常用命令大全(非常全!!!)

Linux常用命令大全(非常全!!!) 最近都在和Linux打交道,感觉还不错。我觉得Linux相比windows比较麻烦的就是很多东西都要用命令来控制,当然,这也是很多人喜欢linux的原因,比较短小但却功能强大。我将我了解到的命令列举一下,仅供大家参考: 系统信息 arch 显示机器的处理器架构 uname -m 显示机器的处理器架构 uname -r 显示正在使用的内核版本 d...

因为看了这些书,我大二就拿了华为Offer

四年了,四年,你知道大学这四年我怎么过的么?

深度学习原理+项目实战+算法详解+主流框架(套餐)

深度学习系列课程从深度学习基础知识点开始讲解一步步进入神经网络的世界再到卷积和递归神经网络,详解各大经典网络架构。实战部分选择当下最火爆深度学习框架PyTorch与Tensorflow/Keras,全程实战演示框架核心使用与建模方法。项目实战部分选择计算机视觉与自然语言处理领域经典项目,从零开始详解算法原理,debug模式逐行代码解读。适合准备就业和转行的同学们加入学习! 建议按照下列课程顺序来进行学习 (1)掌握深度学习必备经典网络架构 (2)深度框架实战方法 (3)计算机视觉与自然语言处理项目实战。(按照课程排列顺序即可)

fakeLocation13.5.1.zip

fakeLocation13.5.1 虚拟定位 ios13.5.1的最新驱动下载,iPhone/iPad免越狱虚拟定位工具Location-cleaned驱动已更新

UnityLicence

UnityLicence

Python可以这样学(第一季:Python内功修炼)

Python可以这样学(第一季:Python内功修炼)

Python+OpenCV计算机视觉

Python+OpenCV计算机视觉

土豆浏览器

土豆浏览器可以用来看各种搞笑、电影、电视剧视频

【数据结构与算法综合实验】欢乐连连看(C++ & MFC)案例

这是武汉理工大学计算机学院数据结构与算法综合实验课程的第三次项目:欢乐连连看(C++ & MFC)迭代开发代码。运行环境:VS2017。已经实现功能:开始游戏、消子、判断胜负、提示、重排、计时、帮助。

php+mysql学生成绩管理系统

学生成绩管理系统,分三个模块:学生,教师和管理员。 管理员模块:负责学生、老师信息的增删改;发布课程信息的增删改,以便让学生选课;审核老师提交的学生成绩并且打印成绩存档;按照课号查询每个课号的学生成绩

多功能数字钟.zip

利用数字电子计数知识设计并制作的数字电子钟(含multisim仿真),该数字钟具有显示星期、24小时制时间、闹铃、整点报时、时间校准功能

推荐24个国外黄色网站欣赏

在中国清朝,明黄色的衣服只有皇子才有资格穿,慢慢的黄色在中国就成了高贵的颜色。在人们的色彩印象中,黄色也表现为暂停。所以当你的网页设计采用黄色的时候,会让人们在你的网页前停留。 黄色,就像橙色和红色,黄色也是一个暖色。它有大自然、阳光、春天的涵义,而且通常被认为是一个快乐和有希望的色彩。黄色是所有色相中最能发光的颜色,给人轻快,透明,辉煌,充满希望的色彩印象。 黄色是一个高可见的色...

u-boot-2015.07.tar.bz2

uboot-2015-07最新代码,喜欢的朋友请拿去

一学即懂的计算机视觉(第一季)

一学即懂的计算机视觉(第一季)

学生成绩管理系统(PHP + MYSQL)

做的是数据库课程设计,使用的php + MySQL,本来是黄金搭配也就没啥说的,推荐使用wamp服务器,里面有详细的使用说明,带有界面的啊!呵呵 不行的话,可以给我留言!

Windows版YOLOv4目标检测实战:训练自己的数据集

Windows版YOLOv4目标检测实战:训练自己的数据集

C++语言基础视频教程

C++语言基础视频教程

玩转Python-Python3基础入门

玩转Python-Python3基础入门

相关热词 c# 开发接口 c# 中方法上面的限制 c# java 时间戳 c#单元测试入门 c# 数组转化成文本 c#实体类主外键关系设置 c# 子函数 局部 c#窗口位置设置 c# list 查询 c# 事件 执行顺序
立即提问
相关内容推荐