DPS310MOME

気圧と温度を読める。

画像1

画像2

/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "string.h"

/* USER CODE BEGIN 0 */
char buffer[1024];
void send_uart(char *s){
	HAL_UART_Transmit(&huart2, (uint8_t *) s, strlen(s), 2000);
}

const uint8_t PRODUCT_ID   = 0x0D;
const uint8_t TMP_COEF_SRCE= 0x28;
const uint8_t C0           = 0x10;
const uint8_t C1           = 0x11;
const uint8_t C00          = 0x13;
const uint8_t C10          = 0x15;
const uint8_t C01          = 0x18;
const uint8_t C11          = 0x1A;
const uint8_t C20          = 0x1C;
const uint8_t C21          = 0x1E;
const uint8_t C30          = 0x20;
const uint8_t PRS_CFG      = 0x06;
const uint8_t TMP_CFG      = 0x07;
const uint8_t MEAS_CFG     = 0x08;
const uint8_t CFG_REG      = 0x09;
const uint8_t PSR          = 0x00;
const uint8_t TMP          = 0x03;

uint8_t Data[3] = {0};
double c0 = 0;
double c1 = 0;
double c00 = 0;
double c10 = 0;
double c01 = 0;
double c11 = 0;
double c20 = 0;
double c21 = 0;
double c30 = 0;
double t_raw = 0;
double p_raw = 0;
double t_sc  = 0;
double p_sc  = 0;
double t_comp = 0; // degree
double p_comp = 0; // Pa


void write_spi(uint8_t Add, uint8_t write_val){
	HAL_GPIO_WritePin(SS_GPIO_Port, SS_Pin, GPIO_PIN_RESET);
	HAL_SPI_Transmit(&hspi1,(uint8_t *)&Add,1,100);
	HAL_SPI_Transmit(&hspi1,(uint8_t *)&write_val,1,100);
	HAL_GPIO_WritePin(SS_GPIO_Port, SS_Pin, GPIO_PIN_SET);
}

void read_n_spi(uint8_t Add, uint8_t n){
	Add = Add | 0b10000000;
	HAL_GPIO_WritePin(SS_GPIO_Port, SS_Pin, GPIO_PIN_RESET);
	HAL_SPI_Transmit(&hspi1,(uint8_t *)&Add,1,100);
	HAL_SPI_Receive(&hspi1,(uint8_t *)Data,n,100);
	HAL_GPIO_WritePin(SS_GPIO_Port, SS_Pin, GPIO_PIN_SET);
}

  /* USER CODE BEGIN 2 */
 read_n_spi(PRODUCT_ID, 1);
 sprintf(buffer, "Product ID is %d\n", Data[0]);
 send_uart(buffer);

 read_n_spi(TMP_COEF_SRCE, 1);
 sprintf(buffer, "TMP_COEF_SRCE is %d\n", (Data[0] & 0x80) / 128);
 send_uart(buffer);

 read_n_spi(C0, 2);
 c0 = Data[0] * 16 + (Data[1] & 0xF0) / 16;
 if(c0 > (2048 - 1)){c0 = c0 - 4096;}
 sprintf(buffer, "c0 is %lf\n", c0);
 send_uart(buffer);

 read_n_spi(C1, 2);
 c1 = (Data[0] & 0x0F) * 256 + Data[1];
 if(c1 > (2048 - 1)){c1 = c1 - 4096;}
 sprintf(buffer, "c1 is %lf\n", c1);
 send_uart(buffer);

 read_n_spi(C00, 3);
 c00 = Data[0] * 4096 + Data[1] * 16 + (Data[2] & 0xF0) / 16;
 if(c00 > (524288 - 1)){c00 = c00 - 1048576;}
 sprintf(buffer, "c00 is %lf\n", c00);
 send_uart(buffer);

 read_n_spi(C10, 3);
 c10 = (Data[0] & 0x0F) * 65536 + Data[1] * 256 + Data[2];
 if(c10 > (524288 - 1)){c10 = c10 - 1048576;}
 sprintf(buffer, "c10 is %lf\n", c10);
 send_uart(buffer);

 read_n_spi(C01, 2);
 c01 = Data[0] * 256 + Data[1];
 if(c01 > (32768 - 1)){c01 = c01 - 65536;}
 sprintf(buffer, "c01 is %lf\n", c01);
 send_uart(buffer);

 read_n_spi(C11, 2);
 c11 = Data[0] * 256 + Data[1];
 if(c11 > (32768 - 1)){c11 = c11 - 65536;}
 sprintf(buffer, "c11 is %lf\n", c11);
 send_uart(buffer);

 read_n_spi(C20, 2);
 c20 = Data[0] * 256 + Data[1];
 if(c20 > (32768 - 1)){c20 = c20 - 65536;}
 sprintf(buffer, "c20 is %lf\n", c20);
 send_uart(buffer);

 read_n_spi(C21, 2);
 c21 = Data[0] * 256 + Data[1];
 if(c21 > (32768 - 1)){c21 = c21 - 65536;}
 sprintf(buffer, "c21 is %lf\n", c21);
 send_uart(buffer);

 read_n_spi(C30, 2);
 c30 = Data[0] * 256 + Data[1];
 if(c30 > (32768 - 1)){c30 = c30 - 65536;}
 sprintf(buffer, "c30 is %lf\n", c30);
 send_uart(buffer);

 write_spi(PRS_CFG, 0x26);
 write_spi(TMP_CFG, 0xA0);
 write_spi(CFG_REG, 0b00000100);
 write_spi(MEAS_CFG, 0x07);
 HAL_Delay(100);

 /* USER CODE END 2 */

 /* Infinite loop */
 /* USER CODE BEGIN WHILE */
 while (1)
 {
   /* USER CODE END WHILE */


   /* USER CODE BEGIN 3 */
 	// calculate temperature
   read_n_spi(TMP, 3);
   t_raw = Data[0] * 65536 + Data[1] * 256 + Data[2];
   if(t_raw > (8388608 - 1)){t_raw = t_raw - 16777216;}
   t_sc = t_raw / 524288;
   t_comp = c0 * 0.5 + c1 * t_sc;

		// calculate pressure
		read_n_spi(PSR, 3);
		p_raw = Data[0] * 65536 + Data[1] * 256 + Data[2];
		if(p_raw > (8388608 - 1)){p_raw = p_raw - 16777216;}
		p_sc = p_raw / 1040384;
		p_comp = c00 + p_sc * (c10 + p_sc * (c20 + p_sc * c30)) + t_sc * c01 + t_sc * p_sc * (c11 + p_sc * c21);

		sprintf(buffer, "temperature(deg):%lf, pressure(Pa):%lf\n", t_comp, p_comp);
		send_uart(buffer);
 	HAL_Delay(200);
 }
 /* USER CODE END 3 */
}

この記事が気に入ったらサポートをしてみませんか?