Advertisement
Guest User

Main.c

a guest
Jun 10th, 2015
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. #include "stm32f4xx.h"
  2.  
  3.  
  4. /* Set variables used */
  5. GPIO_InitTypeDef GPIO_InitStruct;
  6. EXTI_InitTypeDef EXTI_InitStruct;
  7. NVIC_InitTypeDef NVIC_InitStruct;
  8.  
  9. /* Configure pin to be interrupts */
  10. void Configure_PA0(void)
  11. {
  12.  
  13.  
  14. /* Enable clock for GPIOA */
  15. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  16. /* Enable clock for SYSCFG */
  17. RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
  18.  
  19. /* Set pin as input */
  20. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
  21. GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  22. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
  23. GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
  24. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
  25. GPIO_Init(GPIOA, &GPIO_InitStruct);
  26.  
  27. /* Tell system that you will use PA0 for EXTI_Line0 */
  28. SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);
  29.  
  30. /* PA0 is connected to EXTI_Line0 */
  31. EXTI_InitStruct.EXTI_Line = EXTI_Line0;
  32. /* Enable interrupt */
  33. EXTI_InitStruct.EXTI_LineCmd = ENABLE;
  34. /* Interrupt mode */
  35. EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
  36. /* Triggers on rising and falling edge */
  37. EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
  38. /* Add to EXTI */
  39. EXTI_Init(&EXTI_InitStruct);
  40.  
  41. /* Add IRQ vector to NVIC */
  42. /* PA0 is connected to EXTI_Line0, which has EXTI0_IRQn vector */
  43. NVIC_InitStruct.NVIC_IRQChannel = EXTI0_IRQn;
  44. /* Set priority */
  45. NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x00;
  46. /* Set sub priority */
  47. NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x00;
  48. /* Enable interrupt */
  49. NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  50. /* Add to NVIC */
  51. NVIC_Init(&NVIC_InitStruct);
  52. }
  53.  
  54.  
  55. void GPIOD_Initialize(void)
  56. {
  57. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE);
  58.  
  59. GPIO_InitStruct.GPIO_Pin=GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
  60. GPIO_InitStruct.GPIO_Mode=GPIO_Mode_OUT;
  61. GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
  62. GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
  63. GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;
  64.  
  65. GPIO_Init(GPIOD,&GPIO_InitStruct);
  66. }
  67.  
  68. void EXTI0_IRQHandler(void)
  69. {
  70. /* Make sure that interrupt flag is set */
  71. if (EXTI_GetITStatus(EXTI_Line0) != RESET)
  72. {
  73. GPIO_ResetBits(GPIOD,GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
  74. /* Clear interrupt flag */
  75. EXTI_ClearITPendingBit(EXTI_Line0);
  76. }
  77. }
  78.  
  79.  
  80. void main()
  81. {
  82. SystemInit();
  83. Configure_PA0();
  84. GPIOD_Initialize();
  85.  
  86. GPIO_SetBits(GPIOD,GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
  87. while(1);
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement