Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.94 KB | None | 0 0
  1.  
  2. #include "stm32f4xx.h"
  3. #include "stm32f4_discovery.h"
  4. int acounter = 0;
  5. int bcounter = 0;
  6. int ccounter = 0;
  7. int a = 0;
  8. int b = 0;
  9. int c = 0;
  10. void TIM5_IRQHandler(void) {
  11.     if(TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET) {
  12.         if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1) == 1 && a== 0) {
  13.             a = 1;
  14.                 }
  15.         if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1) == 1 && a == 1)
  16.         {
  17.             a = 0;
  18.         }
  19.         if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_2) == 1 && b == 0) {
  20.             b = 1;
  21.                 }
  22.         if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_2) == 1 && b == 1)
  23.         {
  24.             b = 0;
  25.         }
  26.         if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_3) == 1 && c== 0) {
  27.             c = 1;
  28.                 }
  29.         if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_3) == 1 && c == 1)
  30.         {
  31.             c = 0;
  32.         }
  33.  
  34.                 if(a==1 || b==1 || c==1)
  35.                 {
  36.                     TIM_Cmd(TIM3, ENABLE);
  37.                 }
  38.                 else{
  39.                     TIM_Cmd(TIM3, DISABLE);
  40.                 }
  41.         TIM_ClearITPendingBit(TIM5, TIM_IT_Update);
  42.     }
  43. }
  44.  
  45. void TIM3_IRQHandler(void) {
  46.     if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) {
  47.         if(a == 1)
  48.         {
  49.         GPIO_ToggleBits(GPIOC, GPIO_Pin_6);
  50.         for(int i = 0;i<10000000;i++);
  51.         }
  52.         if(b == 1)
  53.                 {
  54.                 GPIO_ToggleBits(GPIOC, GPIO_Pin_8);
  55.                 for(int i = 0;i<10000000;i++);
  56.                 }
  57.         if(c == 1)
  58.                 {
  59.                 GPIO_ToggleBits(GPIOC, GPIO_Pin_9);
  60.                 for(int i = 0;i<10000000;i++);
  61.                 }
  62.  
  63.         TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
  64.     }
  65. }
  66.  
  67. int main(void) {
  68.  
  69.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); // RGB
  70.     GPIO_InitTypeDef GPIO_InitDef;
  71.     GPIO_InitDef.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_8 | GPIO_Pin_9; // 6-R 8-G 9-B
  72.     GPIO_InitDef.GPIO_Mode = GPIO_Mode_OUT;
  73.     GPIO_InitDef.GPIO_OType = GPIO_OType_PP;
  74.     GPIO_InitDef.GPIO_Speed = GPIO_Speed_100MHz;
  75.     GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_NOPULL;
  76.     GPIO_Init(GPIOC, &GPIO_InitDef);
  77.  
  78.  
  79.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); // buttons
  80.     GPIO_InitTypeDef GPIO_BUTTONS;
  81.     GPIO_BUTTONS.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
  82.     GPIO_BUTTONS.GPIO_Mode = GPIO_Mode_IN;
  83.     GPIO_BUTTONS.GPIO_OType = GPIO_OType_PP;
  84.     GPIO_BUTTONS.GPIO_Speed = GPIO_Speed_100MHz;
  85.     GPIO_BUTTONS.GPIO_PuPd = GPIO_PuPd_DOWN;
  86.     GPIO_Init(GPIOA, &GPIO_BUTTONS);
  87.  
  88.     GPIO_ResetBits(GPIOD, GPIO_Pin_6 | GPIO_Pin_8 | GPIO_Pin_9);
  89.  
  90.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); // timer 3
  91.  
  92.     TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  93.         TIM_TimeBaseStructure.TIM_Period = 9999;
  94.         TIM_TimeBaseStructure.TIM_Prescaler = 2099;
  95.         TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  96.         TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  97.         TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
  98.         TIM_Cmd(TIM3, ENABLE);
  99.  
  100.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); // timer 5
  101.         TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure5;
  102.                 TIM_TimeBaseStructure5.TIM_Period = 99;
  103.                 TIM_TimeBaseStructure5.TIM_Prescaler = 84;
  104.                 TIM_TimeBaseStructure5.TIM_ClockDivision = TIM_CKD_DIV1;
  105.                 TIM_TimeBaseStructure5.TIM_CounterMode = TIM_CounterMode_Up;
  106.                 TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure5);
  107.                 TIM_Cmd(TIM5, ENABLE);
  108. //przerwania tim3
  109.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  110.         NVIC_InitTypeDef NVIC_InitStructure3;
  111.         NVIC_InitStructure3.NVIC_IRQChannel = TIM3_IRQn;
  112.         NVIC_InitStructure3.NVIC_IRQChannelPreemptionPriority = 0x00;
  113.         NVIC_InitStructure3.NVIC_IRQChannelSubPriority = 0x00;
  114.         NVIC_InitStructure3.NVIC_IRQChannelCmd = ENABLE;
  115.             NVIC_Init(&NVIC_InitStructure3);
  116.             TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
  117.             TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
  118.             //przerwania tim5
  119.                     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  120.                     NVIC_InitTypeDef NVIC_InitStructure;
  121.                         NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
  122.                         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
  123.                         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
  124.                         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  125.                         NVIC_Init(&NVIC_InitStructure);
  126.                         TIM_ClearITPendingBit(TIM5, TIM_IT_Update);
  127.                         TIM_ITConfig(TIM5, TIM_IT_Update, ENABLE);
  128.  
  129.     for(;;) {
  130.  
  131.     }
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement