Anda di halaman 1dari 15

//-f interface/stlink-v2.cfg -f target/stm32f0x_stlink.

cfg
#include
#include
#include
#include
#include
#include
#include
#include

<stdint.h>
"stm32f0xx.h"
"stm32f0xx_spi.h"
"stm32f0xx_rcc.h"
"stm32f0xx_gpio.h"
"lcd_stm32f0.h"
<stdint.h>
"stm32f0xx_tim.h"

//#include "MPU6050.h"
#include "stm32f0xx_i2c.h"
//#include "stm320518_eval.h"
typedef enum RES{
RES6 = (0x18),
RES8 = (0x10),
RES10 = (0x8),
RES12 = (0x0)
} RES_Type;
typedef enum pin{
BUT0 = 0,
BUT1 = 1,
BUT2 = 2,
BUT3 = 3,
POT0 = 5,
POT1 = 6
} PIN_Type;
void main(void);
void init_spi(void);
void chipSelect(void);
void chipDeSelect(void);
uint8_t writeSPIgyro(uint8_t regAdr, uint8_t data);
void setup_gyro_registers(void);
void getGyro(float* out );
int16_t twosCompToDec16(uint16_t val);
static void delay(uint32_t delay_in_us);
void Calculations(float* value , double time, float temp1);
float* gyro[3];
void
void
void
void
void
void
void

tim6Enablev2(void);
tim14Enablev2(void);
InitializeTimer3(void);
InitializePWMChannel1();
InitializePWMChannel2();
init_leds(void);
init_leds2(void);

uint16_t pulse1;
uint16_t pulse2;
double g, g1, g2;
void writeLED(uint16_t pat);

uint8_t isPressed(uint8_t pin);


void enableGPIO();
void ADC_setup(void);
void ADC_switch6(void);
float temp1;
uint32_t readadc(void);
uint8_t c, q, t, I, I2, j;
uint32_t light_reading1, light_reading2;
double tmp;
double difference;
uint16_t P_gain, timeI;
float throttle, steering;
float I_gain, D_gain, I_error, D_error;
TIM_ICInitTypeDef TIM_ICInitStructure;
uint32_t IC2Value1, DutyCycle1, Frequency1, IC2Value2, DutyCycle2, Frequency2;
void
void
void
void

TIM1_Config(void);
TIM2_Config(void);
TIM1_IRQHandler(void);
TIM2_IRQHandler(void);

