Job_SignsPads/STM32/Code/STM32F405/Application/drv_SGM58031.h
2025-04-22 10:29:37 +08:00

59 lines
2.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* @Description 使用IIC1 驱动SGM58031
* @Version: 1.0
* @Autor: lzc
* @Date: 2022-09-02 08:59:57
* @LastEditors: lzc
* @LastEditTime: 2023-08-09 08:48:03
*/
#ifndef __DRV_SGM58031_H
#define __DRV_SGM58031_H
#include "main.h"
#include "stm32f4xx.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_gpio.h"
// 输入电压 = AINP-AINN 默认AINP=AIN0 AINN=AIN1
// 输出数据速率 = 100HZ即10ms更新一次数据
/* SGM58031内部寄存器地址 */
#define Conversion_Reg 0x00 // AD值转换寄存器16bit数据默认值0x0000只读
#define Config_Reg 0x01 // 配置寄存器,默认0x8583可读可写
#define Lo_Thresh_Reg 0x02 // 比较器阈值下限默认0x8000
#define Hi_Thresh_Reg 0x03 // 比较器阈值上限默认0x7FFF
#define Config1_Reg 0x04 // 扩展配置寄存器默认0x0000
#define ChipID_Reg 0x05 // 芯片ID默认0x0080
#define GN_Trim1_Reg 0x06 // 增益修正默认0x03FA
// 输入输出设置io
#define SDA_IN() \
{ \
GPIOB->MODER &= ~(3 << (7 * 2)); \
GPIOB->MODER |= 0 << 7 * 2; \
}
#define SDA_OUT() \
{ \
GPIOB->MODER &= ~(3 << (7 * 2)); \
GPIOB->MODER |= 1 << 7 * 2; \
}
// IO操作函数
#define IIC_SCL PBout(6) // SCL
#define IIC_SDA PBout(7) // SDA
#define READ_SDA GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7) // 输入SDA
// IIC所有操作函数
void IIC_Init(void); // 初始化IIC的IO口
void IIC_Start(void); // 发送IIC开始信号
void IIC_Stop(void); // 发送IIC停止信号
void IIC_Send_Byte(u8 txd); // IIC发送一个字节
u8 IIC_Read_Byte(unsigned char ack); // IIC读取一个字节
u8 IIC_Wait_Ack(void); // IIC等待ACK信号
void IIC_Ack(void); // IIC发送ACK信号
void IIC_NAck(void); // IIC不发送ACK信号
float SGM58031_ReadADCValue(void);
char SGM58031_WriteConfig(uint16_t data);
void SGM58031_WriteData(uint8_t addr, uint16_t data, uint8_t device_addr);
uint16_t SGM58031_ReadData(uint8_t addr, uint8_t device_addr, uint16_t ByteNumToRead); // 寄存器地址,器件地址,要读的字节数
#endif