|
- #include "sys.h"
- #include "delay.h"
- #include "usart.h"
- #include "timer.h"
- #include "lcd.h"
- extern u8 TIM5CH1_CAPTURE_STA; //输入捕获状态
- extern u32 TIM5CH1_CAPTURE_VAL; //输入捕获值
-
-
- //超声波函数引脚初始化
-
- void ChaoShengBo_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);//使能GPIOF时钟
- //GPIOF9,F10初始化设置
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
- GPIO_Init(GPIOF, &GPIO_InitStructure);//初始化
-
- GPIO_ResetBits(GPIOF,GPIO_Pin_10);//GPIOF9,F10设置高,灯灭
- }
-
-
-
-
-
-
- int main(void)
- {
- u8 key; //保存键值
- long long High_TIME=0,distance=0;
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
- delay_init(168); //初始化延时函数
- uart_init(115200);//初始化串口波特率为115200
- ChaoShengBo_Init();
- LCD_Init(); //初始化LCD FSMC接口
- TIM5_CH1_Cap_Init(0XFFFFFFFF,84-1); //以1Mhz的频率计数
- while(1)
- {
- delay_ms(100);
- GPIO_SetBits(GPIOF,GPIO_Pin_10);
- delay_ms(1);
- GPIO_ResetBits(GPIOF,GPIO_Pin_10); // 给超声波一个高电平
- if(TIM5CH1_CAPTURE_STA&0X80) //成功捕获到了一次高电平
- {
- High_TIME=TIM5CH1_CAPTURE_STA&0X3F;
- High_TIME*=0XFFFFFFFF; //溢出时间总和
- High_TIME+=TIM5CH1_CAPTURE_VAL; //得到总的高电平时间 单位us
- distance=High_TIME*340/2/10000; //单位cm
- LCD_ShowString(30,40,210,24,24,"High_TIME:");
- LCD_ShowString(30,70,210,24,24,"distance:");
- LCD_ShowxNum(90,40,High_TIME,15,24,0);
- LCD_ShowxNum(150,70,distance,20,24,0);
- TIM5CH1_CAPTURE_STA=0; //开启下一次捕获
- }
- }
- }
复制代码
全部资料51hei下载地址:
点阵.7z
(1.52 MB, 下载次数: 39)
|
|