参加比赛的程序分享
单片机源程序如下:
- #include"sys.h"
- #include"delay.h"
- //系统头文件
- #include"timer.h"
- #include"gpioin.h"
- //定时器头文件
- #include"l298nin.h"
- #include"pwm.h"
- //电机驱动头文件
- #include"oled.h"
- #include"iic.h"
- //显示屏头文件
- #include"motor.h"
- #include"calculate&pi.h"
- //编码器头文件
- //全局变量
- int Vocality[2];//速度值 取值范围在0~95之间
- int Pwm[2];
- int Encoder_Date[2];
- extern int flagA,flagB,flagC,flagD;
- extern int finishflag8;
- int main()
- {
- Vocality[0] = 78;
- Vocality[1] = 78;
-
- delay_init(168);
- led_init();
-
- gpioin_init();
- l298nin_init();
-
- TIM3_Encoder_init();
- TIM4_Encoder_init();//编码器信号捕捉初始化
-
- TIM13_pwm_init(499,8399);
- TIM14_pwm_init(499,8399);//pwm输出初始化
-
- IIC_Init();
- OLED_Init();//oled初始化
-
- TIM2_init(499,8399);//每50ms进入一次定时器中断,扫描光电开关的情况
- TIM7_CAL_init(499,8399);//每0.1s进入一次定时器中断,进行pi控速
-
-
- while(1)
- {
- motor_straight();
- //由光电开关0进行卡边
- if(gdian0)
- {
- motor_only_left();
- while(gdian0);
- motor_straight();
- }
- //由光电开关1进行转弯
- if(!gdian1)
- {
- if(flagA) finishflag8 = 0;
- motor_only_right();
- while(!gdian1);
- delay(1500);
- if(!flagB)
- {
- delay(1200);
- }
- motor_straight();
- if(flagC) flagD = 1;
- if(flagB) flagC = 1;
- if(flagA) flagB = 1;
- flagA = 1;
- }
- }
-
- }
复制代码
pwm:
- //pwm调速函数,用于调整EnA和EnB的电平
- #include"pwm.h"
- #include"sys.h"
- void TIM13_pwm_init(u16 arr,u16 psc) //PF8引脚的控制即EnA
- {
- GPIO_InitTypeDef GPIO_Initstru;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitstru;
- TIM_OCInitTypeDef TIM_OCInitstru;//结构体指针变量
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM13,ENABLE);
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);//时钟初始化
-
- GPIO_PinAFConfig(GPIOF,GPIO_PinSource8,GPIO_AF_TIM13);//复用功能映射
-
- GPIO_Initstru.GPIO_Mode = GPIO_Mode_AF;
- GPIO_Initstru.GPIO_OType = GPIO_OType_PP;
- GPIO_Initstru.GPIO_Pin = GPIO_Pin_8;
- GPIO_Initstru.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOF,&GPIO_Initstru);//GPIO口初始化
-
- TIM_TimeBaseInitstru.TIM_Period = arr;
- TIM_TimeBaseInitstru.TIM_Prescaler = psc;
- TIM_TimeBaseInitstru.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM13,&TIM_TimeBaseInitstru);//定时器初始化
-
- TIM_OC1PreloadConfig(TIM13,TIM_OCPreload_Enable);
- TIM_ARRPreloadConfig(TIM13,ENABLE);//输出比较通道预装载
-
- TIM_OCInitstru.TIM_OCMode = TIM_OCMode_PWM1;
- TIM_OCInitstru.TIM_OCPolarity = TIM_OCPolarity_Low;
- TIM_OCInitstru.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitstru.TIM_Pulse = 0;
- TIM_OC1Init(TIM13,&TIM_OCInitstru);//输出比较通道初始化
-
- TIM_Cmd(TIM13,ENABLE);//打开定时器
- }
-
-
-
- void TIM14_pwm_init(u16 arr,u16 psc) //PF9引脚的控制即EnB
- {
- GPIO_InitTypeDef GPIO_Initstru;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitstru;
- TIM_OCInitTypeDef TIM_OCInitstru;//结构体指针变量
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14,ENABLE);
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);//时钟初始化
-
- GPIO_PinAFConfig(GPIOF,GPIO_PinSource9,GPIO_AF_TIM14);//复用功能映射
-
- GPIO_Initstru.GPIO_Mode = GPIO_Mode_AF;
- GPIO_Initstru.GPIO_OType = GPIO_OType_PP;
- GPIO_Initstru.GPIO_Pin = GPIO_Pin_9;
- GPIO_Initstru.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOF,&GPIO_Initstru);//GPIO口初始化
-
- TIM_TimeBaseInitstru.TIM_Period = arr;
- TIM_TimeBaseInitstru.TIM_Prescaler = psc;
- TIM_TimeBaseInitstru.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM14,&TIM_TimeBaseInitstru);//定时器初始化
-
- TIM_OC1PreloadConfig(TIM14,TIM_OCPreload_Enable);
- TIM_ARRPreloadConfig(TIM14,ENABLE);//输出比较通道预装载
-
- TIM_OCInitstru.TIM_OCMode = TIM_OCMode_PWM1;
- TIM_OCInitstru.TIM_OCPolarity = TIM_OCPolarity_Low;
- TIM_OCInitstru.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitstru.TIM_Pulse = 0;
- TIM_OC1Init(TIM14,&TIM_OCInitstru);//输出比较通道初始化
-
- TIM_Cmd(TIM14,ENABLE);//打开定时器
- }
复制代码
所有资料51hei提供下载:
32单片机.rar
(482.16 KB, 下载次数: 11)
|