Announcement

REXYGEN forum has been moved. This one is closed.
Head over to the new REXYGEN Community Forum at https://forum.rexygen.com.

Looking forward to meeting you there!
 

#1 2017-06-22 21:25:36

fish19
Member
Registered: 2016-03-24
Posts: 7

MCP3008 ADC via REXLANG with CS

I looked at your example of the REXLANG code example for a MCP3008 and since I am new to this I was looking to see how you would generate the CS need for a hat that I am trying to bring into Rex controls. The example code I have looks like this:

void Update_Analog(unsigned char channel) {

    unsigned int  adc, input;
    unsigned char buf[3];
   
    wiringPiSPISetup(1, 100000);
    input = 0x0600 | (channel << 6);
   
    buf[0] = (input >> 8) & 0xff;
    buf[1] = input & 0xff;
    buf[2] = 0;
   
    digitalWrite(CS_8AI, LOW);   
    wiringPiSPIDataRW(1,buf,3);
    adc = ((buf[1] & 0x0f ) << 8) | buf[2];
    digitalWrite(CS_8AI, HIGH);
           
    AN_AD[channel] = adc;   
    AN_mA[channel] = (float) adc * 20 / mA_20;   
}


void Initialize_Pi_Hardware(void) {
   
    // SPI CS Enable Lines
    pinMode(CS_2AO, OUTPUT);
    pinMode(CS_8DI, OUTPUT);
    pinMode(CS_SPARE2, OUTPUT);
    pinMode(CS_SPARE1, OUTPUT);
    pinMode(CS_LEDS, OUTPUT);
    pinMode(CS_LCD1, OUTPUT);
    pinMode(CS_LCD2, OUTPUT);
    pinMode(CS_8KO, OUTPUT);
    pinMode(CS_8AI, OUTPUT);
    pinMode(DIR_RS485, OUTPUT);
}
I do understand that I would only need to use the CS_8AI to make this work but really don't know where to start making the conversion from this to a REXLANG code.

Offline

#2 2017-06-26 14:13:52

jaroslav_sobota
Administrator
Registered: 2015-10-27
Posts: 535

Re: MCP3008 ADC via REXLANG with CS

Hi fish19,
how many SPI devices are present on the HAT? Is there a specification available? What ADC chip is used for analog inputs?

By default, REXLANG relies on 2 SPI buses present in the system (/dev/spidev0.0 and /dev/spidev0.1), where the CS signal is handled by the operating system. In other words, you don't deal with the CS signal directly, you simply trigger SPI transaction from REXLANG and everything happens automatically (CS signals are on GPIO8 and GPIO7).

Hope this helps. If you provide additional details, you can get additional hints wink

Best regards
Jaroslav

Offline

#3 2017-06-26 19:26:56

fish19
Member
Registered: 2016-03-24
Posts: 7

Re: MCP3008 ADC via REXLANG with CS

I uploaded a schematic of the HAT and the ADC is a MCP3208.

Offline

#4 2017-07-24 23:03:56

fish19
Member
Registered: 2016-03-24
Posts: 7

Re: MCP3008 ADC via REXLANG with CS

i did get the REXLANG function block to work with this hardware using the MCP3008 example code and even playing around reading one channel using the MCP3201 example code. However while this works I have only been able to get 10 bit resolution with either of these codes. (i.e. 987 counts at 3 volts) instead of the 12 bit resolution I should be getting (i.e. 3723 counts at 3 volts ) I understand that you need to combine the two registers to get the correct value but so far I have been unable to get the 12 bits. Can you help point me to the error of my ways or give me a hint in the right direction.

Offline

#5 2017-07-26 12:31:34

jaroslav_sobota
Administrator
Registered: 2015-10-27
Posts: 535

Re: MCP3008 ADC via REXLANG with CS

Hi fish19,
please download and test the example on reading MCP3208 using REXLANG. Please note that I do not have the chip here so the example is not tested at all. Let me know if it works for you.

The source code shows how to combine the obtained data to get a full 12-bit resolution.

Kind regards,
Jaroslav

Offline

Board footer

Powered by FluxBB