Created
July 1, 2022 08:35
-
-
Save knight-ryu12/e831dc551799603a61895e0ce8907cad to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdint.h> | |
#include <driver/gpio.h> | |
#include "esp_log.h" | |
#include <freertos/FreeRTOS.h> | |
#include <freertos/task.h> | |
#include <rom/ets_sys.h> | |
// GPIO22 = SCK | |
// GPIO23 = DIN | |
const uint8_t pulse_count = 25; // send 25 pulse = CHA 128g | |
void hx711_init() { | |
gpio_config_t cfg_sck = { | |
.pin_bit_mask = (1<<22), | |
.mode = GPIO_MODE_OUTPUT, | |
}; | |
gpio_config_t cfg_dio = { | |
.pin_bit_mask = (1<<23), | |
.mode = GPIO_MODE_INPUT, | |
}; | |
gpio_config(&cfg_sck); | |
gpio_config(&cfg_dio); | |
gpio_set_level(GPIO_NUM_22,1); | |
ets_delay_us(100); | |
gpio_set_level(GPIO_NUM_22,0); | |
ets_delay_us(100); | |
} | |
int32_t hx711_read() { | |
int32_t bit = 0; | |
uint32_t d = 0; | |
for(int i=0; i<24; i++) { | |
gpio_set_level(GPIO_NUM_22,1); | |
//gpio_set_level(GPIO_NUM_22,1); | |
ets_delay_us(1); | |
d = gpio_get_level(GPIO_NUM_23); | |
gpio_set_level(GPIO_NUM_22,0); | |
ets_delay_us(1); | |
//ESP_LOGD("HX711","Got level %d",d); | |
bit |= d<<(23-i); | |
//gpio_set_level(GPIO_NUM_22,0); | |
//bit |= gpio_get_level(GPIO_NUM_23)>>(24-i); | |
} | |
gpio_set_level(GPIO_NUM_22,1); | |
ets_delay_us(1); | |
gpio_set_level(GPIO_NUM_22,0); | |
// Wait for DOUT to be 0 | |
//while(gpio_get_level(GPIO_NUM_23) == 1); | |
ESP_LOGI("HX711","Read bit %d",bit); | |
return bit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment