本帖最后由 Eagleson 于 2015-5-7 08:40 编辑 的意图是用电脑的超级终端先向usart1发送任意一个命令,然后usart1返回SBSB这个语句,接着USART2发送出一条命令oreder,和Usart2连接的模块收到这个命令后会返回一个命令rec。但我在超级终端发了一个命令后,连SBSB这个语句都没返回。求教 int main(void) { char i; char j; uint32_t oreder[ ] = {0x01, 0x03,0x00,0x48,0x00,0x08,0xC4,0x1A}; uint32_t rec[ ]={0x01, 0x03,0x00,0x48,0x00,0x08,0xC4,0x1A};; USART1_Config(); USART2_Config(); NVIC_Configuration(); while(1){ while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET) {} printf("SBSB"); while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET) {} for(i=0;i<8;i++) { USART_SendData(USART2, oreder); while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET) {} } for(j=0;i<37;j++) { while (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET) {} rec[j] = (USART_ReceiveData(USART2)); USART_SendData(USART1, rec[j]); while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET) {} } } } 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); USART_Cmd(USART1, ENABLE); } void USART2_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; /* config USART1 clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); /* USART1 GPIO config */ /* Configure USART1 Tx (PA.09) as alternate function push-pull */ 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); /* Configure USART1 Rx (PA.10) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); /* USART1 mode config */ USART_InitStructure.USART_BaudRate = 4800; 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_Cmd(USART2, ENABLE); } /* * 函数名:fputc * 描述 :重定向c库函数printf到USART1 * 输入 :无 * 输出 :无 * 调用 :由printf调用 */ int fputc(int ch, FILE *f) { /* 将Printf内容发往串口 */ USART_SendData(USART1, (unsigned char) ch); while (!(USART1->SR & USART_FLAG_TXE)); return (ch); } |
求求各位大神了 |
帮顶,希望能有答案 |
帮顶 |
你在超级终端发了一个字节后,先让一个LED闪烁,检测一下收数据的情况。 |
看不到哎 |
RCC_APB2Periph_AFIO,把这个时钟开一下试试。 |