- /* Includes ------------------------------------------------------------------*/
- #include "stm32f10x.h"
- #include "stm32f10x_tim.h"
- #include "stm32f10x_flash.h"
- #include "stm32f10x_exti.h"
- #include "stm32f10x_rcc.h"
- #include "systick.h"
- #include "stdio.h"
- #include "misc.h"
- #define infra1 GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_7);
- #define infra2 GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_8);
- #define tube1 GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_0);
- #define tube2 GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_1);
- #define tube3 GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_2);
- #define tube4 GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_3);
- TIM_OCInitTypeDef TIM1_OCInitStructure;
- extern FlagStatus Status;
- extern u16 TIM1Compare1[3] = {0,0x3FFF,0x1FFF};
- extern u16 TIM1Compare2[3] = {0x7FFF,0x3FFF,0x1FFF};
- extern u16 TIM1Compare3[3] = {0,0x3FFF,0x1FFF};
- extern u16 TIM1Compare4[3] = {0x7FFF,0x3FFF,0x1FFF};
- extern u16 tubecnt;
- extern u16 flag;
- extern int add;
- void GPIO_Configuration(void);
- void TIM_Configuration(void);
- void tim2_Configuration(void);
- void exti_Configuration(void);
- void nvic_Configuration(void);
- void zhi1(void);
- void zhi2(void);
- void zhi3(void);
- void zuoxiao(void);
- void zuoda(void);
- void youxiao(void);
- void youda(void);
- void xunji(void);
- u16 ceju(void);
- int main(void)
- {
- RCC_Configuration();
- GPIO_Configuration();
- TIM_Configuration();
- delay_init();
- while(1)
- {
- GPIO_ResetBits(GPIOE,GPIO_Pin_15);
- GPIO_ResetBits(GPIOB,GPIO_Pin_11);
- }
- }
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /*TIM1 PWM 输出:A9:CH1/A10:CH2 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- /*LED显示:LED5:PE15 / LED6:PB11 / LED7:PE14 / LED8:PB10 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_SetBits(GPIOB,GPIO_Pin_10);
- GPIO_SetBits(GPIOB,GPIO_Pin_11);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14|GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOE, &GPIO_InitStructure);
- GPIO_SetBits(GPIOE,GPIO_Pin_14);
- GPIO_SetBits(GPIOE,GPIO_Pin_15);
- /*红外管:infra1-> PC7, infra2-> PC8 浮空输入 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 ;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- /*光电管:tube1-> PC0, tube2-> PC1, tube3-> PC2, tube4-> PC3 浮空输入*/
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1|GPIO_Pin_2 | GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- /*超声波:Trig:PB9 Echo:PB8 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_ResetBits( GPIOB, GPIO_Pin_9);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //PB1接受采集上升沿 PB6低电平
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_ResetBits(GPIOB,GPIO_Pin_8);
- }
- void TIM_Configuration(void)
- {
- TIM_TimeBaseInitTypeDef TIM1_TimeBaseStructure;
- TIM_OCInitTypeDef TIM1_OCInitStructure;
- TIM_DeInit(TIM1);
- /* Time Base configuration */
- TIM1_TimeBaseStructure.TIM_Prescaler = 0x0; //设置了用来作为TIMx时钟频率除数的预分频值
- TIM1_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //选择了计数器模式
- TIM1_TimeBaseStructure.TIM_Period = 0xFFFF; //设置了在下一个更新事件装入活动的自动重装载寄存器周期 ARR
- TIM1_TimeBaseStructure.TIM_ClockDivision = 0x0; //设置了时钟分割
- TIM1_TimeBaseStructure.TIM_RepetitionCounter = 0x0; //设置了周期计数器值
- TIM_TimeBaseInit(TIM1,&TIM1_TimeBaseStructure);//根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位(5个参数)
- //TIM1基本计数器设置(设置PWM频率) //频率=TIM1_CLK/(ARR+1)
- /* Channel 1, 2,3 and 4 Configuration in PWM mode */
- TIM1_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
- TIM1_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM1_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
- TIM1_OCInitStructure.TIM_Pulse = 0x0;
- TIM1_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
- TIM1_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
- TIM1_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
- TIM1_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
- TIM_OC1Init(TIM1,&TIM1_OCInitStructure); //同TimeBaseStructure (8个参数) //TIM1_OC1模块设置(设置1通道占空比)
- TIM1_OCInitStructure.TIM_Pulse = 0x7fff;
- TIM_OC2Init(TIM1,&TIM1_OCInitStructure); //同TimeBaseStructure
- TIM1_OCInitStructure.TIM_Pulse = 0x0;
- TIM_OC3Init(TIM1,&TIM1_OCInitStructure); //同TimeBaseStructure
- TIM1_OCInitStructure.TIM_Pulse = 0x7fff;
- TIM_OC4Init(TIM1,&TIM1_OCInitStructure); //同TimeBaseStructure
- TIM_Cmd(TIM1,ENABLE); //先设置好了TimeBase、OC、BDTR 使能或者失能TIM1外设
- /* Main Output Enable */
- TIM_CtrlPWMOutputs(TIM1,DISABLE); //TIM1_CtrlPWMOutputs(ENABLE); 使能或者失能TIM1的主输出
- }
- void zhi1(void) //直走速度1
- {
- TIM_SetCompare1(TIM1,TIM1Compare1[0]);
- TIM_SetCompare2(TIM1,TIM1Compare2[0]);
- TIM_SetCompare3(TIM1,TIM1Compare3[0]);
- TIM_SetCompare4(TIM1,TIM1Compare4[0]);
- TIM_CtrlPWMOutputs(TIM1,ENABLE);
- }
- void zhi2(void) //直走速度2(慢)
- {
- TIM_SetCompare1(TIM1,TIM1Compare1[0]);
- TIM_SetCompare2(TIM1,TIM1Compare2[1]);
- TIM_SetCompare3(TIM1,TIM1Compare3[0]);
- TIM_SetCompare4(TIM1,TIM1Compare4[1]);
- TIM_CtrlPWMOutputs(TIM1,ENABLE);
- }
- void zhi(void) //直走速度3(很慢)
- {
- TIM_SetCompare1(TIM1,TIM1Compare1[0]);
- TIM_SetCompare2(TIM1,TIM1Compare2[2]);
- TIM_SetCompare3(TIM1,TIM1Compare3[0]);
- TIM_SetCompare4(TIM1,TIM1Compare4[2]);
- TIM_CtrlPWMOutputs(TIM1,ENABLE);
- }
- void zuoxiao(void) //左小转
- {
- TIM_SetCompare1(TIM1,TIM1Compare1[0]);
- TIM_SetCompare2(TIM1,TIM1Compare2[1]);
- TIM_SetCompare3(TIM1,TIM1Compare3[0]);
- TIM_SetCompare4(TIM1,TIM1Compare4[0]);
- TIM_CtrlPWMOutputs(TIM1,ENABLE);
- }
- void zuoda(void) //左大转
- {
- TIM_SetCompare1(TIM1,TIM1Compare1[0]);
- TIM_SetCompare2(TIM1,0);
- TIM_SetCompare3(TIM1,TIM1Compare3[0]);
- TIM_SetCompare4(TIM1,TIM1Compare4[0]);
- TIM_CtrlPWMOutputs(TIM1,ENABLE);
- }
- void youxiao(void) //右小转
- {
- TIM_SetCompare1(TIM1,TIM1Compare1[0]);
- TIM_SetCompare2(TIM1,TIM1Compare2[1]);
- TIM_SetCompare3(TIM1,TIM1Compare3[0]);
- TIM_SetCompare4(TIM1,TIM1Compare4[0]);
- TIM_CtrlPWMOutputs(TIM1,ENABLE);
- }
- void youda(void) //右大转
- {
- TIM_SetCompare1(TIM1,TIM1Compare1[0]);
- TIM_SetCompare2(TIM1,0);
- TIM_SetCompare3(TIM1,TIM1Compare3[0]);
- TIM_SetCompare4(TIM1,TIM1Compare4[0]);
- TIM_CtrlPWMOutputs(TIM1,ENABLE);
- }
- void xunji(void) //循迹
- {
- zhi1();
- if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_7)) youxiao();
- if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_8)) zuoxiao();
- if(1)
- {
- tubecnt++;
- zhi1();
- delay_ms(100);
- zuoda();
- delay_ms(100);
- }
- }
- u16 ceju(void)
- {
- u16 juli;
- flag=0;
- GPIO_SetBits( GPIOB, GPIO_Pin_9);
- TIM_Cmd(TIM2,ENABLE);
- while(juli==0);
- juli=add*3.4;;
- flag=0;
- return juli;
- }
- void tim2_Configuration(void)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2|RCC_APB1Periph_TIM3, ENABLE);
|