Program Fin

download Program Fin

If you can't read please download the document

Transcript of Program Fin

#include"stm32f4xx_exti.h" #include"stm32f4xx_gpio.h" #include"stm32f4xx_rcc.h" #include"stm32f4xx_tim.h" #include"stm32f4xx_usart.h" #include"misc.h" #include #include #define SIGNO1_PIN GPIO_Pin_0//pin del motor 1 #define SIGNO2_PIN GPIO_Pin_1//pin de motor 2 //-------------------------------------------#DEFINE Encoder 1----------------------------------------// | TIM4_CH1 pin (PB.06) | // | TIM4_CH2 pin (PB.07) | // -------------------------//PINS CODEUR A ET B #define Codeur_A GPIO_Pin_6 #define Codeur_A_SOURCE GPIO_PinSource6 #define Codeur_B GPIO_Pin_7 #define Codeur_B_SOURCE GPIO_PinSource7 #define Codeur_GPIO GPIOB #define Codeur_RCC RCC_AHB1Periph_GPIOB #define Codeur_AF GPIO_AF_TIM4 //TIMER4 UTILISE #define Codeur_TIMER #define Codeur_TIMER_CLK #define Codeur_COUNT() TIM4 RCC_APB1Periph_TIM4 Codeur_TIMER->CNT

//-------------------------------------------#DEFINE Encoder 2----------------------------------------// | TIM5_CH1 pin (PA.00) | // | TIM5_CH2 pin (PA.01) | // -------------------------//PINS CODEUR A ET B #define #define #define #define #define #define #define Codeur_A1 Codeur_A_SOURCE1 Codeur_B1 Codeur_B_SOURCE1 Codeur_GPIO1 Codeur_RCC1 Codeur_AF1 GPIO_Pin_0 GPIO_PinSource0 GPIO_Pin_1 GPIO_PinSource1 GPIOA RCC_AHB1Periph_GPIOA GPIO_AF_TIM5 TIM5 RCC_APB1Periph_TIM5 Codeur_TIMER1->CNT

//TIMER4 UTILISE #define Codeur_TIMER1 #define Codeur_TIMER_CLK1 #define Codeur_COUNT1()

/////////////-----------------//-------------------------------------------VARIABLES----------------------------------------//GLOBAL volatile int16_t Codeur_count1; volatile int32_t Codeur_total1; //LOCAL static volatile int16_t Codeur_old1; static volatile int16_t Codeur_actual1;

float kP1,kD1, errorOld1,error1,setPos1,currentPos1,PWM1,Codeur_actualaux1; //------------------------------------------PROTOTYPES---------------------------------------void RCC_Configuration1(void); void GPIO_Configuration1(void); void TIMER_Configuration1(void); void CODEUR_Reset1(void); void CODEUR_Read1(void); //--------------------------------------------end encoder 2 //LEDS DEMOBOARD #define Led_Red #define Led_Green #define Led_Orange #define LED_BLUE #define Led_GPIO #define Led_RCC GPIO_Pin_12 GPIO_Pin_14 GPIO_Pin_13 GPIO_Pin_15 GPIOD RCC_AHB1Periph_GPIOD

//-------------------------------------------VARIABLES---------------------------------------//GLOBAL volatile int16_t Codeur_count; volatile int32_t Codeur_total; //LOCAL static volatile int16_t Codeur_old; static volatile int16_t Codeur_actual; float kP,kD, errorOld,error,setPos,currentPos,PWM,Codeur_actualaux; //------------------------------------------PROTOTYPES1---------------------------------------void RCC_Configuration(void); void GPIO_Configuration(void); void TIMER_Configuration(void); void CODEUR_Reset(void); void CODEUR_Read(void);

//-------------------------------------------------------------------------------------------void Conf_Ent(void); void Conf_Relojes(void); void Retardo(u32 tiempo); void Pines_driver1(void); void tim_config(void); float Dato[3]; int main(void) { int i; Conf_Relojes(); Conf_Ent();

//int i=0; /* DEMO */ // int brightness = 2100;//para 10 KHz vel max RCC_Configuration(); GPIO_Configuration(); TIMER_Configuration(); RCC_Configuration1(); GPIO_Configuration1(); TIMER_Configuration1(); Pines_driver1(); tim_config(); //--int a=1; int b=1; while ( !USART_GetFlagStatus(USART2, USART_FLAG_RXNE) ) ; // Si no se ha recibido ningn dato se espera, cuando exista uno en el registro { for(i=1;i1050) PWM1 =1050; if (error1 CCR2 = PWM1; // set brightness } if(setPos110 50) PWM1 =10 50; if (error1 < -1050) PWM1 =-1 050; TIM3->CCR2 = -1*PWM1; // set brightness

} } if(b==1) { if(setPos>0) { GPIO_SetBits(GPIOC,SIGNO1_PIN); kP=25;// kD=12.389; CODEUR_Read(); Codeur_actualaux = set Pos*8.88888888888889; error =Codeur_actual aux - Codeur_total; PWM = kP * error; errorOld = error; PWM += (kD *(error - errorOld)) ; if(error>1050) PWM =1050; if (error < -1050) PWM =-1050; TIM3->CCR1 =1*PWM; } if(setPos1050) PWM =1050; if (error < -1050) PWM =-1050;

TIM3->CCR1 =-1*PWM; } } }} void CODEUR_RESET (void) { __disable_irq(); Codeur_old = 0; Codeur_total = 0; TIM_SetCounter (Codeur_TIMER, 0); CODEUR_Read(); __enable_irq(); } void TIMER_Configuration(void) { // set them up as encoder inputs // set both inputs to rising polarity to let it use both edges TIM_EncoderInterfaceConfig (Codeur_TIMER, TIM_EncoderMode_TI2, TIM_ICPolarit y_Rising, TIM_ICPolarity_Rising); TIM_SetAutoreload (Codeur_TIMER, 0xffff); // turn on the timer/counters TIM_Cmd (Codeur_TIMER, ENABLE); // Reset CODEUR_RESET (); } void CODEUR_Read (void) { Codeur_old = Codeur_actual; Codeur_actual = TIM_GetCounter (Codeur_TIMER) ; Codeur_count = Codeur_actual - Codeur_old; Codeur_total += Codeur_count; } void RCC_Configuration(void) { // Enable RCC codeur RCC_AHB1PeriphClockCmd(Codeur_RCC, ENABLE); // Enable RCC led RCC_AHB1PeriphClockCmd(Led_RCC, ENABLE); // Enable Timer 4 clock RCC_APB1PeriphClockCmd(Codeur_TIMER_CLK, ENABLE); }

void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; // 2 Inputs for A and B Encoder Channels GPIO_InitStructure.GPIO_Pin = Codeur_A|Codeur_B; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(Codeur_GPIO, &GPIO_InitStructure); //Timer AF Pins Configuration GPIO_PinAFConfig(Codeur_GPIO, Codeur_A_SOURCE, Codeur_AF); GPIO_PinAFConfig(Codeur_GPIO, Codeur_B_SOURCE, Codeur_AF); // Output Leds port D GPIO_InitStructure.GPIO_Pin = Led_Red|Led_Green|LED_BLUE|Led_Orange; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(Led_GPIO, &GPIO_InitStructure); }

void CODEUR_RESET1 (void) { __disable_irq(); Codeur_old1 = 0; Codeur_total1 = 0; TIM_SetCounter (Codeur_TIMER1, 0); CODEUR_Read1(); __enable_irq(); } void TIMER_Configuration1(void) { // set them up as encoder inputs // set both inputs to rising polarity to let it use both edges TIM_EncoderInterfaceConfig (Codeur_TIMER1, TIM_EncoderMode_TI2, TIM_ICPolari ty_Rising, TIM_ICPolarity_Rising); TIM_SetAutoreload (Codeur_TIMER1, 0xffff); // turn on the timer/counters TIM_Cmd (Codeur_TIMER1, ENABLE); // Reset CODEUR_RESET1 (); } void CODEUR_Read1 (void) { Codeur_old1 = Codeur_actual1;

Codeur_actual1 = TIM_GetCounter (Codeur_TIMER1) ; Codeur_count1 = Codeur_actual1 - Codeur_old1; Codeur_total1 += Codeur_count1; } void RCC_Configuration1(void) { // Enable RCC codeur RCC_AHB1PeriphClockCmd(Codeur_RCC1, ENABLE); // Enable RCC led // Enable Timer 4 clock RCC_APB1PeriphClockCmd(Codeur_TIMER_CLK1, ENABLE); } void GPIO_Configuration1(void) { GPIO_InitTypeDef GPIO_InitStructure; // 2 Inputs for A and B Encoder Channels GPIO_InitStructure.GPIO_Pin = Codeur_A1|Codeur_B1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(Codeur_GPIO1, &GPIO_InitStructure); //Timer AF Pins Configuration GPIO_PinAFConfig(Codeur_GPIO1, Codeur_A_SOURCE1, Codeur_AF1); GPIO_PinAFConfig(Codeur_GPIO1, Codeur_B_SOURCE1, Codeur_AF1);

} void tim_config(void) { int PrescalerValue; //valor del preescalador TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;//definicin de la estructura para el tim4 TIM_OCInitTypeDef TIM_OCInitStructure;//Configuracin de modo del timer GPIO_InitTypeDef GPIO_InitStructure;//configuracin de los pines d el timer y leds RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); /* TIM3 clock ena ble */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); /* LEDs are on GPIOD */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//modo de funcin alternativ o GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//vel max

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//push pull GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;//pull up GPIO_Init(GPIOC, &GPIO_InitStructure); /*Configuracn del modo alternativo*/ GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM4); GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_TIM4); /* pwm set up */ /* Compute the prescaler value */ PrescalerValue = (uint16_t) ((SystemCoreClock /2) / 28000000) - 1; /*(168/2)/21 -1*/ /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 1050;//freq seal 31578.94Hz TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;//3 TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//modo de c ontar del timer4 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); /* PWM1 Mode configuration: Channel1 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//modo del pwm TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//activa el pwm TIM_OCInitStructure.TIM_Pulse = 0; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//rising edge TIM_OC1Init(TIM3, &TIM_OCInitStructure);//habilita le estructura de ti m4 TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);//habilita config de autorrecarga /* PWM1 Mode configuration: Channel2 */ TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0; TIM_OC2Init(TIM3, &TIM_OCInitStructure); TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);

TIM_ARRPreloadConfig(TIM3, ENABLE); /* TIM3 enable counter */ TIM_Cmd(TIM3, ENABLE);

}

void Pines_driver1(void) { /* GPIOC Periph clock enable */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = SIGNO1_PIN| SIGNO2_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOC, &GPIO_InitStructure);

}

void Conf_Relojes(void) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);//para la usart RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);//para los puertos } void Conf_Ent(void) { USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; //puerto B GPIO_PinAFConfig(GPIOD,GPIO_PinSource5,GPIO_AF_USART2); GPIO_PinAFConfig(GPIOD,GPIO_PinSource6,GPIO_AF_USART2); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOD, &GPIO_InitStructure); //puerto D

//*usart USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowContro l_None; USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx; USART_Init(USART2, &USART_InitStructure);

USART_Cmd(USART2, ENABLE); } void Retardo(u32 tiempo) { u32 aux1; aux1 = tiempo; while(aux1 != 0) { aux1 --; } }