#ads1115驱动程序 STM32
#include "main.h"
#include "cmsis_os.h"
#include "adc.h"
#include "dma.h"
#include "usart.h"
#include "gpio.h"
#define SET_BSDA HAL_GPIO_WritePin(BSDA_GPIO_Port, BSDA_Pin, GPIO_PIN_SET)
#define SET_BSCL HAL_GPIO_WritePin(BSCL_GPIO_Port, BSCL_Pin, GPIO_PIN_SET)
#define READ_BSDA HAL_GPIO_ReadPin(BSDA_GPIO_Port, BSDA_Pin)
#define READ_BSCL HAL_GPIO_ReadPin(BSCL_GPIO_Port, BSCL_Pin)
#define REG_Conversion 0x00
#define REG_Config 0x01
#define REG_L_thresh 0x02
#define REG_R_thresh 0x03
#define OS 0x01
#define MUX 0x04
#define PGA 0x02
#define MODE 0x01
#define DR 0x04
#define COMP_MODE 0x00
#define COMP_POL 0x00
#define COMP_LAT 0x00
#define COMP_QUE 0x03
#define config_MSB (OS << 7) | (MUX << 4) | (PGA << 1) | MODE
#define config_LSB (DR << 5) | (COMP_MODE << 4) | (COMP_POL << 3) | (COMP_LAT << 2) | COMP_QUE
#define Lo_thresh 0x8000
#define Hi_thresh 0x7FFF
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void MX_FREERTOS_Init(void);
/* USER CODE BEGIN PFP */
void HC595_send_data(uint8_t data, uint8_t HC595_num);
void delay_ms(uint32_t nms);
void delay_us(uint32_t nus);
void I2C_Start(void);
void I2C_Stop(void);
void I2C_SendByte(uint8_t data);
void I2C_ACK(void);
void I2C_NACK(void);
uint8_t I2C_WaitACK(void);
uint8_t I2C_ReadByte(uint8_t ack);
void ADS1115_Write(uint8_t Reg, uint8_t reg_MSB, uint8_t reg_LSB);
uint16_t ADS1115_ReadADC(void);
void ADS1115_config(uint8_t channel);
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_ADC1_Init();
MX_UART4_Init();
MX_UART5_Init();
MX_USART1_UART_Init();
MX_USART2_UART_Init();
MX_USART3_UART_Init();
/* USER CODE BEGIN 2 */
LL_GPIO_SetOutputPin(LEDA_GPIO_Port, LEDA_Pin);
LL_GPIO_SetOutputPin(LEDB_GPIO_Port, LEDB_Pin);
LL_GPIO_SetOutputPin(LEDC_GPIO_Port, LEDC_Pin);
/* USER CODE END 2 */
/* Init scheduler */
osKernelInitialize();
/* Call init function for freertos objects (in cmsis_os2.c) */
MX_FREERTOS_Init();
/* Start scheduler */
osKernelStart();
/* We should never get here as control is now taken by the scheduler */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
static uint8_t channel = 0;
ADS1115_config(channel);
delay_ms(7);
static uint16_t adc_val = 0;
adc_val = ADS1115_ReadADC();
static float voltage = 0;
//
voltage = (float) adc_val * 62.5 / 1000000.0;
//
channel++;
channel %= 4;
delay_ms(1000);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/* USER CODE END PFP */
void delay_us(uint32_t nus)
{
uint32_t fac_us = 0;
uint32_t temp;
uint8_t SYSCLK;
SYSCLK = HAL_RCC_GetHCLKFreq() / 1000 / 1000;
fac_us = SYSCLK / 8;
SysTick->LOAD = nus * fac_us;
SysTick->VAL = 0;
SysTick->CTRL = 1;
do
{
temp = SysTick->CTRL;
} while (temp & 1 && !(temp & (1 << 16)));
SysTick->CTRL = 0;
SysTick->VAL = 0;
}
void delay_ms(uint32_t nms)
{
for (uint32_t num = 0; num < nms; num++)
{
delay_us(1000);
}
}
void I2C_Start(void)
{
SET_BSDA;
SET_BSCL;
delay_us(5);
RESET_BSDA;
delay_us(5);
RESET_BSCL;
delay_us(5);
}
void I2C_Stop(void)
{
RESET_BSDA;
SET_BSCL;
delay_us(5);
SET_BSDA;
}
void I2C_SendByte(uint8_t data)
{
for (uint8_t i = 0; i < 8; i++)
{
if (data & 0x80)
{
SET_BSDA;
}
else
{
RESET_BSDA;
}
delay_us(5);
SET_BSCL;
delay_us(5);
RESET_BSCL;
if (i == 7)
{
SET_BSDA;
}
data <<= 1;
delay_us(5);
}
}
void I2C_ACK(void)
{
RESET_BSDA;
delay_us(5);
SET_BSCL;
delay_us(5);
RESET_BSCL;
delay_us(5);
SET_BSDA;
}
void I2C_NACK(void)
{
SET_BSDA;
delay_us(5);
SET_BSCL;
delay_us(5);
RESET_BSCL;
delay_us(5);
}
uint8_t I2C_WaitACK(void)
{
uint8_t ack;
SET_BSDA;
delay_us(5);
SET_BSCL;
delay_us(5);
if (GPIO_PIN_SET == READ_BSDA)
{
ack = 1;
}
else
{
ack = 0;
}
RESET_BSCL;
delay_us(5);
return ack;
}
uint8_t I2C_ReadByte(uint8_t ack)
{
uint8_t value = 0;
for (uint8_t i = 0; i < 8; i++)
{
value <<= 1;
SET_BSCL;
delay_us(5);
if (GPIO_PIN_SET == READ_BSDA)
{
value++;
}
RESET_BSCL;
delay_us(5);
}
if (0 == ack)
{
I2C_NACK();
}
else
{
I2C_ACK();
}
return value;
}
void ADS1115_Write(uint8_t Reg, uint8_t reg_MSB, uint8_t reg_LSB)
{
I2C_Start();
I2C_SendByte(0x90);
while (I2C_WaitACK());
I2C_SendByte(Reg);
while (I2C_WaitACK());
I2C_SendByte(reg_MSB);
while (I2C_WaitACK());
I2C_SendByte(reg_LSB);
while (I2C_WaitACK());
I2C_Stop();
}
uint16_t ADS1115_ReadADC(void)
{
uint16_t adc;
I2C_Start();
I2C_SendByte(0x90);
while(I2C_WaitACK());
I2C_SendByte(REG_Conversion);
while(I2C_WaitACK());
I2C_Stop();
delay_us(5);
I2C_Start();
I2C_SendByte(0x91);
while(I2C_WaitACK());
adc = I2C_ReadByte(1);
adc = adc << 8;
adc += I2C_ReadByte(1);
I2C_Stop();
return adc;
}
void ADS1115_config(uint8_t channel)
{
ADS1115_Write(REG_Config, config_MSB | (channel << 4), config_LSB);
}
/* USER CODE END 4 */