当前位置:   article > 正文

用stm32f10x实现的车载倒车雷达系统

用stm32f10x实现的车载倒车雷达系统
  1. #include "stm32f10x.h" // Device header设备头文件
  2. #include "led.h" // led灯头文件
  3. #include "deep.h" // 蜂鸣器头文件
  4. #include "delay.h" // 延时函数头文件
  5. #include "stdio.h" // C语言标准库文件
  6. #include "usart.h" // 串口通信头文件
  7. #include "dis.h" // 超声波测距头文件
  8. int main()
  9. {
  10. //定义变量
  11. uint32_t i=0;
  12. uint16_t j=0;
  13. uint32_t dis=0;
  14. //设备初始化
  15. led_init();
  16. myled_init();
  17. beep_init();
  18. usart_init();
  19. dis_init();
  20. //io操控
  21. while(1)
  22. {
  23. dis=get_dis();//获取距离
  24. printf("dis == %d mm\n",dis);//利用串口通信,让芯片把处理过的数据传过来,打印在电脑上
  25. if(dis>2000){
  26. myled0_on();//第0号灯亮
  27. delay_ms(700);
  28. beep_time_fre(300,2000);
  29. myled0_off();//第0号灯灭
  30. }
  31. else if(dis>1000){
  32. myled1_on();//第1号灯亮
  33. delay_ms(500);
  34. beep_time_fre(300,2000);
  35. myled1_off();//第1号灯灭
  36. }
  37. else if(dis>500){
  38. myled2_on();//第2号灯亮
  39. delay_ms(30);
  40. beep_time_fre(300,2000);
  41. myled2_off();//第2号灯灭
  42. }
  43. else{
  44. myled0_on();//三灯齐亮
  45. myled1_on();
  46. myled2_on();
  47. delay_ms(100);
  48. beep_time_fre(300,2000);
  49. myled0_off();//三灯齐灭
  50. myled1_off();
  51. myled2_off();
  52. }
  53. // delay_ms(100);
  54. //USART_SendData(USART1,'a');//这些是曾今测试串口通信的代码
  55. //j=USART_ReceiveData(USART1);
  56. //USART_SendData(USART1,j);
  57. //printf("apple tree \n");
  58. //printf("%d",i);
  59. //delay_ms(500);
  60. }
  61. }

这是led灯的内容

先是对芯片GPIO参进行时钟使能,然后进行参数配置,最后对该引口设置高电平输出还是低电平输出,因为led灯

  1. #include "stm32f10x.h" // Device header
  2. void led_init(void){
  3. //时钟使能
  4. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
  5. //参数配置
  6. GPIO_InitTypeDef a;
  7. a.GPIO_Mode=GPIO_Mode_Out_PP;
  8. a.GPIO_Pin=GPIO_Pin_13;
  9. a.GPIO_Speed=GPIO_Speed_50MHz;
  10. GPIO_Init(GPIOC,&a);
  11. }
  12. //外接上三个LED灯
  13. //接pb5,pb6,pb7
  14. void myled_init(void){
  15. //时钟使能
  16. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  17. //参数配置
  18. GPIO_InitTypeDef a;
  19. a.GPIO_Mode=GPIO_Mode_Out_PP;
  20. a.GPIO_Pin=GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
  21. a.GPIO_Speed=GPIO_Speed_50MHz;
  22. GPIO_Init(GPIOB,&a);
  23. /*a.GPIO_Mode=GPIO_Mode_Out_PP;
  24. a.GPIO_Pin=GPIO_Pin_6;
  25. a.GPIO_Speed=GPIO_Speed_50MHz;
  26. GPIO_Init(GPIOC,&a);
  27. a.GPIO_Mode=GPIO_Mode_Out_PP;
  28. a.GPIO_Pin=GPIO_Pin_7;
  29. a.GPIO_Speed=GPIO_Speed_50MHz;
  30. GPIO_Init(GPIOC,&a);*/
  31. }
  32. void myled0_on()
  33. {
  34. GPIO_WriteBit(GPIOB, GPIO_Pin_5,0);//LED亮
  35. }
  36. void myled0_off()
  37. {
  38. GPIO_WriteBit(GPIOB, GPIO_Pin_5,1);//LED灭
  39. }
  40. void myled1_on()
  41. {
  42. GPIO_WriteBit(GPIOB, GPIO_Pin_6,0);//LED亮
  43. }
  44. void myled1_off()
  45. {
  46. GPIO_WriteBit(GPIOB, GPIO_Pin_6,1);//LED灭
  47. }
  48. void myled2_on()
  49. {
  50. GPIO_WriteBit(GPIOB, GPIO_Pin_7,0);//LED亮
  51. }
  52. void myled2_off()
  53. {
  54. GPIO_WriteBit(GPIOB, GPIO_Pin_7,1);//LED灭
  55. }

这是蜂鸣器的内容

  1. #include "stm32f10x.h" // Device header
  2. #include "delay.h"
  3. void beep_init()
  4. {
  5. //时钟使能
  6. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  7. //引脚配置
  8. GPIO_InitTypeDef b;
  9. b.GPIO_Mode=GPIO_Mode_Out_PP;
  10. b.GPIO_Pin=GPIO_Pin_1;
  11. b.GPIO_Speed=GPIO_Speed_50MHz;
  12. GPIO_Init(GPIOB,&b);
  13. GPIO_WriteBit(GPIOB, GPIO_Pin_1,1);//关
  14. }
  15. void beep_on()
  16. {
  17. GPIO_WriteBit(GPIOB, GPIO_Pin_1,0);//开
  18. }
  19. void beep_off()
  20. {
  21. GPIO_WriteBit(GPIOB, GPIO_Pin_1,1);//关
  22. }
  23. void beep_fre(uint32_t fre)
  24. {
  25. uint32_t beep_time = 500000/fre;
  26. GPIO_WriteBit(GPIOB, GPIO_Pin_1,0);//beep开
  27. delay_us(beep_time);
  28. GPIO_WriteBit(GPIOB, GPIO_Pin_1,1);//beep关
  29. delay_us(beep_time);
  30. }
  31. void beep_time_fre(uint32_t time,uint32_t fre)
  32. {
  33. uint32_t beep_time = 500000/fre;
  34. while(time--){
  35. GPIO_WriteBit(GPIOB, GPIO_Pin_1,0);//beep开
  36. delay_us(beep_time);
  37. GPIO_WriteBit(GPIOB, GPIO_Pin_1,1);//beep关
  38. delay_us(beep_time);
  39. }
  40. }

这是串口通信的内容

  1. #include "stm32f10x.h" // Device header
  2. #include "stdio.h"
  3. int fputc(int ch,FILE *pf)//重定义printf,使其能够串口通信
  4. {USART_SendData(USART1,ch);
  5. while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
  6. return ch;
  7. }
  8. void usart_init(void){
  9. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  10. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//USART1的时钟使能
  11. GPIO_InitTypeDef a;
  12. a.GPIO_Mode=GPIO_Mode_AF_PP;
  13. a.GPIO_Pin=GPIO_Pin_9|GPIO_Pin_10;
  14. a.GPIO_Speed=GPIO_Speed_50MHz;
  15. //串口初始化
  16. #include <stm32f10x.h>
  17. #include "RTE_Components.h" // Component selection
  18. USART_InitTypeDef b;
  19. b.USART_BaudRate=9600;
  20. b.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
  21. b.USART_Mode=USART_Mode_Tx;
  22. b.USART_Parity=USART_Parity_No;
  23. b.USART_StopBits=USART_StopBits_1;
  24. b.USART_WordLength=USART_WordLength_8b;
  25. USART_Init(USART1,&b);
  26. //让串口工作
  27. USART_Cmd(USART1,ENABLE);
  28. }

这时超声波的内容

  1. #include "stm32f10x.h" // Device header
  2. #include "delay.h"
  3. #include "stdio.h"
  4. void dis_init(){
  5. //时钟使能
  6. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  7. //参数配置
  8. GPIO_InitTypeDef c;
  9. c.GPIO_Mode=GPIO_Mode_Out_PP;
  10. c.GPIO_Pin=GPIO_Pin_11;
  11. c.GPIO_Speed=GPIO_Speed_50MHz;
  12. GPIO_Init(GPIOA,&c);
  13. GPIO_InitTypeDef d;
  14. d.GPIO_Mode=GPIO_Mode_IPD;
  15. d.GPIO_Pin=GPIO_Pin_12;
  16. d.GPIO_Speed=GPIO_Speed_50MHz;
  17. GPIO_Init(GPIOA,&d);
  18. }
  19. uint32_t get_dis(){
  20. uint32_t time=0;
  21. uint32_t dis=0;
  22. //测量数据(给传感器说)
  23. GPIO_WriteBit(GPIOA,GPIO_Pin_11,0);
  24. GPIO_WriteBit(GPIOA,GPIO_Pin_11,1);
  25. delay_us(12);
  26. GPIO_WriteBit(GPIOA,GPIO_Pin_11,0);
  27. //传感器测量数据开始,但需要时间
  28. while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_12)==0);
  29. //传感器高电平持续时间来告诉我们距离有多远
  30. while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_12)==1)
  31. {
  32. //测量PA12高电平的时长
  33. delay_us(50);
  34. time++;
  35. }
  36. dis=(34)*time/2; //声波在空气中传播是340米/秒
  37. return dis;
  38. }

这是延时函数的内容

使用了val寄存器,类似于51单片机的中断

  1. #include "stm32f10x.h" // Device header
  2. void delay_ms(uint32_t time)
  3. {
  4. //数 time=SystemCoreClock/100
  5. while(time--){
  6. SysTick->CTRL=0;
  7. SysTick->LOAD= SystemCoreClock/1000;
  8. SysTick->VAL=0;
  9. SysTick->CTRL=5; //开始工作,速度72MHz,1也可以,但要除以8.。。。0101
  10. while((SysTick->CTRL&0x00010000)==0);//等待val寄存器溢出,会自动把CTRL的第16位置1;
  11. SysTick->CTRL=0;
  12. }
  13. //小数据转大数据必会溢出
  14. }
  15. void delay_us(uint32_t time)
  16. {
  17. //数 time=SystemCoreClock/100
  18. while(time--){
  19. SysTick->CTRL=0;
  20. SysTick->LOAD= SystemCoreClock/1000000;
  21. SysTick->VAL=0;
  22. SysTick->CTRL=5; //开始工作,速度72MHz,1也可以,但要除以8
  23. while((SysTick->CTRL&0x00010000)==0);//等待val寄存器溢出,会自动把CTRL的第16位置1;
  24. SysTick->CTRL=0;
  25. }
  26. //小数据转大数据必会溢出
  27. }

最后成品就是这样

车载超声波测距

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/755463
推荐阅读
相关标签
  

闽ICP备14008679号