拓展项目三:由四位红外传感器控制电机-基础版

前言:

在前面的基础上,将传感器的管脚初始化,读取传感器状态,写逻辑进行控制。该传感器返回数字信号(0/1)。

一、4种输入模式:

       输入浮空        输入上拉        输入下拉        模拟输入

由于传感器返回的是0/1状态,采用输入浮空模式\上拉\下拉\。

二、电路图:

三、查阅相关手册

四个管脚没有特殊功能。

 四、外加代码

main.c

#include "motor.h"
#include "stdio.h"
#include "delay.h"
#include "stm32f10x.h"
#include "followline.h"
 u8 UART3_data,UART1_data;
 u8 UART3_rcv[20],UART3_rcv_count;
 u8 UART1_rcv[50],UART1_rcv_count,Uart1_finish;
int main(void)
{
	delay_init();
	GPIO_init();
	followline_init();
	TIME4_init(7199,10);
	while(1)
	{
	if(right_2)
	{
	  car_go(5);
	}
	else
	{
	car_stop();
	}
	}
}

followline.c

#include "followline.h"

void followline_init(void)
{
	
	//定义管脚的结构体
	GPIO_InitTypeDef  GPIO_InitStruct_C,GPIO_InitStruct_B;
	
	//配置PC15\14\13管脚的参数,用于驱动电机
	GPIO_InitStruct_C.GPIO_Mode=GPIO_Mode_IPU;
	GPIO_InitStruct_C.GPIO_Pin=GPIO_Pin_14|GPIO_Pin_15|GPIO_Pin_13;
	
	//配置PB12管脚的参数,用于驱动电机
	GPIO_InitStruct_C.GPIO_Mode=GPIO_Mode_IPU;
	GPIO_InitStruct_C.GPIO_Pin=GPIO_Pin_12;
  
	GPIO_Init(GPIOC, &GPIO_InitStruct_B);
	GPIO_Init(GPIOB, &GPIO_InitStruct_C);

}

follow.h

#ifndef __FOLLOWLINE_H__
#define __FOLLOWLINE_H__

#include "stm32f10x_gpio.h"

#define right_2 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_12)
#define right_1 GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_15)
#define left_2 GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_14)
#define left_1 GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_13)

void followline_init(void);
 
#endif

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值