Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.89 KB | None | 0 0
  1. #include "stm32f10x.h"
  2.  
  3. uint8_t rec = 0;
  4.  
  5. /* Private variables ---------------------------------------------------------*/
  6. USART_InitTypeDef USART_InitStructure;  
  7.  
  8. /* Private function prototypes -----------------------------------------------*/
  9. void RCC_Configuration(void);
  10. void GPIO_Configuration(void);
  11.  
  12.  
  13. void GPIO_Configuration(void)
  14. {
  15.   GPIO_InitTypeDef GPIO_InitStructure;
  16.   GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);  
  17.  
  18.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  19.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  20.   GPIO_Init(GPIOD, &GPIO_InitStructure);
  21.  
  22.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  23.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  24.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  25.   GPIO_Init(GPIOD, &GPIO_InitStructure);
  26. }
  27.  
  28. void RCC_Configuration(void)
  29. {    
  30.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);
  31.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  32. }
  33.  
  34. void USART_Configuration()
  35. {
  36.   USART_InitStructure.USART_BaudRate = 9600;
  37.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  38.   USART_InitStructure.USART_StopBits = USART_StopBits_1;
  39.   USART_InitStructure.USART_Parity = USART_Parity_No;
  40.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  41.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  42.   USART_Init(USART2, &USART_InitStructure);
  43.   USART_Cmd(USART2, ENABLE);
  44. }
  45.  
  46.  
  47. int main(void)
  48. {
  49.     RCC_Configuration();
  50.     GPIO_Configuration();
  51.     USART_Configuration();
  52.    
  53.     while (1)
  54.   {
  55.         /* Loop until the USARTz Receive Data Register is not empty */
  56.     while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET)
  57.     {
  58.     }
  59.         rec = USART_ReceiveData(USART2);
  60.        
  61.     /* Loop until USARTy DR register is empty */
  62.     while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
  63.     {
  64.     }
  65.         USART_SendData(USART2, rec);
  66.  
  67.     }
  68.        
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement