Page 1 of 1

STM32F334R8 running light

Posted: Tue May 30, 2017 8:34 pm
by ayri

Code: Select all

#include "stm32f30x_conf.h"

uint16_t read_pos(void);
void PC_Conf(void);
uint8_t get_bit(uint8_t, uint8_t);

// PROCESSORTACT = 64 MHz
// AHB  Prescaler = 1
// APB1 Prescaler = 2
// APB2 Prescaler = 1

long pin = 1;
long dir = 1;
char recvd;

int main(void){
    /*

    */
    //Definitions
    GPIO_InitTypeDef GPIO_Initstructure;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
    NVIC_InitTypeDef NVIC_NVICInitStructure;

    //ENABLE CLOCK
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

    //GPIO
    GPIO_Initstructure.GPIO_Mode=GPIO_Mode_OUT;
    GPIO_Initstructure.GPIO_OType=GPIO_OType_PP;
    GPIO_Initstructure.GPIO_Pin=GPIO_Pin_All;
    GPIO_Initstructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
    GPIO_Initstructure.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_Initstructure);

    //TIMER
    TIM_TimeBaseInitStructure.TIM_Prescaler = (64000-1); //TIM2 cycle to 1kHz
    TIM_TimeBaseInitStructure.TIM_Period = 500; //Every 500ms
    TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);

    //NVIC
    NVIC_NVICInitStructure.NVIC_IRQChannel=TIM2_IRQn;
    NVIC_NVICInitStructure.NVIC_IRQChannelCmd=ENABLE;
    NVIC_NVICInitStructure.NVIC_IRQChannelPreemptionPriority=0;
    NVIC_NVICInitStructure.NVIC_IRQChannelSubPriority=0;
    NVIC_Init(&NVIC_NVICInitStructure);

    //Enable everything
    TIM_Cmd(TIM2, ENABLE);
    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);

  /* Infinite loop */
  while (1)
  {
  }
}

void TIM2_IRQHandler(void)
{   
    GPIOB->ODR &= 0;
    GPIOB->ODR ^=1<<pin;

    //Pin to new value
    if(dir == 1)
    {
        if(pin == 5)
            pin=11;
        else if(pin == 15)
        {   pin-=1;
            dir = 0;
        }
        else
            pin+=1;
    }
    else
    {
        if(pin == 11)
            pin = 5;
        else if(pin == 1)
        {
            pin+=1;
            dir = 1;
        }
        else
            pin-=1;
    }

    TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
}

I have trouble understanding this code. First part is okay, but I dont understand the last function.

Can someone explain me simply what is going on in the last part of this code in TIM2_IRQHandler(void)?

I am new in this, any help would be welcome.