您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 酒店餐饮 > 基于单片机智能停车场的设计程序
车位诱导程序:(1)系统初始化:一般情况,直接调用此函数将单片机系统时钟设置在72MHzSystemInit();程序中经常要用到延时函数,在这里为了提高CPU工作效率,不再使用死等待的演示方式,而是采用定时器作延时。voidDelay_Configuration(u8SYSCLK){RCC_APB1PeriphClockCmd(Delay_RCC_APB1Periph_TIMx,ENABLE);TIM_PrescalerConfig(Delay_TIMx,SYSCLK,TIM_PSCReloadMode_Update);}(2)超声波检测模块初始化//超声波软件系统初始化voidSonic_Init(void){Sonic_RCC_Configuration();Sonic_GPIO_Configuration();Sonic_NVIC_Configuration();Sonic_TIM_Configuration();}://打开超声波需要使用的系统资源的时钟voidSonic_RCC_Configuration(void){RCC_APB2PeriphClockCmd(SONIC_RCC_APB2Periph_GPIOx_OUT|SONIC_RCC_APB2Periph_GPIOx_IN,ENABLE);//打开时钟RCC_APB1PeriphClockCmd(SONIC_RCC_APB1Periph_TIMx,ENABLE);}//这里设置超声波检测所需要的引脚的相应功能voidSonic_GPIO_Configuration(void){GPIO_InitTypeDefGPIO_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);}//打开中断,设置中断优先级voidSonic_NVIC_Configuration(void){NVIC_InitTypeDefNVIC_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);}//定时器的初始化配置voidSonic_TIM_Configuration(void){TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;TIM_OCInitTypeDefTIM_OCInitStructure;TIM_ICInitTypeDefTIM_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-CCMR2OC4PE输出比较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);/*SelecttheTIM3InputTrigger:TI2FP2*/TIM_SelectInputTrigger(SONIC_TIMx,TIM_TS_TI2FP2);/*SelecttheslaveMode:ResetMode*/TIM_SelectSlaveMode(SONIC_TIMx,TIM_SlaveMode_Reset);/*EnabletheMaster/SlaveMode*/TIM_SelectMasterSlaveMode(SONIC_TIMx,TIM_MasterSlaveMode_Enable);/*TIMenablecounter*/TIM_Cmd(SONIC_TIMx,ENABLE);/*EnabletheCC2InterruptRequest*/TIM_ITConfig(SONIC_TIMx,TIM_IT_CC2,ENABLE);}//定时器中断里边的内容voidTIMx_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液晶显示模块初始化//初始化液晶,引脚初始化,发送初始化命令voidinitlcd12864(){GPIO_Config();write_com(0x30);delayms(1);write_com(0x0c);//整体显示开游标关游标位置关delayms(1);write_com(0x01);delayms(5);}//液晶引脚资源初始化voidGPIO_Config(void){GPIO_InitTypeDefGPIO_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOx|RCC_APB2Periph_GPIOy,ENABLE);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;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;GPIO_Init(GPIOy,&GPIO_InitStructure);}//打印显示汉字程序,包括设定显示坐标,显示内容voidhanzi(unsignedcharx,unsignedchary,unsignedchar*z)//显示汉字字符{unsignedcharpos;if(x==0)x=0x80;if(x==1)x=0x90;if(x==2)x=0x88;if(x==3)x=0x98;pos=x+y;write_com(pos);while(*z!='\0'){write_date(*z);z++;}}//主函数内容intmain(void){u8a,b,c,d;SystemInit();GPIO_Configuration();//串口引脚初始化USART_Configuration(9600);//串口初始化,方便系统调试Delay_Configuration(72);//系统延时函数初始化Sonic_Init();//超声波初始化initlcd12864();//液晶显示模块初始化printf(\nOK\n);//初始化结束串口打印OK信息while(1){a=Sonic1();b=Sonic2();c=Sonic3();d=Sonic4();if(a=100)hanzi(0,0,1号车位无车);elsehanzi(0,0,1号车位有车);if(b=100)hanzi(0,0,2号车位无车);elsehanzi(0,0,2号车位有车);if(c=100)hanzi(0,0,3号车位无车);elsehanzi(0,0,3号车位有车);if(d=100)hanzi(0,0,4号车位无车);elsehanzi(0,0,4号车位有车);delayms(100);}}
本文标题:基于单片机智能停车场的设计程序
链接地址:https://www.777doc.com/doc-5115081 .html