FW 심화 과정/[2] STM32심화실습

0816 조도센서 / 가속도온도 센서 실습 (MPU-6000)

천숭이 2022. 8. 16. 13:10
  • 조도센서 튀는 값 보정
	// 0816 cds 
	HAL_ADC_Start(&hadc1);
	float a =0.9, b=0.1, newValue;
	int cnt;
	newValue = HAL_ADC_GetValue(&hadc1);
	while(1)
	{
		newValue = newValue * a + HAL_ADC_GetValue(&hadc1) * b;
		HAL_Delay(10);
		cnt ++ ;
		if ((cnt % 50)==0) printf("newvalue : %d    oldvalue : %d \r\n", (uint32_t)(newValue+0.5), HAL_ADC_GetValue(&hadc1));
	}

0.5를 더해줌으로써 정수 캐스팅 (반올림)

 

 


  • dma를 사용해 조도 센서 값 받기
	while(1) // dma cds
	{
		HAL_ADC_Start_DMA(&hadc1, (uint32_t *)adc_dma_buf, sizeof(uint32_t)/sizeof(uint16_t));
	}

 

 

 


  • 연결여부(WHO_AM_I), 가속도 값, 온도 확인 
  • i2c_buf 배열의 사이즈는 8
x_high x_low y_high y_low z_high z_low temp_h temp_l

	uint8_t data = 0;
	uint8_t i2c_buf[8];
    
	HAL_I2C_Mem_Write(&hi2c1, 0x68<<1, 0x6B, 1, &data, 1, (uint32_t)-1);
	while(1)
	{
		// WHO_AM_I
		HAL_I2C_Mem_Read(&hi2c1, 0x68 << 1, 0x75, 1, &received_data, 1, (uint32_t)-1);
        // X, Y, Z, temp
		HAL_I2C_Mem_Read(&hi2c1, 0x68 << 1, 0x3B, 1, i2c_buf, sizeof(i2c_buf), (uint32_t)-1);
		
		int16_t temp=(i2c_buf[6]<<8)|i2c_buf[7];
		
		printf("x : %d\r\n", (int16_t)(i2c_buf[0]<<8)|i2c_buf[1]);
		printf("y : %d\r\n", (int16_t)(i2c_buf[2]<<8)|i2c_buf[3]);
		printf("z : %d\r\n", (int16_t)(i2c_buf[4]<<8)|i2c_buf[5]);
		printf("temp : %.2f\r\n", ((float)temp/340+36.53));
		HAL_Delay(100);
	}

 < 인자 살펴보기 >
HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress,
                                    uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
1. I2C_HandleTypeDef *hi2c : 구조체의 주소 
2. uint16_t DevAddress : 16bit device address 
   -> 0x68이 디바이스 주소
   -> 왼쪽1쉬프트 하는 이유 : 함수의 용법에 상관없이 해당 메모리에 쓰는 행위를 하면 맨끝 비트트 0이 되어야 함. 따라서 왼쪽 1쉬프트 함으로써 LSB 0으로 만들어 줌.
3. uint16_t MemAddress : 데이터 시트를 보고, 해당 주소값 작성
4. uint16_t MemAddSize : Byte 단위
5. uint8_t *pData : 해당 Memory address에 작성하려는 값
6. uint16_t Size : Byte 단위, Memory의 사이즈
7. uint32_t Timeout : 해당 시간동안 기다림

 

PWR_MGMT

 

// I2C센서를 작동하기 위해서는 0x6B 주소값에 0을 넣어야 한다.
// 미리 선언한 data=0의 주소를 인자로 넣는다
HAL_I2C_Mem_Write(&hi2c1, 0x68<<1, 0x6B, 1, &data, 1, (uint32_t)-1);

'FW 심화 과정 > [2] STM32심화실습' 카테고리의 다른 글

0819 조도센서, i2c, RTOS로 설계 실습  (0) 2022.08.19
FreeRTOS 코드 백업  (0) 2022.08.19
0811 PWM / FND  (0) 2022.08.11
0809 CubeMX 시작  (0) 2022.08.09
0808 까지 main 코드 백업  (0) 2022.08.09
0809 함수 및 라이브러리 생성  (0) 2022.08.09