赞
踩
- #include "stm32f10x.h" // Device header设备头文件
- #include "led.h" // led灯头文件
- #include "deep.h" // 蜂鸣器头文件
- #include "delay.h" // 延时函数头文件
- #include "stdio.h" // C语言标准库文件
- #include "usart.h" // 串口通信头文件
- #include "dis.h" // 超声波测距头文件
-
- int main()
- {
- //定义变量
- uint32_t i=0;
- uint16_t j=0;
- uint32_t dis=0;
-
- //设备初始化
- led_init();
- myled_init();
- beep_init();
- usart_init();
- dis_init();
-
- //io操控
- while(1)
- {
- dis=get_dis();//获取距离
- printf("dis == %d mm\n",dis);//利用串口通信,让芯片把处理过的数据传过来,打印在电脑上
-
- if(dis>2000){
- myled0_on();//第0号灯亮
- delay_ms(700);
- beep_time_fre(300,2000);
- myled0_off();//第0号灯灭
- }
- else if(dis>1000){
- myled1_on();//第1号灯亮
- delay_ms(500);
- beep_time_fre(300,2000);
- myled1_off();//第1号灯灭
- }
- else if(dis>500){
- myled2_on();//第2号灯亮
- delay_ms(30);
- beep_time_fre(300,2000);
- myled2_off();//第2号灯灭
- }
- else{
- myled0_on();//三灯齐亮
- myled1_on();
- myled2_on();
- delay_ms(100);
- beep_time_fre(300,2000);
- myled0_off();//三灯齐灭
- myled1_off();
- myled2_off();
- }
-
- // delay_ms(100);
-
-
- //USART_SendData(USART1,'a');//这些是曾今测试串口通信的代码
- //j=USART_ReceiveData(USART1);
- //USART_SendData(USART1,j);
-
- //printf("apple tree \n");
- //printf("%d",i);
- //delay_ms(500);
- }
-
- }
-

这是led灯的内容
先是对芯片GPIO参进行时钟使能,然后进行参数配置,最后对该引口设置高电平输出还是低电平输出,因为led灯
- #include "stm32f10x.h" // Device header
-
- void led_init(void){
- //时钟使能
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
-
- //参数配置
- GPIO_InitTypeDef a;
- a.GPIO_Mode=GPIO_Mode_Out_PP;
- a.GPIO_Pin=GPIO_Pin_13;
- a.GPIO_Speed=GPIO_Speed_50MHz;
-
- GPIO_Init(GPIOC,&a);
- }
-
- //外接上三个LED灯
- //接pb5,pb6,pb7
- void myled_init(void){
- //时钟使能
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
-
- //参数配置
- GPIO_InitTypeDef a;
- a.GPIO_Mode=GPIO_Mode_Out_PP;
- a.GPIO_Pin=GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
- a.GPIO_Speed=GPIO_Speed_50MHz;
-
- GPIO_Init(GPIOB,&a);
-
- /*a.GPIO_Mode=GPIO_Mode_Out_PP;
- a.GPIO_Pin=GPIO_Pin_6;
- a.GPIO_Speed=GPIO_Speed_50MHz;
-
- GPIO_Init(GPIOC,&a);
-
- a.GPIO_Mode=GPIO_Mode_Out_PP;
- a.GPIO_Pin=GPIO_Pin_7;
- a.GPIO_Speed=GPIO_Speed_50MHz;
-
- GPIO_Init(GPIOC,&a);*/
- }
-
- void myled0_on()
- {
- GPIO_WriteBit(GPIOB, GPIO_Pin_5,0);//LED亮
- }
-
- void myled0_off()
- {
- GPIO_WriteBit(GPIOB, GPIO_Pin_5,1);//LED灭
- }
-
- void myled1_on()
- {
- GPIO_WriteBit(GPIOB, GPIO_Pin_6,0);//LED亮
- }
-
- void myled1_off()
- {
- GPIO_WriteBit(GPIOB, GPIO_Pin_6,1);//LED灭
- }
-
- void myled2_on()
- {
- GPIO_WriteBit(GPIOB, GPIO_Pin_7,0);//LED亮
- }
-
- void myled2_off()
- {
- GPIO_WriteBit(GPIOB, GPIO_Pin_7,1);//LED灭
- }
-
-

这是蜂鸣器的内容
- #include "stm32f10x.h" // Device header
- #include "delay.h"
-
- void beep_init()
- {
- //时钟使能
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
-
- //引脚配置
- GPIO_InitTypeDef b;
- b.GPIO_Mode=GPIO_Mode_Out_PP;
- b.GPIO_Pin=GPIO_Pin_1;
- b.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOB,&b);
- GPIO_WriteBit(GPIOB, GPIO_Pin_1,1);//关
- }
-
- void beep_on()
- {
- GPIO_WriteBit(GPIOB, GPIO_Pin_1,0);//开
- }
-
- void beep_off()
- {
- GPIO_WriteBit(GPIOB, GPIO_Pin_1,1);//关
- }
-
- void beep_fre(uint32_t fre)
- {
- uint32_t beep_time = 500000/fre;
- GPIO_WriteBit(GPIOB, GPIO_Pin_1,0);//beep开
- delay_us(beep_time);
- GPIO_WriteBit(GPIOB, GPIO_Pin_1,1);//beep关
- delay_us(beep_time);
- }
- void beep_time_fre(uint32_t time,uint32_t fre)
- {
- uint32_t beep_time = 500000/fre;
- while(time--){
- GPIO_WriteBit(GPIOB, GPIO_Pin_1,0);//beep开
- delay_us(beep_time);
- GPIO_WriteBit(GPIOB, GPIO_Pin_1,1);//beep关
- delay_us(beep_time);
- }
- }
-

这是串口通信的内容
- #include "stm32f10x.h" // Device header
- #include "stdio.h"
-
- int fputc(int ch,FILE *pf)//重定义printf,使其能够串口通信
- {USART_SendData(USART1,ch);
- while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
- return ch;
- }
-
- void usart_init(void){
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//USART1的时钟使能
-
- GPIO_InitTypeDef a;
- a.GPIO_Mode=GPIO_Mode_AF_PP;
- a.GPIO_Pin=GPIO_Pin_9|GPIO_Pin_10;
- a.GPIO_Speed=GPIO_Speed_50MHz;
-
- //串口初始化
- #include <stm32f10x.h>
- #include "RTE_Components.h" // Component selection
- USART_InitTypeDef b;
- b.USART_BaudRate=9600;
- b.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
- b.USART_Mode=USART_Mode_Tx;
- b.USART_Parity=USART_Parity_No;
- b.USART_StopBits=USART_StopBits_1;
- b.USART_WordLength=USART_WordLength_8b;
- USART_Init(USART1,&b);
-
- //让串口工作
- USART_Cmd(USART1,ENABLE);
- }

这时超声波的内容
- #include "stm32f10x.h" // Device header
- #include "delay.h"
- #include "stdio.h"
-
-
- void dis_init(){
- //时钟使能
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
-
- //参数配置
- GPIO_InitTypeDef c;
- c.GPIO_Mode=GPIO_Mode_Out_PP;
- c.GPIO_Pin=GPIO_Pin_11;
- c.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOA,&c);
-
- GPIO_InitTypeDef d;
- d.GPIO_Mode=GPIO_Mode_IPD;
- d.GPIO_Pin=GPIO_Pin_12;
- d.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOA,&d);
- }
-
- uint32_t get_dis(){
- uint32_t time=0;
- uint32_t dis=0;
- //测量数据(给传感器说)
- GPIO_WriteBit(GPIOA,GPIO_Pin_11,0);
- GPIO_WriteBit(GPIOA,GPIO_Pin_11,1);
- delay_us(12);
- GPIO_WriteBit(GPIOA,GPIO_Pin_11,0);
-
- //传感器测量数据开始,但需要时间
- while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_12)==0);
-
- //传感器高电平持续时间来告诉我们距离有多远
- while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_12)==1)
- {
- //测量PA12高电平的时长
- delay_us(50);
- time++;
- }
-
- dis=(34)*time/2; //声波在空气中传播是340米/秒
- return dis;
- }
-
-

这是延时函数的内容
使用了val寄存器,类似于51单片机的中断
- #include "stm32f10x.h" // Device header
-
-
- void delay_ms(uint32_t time)
- {
- //数 time=SystemCoreClock/100
- while(time--){
- SysTick->CTRL=0;
- SysTick->LOAD= SystemCoreClock/1000;
- SysTick->VAL=0;
- SysTick->CTRL=5; //开始工作,速度72MHz,1也可以,但要除以8.。。。0101
- while((SysTick->CTRL&0x00010000)==0);//等待val寄存器溢出,会自动把CTRL的第16位置1;
- SysTick->CTRL=0;
- }
- //小数据转大数据必会溢出
- }
-
- void delay_us(uint32_t time)
- {
- //数 time=SystemCoreClock/100
- while(time--){
- SysTick->CTRL=0;
- SysTick->LOAD= SystemCoreClock/1000000;
- SysTick->VAL=0;
- SysTick->CTRL=5; //开始工作,速度72MHz,1也可以,但要除以8
- while((SysTick->CTRL&0x00010000)==0);//等待val寄存器溢出,会自动把CTRL的第16位置1;
- SysTick->CTRL=0;
- }
- //小数据转大数据必会溢出
- }

最后成品就是这样
车载超声波测距
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。