void main(void)
{
//MPU6050_I2C_Init();
//MPU6050_Initialize();
volatile uint32_t delay_counter = 0;
P_gain = 360;
I_gain = 0.00;
D_gain = 0.00;
TIM1_Config();
TIM2_Config();

lcd_init();
enableGPIO();
init_spi();
setup_gyro_registers();
tim6Enablev2();
tim14Enablev2();
ADC_setup();
InitializeTimer3();
init_leds();
init_leds2();
delay(2000000);
for(;;)
{

//120

//
//
//
//
//
//
//
//
//
_counter++)
//
//
//
//
//
//

if(isPressed(BUT0))
{
lcd_command(LCD_CLEAR_DISPLAY);
lcd_string("Calibrating Gyro");
delay(200000);
lcd_command(LCD_GOTO_LINE_2);
for(delay_counter = 0; delay_counter < 16; delay
{
delay(25000);
lcd_string(">");
}
g = 0;
}
temp1 = readadc();
double time;
TIM_SetCounter(TIM6, 0);
getGyro(gyro);
time = TIM_GetCounter(TIM6);
TIM_SetCounter(TIM6, 0);
Calculations(gyro, time, temp1);
}

}
void Calculations(float* value, double time, float temp1)
{
//
if (t == 0)
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//light 1 is on
//
//
//
//
//
//

{
delay(10000);
light_reading1 = 4*readadc();
delay(10000);
t = 1;
writeLED(0b0000000000);
}
if (t < 6)
{
light_reading2 = 4*readadc();
if (light_reading2 <= 5)
}
if ((light_reading2 >= 1.45*light_reading1) && (t == 1))
{
t = 2;
writeLED(0b0100000000);
}
if ((light_reading2 <= 1.15*light_reading1) && (t == 2))

//light 1 is off
//
//
//
//
//
//
//light 2 is on
//
//
//
//
//
//
//light 2 is off
//
//
//
//
//
//
|| (light_reading2 <=
//
//
//
//
//
//

{
t = 3;
writeLED(0b0000000000);
}
if ((light_reading2 >= 1.45*light_reading1) && (t == 3))
{
t = 4;
writeLED(0b0100000001);
}
if ((light_reading2 <= 1.15*light_reading1) && (t == 4))
{
t = 5;
writeLED(0b0000000000);
}
if (((light_reading2 >= 1.4*light_reading1) && (t == 5))
0.4*light_reading1))
//light 3 is on
{
t = 6;
writeLED(0b1000000000);
ADC_switch6();
temp1 = readadc();
}

t = 6;
writeLED(0b0000000000);
ADC_switch6();
temp1 = readadc();
tmp = value[2];
//tmp = (tmp - (0.086*(temp1) - 6));
uint8_t a, b, x, y;
uint16_t duty_cycleF1, duty_cycleF2, period;
if (t == 6)
{
if ((tmp > -2 && tmp < 2) || (t < 6))
{
tmp=0;
}
if ((tmp > 300 || tmp < -300) || (t < 6))
{
tmp=0;
}
//

period = 20000;
g = g + 0.1*(tmp)*(time)*0.0022458;

//
//

if (I == 0)
{

//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//

TIM_SetCounter(TIM14, 0);
g1 = g;
I++;
}
if (I2 == 1)
{
timeI = TIM_GetCounter(TIM14);
difference = g - g1;
I_error = 0.1*difference*timeI;
I_error = I_gain*I_error;
D_error = ((10*difference)/timeI)*D_gain;
D_error = D_gain*D_error;
I = 0;
}
I2++;
if(I2 == 2)
{
I2 = 0;
}
TIM1_IRQHandler();
TIM2_IRQHandler();
throttle = ((DutyCycle1*0.274)-138);
steering = ((DutyCycle2*0.274)-138);
pulse1 = (20000*(steering/100)) ;

//

- (P_gain*(temp1 + I_error + D_error));


pulse2 = (20000*(steering/100));

//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//

if (q < 4)
{
if(q == 0)
{
pulse1 = 0.73*pulse1;
pulse2 = 0.62*pulse2;
}
if(q == 1)
{
pulse1 = 0.77*pulse1;
pulse2 = 0.70*pulse2;
}
if(q == 2)
{
pulse1 = 0.85*pulse1;
pulse2 = 0.80*pulse2;
}
if(q == 3)
{
pulse1 = 0.97*pulse1;
pulse2 = 0.92*pulse2;
q = 4;

//
//

}
}
InitializePWMChannel1();
InitializePWMChannel2();

//
//
//
//
//
//
//
//
//
//
//
//
//
//

c++;
if ( q < 4 )
{
if (c == 115)
{
q++;
c = 0;
}
}
duty_cycleF1 = ((pulse1*100)/period);
duty_cycleF2 = ((pulse2*100)/period);
a = DutyCycle1/10;
b = (DutyCycle1-a)*10;
x = g;
y = (g-x)*10;
c++;
if ( c == 50 )
{
j++;
lcd_command(LCD_CLEAR_DISPLAY);
lcd_command(LCD_CURSOR_HOME);
lcd_string("Throttle:");
LCD_display_uint8(throttle);
//lcd_string("D:");

//
//
//
//
//
//
//

if ( tmp < 0 )
{
a = 256-a;
b = 256-b;
lcd_string("-");
}
//LCD_display_uint8(a);
//lcd_string(".");
//LCD_display_uint8(b);
lcd_command(LCD_GOTO_LINE_2);
lcd_string("Steering:");

//
//

if (g < 0)
{

//
//
//
//

x = 256-x;
y = 256-y;
lcd_string("-");
}
LCD_display_uint8(steering);
//lcd_string(" ");
//LCD_display_uint8(IC2Value);
//lcd_string(" ");
//LCD_display_uint8(Frequency);

//
//
//

lcd_string(" t:");
LCD_display_uint8(duty_cycleF1);
c = 0;
}
}

void tim6Enablev2(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
TIM_TimeBaseInitTypeDef timer6InitStructure;
TIM_TimeBaseStructInit(&timer6InitStructure);
timer6InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
timer6InitStructure.TIM_Period = 2000;
timer6InitStructure.TIM_Prescaler = 9000;
TIM_TimeBaseInit(TIM6, &timer6InitStructure);
TIM_Cmd(TIM6, ENABLE);
}
void tim14Enablev2(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE);
TIM_TimeBaseInitTypeDef timer14InitStructure;
TIM_TimeBaseStructInit(&timer14InitStructure);
timer14InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
timer14InitStructure.TIM_Period = 20000;
timer14InitStructure.TIM_Prescaler = 9000;
TIM_TimeBaseInit(TIM6, &timer14InitStructure);
TIM_Cmd(TIM14, ENABLE);
}
void InitializeTimer3(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseInitTypeDef timerInitStructure;
timerInitStructure.TIM_Prescaler = 40;
timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
timerInitStructure.TIM_Period = 20000;
timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
timerInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM3, &timerInitStructure);

