View previous topic :: View next topic |
Author |
Message |
clau19
Joined: 17 Sep 2014 Posts: 4
|
How to program PGA |
Posted: Wed Sep 17, 2014 6:00 am |
|
|
Hi everybody,
Last edited by clau19 on Mon Oct 20, 2014 10:15 am; edited 1 time in total |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Wed Sep 17, 2014 8:21 am |
|
|
Well it appears to use SPI mode 1,1 and will work at 5 volts ,so the hardware configuration is dead simple.
Connect it to one of the hardware SPI peripherals of the PIC.
I'd suggest connecting the analog inputs to pots,that way you can test for 0V, 5V and somewhere in between.
I'd also be sure to test the PIC with 'dummy data' before you try to use the PGA. that way you KNOW the PIC runs properly and can send data to a PC terminal program( or LCD module ?).
You might want to search this site for code examples, there may be one in the library..... or just Google and see what the WWW has to offer.
hth
jay |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Sep 17, 2014 3:01 pm |
|
|
Quote: | I'm using MPLAB... |
Do you have the CCS C compiler ? That's what this forum uses.
If you just downloaded MPLAB (only), then you need to ask for
help on the Microchip forum. |
|
|
clau19
Joined: 17 Sep 2014 Posts: 4
|
|
Posted: Wed Sep 17, 2014 5:16 pm |
|
|
Yes i have CCS Compiler |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Sep 18, 2014 12:08 am |
|
|
I don't have the MCP6S91 chip, but I think this driver will work.
The 18F4550 isn't a good PIC for SPI, because it multiplexes the
hardware SDO pin with the UART's Rx pin. So you have to give up
either the hardware UART or SPI. To avoid giving up the UART,
I used software SPI below in the #use spi() statement. I used some
free i/o pins on PortD for the SPI. You could use any free i/o pins.
Code: |
#include <18F4550.h>
#fuses INTRC_IO,NOWDT,BROWNOUT,PUT
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)
#use spi(Master, Mode=0, Bits=8, MSB_FIRST, DO=PIN_D0, CLK=PIN_D1)
#define MCP6S91_CS PIN_D2
#define MCP6S91_GAIN_CMD 0x40
#define MCP6S91_SHUTDOWN_CMD 0x20
// Parameters for set_mcp6s91_gain():
#define MCP6S91_GAIN_1 0
#define MCP6S91_GAIN_2 1
#define MCP6S91_GAIN_4 2
#define MCP6S91_GAIN_5 3
#define MCP6S91_GAIN_8 4
#define MCP6S91_GAIN_10 5
#define MCP6S91_GAIN_16 6
#define MCP6S91_GAIN_32 7
//--------------------------
// Set gain for Channel 0.
// The mcp6s91 only has one channel.
void set_mcp6S91_gain(int8 gain)
{
output_low(MCP6S91_CS);
spi_xfer(MCP6S91_GAIN_CMD);
spi_xfer(gain);
output_high(MCP6S91_CS);
}
//--------------------------
// Set mcp6s91 output pin to High-Z mode.
void shutdown_mcp6S91(void)
{
output_low(MCP6S91_CS);
spi_xfer(MCP6S91_SHUTDOWN_CMD);
spi_xfer(0);
output_high(MCP6S91_CS);
}
//--------------------------
// Initialize internal state machine
// per mcp6s91 data sheet section 5.2.1.
void init_mcp6s91(void)
{
delay_ms(1);
set_mcp6S91_gain(0);
}
//=============================
void main()
{
init_mcp6s91();
// Example: Set gain to 4.
set_mcp6S91_gain(MCP6S91_GAIN_4);
while(1);
}
|
|
|
|
clau19
Joined: 17 Sep 2014 Posts: 4
|
|
Posted: Tue Sep 30, 2014 6:19 am |
|
|
Thank you very much
Last edited by clau19 on Mon Oct 20, 2014 10:14 am; edited 1 time in total |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Tue Sep 30, 2014 8:42 am |
|
|
Hi,
C'mon man, this is a 'help you' forum, not a 'do it for you' forum. PCM programmer handed you some working code for your project on a silver platter, so what have you done since then? What you are asking isn't hard to accomplish, so why don't you show us first what progress you've made toward your goal, and we'll help you to finish it!
Fair enough?
John |
|
|
clau19
Joined: 17 Sep 2014 Posts: 4
|
|
Posted: Fri Oct 03, 2014 8:56 am |
|
|
Thank you
Last edited by clau19 on Mon Oct 20, 2014 10:13 am; edited 1 time in total |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Fri Oct 03, 2014 11:44 am |
|
|
Hi,
The MCP6S91 is simply an op-amp with a programmable gain via SPI. You'll need to setup the A/D converter of the PIC, and read the output from the MCP6S91. There are a couple of CCS example programs that show how to configure the A/D, such as 'ex_adxx.c'. This topic has also been covered extensively right here in the forum.
Once you can read the A/D, you'll then have to figure out an intelligent means to control your op-amp gain. Typically, this is an iterative process where you start at the lowest gain setting (g=1), and take a reading. Then depending on the magnitude of the reading you either stay at that gain, or you bump the gain up by one level and take another reading. This process continues until you can utilize the highest gain possible without exceeding the max. PIC A/D input voltage.
So, to keep you going on this project, get the A/D readings going first, and then play with the gain to see how the readings change. At that point you should be well poised to write some working code!
John |
|
|
|