cancel
Showing results for 
Search instead for 
Did you mean: 

STM8S105C6 TIM3 CH1 FOR SPEED MEASUREMENT

paresh
Associate
Posted on July 28, 2013 at 18:44

DEAR ALL,

I WANT TO USE STM8S TIM3 CH1 AS SPEED MEASUREMENT OF MOTOR VIA PULSE INPUT. I HAVE 12 PULSE PER ROTATION. HOW TO START OR ANY IDEA FOR GETTING STARTED WITH THIS.

#st
1 REPLY 1
werapon_kmitl
Associate
Posted on October 30, 2013 at 10:19

/* Private function prototypes -----------------------------------------------*/

/* Private functions ---------------------------------------------------------*/

void CLK_Configuration(void);

void GPIO_Configuration(void);

void EXIT_Configuration(void);

void TIM1_Configuration(void);

void Delay(u16 Delay_DAT);

u32 TIM1ClockFreq = 4000000;

u8 TIM1_OVER_FLOW, START_CHECK_TIME;

u16 TIME_CAPTURE;

u32 FREQUENCY;

float SPEED_RPM;

void main(void)

{

    CLK_Configuration();

    TIM1_Configuration();

    GPIO_Configuration();

    EXIT_Configuration();

    enableInterrupts();

    

    

  /* Infinite loop */

  while (1)

  {

        if(TIM1_OVER_FLOW==0){

            FREQUENCY = TIM1ClockFreq/((u32)TIME_CAPTURE*64);

            

            SPEED_RPM = (TIM1ClockFreq*60)/((u32)TIME_CAPTURE*64);

        }

        else {

            FREQUENCY=0;

            SPEED_RPM=0;

        }

  }

 

}

void CLK_Configuration(void)

{

    CLK->CKDIVR = 0x10;            // Internal 4MHz

}

void GPIO_Configuration(void)

{

    /* GPIOD PD7 is input floating */

    GPIOB->DDR &=0xfd;

    GPIOB->CR1 |=0x02;        // floating input

    GPIOB->CR2 |=0x02;        // PB1 enable interrupt

}

void EXIT_Configuration(void)

{

    sim();                        // CCR I1 and I0 = 1

    EXTI->CR2 = 0x00; //TL interrupt Falling edge only

    EXTI->CR1 = 0x08;

}

void TIM1_Configuration(void)

{

    /*--------------------------------

                    Prescaler+1 * Autoreload

    time =  ----------------------   

                            4MHz

    --------------------------------*/

    

    /* Set the Autoreload value */

    TIM1->ARRH = 0xff;            

  TIM1->ARRL = 0xff;    

    TIM1->CNTRH = 0;

    TIM1->CNTRL = 0;

    

    /* Set the Prescaler value */

  TIM1->PSCRH = 0;        // Prescaler = 1 => 64

  TIM1->PSCRL = 63;

    

    /* Update interrupt enable */

    TIM1->IER |= 0x01;

    //enableInterrupts();

    

    /* TIM1 Counter enable */

    TIM1->CR1 |= 0x01;

}

void Software_Priority(void)

{

    ITC->ISPR4 &= 0xf3;  // TIM2 Update/overflow Level 2 (0 0)

    ITC->ISPR4 &= 0x7f;  // TIM3 Update/overflow Level 1 (0 1)

    

    

}

void Delay(u16 Delay_DAT)

{

    static u8 i;

    static u16 j;

    

    for(j=0;j<Delay_DAT;j++){

        for(i=0;i<255;i++);

    }

}