前言:
在前面的基礎上,將傳感器的管腳初始化,讀取傳感器狀態,寫邏輯進行控制,該傳感器回傳數字信號(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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/296948.html
標籤:其他
