Advertisement
Guest User

Untitled

a guest
Dec 15th, 2012
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. #include "stm32f10x.h"
  2.  
  3. #define RXUSART USART1
  4. #define RXUSART_GPIO GPIOA
  5. #define RXUSART_CLK RCC_APB2Periph_USART1
  6. #define RXUSART_GPIO_CLK RCC_APB2Periph_GPIOA
  7. #define RXUSART_RxPin GPIO_Pin_10
  8. #define RXUSART_IRQn USART1_IRQn
  9.  
  10. #define RXUSART_Invert_Pin GPIO_Pin_11
  11. #define RXUSART_Invert_GPIO GPIOC
  12. #define RXUSART_Invert_GPIO_CLK RCC_APB2Periph_GPIOC
  13.  
  14.  
  15. int main(void)
  16. {
  17.  
  18. /* Enable GPIO clock */
  19. RCC_APB2PeriphClockCmd(RXUSART_GPIO_CLK | RXUSART_Invert_GPIO_CLK , ENABLE);
  20.  
  21. /* Enable USART Clock */
  22. RCC_APB2PeriphClockCmd(RXUSART_CLK, ENABLE);
  23.  
  24.  
  25. GPIO_InitTypeDef GPIO_InitStructure;
  26.  
  27. /* Configure USART Rx as input floating */
  28. GPIO_InitStructure.GPIO_Pin = RXUSART_RxPin;
  29. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  30. GPIO_Init(RXUSART_GPIO, &GPIO_InitStructure);
  31.  
  32. // configures the Inversion input
  33. GPIO_InitStructure.GPIO_Pin = RXUSART_Invert_Pin;
  34. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  35. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  36. GPIO_Init(RXUSART_Invert_GPIO, &GPIO_InitStructure);
  37.  
  38. // Sets or reset the inversion Pin
  39. //GPIO_SetBits(RXUSART_Invert_GPIO, RXUSART_Invert_Pin);
  40. GPIO_ResetBits(RXUSART_Invert_GPIO, RXUSART_Invert_Pin);
  41.  
  42.  
  43. NVIC_InitTypeDef NVIC_InitStructure;
  44.  
  45. /* Enable the RXUSART Interrupt */
  46. NVIC_InitStructure.NVIC_IRQChannel = RXUSART_IRQn;
  47. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  48. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  49. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  50.  
  51. NVIC_Init(&NVIC_InitStructure);
  52.  
  53.  
  54. USART_InitTypeDef USART_InitStructure;
  55.  
  56. USART_InitStructure.USART_BaudRate = 115200;
  57. //USART_InitStructure.USART_WordLength = USART_WordLength_9b;
  58. //USART_InitStructure.USART_StopBits = USART_StopBits_2;
  59. //USART_InitStructure.USART_Parity = USART_Parity_Even;
  60. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  61. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  62. USART_InitStructure.USART_Parity = USART_Parity_No;
  63.  
  64. // Configure the other fields
  65. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  66. USART_InitStructure.USART_Mode = USART_Mode_Rx;
  67.  
  68. USART_Init(RXUSART, &USART_InitStructure);
  69.  
  70. USART_ITConfig(RXUSART, USART_IT_RXNE, ENABLE);
  71.  
  72. USART_Cmd(RXUSART, ENABLE);
  73.  
  74. while(1);
  75. }
  76.  
  77.  
  78. void USART1_IRQHandler(void)
  79. {
  80.  
  81. if(USART_GetITStatus(RXUSART, USART_IT_RXNE) != RESET)
  82. {
  83. USART_ReceiveData(RXUSART);
  84. }
  85.  
  86. if (USART_GetITStatus(RXUSART, USART_IT_PE))
  87. {
  88. while (1);
  89. }
  90.  
  91. if (USART_GetITStatus(RXUSART, USART_IT_TC))
  92. {
  93. while (1);
  94. }
  95.  
  96. if (USART_GetITStatus(RXUSART, USART_IT_IDLE))
  97. {
  98. while (1);
  99. }
  100.  
  101. if (USART_GetITStatus(RXUSART, USART_IT_LBD))
  102. {
  103. while (1);
  104. }
  105.  
  106. if (USART_GetITStatus(RXUSART, USART_IT_CTS))
  107. {
  108. while (1);
  109. }
  110.  
  111.  
  112. if (USART_GetITStatus(RXUSART, USART_IT_ERR))
  113. {
  114. while (1);
  115. }
  116.  
  117. if (USART_GetITStatus(RXUSART, USART_IT_ORE))
  118. {
  119. USART_ClearITPendingBit(USART2, USART_IT_ORE);
  120. }
  121.  
  122. if (USART_GetITStatus(RXUSART, USART_IT_FE))
  123. {
  124. while (1);
  125. }
  126. if (USART_GetITStatus(RXUSART, USART_IT_NE))
  127. {
  128. while (1);
  129. }
  130.  
  131.  
  132. if (USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
  133. {
  134. USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
  135. }
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement