标题:
stm32循迹源码
[打印本页]
作者:
拼命的疯子
时间:
2017-8-9 09:35
标题:
stm32循迹源码
#include <includes.h>
//int x=4,z=0,v=0;
//int flag=0;
/******************************************
*
* 延时程序 ms
*
****************************************/
void delay(unsigned int n)
{
unsigned int y, k;
while(n--)
{
for(y = 0; y < 160; y++)
{
for(k = 0; k < 76; k++)
{
;
}
}
}
}
/**************************************************************************************************************************************
*
* 左拐弯 右拐弯
*
**************************************************************************************************************************************/
void rightbig () //右拐
{
GPIO_ResetBits(GPIOB, GPIO_Pin_11);
TIM2->CCR3=50; //PA^2 //左前
GPIO_ResetBits(GPIOB, GPIO_Pin_10 );
TIM2->CCR2=10; //PA^0 //右前
GPIO_ResetBits(GPIOA, GPIO_Pin_5 );
TIM2->CCR4=50; //PA^3 //左后
GPIO_ResetBits(GPIOA, GPIO_Pin_4 );
TIM2->CCR1=10; //PA^1 //右后
}
void leftbig() //左拐
{
GPIO_ResetBits(GPIOB, GPIO_Pin_11);
TIM2->CCR3=10; //PA^2 //左前
GPIO_ResetBits(GPIOB, GPIO_Pin_10 );
TIM2->CCR2=50; //PA^0 //右前
GPIO_ResetBits(GPIOA, GPIO_Pin_5 );
TIM2->CCR4=10; //PA^3 //左后
GPIO_ResetBits(GPIOA, GPIO_Pin_4 );
TIM2->CCR1=50; //PA^1 //右后
}
/**************************************************************************************************************************************
*
* 停止
*
**************************************************************************************************************************************/
//void stop()
//{
// GPIO_ResetBits(GPIOA ,GPIO_Pin_0 );
// GPIO_ResetBits(GPIOA ,GPIO_Pin_1 );
// GPIO_ResetBits(GPIOB ,GPIO_Pin_2 );
// GPIO_ResetBits(GPIOB ,GPIO_Pin_3 );
//}
/**************************************************************************************************************************************
*
* 左右轮子分别反向
*
**************************************************************************************************************************************/
void leftgorightback()
{
GPIO_ResetBits(GPIOB, GPIO_Pin_11);
TIM2->CCR3=80;
GPIO_ResetBits(GPIOA, GPIO_Pin_5 );
TIM2->CCR4=80; //左前,左后向前走
GPIO_SetBits(GPIOB, GPIO_Pin_10 );
TIM2->CCR2=80;
GPIO_SetBits(GPIOA, GPIO_Pin_4 );
TIM2->CCR1=80; //右前,右后向后走
}
void leftbackrightgo()
{
GPIO_SetBits(GPIOB, GPIO_Pin_11);
TIM2->CCR3=80;
GPIO_SetBits(GPIOA, GPIO_Pin_5 );
TIM2->CCR4=80; //左前,左后向后走
GPIO_ResetBits(GPIOB, GPIO_Pin_10 );
TIM2->CCR2=80;
GPIO_ResetBits(GPIOA, GPIO_Pin_4 );
TIM2->CCR1=80; //右前,右后向左走
}
/**************************************************************************************************************************************
*
* 循迹函数
*
**************************************************************************************************************************************/
void xunji()
{
if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12)==0&&GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13)==1)//右边没有检测到相当于,右拐
{
TIM2->CCR1 =10;
TIM2->CCR2 =10;
TIM2->CCR3 =99;
TIM2->CCR4 =99; //右拐
}
if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12)==1&&GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13)==0)//右边没检测到,左边黑线,左拐
{
TIM2->CCR3 =10;
TIM2->CCR4 =10;
TIM2->CCR1 =99;
TIM2->CCR2 =99; //左拐
}
if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12)==0&&GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13)==0)//都有,直走
{
TIM2->CCR1 =50;
TIM2->CCR2 =50;
TIM2->CCR3 =50;
TIM2->CCR4 =50; //直走
}
}
/**************************************************************************************************************************************
*
* 走到交接处函数
*
**************************************************************************************************************************************/
void begin()
{
if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_2)==1)
{
TIM2->CCR1 =10;
TIM2->CCR2 =10;
TIM2->CCR3 =99;
TIM2->CCR4 =99; //右拐
delay (10);
}
}
void while_1()
{
// rightbig ();
// leftbig ();
// leftgorightback();
leftbackrightgo();
// xunji ();
}
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1