0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Raspberry Pi: bcm2835 ライブラリーを使って MCP3208 の電圧を読む

Posted at

RaspberryPi 4 で確認しました。

プログラム

mcp3208_read.c
// ----------------------------------------------------------------
//	mcp3208_read.c
//
//					Jun/30/2025
// ----------------------------------------------------------------
#include <bcm2835.h>
#include <stdio.h>
#include <stdint.h>

extern uint16_t readMCP3208(uint8_t channel);
// ----------------------------------------------------------------
int main() {
	if (!bcm2835_init()) {
		printf("bcm2835_init failed. Are you running as root?\n");
		return 1;
	}

	if (!bcm2835_spi_begin()) {
		printf("bcm2835_spi_begin failed. Is SPI enabled?\n");
		return 1;
	}

	bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);  // MSB first
	bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);			   // SPI Mode 0
	bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_256); // ~1MHz (MCP3208は最大2MHz)
	bcm2835_spi_chipSelect(BCM2835_SPI_CS0);				  // CE0 (Chip Select 0)
	bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);  // CS Active Low

	printf("Reading MCP3208 values...\n");

	while (1) {
		const uint8_t channels[] = {0,1,2};

		for (uint8_t it = 0; it<3; it++)
			{
		const uint16_t adc_value = readMCP3208(channels[it]);
		
		const float voltage = (adc_value * 3.3f) / 4095.0f;
		
		printf("%d: ADC Value: %4d  Voltage: %.2fV\n", channels[it],adc_value, voltage);
			}
		
		bcm2835_delay(2000);
	}

	// 終了処理
	bcm2835_spi_end();
	bcm2835_close();
	return 0;
}

// ----------------------------------------------------------------
readMCP3208.c
// ----------------------------------------------------------------
//
//	readMCP3208.c
//
//					Jun/30/2025
// ----------------------------------------------------------------
#include <bcm2835.h>
#include <stdio.h>
#include <stdint.h>

// ----------------------------------------------------------------
// MCP3208からSPI経由でデータを読み取る関数
uint16_t readMCP3208(uint8_t channel) {
    // 送信データバッファ (3バイト)
    uint8_t tx_data[3] = {
        0x06 | ((channel & 0x07) >> 2),  // スタートビット + チャンネル選択(上位ビット)
        ((channel & 0x07) << 6),         // チャンネル選択(下位ビット)
        0x00                             // ダミーデータ(受信用)
    };

    // 受信データバッファ
    uint8_t rx_data[3] = {0};

    // SPI転送
    bcm2835_spi_transfernb((char*)tx_data, (char*)rx_data, 3);

    // 受信データを12ビット値に変換
    uint16_t result = ((rx_data[1] & 0x0F) << 8) | rx_data[2];
    return result;
}

// ----------------------------------------------------------------
Makefile
mcp3208_read: mcp3208_read.c readMCP3208.c
	gcc -o mcp3208_read mcp3208_read.c readMCP3208.c -l bcm2835
clean:
	rm -f mcp3208_read

コンパイル

make

実行結果

$ sudo ./mcp3208_read 
Reading MCP3208 values...
0: ADC Value: 4095  Voltage: 3.30V
1: ADC Value: 4090  Voltage: 3.30V
2: ADC Value: 4093  Voltage: 3.30V
0: ADC Value: 4094  Voltage: 3.30V
1: ADC Value: 4090  Voltage: 3.30V
2: ADC Value: 4091  Voltage: 3.30V
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?