Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
unsuccessful using SPI on 18F25K50
22-12-2015, 07:18 PM,
#1
unsuccessful using SPI on 18F25K50
I am unable to set the gain on a Microchip MCP6S26 programmable gain amplifier connected to an 18F25K50 using the SPI library. I've had no trouble doing this with an 18F14K50 using JALv2 (not Pinguino), so I'm wondering if it's something I'm doing wrong? I've checked and rechecked my serial connections and am not finding anything wrong. Here's my code:

#define ADC_DEVICE_PIN A0
#define PGA_CHANNEL_REGISTER 0x41
#define PGA_GAIN_REGISTER 0x40
#define PGA_GAIN_32X 0x07
#define PGA_GAIN_16X 0x06

void read_adc()
{
digitalWrite(SSPIN, LOW);
SPI.transfer(PGA_CHANNEL_REGISTER); // select amplifier channel
SPI.transfer(0);
digitalWrite(SSPIN, HIGH);
CDC.printf("%u = %d\r\n", 0, analogRead(ADC_DEVICE_PIN));
}

void setup()
{
// just a visual indicator....
pinMode(USERLED, OUTPUT);
digitalWrite(USERLED, HIGH);

// set up SPI interface to MCP6S26 opamp
pinMode(SSPIN, OUTPUT);
// MCP6S2X can be either SPI mode 0,0 or mode 1,1
SPI.setDataMode(SPI_MODE0);
// MCP6S2X can have a max freq of 10 Mhz alone, 5.8 Mhz if chained (p.4)
// Allowable values for 18F25K50 are Fosc/4, Fosc/16, or Fosc/64 (p.209)
// using the INTOSC (32 Mhz), we'll do /16
SPI.setClockDivider(SPI_CLOCK_DIV16);
// MSB is first on MCP6S2X (p. 18)
SPI.setBitOrder(SPI_MSBFIRST);
SPI.begin();

// Set gain on the PGA
digitalWrite(SSPIN, LOW);
// issue 'write to register' command
// 0x40 = write to gain register, 0x41 = write to channel register
SPI.transfer(PGA_GAIN_REGISTER); // write to gain register
// gain settings:
// 0x00 = 1x (default), 0x01 = 2x, 0x02 = 4x, 0x03 = 5x,
// 0x04 = 8x, 0x05 = 10x, 0x06 = 16x, 0x07 = 32x
SPI.transfer(PGA_GAIN_32X); // 32x gain
digitalWrite(SSPIN, HIGH);
}

void loop()
{
delay(1000);
read_adc();
toggle(USERLED);
}

Any help you could provide would be GREATLY appreciated. I've been stumped by this for over a week....

-Jim
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)