红外沿黑线程序c语言,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;

}

2019-4-13 18:12 上传

384cb4e21e9a76f136ee23e1e74ab0df.gif

1858ebd83462849153babc3551f87f86.gif

3f829e29e76caa8c3b1f630a6b27d58d.gif

95d1098ed7cbcc93a3c84393e0bd5f5e.gif

2019-4-13 18:10 上传

点击文件名下载附件

下载积分: 黑币 -5

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值