代码已粘贴,请各位大佬帮忙分析下是什么原因造成得。万分感谢
sys_config.c
#include "sys_config.h" #include "stm32f10x.h" #include "stm32f10x_adc.h" #include "stm32f10x_bkp.h" #include "stm32f10x_can.h" #include "stm32f10x_cec.h" #include "stm32f10x_crc.h" #include "stm32f10x_dac.h" #include "stm32f10x_dbgmcu.h" #include "stm32f10x_dma.h" #include "stm32f10x_exti.h" #include "stm32f10x_flash.h" #include "stm32f10x_fsmc.h" #include "stm32f10x_gpio.h" #include "stm32f10x_i2c.h" #include "stm32f10x_iwdg.h" #include "stm32f10x_pwr.h" #include "stm32f10x_rcc.h" #include "stm32f10x_rtc.h" #include "stm32f10x_sdio.h" #include "stm32f10x_spi.h" #include "stm32f10x_tim.h" #include "stm32f10x_usart.h" #include "stm32f10x_wwdg.h" #include "misc.h" /@@*USART1配置*/ void USART1_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA ENABLE ); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 ENABLE ); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //USART1 TX; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA &GPIO_InitStructure); //端口A; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //USART1 RX; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA &GPIO_InitStructure); //端口A; USART_InitStructure.USART_BaudRate = 115200; //波特率; USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据位8位; USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位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);//配置串口参数; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置中断组,4位抢占优先级,4位响应优先级; NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //中断号; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //抢占优先级; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //响应优先级; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_ITConfig(USART1 USART_IT_RXNE ENABLE); USART_Cmd(USART1 ENABLE); //使能串口; } /@@*发送一个字节*/ void USART1_Send_Byte(u8 Data) //发送一个字节; { USART_SendData(USART1Data); while( USART_GetFlagStatus(USART1 USART_FLAG_TC) == RESET ); } void USART1_Send_String(u8 *Data) //发送字符串; { while(*Data) USART1_Send_Byte(*Data++); } void USART1_IRQHandler(void) //中断处理函数; { u8 res; if(USART_GetITStatus(USART1 USART_IT_RXNE) == SET) //判断是否发生中断; { USART_ClearFlag(USART1 USART_IT_RXNE); //清除标志位; res=USART_ReceiveData(USART1); //接收数据; USART1_Send_Byte(res); //用户自定义; } } /@@*USART2配置*/ void USART2_Configuration(void) { 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_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); 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(USART2 &USART_InitStructure); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_ITConfig(USART2 USART_IT_RXNE ENABLE); USART_Cmd(USART2 ENABLE); } void USART2_Send_Byte(u8 Data) //发送一个字节; { USART_SendData(USART2Data); while( USART_GetFlagStatus(USART2 USART_FLAG_TC) == RESET ); } void USART2_Send_String(u8 *Data) //发送字符串; { while(*Data) USART2_Send_Byte(*Data++); } void USART2_IRQHandler(void) //中断处理函数; { u8 res; if(USART_GetITStatus(USART2 USART_IT_RXNE) == SET) //判断是否发生中断; { USART_ClearFlag(USART2 USART_IT_RXNE); //清除标志位; USART_ClearITPendingBit(USART2 USART_IT_RXNE); res=USART_ReceiveData(USART2); //接收数据; USART2_Send_Byte(res); //用户自定义; } } void UART4_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC ENABLE ); RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4 ENABLE ); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOC &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(UART4 &USART_InitStructure); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_ITConfig(UART4 USART_IT_RXNE ENABLE); USART_Cmd(UART4 ENABLE); } void UART4_Send_Byte(u8 Data) { USART_SendData(UART4Data); while( USART_GetFlagStatus(UART4 USART_FLAG_TC) == RESET ); } void UART4_Send_String(u8 *Data) { while(*Data) UART4_Send_Byte(*Data++); } void UART4_IRQHandler(void) { u8 res; if(USART_GetITStatus(UART4 USART_IT_RXNE) == SET) { USART_ClearFlag(UART4 USART_IT_RXNE); res=USART_ReceiveData(UART4); UART4_Send_Byte(res); } } void UART5_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD ENABLE ); RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5 ENABLE ); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOD &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 = 3; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_ITConfig(UART5 USART_IT_RXNE ENABLE); USART_Cmd(UART5 ENABLE); } void UART5_Send_Byte(u8 Data) { USART_SendData(UART5Data); while( USART_GetFlagStatus(UART5 USART_FLAG_TC) == RESET ); } void UART5_Send_String(u8 *Data) { while(*Data) UART5_Send_Byte(*Data++); } void UART5_IRQHandler(void) { u8 res; if(USART_GetITStatus(UART5 USART_IT_RXNE) == SET) { USART_ClearFlag(UART5 USART_IT_RXNE); res=USART_ReceiveData(UART5); UART5_Send_Byte(res); } } /@@*......GPIO配置......*/ void GPIO_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; //定义初始化结构体 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA ENABLE); GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable ENABLE);////关闭jtag功能 //SYS-LED GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA &GPIO_InitStructure); } /@@*重新定义USART1-printf配置*/ int fputc(int ch FILE *f) { while(USART_GetFlagStatus(USART1USART_FLAG_TC)==RESET); USART_SendData(USART1(uint8_t)ch); return ch; } /@@*重新定义scanf到串口,用于scanf,getchar等*/ int fgetc(FILE *f) { /@@*等待串口输入数据*/ while (USART_GetFlagStatus(USART1 USART_FLAG_RXNE) == RESET); return (int)USART_ReceiveData(USART1); } /@@*独立看门狗*/ void IWDG_Feed(void) { IWDG_ReloadCounter(); } //#if 0 //void EXTI_Config(void) //{ // EXTI_InitTypeDef EXTI_InitStructure; // /@@*PD11外部中断输入*/ // EXTI_InitStructure.EXTI_Line = EXTI_Line13; // EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; // EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; // EXTI_InitStructure.EXTI_LineCmd = ENABLE; // EXTI_Init(&EXTI_InitStructure); // // /@@*PD12外部中断输入*/ // EXTI_InitStructure.EXTI_Line = EXTI_Line12; // EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; // EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; // EXTI_InitStructure.EXTI_LineCmd = ENABLE; // EXTI_Init(&EXTI_InitStructure); //} //#endif
sys_config.h
#ifndef __SYS_CONFIG_H_ #define __SYS_CONFIG_H_ #include "stm32f10x_conf.h" #include "stm32f10x.h" #include <stdio.h> /@@*使用位带操作,对单个IO灵活操作*/ #define GPIOA_ODR_Addr (GPIOA_base+12) //0x4001080C #define GPIOB_ODR_Addr (GPIOB_base+12) //0x40010C0C #define GPIOC_ODR_Addr (GPIOC_base+12) //0x4001100C #define GPIOD_ODR_Addr (GPIOD_base+12) //0x4001140C #define GPIOE_ODR_Addr (GPIOE_base+12) //0x4001180C #define GPIOF_ODR_Addr (GPIOF_base+12) //0x40011A0C #define GPIOG_ODR_Addr (GPIOG_base+12) //0x40011E0C
#define GPIOA_IDR_Addr (GPIOA_base+8) //0x40010808 #define GPIOB_IDR_Addr (GPIOB_base+8) //0x40010C08 #define GPIOC_IDR_Addr (GPIOC_base+8) //0x40011008 #define GPIOD_IDR_Addr (GPIOD_base+8) //0x40011408 #define GPIOE_IDR_Addr (GPIOE_base+8) //0x40011808 #define GPIOF_IDR_Addr (GPIOF_base+8) //0x40011A08 #define GPIOG_IDR_Addr (GPIOG_base+8) //0x40011E08 #define PFout(n) *((volatile unsigned long *)(0x42000000+((GPIOF_ODR_Addr-0x40000000)<<5)+(n<<2))) void GPIO_Config(void); void EXTI_Config(void); void IWDG_Feed(void); void USART1_Configuration(void); void USART2_Configuration(void); void UART4_Configuration(void); void UART5_Configuration(void); void USART1_IRQHandler(void); void USART2_IRQHandler(void); void UART4_IRQHandler(void); void UART5_IRQHandler(void); #endif
main.c
#include <stdio.h> #include "stm32f10x.h" #include "sys_config.h" #include "sys_led.h" #include "delay.h" #include "sys_relay.h" #include "stm32f10x_iwdg.h" /@@*SYS_LED*/ int main(void) { uint8_t temp1; USART1_Configuration(); USART2_Configuration(); UART4_Configuration(); UART5_Configuration(); LED_GPIO_Config(); GPIO_Config(); OUT_GPIO_Config(); IWDG_Feed(); printf("\r\n start tset.......................\r\n"); while(1) { LED_ON; delay_ms(2500); LED_OFF; delay_ms(2500); USART_SendData(USART1'1'); printf("\r\n uart1 send:%c\r\n"); USART_SendData(USART2'2'); printf("\r\n uart2 send:%c\r\n"); USART_SendData(UART4'4'); printf("\r\n uart4 send:%c\r\n"); USART_SendData(UART5'5'); printf("\r\n uart5 send:%c\r\n"); printf("\r\n send ok.......................\r\n"); temp1 = USART_ReceiveData(USART1); printf("\r\n received %c\r\n" temp1); if(temp1 == '1'){ printf("\r\n OUT1_ON.......................\r\n"); OUT1_ON; } else if(temp1 == '2'){ printf("\r\n OUT1_OFF.......................\r\n"); OUT1_OFF; } else if(temp1 == '3'){ printf("\r\n OUT2_ON.......................\r\n"); OUT2_ON; } else if(temp1 == '4'){ printf("\r\n OUT2_OFF.......................\r\n"); OUT2_OFF; } else if(temp1 == '5'){ printf("\r\n OUT3_ON.......................\r\n"); OUT3_ON; } else if(temp1 == '6'){ printf("\r\n OUT3_OFF.......................\r\n"); OUT3_OFF; } else if(temp1 == '7'){ printf("\r\n OUT4_ON.......................\r\n"); OUT4_ON; } else if(temp1 == '8'){ printf("\r\n OUT4_OFF.......................\r\n"); OUT4_OFF; } } }
这程序确实有点问题。
你的发送数据,你用过的while();
这个不是关键,最关键的你把这个发送数据用在了串口中断里面。我觉得可能是这个问题。
另外,你控制的LED灯延时2.5S也是有问题的。
改进的话:
1、使用定时器,产生一个2.5S的标志,主函数里面通过这个标志,来反转LED的电平。
2、串口接收中断里不发送串口数据(移出来)。中断要求快进快出