TIM_Cmd(TIM3, ENABLE);
}
void InitializePWMChannel1()
{
TIM_OCInitTypeDef outputChannelInit = {0,};
outputChannelInit.TIM_OCMode = TIM_OCMode_PWM1;
outputChannelInit.TIM_Pulse = pulse1;
outputChannelInit.TIM_OutputState = TIM_OutputState_Enable;
outputChannelInit.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &outputChannelInit);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_1);
}
void InitializePWMChannel2()
{
TIM_OCInitTypeDef outputChannelInit = {0,};
outputChannelInit.TIM_OCMode = TIM_OCMode_PWM1;
outputChannelInit.TIM_Pulse = pulse2;
outputChannelInit.TIM_OutputState = TIM_OutputState_Enable;
outputChannelInit.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM3, &outputChannelInit);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_1);
}
void writeLED(uint16_t pat)
{
GPIOB->ODR = pat;
}
uint8_t isPressed(uint8_t pin)
{
return ((GPIOA->IDR&(1<<pin))==0);
}
//void InitializeLEDs()
//{
//
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
//
GPIO_InitTypeDef gpioStructure;
//
//
gpioStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9;
//
gpioStructure.GPIO_Mode = GPIO_Mode_OUT;
//
gpioStructure.GPIO_Speed = GPIO_Speed_50MHz;
//
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
//
GPIO_Init(GPIOB, &gpioStructure);
//}
void init_leds(void)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9;

GPIO_InitStructure.GPIO_Mode =GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void init_leds2(void)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void init_spi()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB|RCC_AHBPeriph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_12;//CSN =B12
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //output mode
GPIO_InitStructure.GPIO_OType= GPIO_OType_PP; //push pull
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3; //#gottagofast
GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_UP; //doesn't matter
GPIO_Init(GPIOB, &GPIO_InitStructure);
chipDeSelect();
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //alternate mode
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3; //faster than fast
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //push pull that pin yo!
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //Do you even lift?
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_Init(GPIOB, &GPIO_InitStructure);
SPI_InitTypeDef SPI_InitStructure;
SPI_I2S_DeInit(SPI2);
SPI_InitStructure.SPI_Mode= SPI_Mode_Master; //the dev board is the mast
er and the peripheral is the slave
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //rec
eiving
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //8 bit
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStructure);
SPI_Cmd(SPI2, ENABLE);

}
void setup_gyro_registers(void)
{
writeSPIgyro(0x23, 0b10000000);
writeSPIgyro(0x20, 0b11001111);
}
void getGyro(float* out )
{
uint8_t crtlB;
crtlB=(uint8_t)writeSPIgyro(0b10100011,0x00);//determines what range the
gyro is in (250dps, 500dps or 2000dps)
uint8_t
uint8_t
uint8_t
uint8_t
uint8_t
uint8_t

gyroXL=writeSPIgyro(0xA8,0x1);
gyroXH=writeSPIgyro(0xA9,0x1);
gyroYL=writeSPIgyro(0xAA,0x1);
gyroYH=writeSPIgyro(0xAB,0x1);
gyroZL=writeSPIgyro(0xAC,0x1);
gyroZH=writeSPIgyro(0xAD,0x1);

uint8_t buffer[6];
buffer[1]=gyroXL;
buffer[0]=gyroXH;
buffer[3]=gyroYL;
buffer[2]=gyroYH;
buffer[5]=gyroZL;
buffer[4]=gyroZH;
uint16_t t=0;
int i=0;
uint8_t temp=(uint8_t)(crtlB & 0x30);
switch(temp)
{
case (uint8_t)0x00://250dps
for(i=0; i<3; i++)
{
t=(((uint16_t)buffer[2*i] << 8) | buffer[2*i+1]);
int16_t temp2=twosCompToDec16(t);
out[i]=(float)((temp2*8.75/1000.0));
}
break;
case (uint8_t)0x10://500dps
for(i=0; i<3; i++)
{
t=(((uint16_t)buffer[2*i] << 8) |buffer[2*i+1]);
int16_t temp2=twosCompToDec16(t);
out[i]=(float)((temp2*17.5/1000.0));
}
break;
case (uint8_t)0x20://2000dps
for(i=0; i<3; i++)
{
t=(((uint16_t)buffer[2*i] << 8) |buffer[2*i+1]);
int16_t temp2=twosCompToDec16(t);
out[i]=(float)((temp2*70/1000.0));
}
break;

case (uint8_t)0x30://20000dps
for(i=0; i<3; i++)
{
t=(((uint16_t)buffer[2*i] << 8) | buffer[2*i+1]);
int16_t temp2=twosCompToDec16(t);
out[i]=(float)((temp2*70/1000.0));
}
break;
}
}
void chipSelect()
{
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
}
void chipDeSelect()
{
GPIO_SetBits(GPIOB, GPIO_Pin_12);
}
uint8_t writeSPIgyro(uint8_t regAdr, uint8_t data)
{
uint8_t dummyVar;
uint32_t val;
chipSelect();
delay(10);
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET)
{
}
uint16_t temp=(uint16_t)regAdr|(uint16_t)data<<8;
SPI_I2S_SendData16(SPI2, temp);
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET)
{
}
delay(200);
uint8_t val2 =SPI_ReceiveData8(SPI2);
val =SPI_ReceiveData8(SPI2);
chipDeSelect();
return (uint8_t)val;
}
int16_t twosCompToDec16(uint16_t val)
{
uint16_t v=val;
int16_t temp=0;
temp=~val;
temp+=1;
return (int16_t)temp;
}
static void delay(uint32_t delay_in_us)
{
volatile uint32_t counter = 0;
delay_in_us *= 3;
for(; counter < delay_in_us; counter++) {
__asm("nop");
__asm("nop");

}
}
void enableGPIO()
{
RCC->AHBENR |= 0x60000;
GPIOB->MODER |= 0x5555;
GPIOA->PUPDR |= 0x55;
GPIOA->MODER |= 0x3C00;
}
uint32_t readadc(void)
{
uint32_t mask = 1 << 2;
uint32_t reg = 0;
ADC1->CR |= 1 << 2; // kick off conversion
while (reg != mask){ // wait for conversion complete.
reg = ADC1->ISR;
reg &= mask;
}
return ADC1->DR;
}
void ADC_setup(void)
{
uint32_t status = 0;
RCC->AHBENR |= 1 << 17;
GPIOA->PUPDR |= 0b01010101;
GPIOA->MODER |= 3 << 12;
RCC->AHBENR |= 1 << 18;
GPIOB->MODER |= 0x5555;
RCC->APB2ENR |= 1 << 9;
ADC1->CFGR1 |= 1 << 3;
ADC1->CHSELR |= 1 << 5;
ADC1->CR |= 1;
while (status == 0) {
status = ADC1->ISR;
status &= 1;
}
}
void ADC_switch6(void)
{
ADC1->CHSELR =0;
ADC1->CHSELR |= 1 << 6;
}
void TIM1_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* TIM2 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM15, ENABLE);
/* GPIOB clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
/* TIM2 chennel2 configuration : PA.01 */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1| GPIO_Pin_3;


GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Connect TIM pin to AF2 */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_2);
/* Enable the TIM2 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM15_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//NVIC_Init(&NVIC_InitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_PWMIConfig(TIM15, &TIM_ICInitStructure);
/* Select the TIM2 Input Trigger: TI2FP2 */
TIM_SelectInputTrigger(TIM15, TIM_TS_TI2FP2);
/* Select the slave Mode: Reset Mode */
TIM_SelectSlaveMode(TIM15, TIM_SlaveMode_Reset);
TIM_SelectMasterSlaveMode(TIM1,TIM_MasterSlaveMode_Enable);
/* TIM enable counter */
TIM_Cmd(TIM15, ENABLE);
/* Enable the CC2 Interrupt Request */
TIM_ITConfig(TIM15, TIM_IT_CC2, ENABLE);
}
void TIM1_IRQHandler(void)
{
RCC_ClocksTypeDef RCC_Clocks;
RCC_GetClocksFreq(&RCC_Clocks);
/* Clear TIM2 Capture compare interrupt pending bit */
TIM_ClearITPendingBit(TIM15, TIM_IT_CC2);
/* Get the Input Capture value */
IC2Value1 = TIM_GetCapture2(TIM15);
IC2Value1 = 0.01*IC2Value1;
if (IC2Value1 != 0)
{
/* Duty cycle computation */
DutyCycle1 = (TIM_GetCapture1(TIM15) * 100) / IC2Value1;
/* Frequency computation
TIM2 counter clock = (RCC_Clocks.HCLK_Frequency)/2 */
Frequency1 = RCC_Clocks.HCLK_Frequency / IC2Value1;

}
else
{
DutyCycle1 = 0;
Frequency1 = 0;
}
}

void TIM2_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* TIM2 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* Connect TIM pin to AF2 */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_2);
/* Enable the TIM2 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//NVIC_Init(&NVIC_InitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_PWMIConfig(TIM2, &TIM_ICInitStructure);
/* Select the TIM2 Input Trigger: TI2FP2 */
TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);
/* Select the slave Mode: Reset Mode */
TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);
TIM_SelectMasterSlaveMode(TIM2,TIM_MasterSlaveMode_Enable);
/* TIM enable counter */
TIM_Cmd(TIM2, ENABLE);
/* Enable the CC2 Interrupt Request */
TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);
}
void TIM2_IRQHandler(void)
{
RCC_ClocksTypeDef RCC_Clocks;
RCC_GetClocksFreq(&RCC_Clocks);
/* Clear TIM2 Capture compare interrupt pending bit */
TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
/* Get the Input Capture value */
IC2Value2 = TIM_GetCapture2(TIM2);

IC2Value2 = 0.01*IC2Value2;
if (IC2Value2 != 0)
{
/* Duty cycle computation */
DutyCycle2 = (TIM_GetCapture1(TIM2) * 100) / IC2Value2;
/* Frequency computation
TIM2 counter clock = (RCC_Clocks.HCLK_Frequency)/2 */
Frequency2 = RCC_Clocks.HCLK_Frequency / IC2Value2;
}
else
{
DutyCycle2 = 0;
Frequency2 = 0;
}
}