Skip to content

Instantly share code, notes, and snippets.

@asi-msk
Last active April 22, 2022 13:40
Show Gist options
  • Save asi-msk/62c221ffcd8f329e9e9ecc3344f0f2eb to your computer and use it in GitHub Desktop.
Save asi-msk/62c221ffcd8f329e9e9ecc3344f0f2eb to your computer and use it in GitHub Desktop.
static int MCP3008_read(uint8_t ch) {
const uint8_t send[3] = {1, 0b10000000 | (ch << 4) , 0};
uint8_t buf[3] = {0};
gpio_put(PICO_DEFAULT_SPI_CSN_PIN, 0);
sleep_us(1);
spi_write_read_blocking(spi_default, send, buf, 3);
sleep_us(1);
gpio_put(PICO_DEFAULT_SPI_CSN_PIN, 1);
return (buf[1] & 0b11) << 8 | buf[2];
}
int main() {
//省略...
spi_init(spi_default, 100 * 1000);
gpio_set_function(PICO_DEFAULT_SPI_RX_PIN, GPIO_FUNC_SPI);
gpio_set_function(PICO_DEFAULT_SPI_SCK_PIN, GPIO_FUNC_SPI);
gpio_set_function(PICO_DEFAULT_SPI_TX_PIN, GPIO_FUNC_SPI);
gpio_init(PICO_DEFAULT_SPI_CSN_PIN);
gpio_set_dir(PICO_DEFAULT_SPI_CSN_PIN, GPIO_OUT);
gpio_put(PICO_DEFAULT_SPI_CSN_PIN, 1);
//省略...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment