Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.88 KB | None | 0 0
  1. /**
  2. *****************************************************************************
  3. **
  4. ** File : main.c
  5. **
  6. ** Abstract : main function.
  7. **
  8. ** Functions : main
  9. **
  10. ** Environment : Atollic TrueSTUDIO/STM32
  11. ** STMicroelectronics STM32F10x Standard Peripherals Library
  12. **
  13. ** Distribution: The file is distributed �as is,� without any warranty
  14. ** of any kind.
  15. **
  16. ** (c)Copyright Atollic AB.
  17. ** You may use this file as-is or modify it according to the needs of your
  18. ** project. Distribution of this file (unmodified or modified) is not
  19. ** permitted. Atollic AB permit registered Atollic TrueSTUDIO(R) users the
  20. ** rights to distribute the assembled, compiled & linked contents of this
  21. ** file as part of an application binary file, provided that it is built
  22. ** using the Atollic TrueSTUDIO(R) toolchain.
  23. **
  24. **
  25. *****************************************************************************
  26. */
  27.  
  28. /* Includes */
  29. #include <stddef.h>
  30. #include "stm32f10x.h"
  31. #include "stm32f10x_gpio.h"
  32. #include "main.h"
  33. #include "SysTickHandler.h"
  34. #include "abstraction.h"
  35. //#include "STM32vldiscovery.h"
  36. /* Private typedef */
  37. typedef enum{
  38. toggle,
  39. togglevar,
  40. blink,
  41. blinkvar
  42. }ledstate_e;
  43. /* Private define */
  44.  
  45.  
  46. /* Private macro */
  47. /* Private variables */
  48. void waitconst ( int del );
  49. void waitvar ( void );
  50.  
  51. void initled(void);
  52. void initbutton ( void );
  53. /* Private function prototypes */
  54. /* Private functions */
  55. void SwitchMode ( ledstate_e * state ){
  56. static unsigned int pushedButton = 0;
  57. unsigned int pressed = 0;
  58. pressed = GPIO_ReadInputDataBit(B1_BUTTON_PORT, B1_BUTTON_PIN);
  59. if( pressed != pushedButton && pressed>0){
  60. switch(*state){
  61. case togglevar : *state = toggle; break;
  62. case toggle : *state = blink; break;
  63. case blink : *state = blinkvar; break;
  64. case blinkvar: *state = togglevar; break;
  65. default: *state = blink; break;
  66. }
  67. }
  68. pushedButton=pressed;
  69. return;
  70. }
  71.  
  72. /**
  73. **===========================================================================
  74. **
  75. ** Abstract: main program
  76. **
  77. **===========================================================================
  78. */
  79. int main(void){
  80. initled();
  81.  
  82. initSysTickHandler(1);
  83.  
  84. ledstate_e mode=blinkvar;
  85. ledstate_e modeold;
  86.  
  87.  
  88. while (1){
  89. SwitchMode( & mode );
  90. //set initial states for toggle
  91. if(mode != modeold){
  92. if( mode == blink || mode == blinkvar){
  93. GPIO_SetBits(G_LED_PORT,G_LED_PIN);
  94. GPIO_SetBits(B_LED_PORT,B_LED_PIN);
  95. }
  96. else{
  97. GPIO_ResetBits(B_LED_PORT,B_LED_PIN);
  98. GPIO_ResetBits(G_LED_PORT,G_LED_PIN);
  99. }
  100. }
  101. modeold = mode;
  102. //get wait states...
  103. switch(mode){
  104. case togglevar : waitvar(); break;
  105. case toggle : waitconst(1000); break;
  106. case blink : waitconst(1000); break;
  107. case blinkvar: waitvar(); break;
  108. default: mode = blink; break;
  109. }
  110. //toggle LEDs, because the initial states configures the mode of this leds,
  111. //the ports only have to be toggled
  112. G_LED_PORT->ODR ^= G_LED_PIN;
  113. B_LED_PORT->ODR ^= B_LED_PIN;
  114. }
  115.  
  116. return -1;
  117. }
  118.  
  119. void initbutton ( void ){
  120. GPIO_InitTypeDef button_struct;
  121. EXTI_InitTypeDef exti_struct;
  122. NVIC_InitTypeDef nvic_struct;
  123.  
  124. RCC_APB2PeriphClockCmd(B1_BUTTON_RCC,ENABLE); //enable clock for the GPIO
  125. //configure Button-Pin
  126. button_struct.GPIO_Mode=GPIO_Mode_IN_FLOATING;
  127. button_struct.GPIO_Pin=B1_BUTTON_PIN;
  128. button_struct.GPIO_Speed=GPIO_Speed_2MHz;
  129. //Save Settings
  130. GPIO_Init(B1_BUTTON_PORT,&button_struct);//enable GPIO_Port
  131. //Configure Specialfunction for pin!
  132. RCC_APB2PeriphClockCmd(B1_AFIO_RCC,ENABLE); // enable Specialfunction for Interrupt-Handling
  133. //Pin is located at PA0 -> therfore EXTI0 is used for the Interrupt.
  134. GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);
  135.  
  136. exti_struct.EXTI_Line=EXTI_Line0;
  137. exti_struct.EXTI_Mode=EXTI_Mode_Interrupt;
  138. exti_struct.EXTI_Trigger=EXTI_Trigger_Rising;
  139. exti_struct.EXTI_LineCmd=ENABLE;
  140.  
  141. nvic_struct.NVIC_IRQChannel=EXTI0_IRQn;
  142. nvic_struct.NVIC_IRQChannelCmd=ENABLE;
  143. nvic_struct.NVIC_IRQChannelPreemptionPriority=0x0F;
  144. nvic_struct.NVIC_IRQChannelSubPriority=0x0F;
  145. EXTI_Init(&exti_struct);
  146. NVIC_Init(&nvic_struct);
  147. return;
  148.  
  149. }
  150.  
  151. void initled ( void ){
  152. GPIO_InitTypeDef led_struct;
  153.  
  154. RCC_APB2PeriphClockCmd(G_LED_RCC,ENABLE);
  155. led_struct.GPIO_Mode=GPIO_Mode_Out_PP;
  156. led_struct.GPIO_Pin=G_LED_PIN;
  157. led_struct.GPIO_Speed=GPIO_Speed_2MHz;
  158. GPIO_Init(G_LED_PORT, &led_struct);
  159.  
  160. RCC_APB2PeriphClockCmd(B_LED_RCC,ENABLE);
  161. led_struct.GPIO_Mode=GPIO_Mode_Out_PP;
  162. led_struct.GPIO_Pin=B_LED_PIN;
  163. led_struct.GPIO_Speed=GPIO_Speed_2MHz;
  164. GPIO_Init(B_LED_PORT, &led_struct);
  165.  
  166. return;
  167.  
  168. }
  169.  
  170. void waitconst ( int del){
  171. delay(del);
  172. return;
  173. }
  174.  
  175. void waitvar ( void ){
  176. static int wait = 100;
  177. static int mode=0;
  178. delay(wait);
  179. if(mode==0){
  180. wait-=50;
  181. if(wait<100)
  182. mode=1;
  183. }
  184. else{
  185. wait+=50;
  186. if(wait>1500)
  187. mode=0;
  188. }
  189. return;
  190. }
  191. #ifdef USE_FULL_ASSERT
  192.  
  193. /**
  194. * @brief Reports the name of the source file and the source line number
  195. * where the assert_param error has occurred.
  196. * @param file: pointer to the source file name
  197. * @param line: assert_param error line source number
  198. * @retval None
  199. */
  200. void assert_failed(uint8_t* file, uint32_t line)
  201. {
  202. /* User can add his own implementation to report the file name and line number,
  203. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  204.  
  205. /* Infinite loop */
  206. while (1)
  207. {
  208. }
  209. }
  210. #endif
  211.  
  212. /*
  213. * Minimal __assert_func used by the assert() macro
  214. * */
  215. void __assert_func(const char *file, int line, const char *func, const char *failedexpr){
  216. while(1)
  217. {}
  218. }
  219.  
  220. /*
  221. * Minimal __assert() uses __assert__func()
  222. * */
  223. void __assert(const char *file, int line, const char *failedexpr){
  224. __assert_func (file, line, NULL, failedexpr);
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement