Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | None | 0 0
  1. #include "stm32f4xx_conf.h"
  2. #include "stm32f4xx_gpio.h"
  3. #include "stm32f4xx_rcc.h"
  4. #include "stm32f4xx_tim.h"
  5. #include "stm32f4xx_usart.h"
  6. #include "misc.h"
  7.  
  8. uint16_t lewa, znak, prawa, wynik;
  9. unsigned int n = 0;
  10.  
  11. int main(void)
  12. {
  13. SystemInit();
  14.  
  15. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  16.  
  17. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
  18. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  19. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  20. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
  21.  
  22. GPIO_InitTypeDef GPIO_InitStructure;
  23. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
  24. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  25. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  26. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  27. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  28. GPIO_Init(GPIOC, &GPIO_InitStructure);
  29. GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3);
  30. GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3);
  31.  
  32. //GPIO_InitTypeDef GPIO_InitStructure;
  33. GPIO_InitStructure.GPIO_Pin =
  34. GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;
  35. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  36. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  37. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  38. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  39. GPIO_Init(GPIOD, &GPIO_InitStructure);
  40.  
  41. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  42. TIM_TimeBaseStructure.TIM_Period = 9999;
  43. TIM_TimeBaseStructure.TIM_Prescaler = 8399;
  44. TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  45. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  46. TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
  47.  
  48. USART_InitTypeDef USART_InitStructure;
  49. USART_InitStructure.USART_BaudRate = 115200;
  50. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  51. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  52. USART_InitStructure.USART_Parity = USART_Parity_No;
  53. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  54. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  55. USART_Init(USART3, &USART_InitStructure);
  56.  
  57. NVIC_InitTypeDef NVIC_InitStructure;
  58. NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  59. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
  60. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
  61. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  62. NVIC_Init(&NVIC_InitStructure);
  63.  
  64. NVIC_InitTypeDef NVIC_InitStructure2;
  65. USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
  66. NVIC_InitStructure2.NVIC_IRQChannel = USART3_IRQn;
  67. NVIC_InitStructure2.NVIC_IRQChannelPreemptionPriority = 0x01;
  68. NVIC_InitStructure2.NVIC_IRQChannelSubPriority = 0x01;
  69. NVIC_InitStructure2.NVIC_IRQChannelCmd = ENABLE;
  70. NVIC_Init(&NVIC_InitStructure2);
  71. NVIC_EnableIRQ(USART3_IRQn);
  72.  
  73. TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
  74. TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
  75.  
  76. TIM_Cmd(TIM3, ENABLE);
  77. USART_Cmd(USART3, ENABLE);
  78.  
  79. void TIM3_IRQHandler(void);
  80. void USART3_IRQHandler(void);
  81.  
  82. while(1){}
  83. }
  84.  
  85. void TIM3_IRQHandler(void)
  86. {
  87. if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
  88. {
  89. //zad1
  90. // USART_SendData(USART3, 'A');
  91.  
  92. TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
  93. }
  94. }
  95.  
  96. void USART3_IRQHandler(void)
  97. {
  98. if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
  99. {
  100. //zad2
  101. //USART3->DR -= 32;
  102.  
  103. //zad3
  104. //if(USART3->DR == 'a')
  105. // GPIO_ToggleBits(GPIOD, GPIO_Pin_12);
  106. //if(USART3->DR == 's')
  107. // GPIO_ToggleBits(GPIOD, GPIO_Pin_13);
  108. //if(USART3->DR == 'd')
  109. // GPIO_ToggleBits(GPIOD, GPIO_Pin_14);
  110. //if(USART3->DR == 'f')
  111. // GPIO_ToggleBits(GPIOD, GPIO_Pin_15);
  112.  
  113. switch (n%3)
  114. {
  115. case 0:
  116. lewa = USART3->DR;
  117. n++;
  118. break;
  119. case 1:
  120. znak = USART3->DR;
  121. n++;
  122. break;
  123. case 2:
  124. prawa = USART3->DR;
  125. USART_SendData(USART3, '=');
  126. while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
  127. if (znak == '+') wynik = (lewa-48)+(prawa-48);
  128. if (znak == '-') wynik = (lewa-48)-(prawa-48);
  129. if (znak == '*') wynik = (lewa-48)*(prawa-48);
  130. if (wynik/10 > 0)
  131. {
  132. USART_SendData(USART3, (wynik/10)+48);
  133. while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
  134. USART_SendData(USART3, (wynik%10)+48);
  135. while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
  136. }else{
  137. USART_SendData(USART3, wynik+48);
  138. while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
  139. }
  140. USART_SendData(USART3, 10);
  141. // while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
  142. n++;
  143. break;
  144. }
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement