[AT32F423] 【AT-START-F423测评】+ I2C接口测试加速度传感器ADXL345

[复制链接]
4122|19
 楼主| WoodData 发表于 2023-11-11 18:05 | 显示全部楼层 |阅读模式
本次主要使用硬件I2C接口,通过读取加速度传感器ADXL345测试I2C的使用方法。
使用PB8和PB92个端口。
首先初始化I2C接口
  1. void drv_i2c_gpio_init( void )
  2. {
  3.     gpio_init_type gpio_initstructure;
  4.    
  5.     /* i2c periph clock enable */
  6.     crm_periph_clock_enable(CRM_I2C1_PERIPH_CLOCK, TRUE);
  7.     crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
  8.    
  9.     /* configure i2c pins: SCL=PB8,SDA=PB9 */
  10.     gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  11.     gpio_initstructure.gpio_mode           = GPIO_MODE_MUX;
  12.     gpio_initstructure.gpio_out_type       = GPIO_OUTPUT_OPEN_DRAIN;
  13.     gpio_initstructure.gpio_pull           = GPIO_PULL_UP;
  14.     gpio_initstructure.gpio_pins           = GPIO_PINS_8 | GPIO_PINS_9;
  15.     gpio_init(GPIOB, &gpio_initstructure);
  16.    
  17.     /* gpio configuration */
  18.     gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE8, GPIO_MUX_4);
  19.     gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE9, GPIO_MUX_4);
  20.    
  21.     hi2cx.i2cx = I2C1;
  22.     i2c_reset(I2C1);
  23.     /* config i2c */
  24.     i2c_init(I2C1, 0x0F, I2Cx_CLKCTRL);
  25.     i2c_own_address1_set(I2C1, I2C_ADDRESS_MODE_7BIT, 0XB0);
  26.     i2c_enable(I2C1,TRUE);
  27. }


I2C接口驱动,使用了SDK中的i2c_application.c和i2c_application.h。
  1. uint8_t I2C_WriteByte(uint8_t index,uint8_t dev_addr, uint8_t addr,uint8_t Byte)
  2. {
  3.     uint8_t reVal;
  4.     reVal = i2c_memory_write(&hi2cx, I2C_MEM_ADDR_WIDIH_8, dev_addr<<1, addr, &Byte, 1, 10000);
  5.     return reVal;
  6. }

  7. uint8_t I2C_ReadByte(uint8_t index,uint8_t dev_addr, uint8_t addr)
  8. {
  9.     uint8_t reVal;
  10.     i2c_memory_read(&hi2cx, I2C_MEM_ADDR_WIDIH_8, dev_addr<<1, addr, &reVal, 1, 10000);
  11.     return reVal;
  12. }


  13. uint8_t I2C_PageWrite(uint8_t index,uint8_t dev_addr, uint8_t addr,uint8_t *str,uint32_t num)
  14. {
  15.     uint8_t reVal;
  16.     reVal = i2c_memory_write(&hi2cx, I2C_MEM_ADDR_WIDIH_8, dev_addr<<1, addr, str, num, 10000);
  17.     return reVal;
  18. }

  19. uint8_t I2C_PageRead(uint8_t index,uint8_t dev_addr, uint8_t addr,uint8_t *str,uint32_t num)
  20. {
  21.     uint8_t reVal;
  22.     reVal = i2c_memory_read(&hi2cx, I2C_MEM_ADDR_WIDIH_8, dev_addr<<1, addr, str, num, 10000);
  23.     return reVal;
  24. }

1.jpg

下面是ADXL345的驱动
2.jpg
测试加速度读取数据。
6.jpg


在lcd屏幕上显示加速度数值。
3.jpg

4.jpg

5.jpg

1.gif


代码如下:
游客,如果您要查看本帖隐藏内容请回复


MacroKwan 发表于 2023-11-13 09:35 | 显示全部楼层
咔咔咔咔咔咔扩扩扩扩扩扩扩扩扩扩扩扩扩扩扩扩扩扩扩扩扩扩扩扩扩扩
单片小菜 发表于 2023-11-13 17:18 | 显示全部楼层
这个确实不错,想知道这个三轴传感器多少钱?
90houyidai 发表于 2023-11-14 22:50 | 显示全部楼层
403的I2C还没调出来,明天拿这个试试
cxj503 发表于 2023-11-20 14:58 | 显示全部楼层
I2C是个好东西
whp1202 发表于 2024-1-27 15:39 | 显示全部楼层
geraldbetty 发表于 2024-2-2 20:12 | 显示全部楼层
在AT-START-F423中,可能需要使用SDK提供的i2c_application.c文件来进行初始化。
gygp 发表于 2024-2-2 20:43 | 显示全部楼层
可以参考ADXL345的数据手册中的寄存器映射和操作方法。
qiufengsd 发表于 2024-2-2 21:14 | 显示全部楼层
设置I2C时钟速度、配置SDA和SCL引脚为I2C通信模式等。
geraldbetty 发表于 2024-2-2 21:45 | 显示全部楼层
需要注意ADXL345的电源和地线连接。
liszt99 发表于 2024-2-2 22:20 | 显示全部楼层
需要注意ADXL345的寄存器地址
大老师 发表于 2024-3-9 16:06 | 显示全部楼层
lixuewei520 发表于 2024-3-13 17:03 | 显示全部楼层
呐咯密密 发表于 2024-3-18 14:53 | 显示全部楼层
很不错的资料,下载查看一下
shihongxiao 发表于 2024-10-11 08:45 | 显示全部楼层
正在研究,学习学习。谢谢楼主分享
ifourcc 发表于 2024-12-4 01:06 | 显示全部楼层
探讨探讨探讨探讨探讨探讨探讨探讨ttt
mycmj80 发表于 2024-12-12 13:45 | 显示全部楼层
423的I2C还没调出来
owanzi 发表于 2025-4-8 15:13 | 显示全部楼层
感谢分享
DreamWanderer 发表于 2025-4-9 15:38 | 显示全部楼层
传感器的响应速率是多少
hupo99 发表于 2025-7-11 23:12 | 显示全部楼层
感谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则

127

主题

4784

帖子

28

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