CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

How to program PGA

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
clau19



Joined: 17 Sep 2014
Posts: 4

View user's profile Send private message

How to program PGA
PostPosted: Wed Sep 17, 2014 6:00 am     Reply with quote

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: 9241
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Sep 17, 2014 8:21 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Sep 17, 2014 3:01 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Sep 17, 2014 5:16 pm     Reply with quote

Yes i have CCS Compiler
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Sep 18, 2014 12:08 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Sep 30, 2014 6:19 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Sep 30, 2014 8:42 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Oct 03, 2014 8:56 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Oct 03, 2014 11:44 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group