找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4096|回复: 3
打印 上一主题 下一主题
收起左侧

三轴云台EVVGC源代码(PID控制方式 6050传感器驱动)

[复制链接]
跳转到指定楼层
楼主
ID:418253 发表于 2018-10-30 23:28 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
简单三轴云台PID控制方式 6050传感器驱动的云台控制方案。

单片机源程序如下:
  1. #include "stm32f10x.h"
  2. #include "stm32f10x_rcc.h"
  3. #include "stm32f10x_gpio.h"
  4. #include "stm32f10x_usart.h"
  5. #include "stm32f10x_tim.h"
  6. #include "stm32f10x_i2c.h"
  7. #include "stm32f10x_adc.h"
  8. #include <stdio.h>
  9. #include <math.h>

  10. #define MPU6050_ADDRESS     0xD0   // Gyro address
  11. #define EEPROM_ADDRESS      0xAE   //EEPROM address
  12. #define MPU6050_I2C I2C2 //mpu6050 bus
  13. #define LEDon  GPIO_WriteBit(GPIOB, GPIO_Pin_12,   Bit_SET)
  14. #define LEDoff GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_RESET)



  15.        GPIO_InitTypeDef        GPIO_InitStructure;
  16.             TIM_OCInitTypeDef       TIM_OCInitStructure;
  17. TIM_TimeBaseInitTypeDef     TIM_TimeBaseStructure;
  18. TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
  19.     TIM_BDTRInitTypeDef                 TIM_BDTRInitStructure;
  20. USART_ClockInitTypeDef  USART_ClockInitStructure;
  21.                                 ADC_InitTypeDef         ADC_InitStructure;
  22.                                 I2C_InitTypeDef         I2C_InitStructure;



  23. void Periph_clock_enable(void);
  24. void GPIO_Config(void);
  25. void Usart4Init(void);
  26. void USART_PutChar(uint8_t ch);
  27. void USART_PutString(uint8_t * str);
  28. void I2C_Config(void);
  29. void ADC_Config(void);
  30. void Timer1_Config(void);
  31. void Timer8_Config(void);
  32. void Timer2_Config(void);
  33. void Timer5_Config(void);
  34. void Timer4_Config(void);
  35. void MPU6050_Init(void);
  36. void Delay_ms(uint32_t ms);
  37. void MPU6050_I2C_BufferRead(u8 slaveAddr, u8* pBuffer, u8 readAddr, u16 NumByteToRead);
  38. void MPU6050_Gyro_get(void);
  39. void MPU6050_ACC_get(void);
  40. void pitch_PID(void);
  41. void roll_PID(void);
  42. void yaw_PID(void);
  43. u16 readADC1(u8 channel);
  44. void WriteToEEPROM(int addressToWrite, int DataToWrite);
  45. void ReadFromEEPROM(u8 readAddr);
  46. void NVIC_Configuration(void);
  47. void UART4_IRQHandler();
  48. void saveData(void);


  49. int ConfigMode,  w, enable_writing;
  50. int stop=0, i, YawPh1, YawPh2, YawPh3, EEPROM_REGISTER, EEPROM_DATA, EepromData, UART4_DATA;
  51. float roll_amplitude, pitch_amplitude, yaw_amplitude, dt=0.002, ADC1Ch1_vid, sinus, cosinus, ROLL;
  52. uint8_t XMSB,XLSB,YMSB,YLSB,ZMSB,ZLSB;
  53. uint8_t out[] = {0,0,0,0,0,0};
  54. short int gyroADC_PITCH, gyroADC_ROLL, gyroADC_YAW, accADC_ROLL, accADC_PITCH, accADC_YAW, printcounter;
  55. float pitch, Gyro_Pitch_angle, pitch_output, pitch_setpoint=0, pitch_Error_current, pitch_Error_last, pitch_P, pitch_D, pitch_Kp, pitch_Kd, pitch_angle=0, pitch_angle_true, pitch_angle_correction;
  56. float roll,   Gyro_Roll_angle,  roll_output,  roll_setpoint=0,  roll_Error_current,  roll_Error_last,  roll_P,  roll_D,  roll_Kp,  roll_Kd,  roll_angle=0,  roll_angle_correction;
  57. float yaw,     Gyro_Yaw_angle,   yaw_output,   yaw_setpoint=0,   yaw_Error_current,   yaw_Error_last,   yaw_P,   yaw_D,   yaw_Kp,   yaw_Kd,   yaw_angle=0,   yaw_angle_correction;
  58. float acc_pitch_angle, acc_roll_angle, accADC_x, accADC_y, accADC_z, gyroADC_x, gyroADC_y, gyroADC_z, fs, fssmall, acc_pitch_angle_vid, acc_roll_angle_vid;
  59. uint8_t ACCread[6], GYROread[6];
  60. int RXString[3];
  61. char configData[9]={'1','1','1','1','1','1','1','1','1'};
  62. float sinusas[91]={
  63. 0.000, 0.017, 0.035, 0.052, 0.070, 0.087, 0.105, 0.122, 0.139, 0.156,
  64. 0.174, 0.191, 0.208, 0.225, 0.242, 0.259, 0.276, 0.292, 0.309, 0.326,
  65. 0.342, 0.358, 0.375, 0.391, 0.407, 0.423, 0.438, 0.454, 0.469, 0.485,
  66. 0.500, 0.515, 0.530, 0.545, 0.559, 0.574, 0.588, 0.602, 0.616, 0.629,
  67. 0.643, 0.656, 0.669, 0.682, 0.695, 0.707, 0.719, 0.731, 0.743, 0.755,
  68. 0.766, 0.777, 0.788, 0.799, 0.809, 0.819, 0.829, 0.839, 0.848, 0.857,
  69. 0.866, 0.875, 0.883, 0.891, 0.899, 0.906, 0.914, 0.920, 0.927, 0.934,
  70. 0.940, 0.945, 0.951, 0.956, 0.961, 0.966, 0.970, 0.974, 0.978, 0.982,
  71. 0.985, 0.988, 0.990, 0.993, 0.995, 0.996, 0.998, 0.999, 0.999, 1.000,
  72. 1.000};

  73. char buff[10];
  74. int main(void)
  75. {
  76.        
  77.        
  78.         Periph_clock_enable();
  79.         GPIO_Config();       
  80.         Usart4Init();
  81.         I2C_Config();
  82.         ADC_Config();
  83.         Delay_ms(100);
  84.         MPU6050_Init();
  85.         Timer1_Config();
  86.         Timer8_Config();
  87.         Timer2_Config();
  88.         Timer5_Config();
  89.         Timer4_Config();
  90.         NVIC_Configuration();
  91.        
  92.         TIM_Cmd(TIM5, ENABLE);
  93.         TIM_CtrlPWMOutputs(TIM5, ENABLE);
  94.         for (i=1 ; i<1 ; i++) ;
  95.         TIM_Cmd(TIM4, ENABLE);
  96.         TIM_CtrlPWMOutputs(TIM4, ENABLE);
  97.        
  98.         LEDoff;
  99.   
  100.        
  101.        

  102.         Delay_ms(100);
  103.        
  104.                         for(i=0; i<9;i++){
  105.                                                         ReadFromEEPROM(i);
  106.                                                         configData[i]=EepromData;                                                       
  107.                                                         Delay_ms(5);                                                       
  108.                                                         }
  109.                                                          /* Enable Acknowledgement to be ready for another reception */
  110.                                                         I2C_AcknowledgeConfig(I2C2, ENABLE);
  111.         Delay_ms(100);
  112.        
  113. pitch_Kp = 0.160;  //spring
  114. pitch_Kd = 0.80;//damping

  115. roll_Kp = 0.160;  //spring
  116. roll_Kd = 0.80;//damping

  117. yaw_Kp = 0.160;  //spring
  118. yaw_Kd = 0.80;//damping


  119. //pitch_amplitude=250; //500=100%
  120. //roll_amplitude=450; //500=100%
  121.   //yaw_amplitude=450; //500=100%


  122. while(1)
  123.         {       
  124.                 LEDon;
  125.                
  126.                 while(ConfigMode==1){
  127.                 LEDon;
  128.                 Delay_ms(100);
  129.                 LEDoff;
  130.                 Delay_ms(100);
  131.                 TIM4->CCR1=0;
  132.                 TIM4->CCR2=0;
  133.                 TIM4->CCR3=0;
  134.                 TIM5->CCR1=0;
  135.                 TIM5->CCR2=0;
  136.                 TIM5->CCR3=0;
  137.                 TIM1->CCR1=0;
  138.                 TIM1->CCR2=0;
  139.                 TIM1->CCR3=0;
  140.                 TIM8->CCR1=0;
  141.                 TIM8->CCR2=0;
  142.                 TIM8->CCR3=0;       
  143.                 }
  144.                
  145.                 MPU6050_ACC_get();//Getting Accelerometer data
  146.                
  147.                 acc_pitch_angle = -(atan2(accADC_x, accADC_z))-0.10;   //Calculating pitch ACC angle+callibration
  148.                 acc_roll_angle  = +(atan2(accADC_y, accADC_z));   //Calculating roll ACC angle               
  149.                
  150.                 MPU6050_Gyro_get();//Getting Gyroscope data               
  151.        
  152.                 acc_pitch_angle_vid=  ((acc_pitch_angle_vid*99.00)+acc_pitch_angle)/100.00;        //Averaging pitch ACC values
  153.     acc_roll_angle_vid=   ((acc_roll_angle_vid* 99.00)+acc_roll_angle)/ 100.00; //Averaging roll ACC values               
  154.                
  155.                 sinus   = sinusas[(int)(ADC1Ch1_vid*57.3)];      //Calculating sinus               
  156.     cosinus = sinusas[90-(int)(ADC1Ch1_vid*57.3)];   //Calculating cosinus
  157.                
  158.                 ROLL=-gyroADC_z*sinus+gyroADC_y*cosinus;
  159.                 roll_angle =(roll_angle + ROLL*dt)    + 0.0002*(acc_pitch_angle_vid-roll_angle); //Roll Horizon
  160.                
  161.                
  162.                 //ROLL=-gyroADC_z*sinus+gyroADC_y*cosinus;
  163.                 yaw_angle =(yaw_angle + gyroADC_z*dt); //Yaw
  164.                
  165.                
  166.                
  167.                 pitch_angle_true=((pitch_angle_true  + gyroADC_x*dt) + 0.0002*(acc_roll_angle_vid-pitch_angle_true)); //Pitch Horizon
  168.                
  169.                 ADC1Ch1_vid=  ((ADC1Ch1_vid*99.00)+(readADC1(1)/4000.00))/100.00;        //Averaging ADC values
  170.                 ADC1Ch1_vid=0.00;
  171.                
  172.                 pitch_angle=pitch_angle_true-ADC1Ch1_vid;//Adding angle

  173.                 pitch_angle_correction=pitch_angle*150.0;
  174.                 if(pitch_angle_correction> 2.0){pitch_angle_correction= 2.0;}
  175.                 if(pitch_angle_correction<-2.0){pitch_angle_correction=-2.0;}
  176.                 pitch_setpoint=pitch_setpoint+pitch_angle_correction;//Pitch return to zero after collision
  177.                
  178.                 roll_angle_correction=roll_angle*200.0;
  179.                 if(roll_angle_correction> 2.0){roll_angle_correction= 2.0;}
  180.                 if(roll_angle_correction<-2.0){roll_angle_correction=-2.0;}
  181.                 roll_setpoint=roll_setpoint+roll_angle_correction;//Roll return to zero after collision
  182.                
  183.                 /*yaw_angle_correction=yaw_angle*150.0;
  184.                 if(yaw_angle_correction> 1.0){yaw_angle_correction= 1.0;}
  185.                 if(yaw_angle_correction<-1.0){yaw_angle_correction=-1.0;}
  186.                 yaw_setpoint=yaw_setpoint+yaw_angle_correction;//Roll return to zero after collision*/
  187.                
  188.     pitch_PID();//Pitch axis pid
  189.                 roll_PID(); //Roll axis pid
  190.                 yaw_PID(); //Yaw axis pid
  191.                
  192.                
  193.                 printcounter++; //Print data to UART
  194.                 if (printcounter>=100)
  195.                         {
  196.                                 //sprintf (buff, " %d %d %c Labas\n\r", ACCread[0], ACCread[1], ACCread[2]);
  197.                                 //sprintf (buff, " %x %x %x %x %x %x Labas\n\r", ACCread[0], ACCread[1], ACCread[2], ACCread[3], ACCread[4], ACCread[5]);
  198.                                 //sprintf (buff, "Labas %d %d\n\r", ACCread[0], ACCread[1]);
  199.                                 //sprintf (buff, "%3.1f %f\n\r", ADC1Ch1_vid*57.3, sinus);
  200.                                 //sprintf (buff, "Labas %f %f %f \n\r", accADC_x, accADC_y, accADC_z);
  201.                                 //sprintf (buff, "%3.1f %3.1f \n\r", acc_pitch_angle_vid*57.3,  acc_roll_angle_vid *57.3);
  202.                                 //sprintf (buff, "%3.1f %3.1f \n\r", pitch_angle*57.3,  roll_angle*57.3);
  203.                                 //sprintf (buff, "%3.3f\n\r", yaw_angle*57.3);
  204.                                 //USART_PutString(buff);
  205.                                 printcounter=0;
  206.                         }
  207.                
  208.     stop=0;
  209.                 LEDoff;       
  210.                 while(stop==0) {}//Closed loop waits for interrupt

  211.                
  212.         }
  213. }

  214. void Periph_clock_enable(void)
  215. {
  216.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB  |
  217.                          RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD  |
  218.                          RCC_APB2Periph_GPIOE |
  219.                                                                                                  RCC_APB2Periph_AFIO  | RCC_APB2Periph_ADC1        | RCC_APB2Periph_TIM1 | RCC_APB2Periph_TIM8, ENABLE);
  220.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5  | RCC_APB1Periph_TIM2 | RCC_APB1Periph_UART4   
  221.                                                                                                  | RCC_APB1Periph_TIM4, ENABLE);
  222.   RCC_AHBPeriphClockCmd (RCC_AHBPeriph_DMA1,  ENABLE);
  223. }

  224. void GPIO_Config(void)
  225. {
  226.                         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  227.                         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  228.                         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  229.                         GPIO_Init(GPIOB, &GPIO_InitStructure);
  230.        
  231.                         //CS_G
  232.                         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  233.                         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  234.                         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  235.                         GPIO_Init(GPIOC, &GPIO_InitStructure);
  236.        
  237.                         //CS_G
  238.                         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  239.                         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  240.                         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  241.                         GPIO_Init(GPIOA, &GPIO_InitStructure);
  242. }



  243. void Usart4Init(void)
  244. {
  245.                         GPIO_InitTypeDef GPIO_InitStructure;
  246.                         USART_InitTypeDef USART_InitStructure;
  247.                         USART_ClockInitTypeDef USART_ClockInitStructure;

  248.                         //Set USART2 Tx (PA.02) as AF push-pull
  249.                         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  250.                         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  251.                         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  252.                         GPIO_Init(GPIOC, &GPIO_InitStructure);
  253.                         //Set USART2 Rx (PA.03) as input floating
  254.                         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  255.                         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  256.                         GPIO_Init(GPIOC, &GPIO_InitStructure);
  257.        
  258.        
  259.                         USART_ClockStructInit(&USART_ClockInitStructure);
  260.                         USART_ClockInit(UART4, &USART_ClockInitStructure);
  261.                         USART_InitStructure.USART_BaudRate = 9600;
  262.                         USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  263.                         USART_InitStructure.USART_StopBits = USART_StopBits_1;
  264.                         USART_InitStructure.USART_Parity = USART_Parity_No ;
  265.                         USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  266.                         USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  267.                         //Write USART2 parameters
  268.                         USART_Init(UART4, &USART_InitStructure);
  269.                         //Enable UART4 Receive interrupt
  270.                         USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);
  271.                         //Enable USART2
  272.                         USART_Cmd(UART4, ENABLE);
  273.                         }
  274.                        
  275.                        
  276.                        
  277. void USART_PutChar(uint8_t ch)
  278. {
  279.   while(!(UART4->SR & USART_SR_TXE));
  280.   UART4->DR = ch;
  281. }

  282. void USART_PutString(uint8_t * str)
  283. {
  284. while(*str != 0)
  285.   {
  286.   USART_PutChar(*str);
  287.   str++;
  288. }
  289. }

  290. void UART4_IRQHandler()//UART4 Interrupt handler implementation
  291. {
  292.     int eeRreg;
  293.           ConfigMode=1;
  294.                 while ( USART_GetFlagStatus(UART4, USART_FLAG_RXNE) == RESET);
  295.     UART4_DATA=USART_ReceiveData(UART4);
  296. LEDon;
  297.                 if(UART4_DATA==103){ //if "g"
  298.                                                        
  299.                                                         Delay_ms(100);
  300.                                                         sprintf (buff, "x");
  301.                                                         USART_PutString(buff);
  302.                                                         for(eeRreg=0; eeRreg<9;eeRreg++){
  303.                                                         ReadFromEEPROM(eeRreg);
  304.                                                         Delay_ms(5);
  305.                                                         sprintf (buff, "%c",EepromData);
  306.                                                         USART_PutString(buff);
  307.                                                         }
  308.                                                          /* Enable Acknowledgement to be ready for another reception */
  309.                                                         I2C_AcknowledgeConfig(I2C2, ENABLE);
  310.                                                         }
  311.                                                        
  312.                                                        

  313.                                                        
  314.                 if(enable_writing==1){                                                       
  315.                                                         configData[w]=(int)UART4_DATA;
  316.                                                         w++;
  317.                                                         if(w>8){w=0; enable_writing=0; saveData();}
  318.                                                         }
  319.                                                        
  320.                                                        
  321.                 if(UART4_DATA==104){ // if h (write to eeprom)
  322.                                                        
  323.                                                         enable_writing=1;
  324.                                                         }
  325.                
  326.                                                        
  327.                 if(UART4_DATA==105){
  328.                                                        
  329.                                                         ConfigMode=1;
  330.                                                         }       
  331.                                                        
  332.                 if(UART4_DATA==106){
  333.                                                        
  334.                                                         ConfigMode=0;
  335.                                                         }                                                               
  336.                        
  337.                
  338.                
  339.        
  340. }

  341. void NVIC_Configuration(void)
  342. {
  343.   NVIC_InitTypeDef NVIC_InitStructure;

  344.   /* Configure the NVIC Preemption Priority Bits */  
  345.   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  346.   
  347.   /* Enable the USARTy Interrupt */
  348.   NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
  349.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  350.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  351.   NVIC_Init(&NVIC_InitStructure);

  352. }
  353. void I2C_Config(void)
  354. {
  355. //Clock enable
  356.                 RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);       
  357. //GPIO Setup
  358.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
  359.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;  // Open Drain, I2C bus pulled high externally
  360.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  361.     GPIO_Init(GPIOB, &GPIO_InitStructure);

  362. //I2C Setup
  363.           I2C_Cmd(I2C2,ENABLE);               
  364.           I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
  365.     I2C_InitStructure.I2C_ClockSpeed = 400000;
  366.     I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
  367.     I2C_InitStructure.I2C_OwnAddress1 = 0xD0;
  368.     I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
  369.     I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  370.     I2C_Init(I2C2,&I2C_InitStructure);
  371.    
  372.           I2C_AcknowledgeConfig(I2C2, ENABLE);

  373. }


  374. void MPU6050_Init(void)
  375. {
  376.         Delay_ms(5);
  377.   I2C_GenerateSTART(I2C2, ENABLE); // Send I2C1 START condition
  378.   while(!I2C_GetFlagStatus(I2C2, I2C_FLAG_SB));
  379.   I2C_Send7bitAddress(I2C2, MPU6050_ADDRESS, I2C_Direction_Transmitter);// Send EEPROM slave Address for write  
  380.   while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));// Test on I2C1 EV6 and clear it
  381.   I2C_SendData(I2C2, 0x19);// Send I2C1 EEPROM internal address //MPU6050_RA_SMPLRT_DIV
  382.   while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); // Test on I2C1 EV8 and clear it   
  383.   I2C_SendData(I2C2, 0x07);// Send I2C1 EEPROM data   
  384.   while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));// Test on I2C1 EV8 and clear it  
  385.   I2C_GenerateSTOP(I2C2, ENABLE);// Send I2C1 STOP Condition
  386.         while(I2C_GetFlagStatus(I2C2, I2C_FLAG_STOPF));
  387.        
  388.         I2C_GenerateSTART(I2C2, ENABLE); // Send I2C1 START condition
  389.   while(!I2C_GetFlagStatus(I2C2, I2C_FLAG_SB));   
  390.   I2C_Send7bitAddress(I2C2, MPU6050_ADDRESS, I2C_Direction_Transmitter);// Send EEPROM slave Address for write   
  391.   while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));// Test on I2C1 EV6 and clear it  
  392.   I2C_SendData(I2C2, 0x1A);// Send I2C1 EEPROM internal address
  393.   while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); //Test on I2C1 EV8 and clear it
  394.   I2C_SendData(I2C2, 0x02);// Send I2C1 EEPROM data LOW PASS..............................................................................
  395.   while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));// Test on I2C1 EV8 and clear it
  396.   I2C_GenerateSTOP(I2C2, ENABLE);// Send I2C1 STOP Condition
  397.         while(I2C_GetFlagStatus(I2C2, I2C_FLAG_STOPF));
  398.        
  399.         I2C_GenerateSTART(I2C2, ENABLE); // Send I2C1 START condition
  400.   while(!I2C_GetFlagStatus(I2C2, I2C_FLAG_SB));   
  401.   I2C_Send7bitAddress(I2C2, MPU6050_ADDRESS, I2C_Direction_Transmitter);// Send EEPROM slave Address for write   
  402.   while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));// Test on I2C1 EV6 and clear it  
  403.   I2C_SendData(I2C2, 0x1B);// Send I2C1 EEPROM internal address
  404.   while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); //Test on I2C1 EV8 and clear it
  405.   I2C_SendData(I2C2, 0x08);// Send I2C1 EEPROM data
  406.   while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));// Test on I2C1 EV8 and clear it
  407.   I2C_GenerateSTOP(I2C2, ENABLE);// Send I2C1 STOP Condition
  408.         while(I2C_GetFlagStatus(I2C2, I2C_FLAG_STOPF));
  409.        
  410.   I2C_GenerateSTART(I2C2, ENABLE); // Send I2C1 START condition
  411.   while(!I2C_GetFlagStatus(I2C2, I2C_FLAG_SB));   
  412.   I2C_Send7bitAddress(I2C2, MPU6050_ADDRESS, I2C_Direction_Transmitter);// Send EEPROM slave Address for write   
  413.   while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));// Test on I2C1 EV6 and clear it  
  414.   I2C_SendData(I2C2, 0x6B);// Send I2C1 EEPROM internal address
  415.   while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); //Test on I2C1 EV8 and clear it
  416.   I2C_SendData(I2C2, 0x00);// Send I2C1 EEPROM data
  417.   while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));// Test on I2C1 EV8 and clear it
  418.   I2C_GenerateSTOP(I2C2, ENABLE);// Send I2C1 STOP Condition
  419.         while(I2C_GetFlagStatus(I2C2, I2C_FLAG_STOPF));
  420.        
  421.         Delay_ms(5);

  422. }

  423. void ADC_Config(void)
  424. {
  425. // Configure ADC on ADC0_IN0    pin PA0
  426.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  427.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  428.   GPIO_Init(GPIOA, &GPIO_InitStructure);
  429.        

  430.   /* PCLK2 is the APB2 clock */
  431.   /* ADCCLK = PCLK2/6 = 72/6 = 12MHz*/
  432.   RCC_ADCCLKConfig(RCC_PCLK2_Div6);

  433.   /* Enable ADC1 clock so that we can talk to it */
  434.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
  435.   /* Put everything back to power-on defaults */
  436.   ADC_DeInit(ADC1);

  437.   /* ADC1 Configuration ------------------------------------------------------*/
  438.   /* ADC1 and ADC2 operate independently */
  439.   ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  440.   /* Disable the scan conversion so we do one at a time */
  441.   ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  442.   /* Don't do contimuous conversions - do them on demand */
  443.   ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
  444.   /* Start conversin by software, not an external trigger */
  445.   ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  446.   /* Conversions are 12 bit - put them in the lower 12 bits of the result */
  447.   ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  448.   /* Say how many channels would be used by the sequencer */
  449.   ADC_InitStructure.ADC_NbrOfChannel = 1;

  450.   /* Now do the setup */
  451.   ADC_Init(ADC1, &ADC_InitStructure);
  452.   /* Enable ADC1 */
  453.   ADC_Cmd(ADC1, ENABLE);

  454.   /* Enable ADC1 reset calibaration register */
  455.   ADC_ResetCalibration(ADC1);
  456.   /* Check the end of ADC1 reset calibration register */
  457.   while(ADC_GetResetCalibrationStatus(ADC1));
  458.   /* Start ADC1 calibaration */
  459.   ADC_StartCalibration(ADC1);
  460.   /* Check the end of ADC1 calibration */
  461.   while(ADC_GetCalibrationStatus(ADC1));


  462. }

  463. u16 readADC1(u8 channel)
  464. {
  465.   ADC_RegularChannelConfig(ADC1, channel, 1, ADC_SampleTime_28Cycles5);
  466.   // Start the conversion
  467.   ADC_SoftwareStartConvCmd(ADC1, ENABLE);
  468.   // Wait until conversion completion
  469.   while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
  470.   // Get the conversion value
  471.   return ADC_GetConversionValue(ADC1);
  472. }

  473.         void Delay_ms(uint32_t ms)
  474. {
  475.         volatile uint32_t nCount;
  476.         RCC_ClocksTypeDef RCC_Clocks;
  477.     RCC_GetClocksFreq (&RCC_Clocks);

  478.         nCount=(RCC_Clocks.HCLK_Frequency/10000)*ms;
  479.         for (; nCount!=0; nCount--);
  480. }

  481. void MPU6050_I2C_BufferRead(u8 slaveAddr, u8* pBuffer, u8 readAddr, u16 NumByteToRead)
  482. {


  483. /* While the bus is busy */
  484. while(I2C_GetFlagStatus(MPU6050_I2C, I2C_FLAG_BUSY));

  485. /* Send START condition */
  486. I2C_GenerateSTART(MPU6050_I2C, ENABLE);

  487. /* Test on EV5 and clear it */
  488. while(!I2C_CheckEvent(MPU6050_I2C, I2C_EVENT_MASTER_MODE_SELECT));

  489. /* Send MPU6050 address for write */
  490. I2C_Send7bitAddress(MPU6050_I2C, slaveAddr, I2C_Direction_Transmitter);

  491. /* Test on EV6 and clear it */
  492. while(!I2C_CheckEvent(MPU6050_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

  493. /* Clear EV6 by setting again the PE bit */
  494. I2C_Cmd(MPU6050_I2C, ENABLE);

  495. /* Send the MPU6050's internal address to write to */
  496. I2C_SendData(MPU6050_I2C, readAddr);

  497. /* Test on EV8 and clear it */
  498. while(!I2C_CheckEvent(MPU6050_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

  499. /* Send STRAT condition a second time */
  500. I2C_GenerateSTART(MPU6050_I2C, ENABLE);

  501. /* Test on EV5 and clear it */
  502. while(!I2C_CheckEvent(MPU6050_I2C, I2C_EVENT_MASTER_MODE_SELECT));

  503. /* Send MPU6050 address for read */
  504. I2C_Send7bitAddress(MPU6050_I2C, slaveAddr, I2C_Direction_Receiver);

  505. /* Test on EV6 and clear it */
  506. while(!I2C_CheckEvent(MPU6050_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));

  507. /* While there is data to be read */
  508. while(NumByteToRead)
  509. {
  510. if(NumByteToRead == 1)
  511. {
  512. /* Disable Acknowledgement */
  513. I2C_AcknowledgeConfig(MPU6050_I2C, DISABLE);

  514. /* Send STOP Condition */
  515. I2C_GenerateSTOP(MPU6050_I2C, ENABLE);
  516. }

  517. /* Test on EV7 and clear it */
  518. if(I2C_CheckEvent(MPU6050_I2C, I2C_EVENT_MASTER_BYTE_RECEIVED))
  519. {
  520. /* Read a byte from the MPU6050 */
  521. *pBuffer = I2C_ReceiveData(MPU6050_I2C);

  522. /* Point to the next location where the byte read will be saved */
  523. pBuffer++;

  524. /* Decrement the read bytes counter */
  525. NumByteToRead--;
  526. }
  527. }

  528. /* Enable Acknowledgement to be ready for another reception */
  529. I2C_AcknowledgeConfig(MPU6050_I2C, ENABLE);


  530. }


  531. void MPU6050_ACC_get(void)
  532. {
  533.         MPU6050_I2C_BufferRead(MPU6050_ADDRESS, ACCread, 0X3B, 6);
  534.                 accADC_ROLL  =  (((ACCread[0]<<8) | ACCread[1]));
  535.                 accADC_x  =(accADC_ROLL);
  536.                 accADC_PITCH =  (((ACCread[2]<<8) | ACCread[3]));
  537.                 accADC_y=(accADC_PITCH);
  538.                 accADC_YAW   =  (((ACCread[4]<<8) | ACCread[5]));
  539.                 accADC_z    =(accADC_YAW);
  540. }       

  541. void MPU6050_Gyro_get(void)
  542. {
  543.                 MPU6050_I2C_BufferRead(MPU6050_ADDRESS, GYROread, 0X43, 6);
  544.                 gyroADC_ROLL  =  (((GYROread[0]<<8) | GYROread[1]));
  545.                 gyroADC_x=((float)gyroADC_ROLL+300)/8000.00;
  546.                 gyroADC_PITCH =  (((GYROread[2]<<8) | GYROread[3]));
  547.                 gyroADC_y=((float)gyroADC_PITCH+320)/8000.00;
  548.                 gyroADC_YAW   =  (((GYROread[4]<<8) | GYROread[5]));
  549.                 gyroADC_z=((float)gyroADC_YAW+350)/8000.00;
  550. }
  551. void WriteToEEPROM(int addressToWrite, int DataToWrite)//Write data to external EEPROM
  552. {
  553.         Delay_ms(5);
  554.   I2C_GenerateSTART(I2C2, ENABLE); // Send I2C2 START condition
  555.   while(!I2C_GetFlagStatus(I2C2, I2C_FLAG_SB));
  556.   I2C_Send7bitAddress(I2C2, EEPROM_ADDRESS, I2C_Direction_Transmitter);// Send EEPROM slave Address for write  
  557.   while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));// Test on I2C2 EV6 and clear it
  558.   I2C_SendData(I2C2, addressToWrite);// Send I2C2 EEPROM internal address
  559.   while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); // Test on I2C2 EV8 and clear it   
  560.   I2C_SendData(I2C2, DataToWrite);// Send I2C2 EEPROM data   
  561.   while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));// Test on I2C2 EV8 and clear it  
  562.   I2C_GenerateSTOP(I2C2, ENABLE);// Send I2C2 STOP Condition
  563.         while(I2C_GetFlagStatus(I2C2, I2C_FLAG_STOPF));
  564.         Delay_ms(5);
  565. }

  566. void ReadFromEEPROM(u8 readAddr)
  567. {


  568. /* While the bus is busy */
  569. while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY));

  570. /* Send START condition */
  571. I2C_GenerateSTART(I2C2, ENABLE);

  572. /* Test on EV5 and clear it */
  573. while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));

  574. /* Send MPU6050 address for write */
  575. I2C_Send7bitAddress(I2C2, EEPROM_ADDRESS, I2C_Direction_Transmitter);

  576. /* Test on EV6 and clear it */
  577. while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

  578. /* Clear EV6 by setting again the PE bit */
  579. I2C_Cmd(I2C2, ENABLE);

  580. /* Send the MPU6050's internal address to write to */
  581. I2C_SendData(I2C2, readAddr);

  582. /* Test on EV8 and clear it */
  583. while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

  584. /* Send STRAT condition a second time */
  585. I2C_GenerateSTART(I2C2, ENABLE);

  586. /* Test on EV5 and clear it */
  587. while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));

  588. /* Send eeprom address for read */
  589. I2C_Send7bitAddress(I2C2, EEPROM_ADDRESS, I2C_Direction_Receiver);


  590. /* Test on EV6 and clear it */
  591. while(!I2C_CheckEvent(I2C2 ,I2C_EVENT_MASTER_BYTE_RECEIVED));

  592. /* Read a byte from the 24C02 */
  593. EepromData = I2C_ReceiveData(I2C2);

  594. /* Disable Acknowledgement */
  595. I2C_AcknowledgeConfig(I2C2, DISABLE);

  596. /* Send STOP Condition */
  597. I2C_GenerateSTOP(I2C2, ENABLE);



  598. }

  599. void saveData(void){
  600.         int eeRegCount;       
  601.                
  602.        

  603.         for(eeRegCount=0; eeRegCount<9;eeRegCount++){
  604.                                                         LEDon;
  605.                                                         WriteToEEPROM(eeRegCount, configData[eeRegCount]);
  606.                                                         Delay_ms(5);                                                       
  607.                                                         }
  608.                                                        
  609.                                                         LEDoff;
  610.        
  611. }
  612. void Timer1_Config(void)//ROLL Timer configuration
  613. {       

  614.         //Timer1 Config
  615.        
  616.         //PWM pin config
  617.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10;
  618.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  619.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  620.   GPIO_Init(GPIOA, &GPIO_InitStructure);
  621.        
  622.         //PWM pin config
  623.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  624.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  625.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  626.   GPIO_Init(GPIOB, &GPIO_InitStructure);

  627.         //Time Base configuration
  628.         TIM_TimeBaseInitStructure.TIM_Prescaler = 5; // Period*Prescaler=24'000'000Hz  //2400 1s
  629.   TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
  630.   TIM_TimeBaseInitStructure.TIM_Period = 1000; //20000(presc=24)=50hz(servo signal)
  631.   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  632.   TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
  633.   TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStructure);               

  634.   //Configuration in PWM mode
  635.         TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1 ;
  636.         TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  637.         TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
  638.         TIM_OCInitStructure.TIM_Pulse = 0;      
  639.         TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  640.         TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
  641.         TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
  642.         TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;       
  643.   TIM_OC1Init(TIM1, &TIM_OCInitStructure);       
  644.         TIM_OC2Init(TIM1, &TIM_OCInitStructure);       
  645.         TIM_OC3Init(TIM1, &TIM_OCInitStructure);       


  646.   //Automatic Output enable, Break, dead time and lock configuration
  647.   TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
  648.   TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
  649.   TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
  650.   TIM_BDTRInitStructure.TIM_DeadTime = 80;
  651.   TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
  652.   TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
  653.   TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
  654.   TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
  655.        
  656.   //TIM1 counter enable
  657.   TIM_Cmd(TIM1, ENABLE);

  658.   //Main Output Enable
  659.   TIM_CtrlPWMOutputs(TIM1, ENABLE);

  660. }       

  661. void Timer8_Config(void)//Pitch Timer configuration
  662. {       

  663.         //Timer8 Config
  664.        
  665.         //PWM pin config
  666.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8;
  667.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  668.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  669.   GPIO_Init(GPIOC, &GPIO_InitStructure);
  670.        
  671.         //PWM pin config
  672.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  673.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  674.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  675.   GPIO_Init(GPIOA, &GPIO_InitStructure);
  676.        
  677.         //PWM pin config
  678.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  679.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  680.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  681.   GPIO_Init(GPIOB, &GPIO_InitStructure);

  682.         //Time Base configuration
  683.         TIM_TimeBaseInitStructure.TIM_Prescaler = 5; // Period*Prescaler=24'000'000Hz  //2400 1s
  684.   TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
  685.   TIM_TimeBaseInitStructure.TIM_Period = 1000; //20000(presc=24)=50hz(servo signal)
  686.   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  687.   TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
  688.   TIM_TimeBaseInit(TIM8, &TIM_TimeBaseInitStructure);               

  689.   //Configuration in PWM mode
  690.         TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1 ;
  691.         TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  692.         TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
  693.         TIM_OCInitStructure.TIM_Pulse = 0;      
  694.         TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  695.         TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
  696.         TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
  697.         TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;       
  698.   TIM_OC1Init(TIM8, &TIM_OCInitStructure);       
  699.         TIM_OC2Init(TIM8, &TIM_OCInitStructure);       
  700.         TIM_OC3Init(TIM8, &TIM_OCInitStructure);       


  701.   //Automatic Output enable, Break, dead time and lock configuration
  702.   TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
  703.   TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
  704.   TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
  705.   TIM_BDTRInitStructure.TIM_DeadTime = 80;
  706.   TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
  707.   TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
  708.   TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
  709.   TIM_BDTRConfig(TIM8, &TIM_BDTRInitStructure);
  710.        
  711.   //TIM8 counter enable
  712.   TIM_Cmd(TIM8, ENABLE);

  713.   //Main Output Enable
  714.   TIM_CtrlPWMOutputs(TIM8, ENABLE);

  715. }       
  716. void Timer2_Config(void)//Main(loop) Timer configuration
  717. {
  718.         //Timer2 Config
  719.         TIM2->PSC = 71;         // Set prescaler (PSC + 1)
  720.         TIM2->ARR = 2000;           // Auto reload value 2000
  721.         TIM2->DIER = TIM_DIER_UIE; // Enable update interrupt (timer level)
  722.         TIM2->CR1 = TIM_CR1_CEN;   // Enable timer
  723.         NVIC_EnableIRQ(TIM2_IRQn); // Enable interrupt from TIM2 (NVIC level)
  724. }

  725. void TIM2_IRQHandler(void)
  726. {
  727. if(TIM2->SR & TIM_SR_UIF) // if UIF flag is set
  728.   {
  729.   TIM2->SR &= ~TIM_SR_UIF; // clear UIF flag  
  730.         stop=1;
  731.   }
  732. }

  733. void Timer5_Config(void)//Pitch Timer configuration
  734. {       
  735.         //PWM pin config
  736.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;
  737.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  738.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  739.   GPIO_Init(GPIOA, &GPIO_InitStructure);
  740.        
  741.         //Timer5 Config
  742.                 TIM_TimeBaseInitStructure.TIM_Prescaler = 5; // Period*Prescaler=24'000'000Hz  //2400 1s
  743.   TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
  744.   TIM_TimeBaseInitStructure.TIM_Period = 1000; //20000(presc=24)=50hz(servo signal)
  745.   TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV4;
  746.   TIM_TimeBaseInit(TIM5, &TIM_TimeBaseInitStructure);       
  747.        
  748.         //PWM Config on Timer5 CH3
  749.         TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1 ;
  750.         TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  751.         TIM_OCInitStructure.TIM_Pulse = 0;      
  752.         TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
  753.   TIM_OC3Init(TIM5, &TIM_OCInitStructure);
  754.         //PWM Config on Timer5 CH2
  755.         TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1 ;
  756.         TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  757.         TIM_OCInitStructure.TIM_Pulse = 0;      
  758.         TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
  759.   TIM_OC2Init(TIM5, &TIM_OCInitStructure);       
  760.         //PWM Config on Timer5 CH1
  761.         TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1 ;
  762.         TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  763.         TIM_OCInitStructure.TIM_Pulse = 0;      
  764.         TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
  765.   TIM_OC1Init(TIM5, &TIM_OCInitStructure);               
  766.        

  767. }       

  768. void Timer4_Config(void)//Pitch Timer configuration
  769. {       
  770.         //PWM pin config
  771.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_7 | GPIO_Pin_6;
  772.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  773.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  774.   GPIO_Init(GPIOB, &GPIO_InitStructure);
  775.         //Timer4 Config

  776.         TIM_TimeBaseInitStructure.TIM_Prescaler = 5; // Period*Prescaler=24'000'000Hz  //2400 1s
  777.   TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
  778.   TIM_TimeBaseInitStructure.TIM_Period = 1000; //20000(presc=24)=50hz(servo signal)
  779.   TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV4;
  780. ……………………

  781. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
EVVGC _程序.rar (202.4 KB, 下载次数: 88)



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏4 分享淘帖 顶2 踩
回复

使用道具 举报

沙发
ID:285863 发表于 2018-10-31 07:56 | 只看该作者
不错的程序
回复

使用道具 举报

板凳
ID:801180 发表于 2020-7-10 18:01 | 只看该作者
开源项目的代码
回复

使用道具 举报

地板
ID:830831 发表于 2021-2-10 12:09 | 只看该作者
被控对象是啥呀? 舵机? 直流电机?还是无刷电机?  解释一下就好
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表