中级会员 
   
	- 积分
 - 462
 
        - 金钱
 - 462 
 
       - 注册时间
 - 2017-6-7
 
      - 在线时间
 - 137 小时
 
 
 
 | 
 
10金钱 
我看了许多次我的程序,感觉配置不该有问题。后面突然想到了硬件上,然后我看到我板子原理图(战舰),发现好像不能选择USB和usart2相连。是不是我的板子没法用usart通信(硬件用USB) 
 
 
我感觉程序没错,还是贴出来吧。 
int main() 
{ 
        Usart_Init(); 
        delay_init(); 
        while(1) 
        { 
                USART_SendData(USART2, 'a'); 
//                while(!USART_GetFlagStatus(USART2, USART_FLAG_TC)); 
//                USART_ClearFlag(USART2, USART_FLAG_TC); 
                delay_ms(10); 
        } 
        return 0; 
} 
 
void Usart_Init() 
{ 
        USART_InitTypeDef USART_InitStructure; 
        GPIO_InitTypeDef GPIO_InitStructure; 
         
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); 
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; 
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 
        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_Mode = USART_Mode_Tx; 
        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; 
        USART_Init(USART2, &USART_InitStructure); 
        USART_ITConfig(USART2, USART_IT_TC, DISABLE); 
        USART_Cmd(USART2 ,ENABLE); 
} 
 
 
 |   
 
 
 
 
 
 |