车位诱导程序:
(1)系统初始化:
一般情况,直接调用此函数将单片机系统时钟设置在72MHz
SystemInit();
程序中经常要用到延时函数,在这里为了提高CPU工作效率,不再使用死等待的演示方式,而是采用定时器作延时。
void Delay_Configuration(u8 SYSCLK)
{
RCC_APB1PeriphClockCmd(Delay_RCC_APB1Periph_TIMx, ENABLE);
TIM_PrescalerConfig(Delay_TIMx, SYSCLK, TIM_PSCReloadMode_Update);
}
(2)超声波检测模块初始化
//超声波软件系统初始化
void Sonic_Init(void)
{
Sonic_RCC_Configuration();
Sonic_GPIO_Configuration();
Sonic_NVIC_Configuration();
Sonic_TIM_Configuration();
}:
//打开超声波需要使用的系统资源的时钟
void Sonic_RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(SONIC_RCC_APB2Periph_GPIOx_OUT|SONIC_RCC_APB2Periph_GPIOx_IN,ENABLE);//打开时钟
RCC_APB1PeriphClockCmd(SONIC_RCC_APB1Periph_TIMx,ENABLE);
}
//这里设置超声波检测所需要的引脚的相应功能
void Sonic_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*············波形输出 驱动超声波···········*/
GPIO_InitStructure.GPIO_Pin=SONIC_GPIO_Pinx_OUT;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//
GPIO_Init(SONIC_GPIOx_OUT,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = SONIC_GPIO_Pinx_IN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SONIC_GPIOx_IN, &GPIO_InitStructure);
}
//打开中断,设置中断优先级
void Sonic_NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //设置优先级
NVIC_InitStructure.NVIC_IRQChannel=SONIC_TIMx_IRQn;//使能外部中断线1(IRQ通道)
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0; //先占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0; //从优先级
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//定时器的初始化配置
void Sonic_TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
/*通用定时器配置*/
TIM_TimeBaseStructure.TIM_Period =65535; //TIMx->ARR 设置自动装载值
TIM_TimeBaseStructure.TIM_Prescaler =72; //TIMx->PSC 设置预分频器值
TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1;//设置时钟频率
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; /选择计数器模式
TIM_TimeBaseInit(SONIC_TIMx, & TIM_TimeBaseStructure);
TIM_OCStructInit(&TIM_OCInitStructure); //默认参数
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;//模式1设置输出比较3模式
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //设置为输出
TIM_OCInitStructure.TIM_Pulse =21; //设置捕获比较寄存器4值即占空长度
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;//设置输出极性-高电平
TIM_OC4Init(SONIC_TIMx, &TIM_OCInitStructure); //初始化TIMx通道4.
TIM_OC4PreloadConfig(SONIC_TIMx, TIM_OCPreload_Enable); //TIMx->CCMR2 OC4PE 输出比较4的预加载使能位
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_PWMIConfig(SONIC_TIMx, &TIM_ICInitStructure);
/* Select the TIM3 Input Trigger: TI2FP2 */
TIM_SelectInputTrigger(SONIC_TIMx, TIM_TS_TI2FP2);
/* Select the slave Mode: Reset Mode */
TIM_SelectSlaveMode(SONIC_TIMx, TIM_SlaveMode_Reset);
/* Enable the Master/Slave Mode */
TIM_SelectMasterSlaveMode(SONIC_TIMx, TIM_MasterSlaveMode_Enable);
/* TIM enable counter */
TIM_Cmd(SONIC_TIMx, ENABLE);
/* Enable the CC2 Interrupt Request */
TIM_ITConfig(SONIC_TIMx, TIM_IT_CC2, ENABLE);
}
//定时器中断里边的内容
void TIMx_IRQHandler(void)
{
if(TIM_GetITStatus(SONIC_TIMx,TIM_IT_CC2)!=RESET)
{
TIM_ClearITPendingBit(SONIC_TIMx,TIM_IT_CC2);
value1=TIM_GetCapture2(SONIC_TIMx);
if(value1!=0)
{
value2=TIM_GetCapture1(SONIC_TIMx);
}
else
{
value1=0;
value2=0;
}
time=value2;
}
}
//12864液晶显示模块初始化
//初始化液晶,引脚初始化,发送初始化命令
void initlcd12864()
{
GPIO_Config();
write_com(0x30);
delayms(1);
write_com(0x0c);//整体显示开 游标关 游标位置关
delayms(1);
write_com(0x01);
delayms(5);
}
//液晶引脚资源初始化
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOx|RCC_APB2Periph_GPIOy,ENABLE);
单片机printf函数 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOx, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论