Advertisement
Ladislav

AVS úkol 1/2

Mar 1st, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.06 KB | None | 0 0
  1. /**
  2.  ******************************************************************************
  3.  * @file    main.c
  4.  * @author  MCD Application Team
  5.  * @version V1.1.0
  6.  * @date    07-October-2011
  7.  * @brief   Main program body
  8.  ******************************************************************************
  9.  * @attention
  10.  *
  11.  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12.  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13.  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  14.  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15.  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16.  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17.  *
  18.  * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  19.  ******************************************************************************
  20.  */
  21.  
  22. /* Includes ------------------------------------------------------------------*/
  23. #include "stm32_eval.h"
  24.  
  25. /* Private typedef -----------------------------------------------------------*/
  26. /* Private define ------------------------------------------------------------*/
  27. #define SEQUENCE_LEN 13
  28. #define SEQUENCE_STEP 100 /* in milliseconds */
  29. const int sequence[SEQUENCE_LEN] = {0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1};
  30. /* Private macro -------------------------------------------------------------*/
  31. /* Private variables ---------------------------------------------------------*/
  32. uint32_t sequence_counter;
  33. /* Private function prototypes -----------------------------------------------*/
  34. /* Private functions ---------------------------------------------------------*/
  35.  
  36. void initClock() {
  37.     RCC_SYSCLKConfig(RCC_SYSCLKSource_HSE); /* external crystal oscillator at 25 MHz */
  38.     RCC_HCLKConfig(RCC_SYSCLK_Div1); /* AHB clock = SysClock */
  39.     RCC_PCLK1Config(RCC_HCLK_Div1); /* APB1 clock = AHB clock = SysClock = 25 MHz */
  40. }
  41.  
  42. void initGPIO() {
  43.     GPIO_InitTypeDef initStruct;
  44.  
  45.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); /* Enable clock for GPIO */
  46.     GPIO_StructInit(&initStruct);
  47.     initStruct.GPIO_Pin = GPIO_Pin_15 | GPIO_Pin_14;
  48.     initStruct.GPIO_Mode = GPIO_Mode_OUT;
  49.     initStruct.GPIO_OType = GPIO_OType_PP;
  50.     initStruct.GPIO_Speed = GPIO_Speed_50MHz;
  51.     GPIO_Init(GPIOE, &initStruct);
  52. }
  53.  
  54. void initTMR2() {
  55.     TIM_TimeBaseInitTypeDef tim;
  56.     TIM_TimeBaseStructInit(&tim);
  57.    
  58.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  59.    
  60.     tim.TIM_Prescaler = 25000; /* 25 MHz / 25000 = 1 kHz = 1 ms */
  61.     tim.TIM_CounterMode = TIM_CounterMode_Up;
  62.     tim.TIM_Period = SEQUENCE_STEP;
  63.     tim.TIM_ClockDivision = TIM_CKD_DIV1;
  64.     tim.TIM_RepetitionCounter = 0;
  65.     TIM_TimeBaseInit(TIM2, &tim);
  66.     TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
  67.     TIM_Cmd(TIM2, ENABLE);
  68. }
  69.  
  70. void initInterupts() {
  71.     NVIC_InitTypeDef nvicStructure;
  72.     nvicStructure.NVIC_IRQChannel = TIM2_IRQn;
  73.     nvicStructure.NVIC_IRQChannelPreemptionPriority = 0;
  74.     nvicStructure.NVIC_IRQChannelSubPriority = 1;
  75.     nvicStructure.NVIC_IRQChannelCmd = ENABLE;
  76.     NVIC_Init(&nvicStructure);
  77. }
  78.  
  79. void TIM2_IRQHandler()
  80. {
  81.     if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
  82.     {
  83.         TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
  84.  
  85.         if (++sequence_counter == SEQUENCE_LEN)
  86.             sequence_counter = 0;
  87.         if (sequence[sequence_counter])
  88.             GPIO_SetBits(GPIOE, GPIO_Pin_15);
  89.         else
  90.             GPIO_ResetBits(GPIOE, GPIO_Pin_15);
  91.  
  92.         GPIO_ToggleBits(GPIOE, GPIO_Pin_14);
  93.     }
  94. }
  95.  
  96. /**
  97.  * Main program.
  98.  * @return Nothing.
  99.  */
  100. int main(void) {
  101.     initClock(); /* set clock source and frequency */
  102.     initGPIO(); /* initialize the GPIO ports */
  103.     initTMR2(); /* initialize the timer */
  104.     initInterupts(); /* initialize the interrupt system and enable interrupts */
  105.  
  106.     sequence_counter = 0;
  107.    
  108.     while(1) {
  109.         /* everything is handled in the interrupt handler */
  110.     }
  111. }
  112. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement