标题: STM32小车红外黑线循迹源程序 [打印本页]

作者: 512713659    时间: 2019-4-13 18:13
标题: STM32小车红外黑线循迹源程序
#include "interface.h"
void delay_init(void)
{
   SysTick->CTRL&=0xfffffffb;//控制寄存器,选择外部时钟即系统时钟的八分之一(HCLK/8;72M/8=9M)
}
//1us 延时函数
void Delay_us(u32 Nus)   
{   
SysTick->LOAD=Nus*9;          //时间加载    72M主频     
SysTick->CTRL|=0x01;             //开始倒数      
while(!(SysTick->CTRL&(1<<16))); //等待时间到达   
SysTick->CTRL=0X00000000;        //关闭计数器   
SysTick->VAL=0X00000000;         //清空计数器        
}
void Delayms(u32 Nms)
{
while(Nms--)
{
  Delay_us(1000);
}
}
void ServoInit(void)
{
  GPIO_InitTypeDef  GPIO_InitStructure;

RCC_APB2PeriphClockCmd(Servo_CLK , ENABLE);
GPIO_InitStructure.GPIO_Pin = Servo_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//配置GPIO端口速度
GPIO_Init(Servo_GPIO , &GPIO_InitStructure);

Servo_SET;//默认给高电位modfied by LC 2015.09.20 12:00
}
//红外光电对管初始化
void RedRayInit(void)
{
GPIO_InitTypeDef  GPIO_InitStructure;

RCC_APB2PeriphClockCmd(SEARCH_M_CLK , ENABLE);
GPIO_InitStructure.GPIO_Pin = SEARCH_M_PIN;//配置使能GPIO管脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//配置GPIO模式,输入上拉
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//配置GPIO端口速度
GPIO_Init(SEARCH_M_GPIO , &GPIO_InitStructure);

RCC_APB2PeriphClockCmd(SEARCH_R_CLK , ENABLE);
GPIO_InitStructure.GPIO_Pin = SEARCH_R_PIN;//配置使能GPIO管脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//配置GPIO模式,输入上拉
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//配置GPIO端口速度
GPIO_Init(SEARCH_R_GPIO , &GPIO_InitStructure);

RCC_APB2PeriphClockCmd(SEARCH_L_CLK , ENABLE);
GPIO_InitStructure.GPIO_Pin = SEARCH_L_PIN;//配置使能GPIO管脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//配置GPIO模式,输入上拉
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//配置GPIO端口速度
GPIO_Init(SEARCH_L_GPIO , &GPIO_InitStructure);

RCC_APB2PeriphClockCmd(VOID_R_CLK , ENABLE);
GPIO_InitStructure.GPIO_Pin = VOID_R_PIN;//配置使能GPIO管脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//配置GPIO模式,输入上拉
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//配置GPIO端口速度
GPIO_Init(VOID_R_GPIO , &GPIO_InitStructure);

RCC_APB2PeriphClockCmd(VOID_L_CLK , ENABLE);
GPIO_InitStructure.GPIO_Pin = VOID_L_PIN;//配置使能GPIO管脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//配置GPIO模式,输入上拉
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//配置GPIO端口速度
GPIO_Init(VOID_L_GPIO , &GPIO_InitStructure);
}
/**-------------------------------------------------------
  * @函数名 NVIC_TIM5Configuration
  * @功能   配置TIM5中断向量参数函数
  * @参数   无
  * @返回值 无
***------------------------------------------------------*/
static void NVIC_TIM2Configuration(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;
    /* Set the Vector Table base address at 0x08000000 */
    //NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0000);
    /* Enable the TIM5 gloabal Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}

void TIM2_Init(void)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    /* TIM2 clock enable */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
    /* Time base configuration */
    //这个就是自动装载的计数值,由于计数是从0开始的,周期为100us
    TIM_TimeBaseStructure.TIM_Period = (100 - 1);//10kHz
    // 这个就是预分频系数,当由于为0时表示不分频所以要减1
    TIM_TimeBaseStructure.TIM_Prescaler = (72 - 1);//1MHz
    // 高级应用本次不涉及。定义在定时器时钟(CK_INT)频率与数字滤波器(ETR,TIx)
    // 使用的采样频率之间的分频比例
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    //向上计数
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    //初始化定时器5
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
    /* Clear TIM5 update pending flag[清除TIM5溢出中断标志] */
    TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
    /* TIM IT enable */ //打开溢出中断
    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
    /* TIM5 enable counter */
    TIM_Cmd(TIM2, ENABLE);  //计数器使能,开始工作
    /* 中断参数配置 */
    NVIC_TIM2Configuration();
}
void LEDToggle(uint16_t Led)
{
    /* 指定管脚输出异或 1,实现对应的LED指示灯状态取反目的 */
GPIOE->ODR ^= Led;
//若要提高效率,建议直接调用 LEDnOBB = !LEDnOBB;
}



QQ图片20190413181205.png (27.79 KB, 下载次数: 72)

QQ图片20190413181205.png

STM32小车红外黑线循迹.rar

276.4 KB, 下载次数: 48, 下载积分: 黑币 -5






欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1