單片機源程序如下:
- #include "stm32f10x.h"
- //#include"delay.h" //延時函數
- #include "usart.h"
- #include "pid.h"
- #include"pwm.h"
- #include "enc.h"
- #include "DataScope_DP.h"
- int pwm1,pwm2,pwm3; //PID計算后輸出的PWM占空比
- u8 flag=0;
- u16 Capture=0;
- u16 duty_TIM2=0;
- u8 RX_Command[1];
- extern PIDtypedef PID1,PID2,PID3;
- void USART_Configuration(void)
- {
- USART_InitTypeDef USART_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;//定義結構體
- NVIC_InitTypeDef NVIC_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE); //使能A端口、端口復用時鐘
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; // PA.8 PA.8,作為捕獲輸入脈沖中斷的標志,進入中斷點亮LED燈
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽輸出
- GPIO_Init(GPIOA, &GPIO_InitStructure); //結構體初始化
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; // PA.4 PA.4,作為定時器中斷的標志,20ms定時,進入中斷點亮LED燈
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽輸出
- GPIO_Init(GPIOA, &GPIO_InitStructure); //結構體初始化
- GPIO_SetBits(GPIOA,GPIO_Pin_4);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //USART1 TX
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復用推挽輸出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure); //A端口
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //USART1 RX
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //復用浮空輸入
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure); //A端口
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 |GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11
- | GPIO_Pin_12 | GPIO_Pin_13; // PE.8 控制電機正反轉
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽輸出
- GPIO_Init(GPIOE, &GPIO_InitStructure); //結構體初始化
- GPIO_SetBits(GPIOE,GPIO_Pin_8|GPIO_Pin_10 | GPIO_Pin_12); //置1
- GPIO_ResetBits(GPIOE,GPIO_Pin_9|GPIO_Pin_11 | GPIO_Pin_13); //置0
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //使能時鐘
- USART_InitStructure.USART_BaudRate = 115200; //波特率設為9600
- 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_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
- USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // 開啟串口接收中斷
- USART_Init(USART1, &USART_InitStructure);
- USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // 開啟串口接收中斷
-
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //設置優先級分組為2
- NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn; //USART1中斷
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占優先級
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //從優先級
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能
- NVIC_Init(&NVIC_InitStructure); //初始化
- USART_Cmd(USART1,ENABLE);
- }
- void PIDperiodinit(u16 arr,u16 psc)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE); //時鐘使能
-
- //定時器TIM6初始化
- TIM_TimeBaseStructure.TIM_Period = arr; //設置在下一個更新事件裝入活動的自動重裝載寄存器周期的值
- TIM_TimeBaseStructure.TIM_Prescaler =psc; //設置用來作為TIMx時鐘頻率除數的預分頻值
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //設置時鐘分割:TDTS = Tck_tim
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上計數模式
- TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure); //根據指定的參數初始化TIMx的時間基數單位
- TIM_ClearITPendingBit(TIM6, TIM_IT_Update); //清除中斷標志位
-
- TIM_ITConfig(TIM6,TIM_IT_Update,ENABLE ); //使能指定的TIM6中斷,允許更新中斷
- //中斷優先級NVIC設置
- NVIC_InitStructure.NVIC_IRQChannel = TIM6_IRQn; //TIM6中斷
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占優先級1級
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //從優先級1級
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
- NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
- TIM_Cmd(TIM6, ENABLE); //放在主程序中使能
- }
- int curcount2=0;
- int curcount1=0;
- int main(void)
- {
-
- int i=0;
- int j=0;
- int t[20]={0,10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190};
- int n[20]={0};
- u8 jishu=0;
- int pwm=0;
- int sum=0;
- int N1_avr=0;
- int curcount3=0;
- int N1=0;
- int N2=0;
- int N3=0;
-
- USART_Configuration();
- ENC_Init_TIM2(); // 1號電機
- TIM3_PWM_configure(1000-1,0); //PWM輸出,1000HZ
- // ENC_Init_TIM4(); // 2號電機
- // ENC_Init_TIM5(); // 3號電機
-
- // incPIDinit(); //初始化,將PID參數置0
- // PID_setpoint(&PID1,50); //設定1號電機轉速
- // PID_setpoint(&PID2,50); //設定2號電機轉速
- // PID_setpoint(&PID3,50); //設定3號電機轉速
- // set_speed(80);
- // PID_set(&PID1,5,0,0); //設定1號電機PID參數
- // PID_set(&PID2,2,0.5,0); //設定2號電機PID參數
- // PID_set(&PID3,2,0.5,0); //設定3號電機PID參數
- PIDperiodinit(2000-1,720-1); //PID 采樣定時器設定,采樣周期20ms
- while(1)
- {
- if(flag==1)
- {
-
- curcount1=TIM2->CNT-32768;
- TIM2->CNT=32768;
- jishu++;
- // curcount2=TIM4->CNT-32768;
- // TIM4->CNT=32768;
- //
- // curcount3=TIM5->CNT-32768;
- // TIM5->CNT=32768;
- //
- N1=15*(curcount1)/44; // N=125*curcount/188; N=125*curcount/1792;
- Data_Send_speed(curcount1,N1);
- // sum=sum+N1;
- // Data_Send_speed(t,N1);
- // printf("%d\n", sum);
- // if(jishu==10)
- // {
- // if(pwm>1000) { pwm=0; }
- // N1_avr=sum/10;
- // sum=0;
- // jishu=0;
- //
- // Data_Send_speed(pwm,N1_avr);
- // pwm=pwm+30;
- // TIM_SetCompare1(TIM3,pwm);
- //// printf("%d\n", N1_avr);
- //
- // }
- flag=0;
-
- }
- }
- }
- void TIM6_IRQHandler(void) // 采樣時間到,中斷處理函數
- {
-
- if (TIM_GetITStatus(TIM6, TIM_IT_Update) != RESET)//更新中斷
- {
- TIM_ClearITPendingBit(TIM6, TIM_IT_Update); //清除中斷標志位
- // GPIO_WriteBit(GPIOA,GPIO_Pin_4,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_4)));
- flag=1;
- }
- }
- void USART1_IRQHandler(void)
- {
- if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET) //判斷是否來中斷
- {
- USART_ClearITPendingBit(USART1,USART_IT_RXNE); //清除中斷標志位
- RX_Command[0]=(USART_ReceiveData(USART1));
- if(RX_Command[0]==0x0)
- {
- AIN1=0;
- AIN2=0;
- BIN1=0;
- BIN2=0;
- CIN1=0;
- CIN2=0;
- }
- if(RX_Command[0]==0x01)
- {
- AIN1=1;
- AIN2=0;
- BIN1=1;
- BIN2=0;
- CIN1=1;
- CIN2=0;
- }
- if(RX_Command[0]==0x02)
- {
- AIN1=0;
- AIN2=1;
- BIN1=0;
- BIN2=1;
- CIN1=0;
- CIN2=1;
- }
- if((RX_Command[0] > 0x32) && (RX_Command[0] < 0x64)) //判斷發送數據是否在0x32~0x64之間
- {
- u16 val;
- val=10*((RX_Command[0]) & (0x00FF)); //根據發送數據,計算占空比的實際值
- TIM_SetCompare1(TIM3,val);
- TIM_SetCompare2(TIM3,val);
- TIM_SetCompare3(TIM3,val); //設置占空比
- // USART_SendData(USART1,RX_Command[0]);
-
- }
- }
- }
復制代碼
所有資料51hei提供下載:
Orthogonal encoder.7z
(194.08 KB, 下載次數: 29)
2019-4-17 17:58 上傳
點擊文件名下載附件
電機控制 下載積分: 黑幣 -5
|