1.采用串口必须开启相应的GPIO时钟吗?
2.写了一个串口的程序,从上位机向芯片发送数据很成功,但是从芯片向pc发送数据则毫无反应,采用的IAR.下为程序(向芯片发送a 则led灯亮 发b则灯灭 printf和senddata都用了):
#include "stm32f10x.h"
#include <stdio.h>
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
uint8_t chars;
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
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(USART1, &USART_InitStructure);
USART_Cmd(USART1,ENABLE);
printf("\n\rMMMMMMMMMMMMMMMMMMMM\n\r");
printf("\n\rxxxxxxxxxxxxxxxxxx\n\r");
while(1)
{
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
chars=(USART_ReceiveData(USART1));
USART_SendData(USART1,chars);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1,0);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
if(chars=='a')
{GPIO_SetBits(GPIOA,GPIO_Pin_2);}
if(chars=='b')
{GPIO_ResetBits(GPIOA,GPIO_Pin_2);
}
}
}
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(USART1, (uint8_t) ch);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{}
return ch;
} |