Guest User

Untitled

a guest
Jan 12th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.18 KB | None | 0 0
  1. /**
  2. ******************************************************************************
  3. * File Name : main.c
  4. * Description : Main program body
  5. ******************************************************************************
  6. *
  7. * COPYRIGHT(c) 2018 STMicroelectronics
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. * 1. Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright notice,
  14. * this list of conditions and the following disclaimer in the documentation
  15. * and/or other materials provided with the distribution.
  16. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. ******************************************************************************
  32. */
  33. /* Includes ------------------------------------------------------------------*/
  34. #include "stm32f3xx_hal.h"
  35. #include "spi.h"
  36. #include "gpio.h"
  37.  
  38. /* USER CODE BEGIN Includes */
  39. #include "MCP2515.h"
  40. /* USER CODE END Includes */
  41.  
  42. /* Private variables ---------------------------------------------------------*/
  43.  
  44. /* USER CODE BEGIN PV */
  45. /* Private variables ---------------------------------------------------------*/
  46.  
  47. /* USER CODE END PV */
  48.  
  49. /* Private function prototypes -----------------------------------------------*/
  50. void SystemClock_Config(void);
  51.  
  52. /* USER CODE BEGIN PFP */
  53. /* Private function prototypes -----------------------------------------------*/
  54.  
  55. /* USER CODE END PFP */
  56.  
  57. /* USER CODE BEGIN 0 */
  58.  
  59. /* USER CODE END 0 */
  60.  
  61. int main(void)
  62. {
  63.  
  64. /* USER CODE BEGIN 1 */
  65. tMCP2515Device mcp;
  66. tMCPHardwareConfig hw;
  67. CAN_Frame can_msg;
  68. CAN_Frame tx;
  69. char nowy = 0;
  70. char stary = 0;
  71. /* USER CODE END 1 */
  72.  
  73. /* MCU Configuration----------------------------------------------------------*/
  74.  
  75. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  76. HAL_Init();
  77.  
  78. /* Configure the system clock */
  79. SystemClock_Config();
  80.  
  81. /* Initialize all configured peripherals */
  82. MX_GPIO_Init();
  83. MX_SPI3_Init();
  84.  
  85. /* USER CODE BEGIN 2 */
  86. hw.GPIO_CS = GPIOD;
  87. hw.GPIO_Pin_CS = GPIO_PIN_0;
  88. hw.hspi = &hspi3;
  89. MCP2515_Init(&mcp,&hw);
  90. MCP2515_Start(&mcp,125000,eNORMAL);
  91. /* USER CODE END 2 */
  92.  
  93. /* Infinite loop */
  94. /* USER CODE BEGIN WHILE */
  95. while (1)
  96. {
  97. HAL_Delay(10);
  98. if(MCP2515_NewMessage(&mcp))
  99. {
  100. MCP2515_ReadMessage(&mcp,&can_msg);
  101. }
  102. if(can_msg.valid && (can_msg.id==17) && (can_msg.extended==0))
  103. {
  104. GPIOE->ODR&=0x00FF;
  105. GPIOE->ODR|=((unsigned short)can_msg.data[0])<<8;
  106. }
  107.  
  108. nowy=HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0);
  109. if(nowy != stary){
  110. if(nowy){
  111. //przycisk wcisniety
  112. tx.data[0]=1;// pierwszy bajt danych
  113. tx.extended=0;
  114. tx.id= 16; //identyfikator
  115. tx.length=1; //liczba bajtów danych przesylanych w ramce
  116. //wysylam ramke
  117. MCP2515_WriteMessage(&mcp,&tx);
  118. }else{
  119. //przycisk puszczony
  120. tx.data[0]=0;// pierwszy bajt danych
  121. tx.extended=0;
  122. tx.id= 16; //identyfikator
  123. tx.length=1; //liczba bajtów danych przesylanych w ramce
  124. //wysylam ramke
  125. MCP2515_WriteMessage(&mcp,&tx);
  126.  
  127.  
  128. }
  129. }
  130. stary = nowy;
  131. }
  132.  
  133. /* USER CODE END WHILE */
  134.  
  135. /* USER CODE BEGIN 3 */
  136.  
  137. }
  138. /* USER CODE END 3 */
  139.  
  140. /** System Clock Configuration
  141. */
  142. void SystemClock_Config(void)
  143. {
  144.  
  145. RCC_OscInitTypeDef RCC_OscInitStruct;
  146. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  147.  
  148. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  149. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  150. RCC_OscInitStruct.HSICalibrationValue = 16;
  151. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  152. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  153. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
  154. HAL_RCC_OscConfig(&RCC_OscInitStruct);
  155.  
  156. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1;
  157. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  158. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  159. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  160. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  161. HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
  162.  
  163. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  164.  
  165. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  166.  
  167. }
  168.  
  169. /* USER CODE BEGIN 4 */
  170.  
  171. /* USER CODE END 4 */
  172.  
  173. #ifdef USE_FULL_ASSERT
  174.  
  175. /**
  176. * @brief Reports the name of the source file and the source line number
  177. * where the assert_param error has occurred.
  178. * @param file: pointer to the source file name
  179. * @param line: assert_param error line source number
  180. * @retval None
  181. */
  182. void assert_failed(uint8_t* file, uint32_t line)
  183. {
  184. /* USER CODE BEGIN 6 */
  185. /* User can add his own implementation to report the file name and line number,
  186. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  187. /* USER CODE END 6 */
  188.  
  189. }
  190.  
  191. #endif
  192.  
  193. /**
  194. * @}
  195. */
  196.  
  197. /**
  198. * @}
  199. */
  200.  
  201. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment