[学习笔记] 【杰发科技AC7802x测评】点灯测试|串口测试|超声波

[复制链接]
 楼主| chenqiguang1998 发表于 2023-6-29 09:51 | 显示全部楼层 |阅读模式
<
#申请开发板# #申请原创# 新品AC7802x开发板尝鲜测试
一、编译环境搭建
1、MDK5 开发环境安装,我的开发环境是5.37。MDK5 安装包可以从keil 官网下载;
2、进行AutoChips.AC7802x_DFP.1.0.0.pack 驱动包安装,下载链接如下:
https://www.autochips.com/uploads/file/20230515/2023051513170052.rar
以上安装好后就可以进行开发了!
点灯与串口测试
在官方提供的资料中,已经有点灯和串口的历程了(例程下载中,只是我解压在了外面,好规避中文路径)

进行开发板连接

我使用的是SWD烧录器,当然wch-link、dap-link这些淘宝便宜的也完全可以胜任啦,直接将接口插入末端插槽即可。
烧录示例:点灯测试代码【01_GPIO_Basic】


烧录成功后,灯光开始闪烁。
烧录示例:串口测试代码【01_UART_Polling】


在输入框输入“123”,发送后,在接收框可以看到返回打印;
超声波测距部分代码
1、实现文件

  1. #include "string.h"

  2. #include "ac780x_timer.h"

  3. #include "ac780x_timer_reg.h"

  4. #include "ac780x_gpio.h"

  5. #include "HCSR04.h"

  6. uint64_t time = 0;//声明变量,用来计时

  7. uint64_t time_end = 0;//声明变量,存储回波信号时间

  8. void Timer0_Init ( void )

  9. {

  10. TIMER_ConfigType timerConfig0;

  11. memset ( &timerConfig0, 0, sizeof ( timerConfig0 ) );

  12. /* TIMER0配置函数 */

  13. timerConfig0.periodValue = Delay10ms; /*! 10ms计时 */

  14. timerConfig0.interruptEn = DISABLE; /*! 禁能定时器中断 */

  15. timerConfig0.linkModeEn = DISABLE; /*! 禁能链接模式 */

  16. timerConfig0.timerEn = DISABLE; /*! 先关掉定时器,以使得计时时间更精确,具体位置在GPIO中断处理函数中 */

  17. timerConfig0.callBack = NULL; /*! 回调函数为空 */

  18. TIMER_Init ( TIMER_CHANNEL0, &timerConfig0 ); /*! TIMER初始化函数生效 */

  19. mdelay ( 10 ); /*! 防止在定时器溢出后产生误差 */

  20. }

  21. uint32_t sonar_mm ()//测距并返回单位为毫米的距离结果

  22. {

  23. uint32_t Distance, Distance_mm = 0;//返回测距结果

  24. TRIG_ON;//输出高电平

  25. mdelay ( 15 );//延时15微秒

  26. TRIG_OFF;//输出低电平

  27. TIMER_SetChannel ( TIMER_CHANNEL0, ENABLE );

  28. while ( GET_ECHO_STS == FALSE );//等待低电平结束

  29. time = TIMER_GetCurrentValue ( TIMER_CHANNEL0 );//计时清零

  30. while ( GET_ECHO_STS == TRUE );//等待高电平结束

  31. TIMER_SetChannel ( TIMER_CHANNEL0, DISABLE ); /*!此处关闭定时器 */

  32. time_end = TIMER_GetCurrentValue ( TIMER_CHANNEL0 );

  33. time_end = time_end - time; //记录结束时的时间

  34. if ( time_end / 100 < 38 )//判断是否小于38毫秒,大于38毫秒的就是超时,直接调到下面返回0

  35. {

  36. Distance = ( time_end * 346 ) / 2;//计算距离,25°C空气中的音速为346m/s

  37. Distance_mm = Distance / 100;//因为上面的time_end的单位是10微秒,所以要得出单位为毫米的距离结果,还得除以100

  38. }

  39. return Distance_mm;

  40. }

  41. void gpio_init ( void )

  42. {

  43. GPIO_SetFunc ( TRIG_PORT, TRIG_PIN, GPIO_FUN0 );

  44. GPIO_SetFunc ( ECHO_PORT, ECHO_PIN, GPIO_FUN0 );

  45. GPIO_SetDir ( TRIG_PORT, TRIG_PIN, GPIO_OUT );

  46. GPIO_SetDir ( ECHO_PORT, ECHO_PIN, GPIO_IN );

  47. GPIO_SetPulldown ( ECHO_PORT, ECHO_PIN, ENABLE );

  48. }

  49. uint32_t HC_SR04_READ ( void )

  50. {

  51. gpio_init();

  52. Timer0_Init();

  53. return sonar_mm ();

  54. }


头文件

  1. #ifndef HCSR04_H_

  2. #define HCSR04_H_

  3. #ifdef __cplusplus

  4. extern "C" {

  5. #endif /*__cplusplus */

  6. /* =========================================== Includes =========================================== */

  7. #include "stdio.h"

  8. #include "stdint.h"

  9. /* ============================================ Define ============================================ */

  10. #define Timer_CLK (APB_BUS_FREQ) /*! 定义定时器时钟为APB */

  11. #define DelayS(n) (Timer_CLK*n-1) /*! n 秒超时值 */

  12. #define Delay1ms (Timer_CLK/1000-1) /*! 1ms超时值 */

  13. #define Delay10ms (Timer_CLK/100-1) /*! 10ms超时值 */

  14. /* =========================================== Typedef ============================================ */

  15. /* ========================================== Variables =========================================== */

  16. /* ==================================== Functions declaration ===================================== */

  17. void Timer0_Init ( void );

  18. uint32_t sonar_mm (void);

  19. void gpio_init ( void );

  20. uint32_t HC_SR04_READ ( void );

  21. /* ====================================== Functions define ======================================== */

  22. #define TRIG_PORT (GPIOA)

  23. #define TRIG_PIN (GPIO_PIN2)

  24. #define ECHO_PORT (GPIOA)

  25. #define ECHO_PIN (GPIO_PIN3)

  26. #define TRIG_ON do{GPIO_SetPinLevel(TRIG_PORT, TRIG_PIN, GPIO_LEVEL_HIGH);}while(0)

  27. #define TRIG_OFF do{GPIO_SetPinLevel(TRIG_PORT, TRIG_PIN, GPIO_LEVEL_LOW);}while(0)

  28. #define GET_ECHO_STS (GPIO_GetPinLevel(ECHO_PORT, ECHO_PIN))

  29. #ifdef __cplusplus

  30. }

  31. #endif /*__cplusplus */

  32. #endif /* HCSR04_H_ */



您需要登录后才可以回帖 登录 | 注册

本版积分规则

10

主题

59

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部

10

主题

59

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部