標(biāo)題:
stm32循跡源碼
[打印本頁]
作者:
拼命的瘋子
時(shí)間:
2017-8-9 09:35
標(biāo)題:
stm32循跡源碼
#include <includes.h>
//int x=4,z=0,v=0;
//int flag=0;
/******************************************
*
* 延時(shí)程序 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; //右前,右后向左走
}
/**************************************************************************************************************************************
*
* 循跡函數(shù)
*
**************************************************************************************************************************************/
void xunji()
{
if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12)==0&&GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13)==1)//右邊沒有檢測到相當(dāng)于,右拐
{
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; //直走
}
}
/**************************************************************************************************************************************
*
* 走到交接處函數(shù)
*
**************************************************************************************************************************************/
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 ();
}
復(fù)制代碼
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1