当前位置:编程学习 > 网站相关 >>

STM32F407 Discovery uart1串口通信

#include "stm32f4_discovery.h"  
#include <stdio.h>  
  
#define LED1_ON     GPIO_SetBits(GPIOD,GPIO_Pin_12)  
#define LED1_OFF    GPIO_ResetBits(GPIOD,GPIO_Pin_12)  
 
#define LED2_ON     GPIO_SetBits(GPIOD,GPIO_Pin_13)  
#define LED2_OFF    GPIO_ResetBits(GPIOD,GPIO_Pin_13)  
 
extern uint8_t NbrOfDataToTransfer; 
extern uint8_t NbrOfDataToRead; 
extern __IO uint8_t TxCounter;  
extern __IO uint16_t RxCounter;  
 
void NVIC_Config(void); 
void GPIO_Configuration(void); 
void STM_EVAL_COMInit(void); 
void USART_Configuration(int BaudRate); 
void LED_Config(void); 
void Delay(__IO uint32_t nCount); 
  
int main(void) 

  /* Configure the system clocks */ 
  NVIC_Config();                /* NVIC Configuration */ 
  
//GPIO_Configuration();         /* Configure the GPIOs */  
  STM_EVAL_COMInit();   
   
  USART_Configuration(115200);        /* Configure the USART1 's mode */ 
                                /* Enable the EVAL_COM1 Transmit interrupt: this interrupt is generated when the 
                                   EVAL_COM1 transmit data register is empty */   
 
  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); 
   
  LED_Config(); 
   
  while (1) 
  { 
      Delay(0xfFFFFF);  
      Delay(0xfFFFFF);  
      printf("\n\rUSART Printf Example\n\r"); 
  } 

 
 
void Delay(__IO uint32_t nCount) 

  while(nCount--) 
  { 
  } 

 
 
/*******************************************************************************
* Function Name  : GPIO_Configuration
* Description    : Configures the different GPIO ports.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/ 
void GPIO_Configuration(void) 

  GPIO_InitTypeDef GPIO_InitStructure; 
 
  /* Configure USART1 Tx (PA.09) as alternate function push-pull */ 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; 
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; 
  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; 
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; 
  GPIO_Init(GPIOA, &GPIO_InitStructure); 

 
 
 
/**
  * @brief  Configures COM port.
  * @param  COM: Specifies the COM port to be configured.
  *   This parameter can be one of following parameters:    
  *     @arg COM1
  *     @arg COM2  
  * @param  USART_InitStruct: pointer to a USART_InitTypeDef structure that
  *   contains the configuration information for the specified USART peripheral.
  * @retval None
  */ 
void STM_EVAL_COMInit(void) 

  GPIO_InitTypeDef GPIO_InitStructure; 
 
  /* Enable GPIO clock */ 
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); 
  /* Enable UART clock */ 
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); 
  
  /* Connect PXx to USARTx_Tx*/ 
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1); 
  /* Connect PXx to USARTx_Rx*/ 
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1); 
 
  /* Configure USART Tx as alternate function  */ 
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 
 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
  GPIO_Init(GPIOB, &GPIO_InitStructure); 
 
  /* Configure USART Rx as alternate function  */ 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; 
  GPIO_Init(GPIOB, &GPIO_InitStructure); 

 
 
 
 
/*******************************************************************************
* Function Name  : USART_Configuration
* Description    : Configures the USART1.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/ 
void USART_Configuration(int BaudRate) 

    USART_InitTypeDef USART_InitStructure; 
 
    USART_InitStructure.USART_BaudRate = BaudRate; 
    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); /* Configure USART1 basic and asynchronous paramters */ 
    USART_Cmd(USART1, ENABLE);   /* Enable USART1 */ 

 
 
 
void LED_Config(void) 

    GPIO_InitTypeDef GPIO_InitStructure; 
    RCC_AHB1PeriphClock
补充:综合编程 , 其他综合 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,