Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.78 KB | None | 0 0
  1. /**
  2.  ******************************************************************************
  3.  * @file    main.c
  4.  * @author  Ac6
  5.  * @version V1.0
  6.  * @date    01-December-2013
  7.  * @brief   Default main function.
  8.  ******************************************************************************
  9.  */
  10.  
  11. #include "stm32f4xx.h"
  12. #include "stm32f4_discovery.h"
  13.  
  14. int licznik1 = 0;
  15. int licznik2 = 0;
  16.  
  17. void TIM3_IRQHandler(void) {
  18.     if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) {
  19.  
  20.         if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11)) {
  21.             licznik1++;
  22.         }
  23.         else {
  24.             licznik1--;
  25.             if(licznik1 > 0) {
  26.                 GPIO_SetBits(GPIOD, GPIO_Pin_12);
  27.                 for(int i = 0; i < 1000; i++);
  28.             }
  29.             if(licznik1 == 0) {
  30.                 GPIO_ResetBits(GPIOD, GPIO_Pin_12);
  31.                 for(int i = 0; i < 1000; i++);
  32.             }
  33.         }
  34.  
  35. //      if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12)) {
  36. //          licznik2++;
  37. //      }
  38. //      else {
  39. //          licznik2--;
  40. //          if(licznik2 > 0) {
  41. //              GPIO_SetBits(GPIOD, GPIO_Pin_13);
  42. //              for(int i = 0; i < 1000; i++);
  43. //          }
  44. //          if(licznik2 == 0) {
  45. //              GPIO_ResetBits(GPIOD, GPIO_Pin_13);
  46. //              for(int i = 0; i < 1000; i++);
  47. //          }
  48. //      }
  49.  
  50.         TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
  51.     }
  52. }
  53.  
  54. int main(void) {
  55.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); // LEDs
  56.     GPIO_InitTypeDef GPIO_InitDef;
  57.     GPIO_InitDef.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13;
  58.     GPIO_InitDef.GPIO_Mode = GPIO_Mode_OUT;
  59.     GPIO_InitDef.GPIO_OType = GPIO_OType_PP;
  60.     GPIO_InitDef.GPIO_Speed = GPIO_Speed_100MHz;
  61.     GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_NOPULL;
  62.     GPIO_Init(GPIOD, &GPIO_InitDef);
  63.  
  64.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); // buttons
  65.     GPIO_InitTypeDef GPIO_BUTTONS;
  66.     GPIO_BUTTONS.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12;
  67.     GPIO_BUTTONS.GPIO_Mode = GPIO_Mode_IN;
  68.     GPIO_BUTTONS.GPIO_OType = GPIO_OType_PP;
  69.     GPIO_BUTTONS.GPIO_Speed = GPIO_Speed_100MHz;
  70.     GPIO_BUTTONS.GPIO_PuPd = GPIO_PuPd_DOWN;
  71.     GPIO_Init(GPIOB, &GPIO_BUTTONS);
  72.  
  73.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); // timer 3
  74.     TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  75.     TIM_TimeBaseStructure.TIM_Period = 999;
  76.     TIM_TimeBaseStructure.TIM_Prescaler = 8399;
  77.     TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  78.     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  79.     TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
  80.     TIM_Cmd(TIM3, ENABLE);
  81.  
  82.     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  83.     NVIC_InitTypeDef NVIC_InitStructure;
  84.     NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  85.     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
  86.     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
  87.     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  88.     NVIC_Init(&NVIC_InitStructure);
  89.     TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
  90.     TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
  91.  
  92.     GPIO_ResetBits(GPIOD, GPIO_Pin_12 | GPIO_Pin_13);
  93.  
  94.     for(;;) {
  95.  
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement