单片机源程序如下:
- #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)
|