Advertisement
Guest User

px

a guest
Jan 17th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. #include "stm32f4_discovery.h"
  2.  
  3. int8_t podatki[16];
  4.  
  5. void delay();
  6.  
  7. void delay() {
  8. uint32_t d = 500;
  9. while(d--);
  10. }
  11.  
  12.  
  13.  
  14. void initUSART1()
  15. {
  16. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // USART naprava
  17. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  18. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
  19. USART_InitTypeDef USART_InitStruct;
  20. USART_InitStruct.USART_BaudRate = 115200;
  21. USART_InitStruct.USART_WordLength = USART_WordLength_8b;
  22. USART_InitStruct.USART_StopBits = USART_StopBits_1;
  23. USART_InitStruct.USART_Parity = USART_Parity_No;
  24. USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  25. USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
  26. USART_Init(USART1, &USART_InitStruct);
  27. USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);
  28. USART_Cmd(USART1, ENABLE);
  29. GPIO_InitTypeDef GPIO_InitStructure;
  30. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  31. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  32. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  33. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  34. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  35. GPIO_Init(GPIOB, &GPIO_InitStructure);
  36. //nastavitve AF za pine
  37. GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1);
  38. GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1);
  39. }
  40.  
  41. void initDMA2()
  42. {
  43. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
  44.  
  45. DMA_InitTypeDef DMA_InitStructure;
  46. DMA_InitStructure.DMA_Channel = DMA_Channel_4;
  47. DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
  48. DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t) podatki;
  49. DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) &(USART1->DR);
  50. DMA_InitStructure.DMA_BufferSize = 16;
  51. DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  52. DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  53. DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  54. DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  55. DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  56. DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
  57. DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
  58. DMA_Init(DMA2_Stream2, &DMA_InitStructure);
  59.  
  60. DMA_Cmd(DMA2_Stream2, ENABLE);
  61. }
  62.  
  63.  
  64. int main(void)
  65. {
  66. //lucke
  67. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  68. GPIO_InitTypeDef leds;
  69. leds.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  70. leds.GPIO_Mode = GPIO_Mode_OUT;
  71. leds.GPIO_OType = GPIO_OType_PP;
  72. leds.GPIO_PuPd = GPIO_PuPd_NOPULL;
  73. leds.GPIO_Speed = GPIO_Speed_2MHz;
  74. GPIO_Init(GPIOD, &leds);
  75.  
  76. initUSART1();
  77. initDMA2();
  78.  
  79. volatile int8_t x, y;
  80.  
  81. while (1)
  82. {
  83. x = 0;
  84. for(int i = 0; i<16;i+2){
  85. x += podatki[i];
  86. }
  87. x = x/8;
  88.  
  89. y = 0;
  90. for(int i = 1; i<16;i+2){
  91. y += podatki[i];
  92. }
  93. y = y/8;
  94.  
  95. // x-axis leds
  96. if (x < -6) {
  97. // Vklopi ledico
  98. GPIO_SetBits(GPIOD, GPIO_Pin_12);
  99. delay();
  100. GPIO_ResetBits(GPIOD, GPIO_Pin_12);
  101. }
  102.  
  103. if (x > 6) {
  104. GPIO_SetBits(GPIOD, GPIO_Pin_14);
  105. delay();
  106. GPIO_ResetBits(GPIOD, GPIO_Pin_14);
  107. }
  108.  
  109. if (y < -6) {
  110. GPIO_SetBits(GPIOD, GPIO_Pin_13);
  111. delay();
  112. GPIO_ResetBits(GPIOD, GPIO_Pin_13);
  113. }
  114.  
  115. if (y > 6) {
  116. GPIO_SetBits(GPIOD, GPIO_Pin_15);
  117. delay();
  118. GPIO_ResetBits(GPIOD, GPIO_Pin_15);
  119. }
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement