按键初始化就是GPIO_Pin_8 | GPIO_Pin_9初始化,
void Key_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
PPM_Init(); //PPM遥控器接收初始化
void PPM_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;//GPIO_Pin_0
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;//输入下拉
//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource8);//选择GPIO管脚用作外部中断线路,
//选择PIOB用作外部中断线源的GPIO端口,GPIO_PinSource8待设置的外部中断线路
EXTI_InitStructure.EXTI_Line = EXTI_Line8;//外部中断线8
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//设置EXTI线路为中断请求
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;//设置输入线路上升沿为中断请求
EXTI_InitStructure.EXTI_LineCmd = ENABLE;//用来定义选中线路的新状态。它可以被设为ENABLE或者DISABLE
EXTI_Init(&EXTI_InitStructure);//数初始化外设EXTI寄存器
//EXTI->IMR &=~EXTI_Line2;//关闭外部中断
}
HC_SR04_Init(); //超声波初始化
void HC_SR04_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;//下拉输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1);
EXTI_InitStructure.EXTI_Line = EXTI_Line1;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
EXTI->IMR &=~EXTI_Line1;//关闭外部中断???????
}
PWM_Init(); //PWM初始化—TIM4
void PWM_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
uint16_t prescalerValue = 0, ccr1_PWMVal = 0;
PWM_GPIO_Init();
prescalerValue = (u16) (SystemCoreClock / 1000000)-1;//10us
//-----TIM3定时配置-----//
TIM_TimeBaseStructure.TIM_Period = MAX_PWM; //40000/2M=20ms-->50Hz,
//从0开始计数,这个值被写入到Auto-Reload Register中,设置了在下一个更新事件装入活动的自动重装载寄存器周期的值。它的取值必须在0x0000和0xFFFF之间。
TIM_TimeBaseStructure.TIM_Prescaler = 0; //暂时不分频,设置了用来作为TIMx时钟频率除数的预分频值。它的取值必须在0x0000和0xFFFF之间。
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //时钟分割,设置了时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; //重复比较次数更新事件
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_PrescalerConfig(TIM3, prescalerValue, TIM_PSCReloadMode_Immediate);//预分频,现在计时器频率为20MHz
//-----PWM配置-----//
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式1-->向上计数为有效电平
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_Pulse = ccr1_PWMVal; //duty cycle
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OC1Init(TIM3, &TIM_OCInitStructure); //初始化外设TIM3 OC1-->Motor1
TIM_OC2Init(TIM3, &TIM_OCInitStructure); //初始化外设TIM3 OC2-->Motor2
TIM_OC3Init(TIM3, &TIM_OCInitStructure); //初始化外设TIM3 OC3-->Motor3
TIM_OC4Init(TIM3, &TIM_OCInitStructure); //初始化外设TIM3 OC4-->Motor4
TIM_ARRPreloadConfig(TIM3, ENABLE);//自动重载寄存器使能,下一个更新事件自动更新影子寄存器
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);//使能TIM3在CCR2上的预装载寄存器,在更新事件时,值才被写入到CCR
TIM_Cmd(TIM3, ENABLE);
PWM_Set(1000,1000,1000,1000);
}
其中:
void PWM_GPIO_Init(void )
{
GPIO_InitTypeDef GPIO_InitStructure;
//开始TIM4 的时钟 及GPIOB时钟 和AFIO时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//定时器3作为PWM输出
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO ,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
//设置PA5、PA66、PB0、PB1 为推挽输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;//
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//输出速率
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;//
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
而对于PWM_Set具体定义
函数名:void PWM_Set(const u16 pwm1, const u16 pwm2, const u16 pwm3, const u16 pwm4)
说明:PWM设置
入口:无
出口:无
备注:根据PWM的数值给与相应PWM信号灯的状态:
当PWM为0时,灯灭
不为0时,灯亮
************************************************************************************/
void PWM_Set(const uint16_t pwm1, const uint16_t pwm2, const uint16_t pwm3, const uint16_t pwm4)
{
TIM_SetCompare1(TIM3, pwm1);
TIM_SetCompare2(TIM3, pwm2);
TIM_SetCompare3(TIM3, pwm3);
TIM_SetCompare4(TIM3, pwm4);
}
Sensor_Init_Flag.NRF24L01_Okay=1;变量赋值
QuadInit();//初始化并显示
uint8 QuadShow_Cnt=0;
void QuadInit(void)
{
QuadShow_Cnt++;
delay_ms(100);
if(QuadShow_Cnt==4) {LCD_CLS();delay_ms(500);}
if(QuadShow_Cnt<=3)
{
LCD_P8x16Str(0,0,"NamelessCotrun");
if(Sensor_Init_Flag.NRF24L01_Okay==1) LCD_P8x16Str(10,2,"NRF Is Okay");
if(Sensor_Init_Flag.MPU6050_Okay==1) LCD_P8x16Str(10,4,"MPU Is Okay");
if(Sensor_Init_Flag.Mag_Okay==1) LCD_P8x16Str(10,6,"Mag Is Okay");
}
else
{
LCD_P8x16Str(0,0,"NamelessCotrun");
if(Sensor_Init_Flag.Baro_Okay==1) LCD_P8x16Str(10,2,"MS Is Okay");
}
这个初始化里面用了好多LCD里面的函数查查!
delay_ms(2000);//延时
/***********MPU6050初始化***************/
IIC_GPIO_Config();
InitMPU6050_GY86();
delay_ms(500);
IMU_Calibration();
其中第一个函数看定义应该是GPIO的IIC设置,但是具体定义是初始化GPIO,点亮GPIOB6和7管脚
void IIC_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
delay_ms(10);
GPIO_SetBits(GPIOB,GPIO_Pin_6 | GPIO_Pin_7);
}
Sensor_Init_Flag.MPU6050_Okay=1;
按键扫描:
void Key_Scan()
{
if(QuadKey1==0)
{
delay_ms(10);///消抖
if(QuadKey1==0)
{
while(QuadKey1==0);//向下翻页
Page_Number++;
if(Page_Number>=10) Page_Number=10;
LCD_CLS();//LCD复位
}
}
if(QuadKey2==0)
{
delay_ms(10);
if(QuadKey2==0)
{
while(QuadKey2==0);
Page_Number--;//向前翻页
if(Page_Number<=0) Page_Number=0;
LCD_CLS();
}
}
}
按键初始化就是GPIO_Pin_8 | GPIO_Pin_9初始化,
void Key_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
PPM_Init(); //PPM遥控器接收初始化
void PPM_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;//GPIO_Pin_0
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;//输入下拉
//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource8);//选择GPIO管脚用作外部中断线路,
//选择PIOB用作外部中断线源的GPIO端口,GPIO_PinSource8待设置的外部中断线路
EXTI_InitStructure.EXTI_Line = EXTI_Line8;//外部中断线8
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//设置EXTI线路为中断请求
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;//设置输入线路上升沿为中断请求
EXTI_InitStructure.EXTI_LineCmd = ENABLE;//用来定义选中线路的新状态。它可以被设为ENABLE或者DISABLE
EXTI_Init(&EXTI_InitStructure);//数初始化外设EXTI寄存器
//EXTI->IMR &=~EXTI_Line2;//关闭外部中断
}
HC_SR04_Init(); //超声波初始化
void HC_SR04_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;//下拉输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1);
EXTI_InitStructure.EXTI_Line = EXTI_Line1;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
EXTI->IMR &=~EXTI_Line1;//关闭外部中断???????
}
PWM_Init(); //PWM初始化—TIM4
void PWM_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
uint16_t prescalerValue = 0, ccr1_PWMVal = 0;
PWM_GPIO_Init();
prescalerValue = (u16) (SystemCoreClock / 1000000)-1;//10us
//-----TIM3定时配置-----//
TIM_TimeBaseStructure.TIM_Period = MAX_PWM; //40000/2M=20ms-->50Hz,
//从0开始计数,这个值被写入到Auto-Reload Register中,设置了在下一个更新事件装入活动的自动重装载寄存器周期的值。它的取值必须在0x0000和0xFFFF之间。
TIM_TimeBaseStructure.TIM_Prescaler = 0; //暂时不分频,设置了用来作为TIMx时钟频率除数的预分频值。它的取值必须在0x0000和0xFFFF之间。
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //时钟分割,设置了时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; //重复比较次数更新事件
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_PrescalerConfig(TIM3, prescalerValue, TIM_PSCReloadMode_Immediate);//预分频,现在计时器频率为20MHz
//-----PWM配置-----//
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式1-->向上计数为有效电平
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_Pulse = ccr1_PWMVal; //duty cycle
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OC1Init(TIM3, &TIM_OCInitStructure); //初始化外设TIM3 OC1-->Motor1
TIM_OC2Init(TIM3, &TIM_OCInitStructure); //初始化外设TIM3 OC2-->Motor2
TIM_OC3Init(TIM3, &TIM_OCInitStructure); //初始化外设TIM3 OC3-->Motor3
TIM_OC4Init(TIM3, &TIM_OCInitStructure); //初始化外设TIM3 OC4-->Motor4
TIM_ARRPreloadConfig(TIM3, ENABLE);//自动重载寄存器使能,下一个更新事件自动更新影子寄存器
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);//使能TIM3在CCR2上的预装载寄存器,在更新事件时,值才被写入到CCR
TIM_Cmd(TIM3, ENABLE);
PWM_Set(1000,1000,1000,1000);
}
其中:
void PWM_GPIO_Init(void )
{
GPIO_InitTypeDef GPIO_InitStructure;
//开始TIM4 的时钟 及GPIOB时钟 和AFIO时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//定时器3作为PWM输出
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO ,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
//设置PA5、PA66、PB0、PB1 为推挽输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;//
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//输出速率
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;//
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
而对于PWM_Set具体定义
函数名:void PWM_Set(const u16 pwm1, const u16 pwm2, const u16 pwm3, const u16 pwm4)
说明:PWM设置
入口:无
出口:无
备注:根据PWM的数值给与相应PWM信号灯的状态:
当PWM为0时,灯灭
不为0时,灯亮
************************************************************************************/
void PWM_Set(const uint16_t pwm1, const uint16_t pwm2, const uint16_t pwm3, const uint16_t pwm4)
{
TIM_SetCompare1(TIM3, pwm1);
TIM_SetCompare2(TIM3, pwm2);
TIM_SetCompare3(TIM3, pwm3);
TIM_SetCompare4(TIM3, pwm4);
}
Sensor_Init_Flag.NRF24L01_Okay=1;变量赋值
QuadInit();//初始化并显示
uint8 QuadShow_Cnt=0;
void QuadInit(void)
{
QuadShow_Cnt++;
delay_ms(100);
if(QuadShow_Cnt==4) {LCD_CLS();delay_ms(500);}
if(QuadShow_Cnt<=3)
{
LCD_P8x16Str(0,0,"NamelessCotrun");
if(Sensor_Init_Flag.NRF24L01_Okay==1) LCD_P8x16Str(10,2,"NRF Is Okay");
if(Sensor_Init_Flag.MPU6050_Okay==1) LCD_P8x16Str(10,4,"MPU Is Okay");
if(Sensor_Init_Flag.Mag_Okay==1) LCD_P8x16Str(10,6,"Mag Is Okay");
}
else
{
LCD_P8x16Str(0,0,"NamelessCotrun");
if(Sensor_Init_Flag.Baro_Okay==1) LCD_P8x16Str(10,2,"MS Is Okay");
}
这个初始化里面用了好多LCD里面的函数查查!
delay_ms(2000);//延时
/***********MPU6050初始化***************/
IIC_GPIO_Config();
InitMPU6050_GY86();
delay_ms(500);
IMU_Calibration();
其中第一个函数看定义应该是GPIO的IIC设置,但是具体定义是初始化GPIO,点亮GPIOB6和7管脚
void IIC_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
delay_ms(10);
GPIO_SetBits(GPIOB,GPIO_Pin_6 | GPIO_Pin_7);
}
Sensor_Init_Flag.MPU6050_Okay=1;
按键扫描:
void Key_Scan()
{
if(QuadKey1==0)
{
delay_ms(10);///消抖
if(QuadKey1==0)
{
while(QuadKey1==0);//向下翻页
Page_Number++;
if(Page_Number>=10) Page_Number=10;
LCD_CLS();//LCD复位
}
}
if(QuadKey2==0)
{
delay_ms(10);
if(QuadKey2==0)
{
while(QuadKey2==0);
Page_Number--;//向前翻页
if(Page_Number<=0) Page_Number=0;
LCD_CLS();
}
}
}
举报