2020-01-27 03:41 AM
I'm following the 'value' variable in the code below from the STM Studio interface. When no pressure is applied, the 'value' variable starts from a fixed number (3.62049). When I apply pressure to the Load-Cell, the digit of this value changes after the comma. How can I fix this. Please help.
CODE:
#include "main.h"
#include "stm32f4xx_hal.h"
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
uint8_t value=0;
uint8_t i = 0;
int32_t adc_value = 0;
int32_t c_value = 0;
uint8_t symb_sel;
uint8_t symbols[10] = {0x3f,0x6,0x5b,0x4f,0x66,0x6d,0x7d,0x7,0x7f,0x6f};
uint8_t s[3];
uint8_t temp;
GPIO_PinState dataadc=GPIO_PIN_SET;
#define pause for(int n=0;n<5;n++){};
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
while (1)
{
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13,GPIO_PIN_SET);
temp=value;
s[0]=0;s[1]=0;s[2]=0;
while (temp>=100){temp=temp-100;s[2]++;}
while (temp>=10){temp=temp-10;s[1]++;}
s[0]=temp;
symb_sel++;
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_2,GPIO_PIN_SET);
if(symb_sel>3)symb_sel=1;
if(symb_sel==1)HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0,GPIO_PIN_RESET);
if(symb_sel==2)HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1,GPIO_PIN_RESET);
if(symb_sel==3)HAL_GPIO_WritePin(GPIOC, GPIO_PIN_2,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, 0x1&symbols[s[symb_sel-1]]);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, 0x2&symbols[s[symb_sel-1]]);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, 0x4&symbols[s[symb_sel-1]]);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, 0x8&symbols[s[symb_sel-1]]);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, 0x10&symbols[s[symb_sel-1]]);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, 0x20&symbols[s[symb_sel-1]]);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, 0x40&symbols[s[symb_sel-1]]);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, 0x80&symbols[s[symb_sel-1]]);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13,GPIO_PIN_RESET);
while (HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_15));
for(i=0;i<24;i++)
{
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13,GPIO_PIN_SET);
pause
adc_value <<= 1;
if(HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_15))adc_value++;
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13,GPIO_PIN_RESET);
};
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13,GPIO_PIN_SET);
pause
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13,GPIO_PIN_RESET);
/*HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13,GPIO_PIN_SET);
// pause
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13,GPIO_PIN_SET);
// pause
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13,GPIO_PIN_RESET);
i++;
};*/
//while(adc_value>=255)adc_value>>=8;
c_value=adc_value;
value=adc_value;
adc_value=0;
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 192;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOD, LD4_Pin|LD3_Pin|LD5_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin : CLK_IN_Pin */
GPIO_InitStruct.Pin = CLK_IN_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
HAL_GPIO_Init(CLK_IN_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pins : LD4_Pin LD3_Pin LD5_Pin */
GPIO_InitStruct.Pin = LD4_Pin|LD3_Pin|LD5_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
/*Configure GPIO pin : PD15 */
GPIO_InitStruct.Pin = GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13,GPIO_PIN_SET);
HAL_Delay(50);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13,GPIO_PIN_RESET);
}
void Error_Handler(void)
{
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
2020-01-28 10:42 PM
When you read the adc_value bit by bit in the for loop, there is a pause after HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13,GPIO_PIN_SET); but no pause after HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13,GPIO_PIN_RESET);. Try adding a pause here too to meet min. low time.
2021-11-17 07:51 AM
hey, did your hx711 work with this code ?