Skip to content

Adapting code for MCP3208 #2

@noeldum

Description

@noeldum

I have been attempting to modify the code and get it to run with the MCP3208.

I have modified both transmission and receiving request like this below.

// ADC sample size (2 bytes, with 11 data bits)
#define ADC_RAW_LEN     3

...

// Definitions for 2 bytes per ADC sample (11-bit)
//#define ADC_REQUEST(c)  {0xc0 | (c)<<5, 0x00}
#define ADC_REQUEST(c)  {0x60 | (c)<<2, 0x00, 0x00}
#define ADC_VOLTAGE(n)  (((n) * 4.096) / 4096.0)
#define ADC_MILLIVOLTS(n) ((int)((((n) * 3300) + 1024) / 2048))
//Yp#define ADC_RAW_VAL(d)  (((uint16_t)(d)<<8 | (uint16_t)(d)>>8) & 0x7ff)
#define ADC_RAW_VAL(d)  (((uint16_t)(d)>>4) & 0xfff)

I also modified line 515 line below as I prefer seeing raw adc values rather than voltage.

                   //slen += sprintf(&vals[slen], "%s%4.3f", slen ? "," : "",
                        //ADC_VOLTAGE(ADC_RAW_VAL(rx_buff[i])));
                    slen += sprintf(&vals[slen], "%s%d", slen ? "," : "",
                        ADC_RAW_VAL(rx_buff[i]));

After compilation it does run well and produce the following without any options.

pi@raspberrypi:~/streaming-main $ sudo ./rpi_adc_stream 
RPi ADC streamer v0.20
VC mem handle 6, phys 0xbebc4000, virt 0xb6fcc000
SPI frequency 1000000 Hz
ADC value 2063 = 2.063V
Closing

The 2063 value is clearly expected and matches with other methods I have used to read the adc.

Now the issue seems to be when I start the streaming as the raw value I get in this instance is 15. For example below

pi@raspberrypi:~/streaming-main $ sudo ./rpi_adc_stream -n 1
RPi ADC streamer v0.20
VC mem handle 6, phys 0xbebc4000, virt 0xb6f03000
Reading 1 samples at 100 S/s
15
Closing
Total samples 1, overruns 0

I must have missed some part of the code to modify but that is not too obvious to me at that point.

WOuld you be able to guide me in the right direction?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions