Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "main.h"
  3. #include "stm32f30x_usart.h"
  4. #include "stm32f30x_rcc.h"
  5.  
  6.  
  7. /** @addtogroup STM32F3_Discovery_Peripheral_Examples
  8. * @{
  9. */
  10.  
  11. /** @addtogroup GPIO_IOToggle
  12. * @{
  13. */
  14.  
  15. /* Private typedef -----------------------------------------------------------*/
  16. /* Private define ------------------------------------------------------------*/
  17. #define BSRR_VAL 0xC000
  18. /* Private macro -------------------------------------------------------------*/
  19. /* Private variables ---------------------------------------------------------*/
  20. GPIO_InitTypeDef GPIO_InitStructure;
  21. USART_InitTypeDef USART_InitStructure;
  22. NVIC_InitTypeDef NVIC_InitStructure;
  23. static __IO uint32_t TimingDelay;
  24. volatile uint16_t usart_buffer = 0;
  25.  
  26.  
  27.  
  28. volatile char usartMessage[] = "message";
  29.  
  30. /* Private function prototypes -----------------------------------------------*/
  31. void Delay(__IO uint32_t nTime);
  32. void USART1_IRQHandler(void);
  33.  
  34. /* Private functions ---------------------------------------------------------*/
  35. void USART_print (USART_TypeDef* USARTx, volatile char *buffer)
  36. {
  37. /* transmit till NULL character is encountered */
  38. while(*buffer)
  39. {
  40. USART_SendData(USARTx, *buffer++);
  41. while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);
  42.  
  43. }
  44. }
  45. /**
  46. * @brief Main program.
  47. * @param None
  48. * @retval None
  49. */
  50. int main(void)
  51. {
  52.  
  53. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  54.  
  55.  
  56. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
  57. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  58. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  59. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  60. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  61. GPIO_Init(GPIOA, &GPIO_InitStructure);
  62.  
  63.  
  64. GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7);
  65. GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7);
  66.  
  67.  
  68. /* Configure USART1 pins: --------------------------------------*/
  69. RCC_APB1PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  70.  
  71. USART_DeInit(USART1);
  72. USART_InitStructure.USART_BaudRate = 9600;
  73. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  74. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  75. USART_InitStructure.USART_Parity = USART_Parity_No;
  76. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  77. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  78. USART_Init(USART1,&USART_InitStructure);
  79.  
  80. USART_Cmd(USART1, ENABLE);
  81.  
  82. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // enable the USART1 receive interrupt
  83.  
  84. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; // we want to configure the USART1 interrupts
  85. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // this sets the priority group of the USART1 interrupts
  86. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // this sets the subpriority inside the group
  87. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // the USART2 interrupts are globally enabled
  88. NVIC_Init(&NVIC_InitStructure); // the properties are passed to the NVIC_Init function which takes care of the low level stuff
  89.  
  90. // finally this enables the complete USART1 peripheral
  91. USART_Cmd(USART1, ENABLE);
  92.  
  93.  
  94. if (SysTick_Config(SystemCoreClock / 1000))
  95. {
  96. /* Capture error */
  97. while (1);
  98. }
  99.  
  100. STM_EVAL_LEDInit(LED5);
  101.  
  102.  
  103.  
  104. while (1)
  105. {
  106. int i = USART_ReceiveData(USART1);
  107. if(i == '1'){
  108. USART_print(USART1, "messsage");
  109. }
  110. /*for(i=0; usartMessage[i] != 0; i++){
  111.  
  112. USART_print(USART1, &usartMessage[i]);
  113. }*/
  114. Delay(200);
  115.  
  116. }
  117. }
  118.  
  119.  
  120.  
  121.  
  122. void USART1_IRQHandler(void){
  123. Delay(100);
  124. USART_print(USART1, "message INTERRUPT!");
  125. if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET){
  126. usart_buffer = USART_ReceiveData(USART1);
  127.  
  128. }
  129. }
  130.  
  131. void Delay(__IO uint32_t nTime)
  132. {
  133. TimingDelay = nTime;
  134.  
  135. while(TimingDelay != 0);
  136. }
  137.  
  138. /**
  139. * @brief Decrements the TimingDelay variable.
  140. * @param None
  141. * @retval None
  142. */
  143. void TimingDelay_Decrement(void)
  144. {
  145. if (TimingDelay != 0x00)
  146. {
  147. TimingDelay--;
  148. }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement