找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 4205|回復(fù): 0
收起左側(cè)

STM32小車紅外黑線循跡源程序

[復(fù)制鏈接]
ID:326666 發(fā)表于 2019-4-13 18:13 | 顯示全部樓層 |閱讀模式
#include "interface.h"
void delay_init(void)
{
   SysTick->CTRL&=0xfffffffb;//控制寄存器,選擇外部時(shí)鐘即系統(tǒng)時(shí)鐘的八分之一(HCLK/8;72M/8=9M)
}
//1us 延時(shí)函數(shù)
void Delay_us(u32 Nus)   
{   
SysTick->LOAD=Nus*9;          //時(shí)間加載    72M主頻     
SysTick->CTRL|=0x01;             //開始倒數(shù)      
while(!(SysTick->CTRL&(1<<16))); //等待時(shí)間到達(dá)   
SysTick->CTRL=0X00000000;        //關(guān)閉計(jì)數(shù)器   
SysTick->VAL=0X00000000;         //清空計(jì)數(shù)器        
}
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;//默認(rèn)給高電位modfied by LC 2015.09.20 12:00
}
//紅外光電對(duì)管初始化
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);
}
/**-------------------------------------------------------
  * @函數(shù)名 NVIC_TIM5Configuration
  * @功能   配置TIM5中斷向量參數(shù)函數(shù)
  * @參數(shù)   無
  * @返回值 無
***------------------------------------------------------*/
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 */
    //這個(gè)就是自動(dòng)裝載的計(jì)數(shù)值,由于計(jì)數(shù)是從0開始的,周期為100us
    TIM_TimeBaseStructure.TIM_Period = (100 - 1);//10kHz
    // 這個(gè)就是預(yù)分頻系數(shù),當(dāng)由于為0時(shí)表示不分頻所以要減1
    TIM_TimeBaseStructure.TIM_Prescaler = (72 - 1);//1MHz
    // 高級(jí)應(yīng)用本次不涉及。定義在定時(shí)器時(shí)鐘(CK_INT)頻率與數(shù)字濾波器(ETR,TIx)
    // 使用的采樣頻率之間的分頻比例
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    //向上計(jì)數(shù)
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    //初始化定時(shí)器5
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
    /* Clear TIM5 update pending flag[清除TIM5溢出中斷標(biāo)志] */
    TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
    /* TIM IT enable */ //打開溢出中斷
    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
    /* TIM5 enable counter */
    TIM_Cmd(TIM2, ENABLE);  //計(jì)數(shù)器使能,開始工作
    /* 中斷參數(shù)配置 */
    NVIC_TIM2Configuration();
}
void LEDToggle(uint16_t Led)
{
    /* 指定管腳輸出異或 1,實(shí)現(xiàn)對(duì)應(yīng)的LED指示燈狀態(tài)取反目的 */
GPIOE->ODR ^= Led;
//若要提高效率,建議直接調(diào)用 LEDnOBB = !LEDnOBB;
}



QQ圖片20190413181205.png

STM32小車紅外黑線循跡.rar

276.4 KB, 下載次數(shù): 48, 下載積分: 黑幣 -5

評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表