单片机
返回首页

TCRT5000+STM32红外循迹小车程序

2020-08-11 来源:51hei

用正点原子的精英版作为主控,红外循迹用TCRT5000芯片。


单片机源程序如下:

/* 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);


        TIM_DeInit(TIM2);

        TIM_TimeBaseStructure.TIM_Period=72-1;

        TIM_TimeBaseStructure.TIM_Prescaler=15-1;

        TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

        TIM_TimeBaseInit(TIM2, & TIM_TimeBaseStructure);

        TIM_ClearFlag(TIM2,TIM_FLAG_Update);

    TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);

    TIM_Cmd(TIM2,DISABLE);

       

        TIM_DeInit(TIM3);

        TIM_TimeBaseStructure.TIM_Prescaler =72-1;  //设置了用来作为TIMx时钟频率除数的预分频值

    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;         //选择了计数器模式

    TIM_TimeBaseStructure.TIM_Period = 20-1;   //设置了15us在下一个更新事件装入活动的自动重装载寄存器周期  ARR

        TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; //设置了时钟分割

        TIM_TimeBaseInit(TIM3, & TIM_TimeBaseStructure);

        TIM_ClearFlag(TIM3,TIM_FLAG_Update);

    TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);

        TIM_Cmd(TIM3,DISABLE);

}


void exti_Configuration(void)

{

        EXTI_InitTypeDef EXTI_InitStructure;


        EXTI_DeInit();

        GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource8);


        EXTI_InitStructure.EXTI_Line = EXTI_Line8;

        EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;

        EXTI_InitStructure.EXTI_Trigger =EXTI_Trigger_Rising_Falling;

        EXTI_InitStructure.EXTI_LineCmd = ENABLE;

        EXTI_Init(&EXTI_InitStructure);          

}


void nvic_Configuration(void)

{

        NVIC_InitTypeDef NVIC_InitStructure;


        #ifdef  VECT_TAB_RAM  

          /* Set the Vector Table base location at 0x20000000 */

          NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);

        #else  /* VECT_TAB_FLASH  */

          /* Set the Vector Table base location at 0x08000000 */

         

        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

       

        NVIC_InitStructure.NVIC_IRQChannel =TIM2_IRQChannel;

        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;

        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

        NVIC_Init(&NVIC_InitStructure);


        NVIC_InitStructure.NVIC_IRQChannel =TIM3_IRQChannel;

        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;

        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

        NVIC_Init(&NVIC_InitStructure);


        NVIC_InitStructure.NVIC_IRQChannel =EXTI9_5_IRQChannel ;

        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =1;

        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

        NVIC_Init(&NVIC_InitStructure);


        #endif

……………………


进入单片机查看更多内容>>
相关视频
  • RISC-V嵌入式系统开发

  • SOC系统级芯片设计实验

  • 云龙51单片机实训视频教程(王云,字幕版)

  • 2022 Digi-Key KOL 系列: 你见过1GHz主频的单片机吗?Teensy 4.1开发板介绍

  • TI 新一代 C2000™ 微控制器:全方位助力伺服及马达驱动应用

  • MSP430电容触摸技术 - 防水Demo演示

精选电路图
  • 光控电路设计与分析

  • IGBT模块通过控制门极阻断过电流

  • CCFL的工作原理及电子驱动电路解析

  • 开关电源的基本组成及工作原理

  • 基于M66T旋律发​​生器的电路图解析

  • 基于CA3193的热电偶放大器电路

    相关电子头条文章