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