Professional Documents
Culture Documents
h>
//errors variables
float previous_error = 0;float current_error = 0;
/* GPIOA Configuration*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* TIM1 Configuration
* TIM1 input clock (TIM1CLK) is set to APB2 clock (PCLK2)
* => TIM1CLK = PCLK2 = SystemCoreClock
* TIM1CLK = SystemCoreClock, Prescaler = 0, TIM1 counter clock = SystemCoreClock
* SystemCoreClock is set to 168 MHz for STM32F30x devices
* TIM1_Period = (SystemCoreClock / 17570) - 1
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
int main()
{
// Initializations of GPIO Modules
// GPIOE8 for LED
// GPIOA1 for Input Pin
GPIO_InitTypeDef gpio;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE );
// Setting
Properties of Pins
// PinE8 is
for Identification of Completion of Revolution
//
GPIO_StructInit( &gpio );
//
gpio.GPIO_Mode = GPIO_Mode_OUT;
//
gpio.GPIO_Pin = GPIO_Pin_8;
//
GPIO_Init( GPIOE, &gpio );
// PinA1 is for Input speed
GPIO_StructInit( &gpio );
gpio.GPIO_Mode = GPIO_Mode_IN;
gpio.GPIO_Pin = GPIO_Pin_1;
GPIO_Init( GPIOA, &gpio );
GPIO_StructInit( &gpio );
Timer_configuration();
///////////////////////////////////////////////////////////////////////////////
//////////////////////////
///////////////////////////////////////////////////////////////////////////////
//////////////////////////
///////////////////////////////////////////////////////////////////////////////
//////////////////////////
///////////////////////////////////////////////////////////////////////////////
//////////////////////////
//************************************The main code starts
here****************************************//
///////////////////////////////////////////////////////////////////////////////
//////////////////////////
///////////////////////////////////////////////////////////////////////////////
//////////////////////////
///////////////////////////////////////////////////////////////////////////////
//////////////////////////
///////////////////////////////////////////////////////////////////////////////
//////////////////////////
Timer_start_func(); //
while(1)
{
// reads the current state of PA1
// default function used
if ( GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1) == Bit_RESET){
while(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1) == Bit_RESET){}
if (res==0){
// res here is a flag to see whether to start counter or to stop
it
Timer_start_func();
res=1;
} else if (res==1){
speed = 3000/iteration_time;
//******************************************///
//PId has been implemented here
//PID constants are
// KP =0.1 Kd = 1 KI =10
// input to the pid setup is the current_error
current_error = ref_speed - input;
integration_sum += (current_error * iteration_time);
duty = KP * current_error + KI * integration_sum + KD * 1000 *
(current_error -previous_error)/iteration_time;
//*******************************************///
res=0;
}
}
}
}