Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- ******************************************************************************
- * @file main.c
- * @author MCD Application Team
- * @version V1.1.0
- * @date 07-October-2011
- * @brief Main program body
- ******************************************************************************
- * @attention
- *
- * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
- * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
- * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
- * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
- * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
- * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
- *
- * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "stm32_eval.h"
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- #define SEQUENCE_LEN 13
- #define SEQUENCE_STEP 100 /* in milliseconds */
- const int sequence[SEQUENCE_LEN] = {0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1};
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- uint32_t sequence_counter;
- /* Private function prototypes -----------------------------------------------*/
- /* Private functions ---------------------------------------------------------*/
- void initClock() {
- RCC_SYSCLKConfig(RCC_SYSCLKSource_HSE); /* external crystal oscillator at 25 MHz */
- RCC_HCLKConfig(RCC_SYSCLK_Div1); /* AHB clock = SysClock */
- RCC_PCLK1Config(RCC_HCLK_Div1); /* APB1 clock = AHB clock = SysClock = 25 MHz */
- }
- void initGPIO() {
- GPIO_InitTypeDef initStruct;
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); /* Enable clock for GPIO */
- GPIO_StructInit(&initStruct);
- initStruct.GPIO_Pin = GPIO_Pin_15 | GPIO_Pin_14;
- initStruct.GPIO_Mode = GPIO_Mode_OUT;
- initStruct.GPIO_OType = GPIO_OType_PP;
- initStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOE, &initStruct);
- }
- void initTMR2() {
- TIM_TimeBaseInitTypeDef tim;
- TIM_TimeBaseStructInit(&tim);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
- tim.TIM_Prescaler = 25000; /* 25 MHz / 25000 = 1 kHz = 1 ms */
- tim.TIM_CounterMode = TIM_CounterMode_Up;
- tim.TIM_Period = SEQUENCE_STEP;
- tim.TIM_ClockDivision = TIM_CKD_DIV1;
- tim.TIM_RepetitionCounter = 0;
- TIM_TimeBaseInit(TIM2, &tim);
- TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
- TIM_Cmd(TIM2, ENABLE);
- }
- void initInterupts() {
- NVIC_InitTypeDef nvicStructure;
- nvicStructure.NVIC_IRQChannel = TIM2_IRQn;
- nvicStructure.NVIC_IRQChannelPreemptionPriority = 0;
- nvicStructure.NVIC_IRQChannelSubPriority = 1;
- nvicStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&nvicStructure);
- }
- void TIM2_IRQHandler()
- {
- if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
- {
- TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
- if (++sequence_counter == SEQUENCE_LEN)
- sequence_counter = 0;
- if (sequence[sequence_counter])
- GPIO_SetBits(GPIOE, GPIO_Pin_15);
- else
- GPIO_ResetBits(GPIOE, GPIO_Pin_15);
- GPIO_ToggleBits(GPIOE, GPIO_Pin_14);
- }
- }
- /**
- * Main program.
- * @return Nothing.
- */
- int main(void) {
- initClock(); /* set clock source and frequency */
- initGPIO(); /* initialize the GPIO ports */
- initTMR2(); /* initialize the timer */
- initInterupts(); /* initialize the interrupt system and enable interrupts */
- sequence_counter = 0;
- while(1) {
- /* everything is handled in the interrupt handler */
- }
- }
- /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement