timurkanaz

STM32 Задержки

Jan 9th, 2021 (edited)
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 2.93 KB | None | 0 0
  1. #include "stm32f4xx.h"
  2. #include "stm32f4xx_gpio.h"
  3. #include "stm32f4xx_rcc.h"
  4. #include "stm32f4xx_tim.h"
  5. #include "misc.h"
  6.  
  7. int i=0; //Counter
  8. void SetSysClockToHSE(void)
  9. {
  10.     ErrorStatus HSEStartUpStatus;
  11.    
  12.     RCC_DeInit();
  13.  
  14.    
  15.     RCC_HSEConfig( RCC_HSE_ON);
  16.  
  17.    
  18.     HSEStartUpStatus = RCC_WaitForHSEStartUp();
  19.  
  20.     if (HSEStartUpStatus == SUCCESS)
  21.     {
  22.        
  23.         RCC_HCLKConfig( RCC_SYSCLK_Div1);
  24.  
  25.        
  26.         RCC_PCLK2Config( RCC_HCLK_Div1);
  27.  
  28.        
  29.         RCC_PCLK1Config(RCC_HCLK_Div1);
  30.  
  31.        
  32.         RCC_SYSCLKConfig( RCC_SYSCLKSource_HSE);
  33.  
  34.        
  35.         while (RCC_GetSYSCLKSource() != 0x04)
  36.         {
  37.         }
  38.     }
  39.     else
  40.    
  41.         while (1)
  42.         {
  43.         }
  44.     }
  45. }
  46.  
  47. void TIM4_IRQHandler(void)
  48. {
  49.     TIM6->SR &= ~TIM_SR_UIF; //Сброс прерывания
  50.  
  51.         if(i==0) GPIO_SetBits(GPIOD, GPIO_Pin_12); // «1» на PD12
  52.         else if(i==1) GPIO_SetBits(GPIOD, GPIO_Pin_13); // «1» на PD13
  53.         else if(i==2) GPIO_SetBits(GPIOD, GPIO_Pin_14); // «1» на PD14
  54.         else if(i==3) GPIO_SetBits(GPIOD, GPIO_Pin_15); // «1» на PD15
  55.         else if(i==4)GPIO_ResetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15); //Сброс всего на «0»
  56.         i++; //Счетчик +1
  57.         if(i==5) i=0; //Если i=5, счетчик=0
  58. }
  59.  
  60. int main(void)
  61. {
  62.     SetSysClockToHSE();
  63.  
  64.     /* Initialize LED which connected to PC13 */
  65.     GPIO_InitTypeDef  GPIO_InitStructure;
  66.     // Enable PORTC Clock
  67.     RCC_APB2PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
  68.     /* Configure the GPIO_LED pin */
  69.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12| GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15; //Выбор нужного вывода;
  70.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  71.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  72.     GPIO_Init(GPIOC, &GPIO_InitStructure);
  73.  
  74.     GPIO_ResetBits(GPIOC, GPIO_Pin_13); // Set C13 to Low level ("0")
  75.     GPIO_ResetBits(GPIOC, GPIO_Pin_14);
  76.     GPIO_ResetBits(GPIOC, GPIO_Pin_15);
  77.     GPIO_ResetBits(GPIOC, GPIO_Pin_12);
  78.     // TIMER4
  79.     TIM_TimeBaseInitTypeDef TIMER_InitStructure;
  80.     NVIC_InitTypeDef NVIC_InitStructure;
  81.  
  82.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
  83.  
  84.     TIM_TimeBaseStructInit(&TIMER_InitStructure);
  85.     TIMER_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
  86.     TIMER_InitStructure.TIM_Prescaler = 8000;
  87.     TIMER_InitStructure.TIM_Period = 500;
  88.     TIM_TimeBaseInit(TIM4, &TIMER_InitStructure);
  89.     TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
  90.     TIM_Cmd(TIM4, ENABLE);
  91.     /* NVIC Configuration */
  92.     /* Enable the TIM4_IRQn Interrupt */
  93.     NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
  94.     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  95.     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  96.     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  97.     NVIC_Init(&NVIC_InitStructure);
  98.  
  99.     while(1)
  100.     {
  101.     }
Add Comment
Please, Sign In to add comment