Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapting code for MCP3208 #2

Open
noeldum opened this issue Nov 27, 2020 · 6 comments
Open

Adapting code for MCP3208 #2

noeldum opened this issue Nov 27, 2020 · 6 comments

Comments

@noeldum
Copy link

noeldum commented Nov 27, 2020

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?

@jbentham
Copy link
Owner

jbentham commented Nov 28, 2020 via email

@noeldum
Copy link
Author

noeldum commented Nov 28, 2020

In order to read the full 12 bits I modified
#define ADC_RAW_LEN 2
to
#define ADC_RAW_LEN 3
(mcp3208 is 12bits)
As the mcp3208 uses 5 bit command we must use a 24 bit transfer.
I guess there are many ways one could do this as only 17bits over the 24 are relevant. That part is working well and does not concern me that much.

Thank you for the suggestion with the hex output. I'll try and investigate this way.

@chandu07
Copy link

chandu07 commented Jan 18, 2021

Hello @noeldum, have you been able to run it with MCP3208? any help or hint on sort of the modifications you made to the code will be very much helpful.

Nice work @jbentham!

@noeldum
Copy link
Author

noeldum commented Jan 23, 2021

Hi Chandu, I got somewhat close to get it working but not to completion yet. Time for me to dive into this again.
If you make some progress yourself just let me know as well.

@noeldum
Copy link
Author

noeldum commented May 3, 2021

@chandu07
I gave up as I could not get anywhere. However I have been much more successful using Pigpio. It also uses dma.
https://github.com/joan2937/pigpio

There is an example for MCP3202 that I am adapting for mcp3208. Nearly there. I can share if someone need it.

@chandu07
Copy link

chandu07 commented May 4, 2021

@chandu07
I gave up as I could not get anywhere. However I have been much more successful using Pigpio. It also uses dma.
https://github.com/joan2937/pigpio

There is an example for MCP3202 that I am adapting for mcp3208. Nearly there. I can share if someone need it.

Hey Noeldum, we were able to figure it out with Pigpio example. it was kinda simple, just had to change the number of bits and the exact bits we were reading accordingly and vola it works.

Thanks for the effort though, will share the changes we made in the repo for Pigpio may be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants