0

I am tring to make a simple C++ loop program to read analog values from MCP3004 via SPI. I am using wiringPi libs to make code super clear. Functions works fine, I get proper numbers but not in a loop. I get only 3 first reads and after that only 0. I looked in wiringPi references, libs and I found nothing that would help me. I tried to change delay times but still getting only 3 first proper values. Maybe somebody had that kind of problem and know the answer? Would really appreciate any help.

#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <mcp3004.h>

int main()
{
    int wart;
    wiringPiSetupGpio();
    mcp3004Setup (100,0);
    while(true)
    {
        for(int i=0;i<4;i++)
        {
            wart=analogRead(100);
            printf("Value: %d\n", wart);
            delay(1000);
        }
    delay(5000);
    }
}

this code gives me for eg.: Value:1004, Value:1003, Value:500, and than only Value:0

Janusz020
  • 11
  • 2

1 Answers1

1

I use the mcp3008 all the time which is the 8 ADC version in SPI from Windows and I do not have any problem.

First are you really reading the port 100 from the mcp3008.

wart=analogRead(100);

You need to provide the code from mcp3004.cpp

The is the C# code to read a value from the MCP3008

private List<int> _channelInSingleMode = new List<int>() {
            0x08,
            0x09,
            0x0A,
            0x0B,
            0x0C,
            0x0D,
            0x0E,
            0x0F
        };

        public int Read(int port)
        {
            if ((port > 7) || (port < 0))
                throw new ArgumentException(string.Format("Invalid analog port {0}", port));

            const byte junk = (byte)0;
            var port2       = (byte)((_channelInSingleMode[port] << 4));
            var r1          = this._spiEngine.Transfer(new List<Byte>() { 0x1, port2, junk });

            return ValidateOperation(r1);
        }
TheLostMind
  • 35,966
  • 12
  • 68
  • 104