
这是main程序 #include "stm32f10x.h" #include "bsp_usart1.h" #include "bsp_led.h" int main(void) { LED_GPIO_Config(); USART1_Config(); NVIC_Configuration(); while(1) { char ch; if(ch=='A') { LED1(ON); } else if(ch=='B') { LED1(OFF); } } } 这是bsp.usart.c程序 #include "bsp_usart1.h" /** * @brief USART1 GPIO ÅäÖÃ,¹¤×÷ģʽÅäÖá£9600 8-N-1 * @param ÎÞ * @retval ÎÞ */ void USART1_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; /* config USART1 clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE); /* USART1 GPIO config */ /* Configure USART1 Tx (PA.09) as alternate function push-pull */ 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); /* Configure USART1 Rx (PA.10) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); /* USART1 mode config */ 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); /* ʹÄÜ´®¿Ú1½ÓÊÕÖÐ¶Ï */ USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // USART_ITConfig(USART1, USART_IT_TXE, ENABLE); USART_Cmd(USART1, ENABLE); } /// ÅäÖÃUSART1½ÓÊÕÖÐ¶Ï void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; /* Configure the NVIC Preemption Priority Bits */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); /* Enable the USARTy Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } /****************** ÖжϷþÎñ³ÌÐò *****************/ void USART1_IRQHandler(void) { uint8_t ch; if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { ch = USART_ReceiveData(USART1); printf( "\r\n%02x\r\n", ch ); //printf·¢ËÍ·½Ê½ USART_SendData(USART1,ch); //32¿âº¯Êý·¢ËÍ·½Ê½ while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET); USART_ClearFlag(USART1,USART_FLAG_TC); } } /****************** ¿ÉÒÔ·¢ËÍÒ»¸ö×Ö·û´® *******************/ void USART1_Send_s(u8 *ch) { while(*ch) { USART_SendData(USART1,*ch); while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET); USART_ClearFlag(USART1,USART_FLAG_TC); } } /// ÖØ¶¨Ïòc¿âº¯Êýprintfµ½USART1 int fputc(int ch, FILE *f) { /* ·¢ËÍÒ»¸ö×Ö½ÚÊý¾Ýµ½USART1 */ USART_SendData(USART1, (uint8_t) ch); /* µÈ´ý·¢ËÍÍê±Ï */ while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET); return (ch); } /// ÖØ¶¨Ïòc¿âº¯Êýscanfµ½USART1 int fgetc(FILE *f) { /* µÈ´ý´®¿Ú1ÊäÈëÊý¾Ý */ while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET); return (int)USART_ReceiveData(USART1); } 本人菜鸟,求大神指导 |
LED1(ON);
和
LED1(OFF);
如果有效证明问题可能在通讯方面,否则有可能是引脚定义与LED的位置不符。
评分
查看全部评分
就是在主函数中去掉与LED1无关的语句,再用LED1(ON);如果亮(也可能是LED1(OFF);视硬件的接法),则说明对LED1的控制是有效的,问题是处在其他方面。
2,没看到LED_GPIO_Config();,想来应该不会有问题;
3,把printf( "\r\n%02x\r\n", ch ); 放到LED(ON);和LED(OFF);至少可以确定通讯是否有问题。
评分
查看全部评分
我把ch改为全局变量了,灯亮了,但是进入死循环出不来,只有复位后发送B才能灭,否则一直亮着
那么第一,你应该先把串口的收和发的功能都实现;
第二步才来实现根据接收的数据实现对应的功能。