|
|
View previous topic :: View next topic |
Author |
Message |
jambox5
Joined: 06 Mar 2013 Posts: 4 Location: St. Cloud State University
|
SPI communication between pic18f4455 and Mcp3909 |
Posted: Thu May 09, 2013 11:42 am |
|
|
So I'm having some troubles getting communication between my pic and Mcp3909 measuring chip, I've made the connections:
PIC MCP
An0--->CS
An1--->MCLR
SDO-->SDI
SDI--->SDO
CCP2->CLKI
SCK-->SCK
I'm trying to read the AC voltage(Ch1) and current(Ch0) being read into the MCP3909. and display 50 samples in serial monitor. here is my C code as devised with CCS:
Code: | //*web refrences*//
////////////////////////////////////////////////////////////////////////////////
#include <18F4455.h>
#fuses HS,HSPLL,NOWDT,PUT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN,NOMCLR
#use delay(clock=20000000)
#define cs PIN_A0
#define MCP3909_MCLR PIN_A1
#include <usb_bootloader.h>
#include <stdlib.h>
#include <ctype.h>
#include <usb_cdc.h>
#include <lcd.c>
char Hit=0;
int data1=1, data2=1;
int X[50]; int Y[50];
void readMCP3909()
{
printf(lcd_putc,"\fReading in\nVoltage..");
for(int i=0;i<50;i++)
{
output_low(cs);
X[i]=spi_read(0);
//Y[i]=spi_read2(0); //doesn't work
output_high(cs);
}
}
void sendArray()
{
for(int f=0;f<50;f++)
{
//printf(usb_cdc_putc," %u:%u ",X[f],Y[f]); //again won't work
printf(usb_cdc_putc," %u,",X[f]);
}
}
void init_MCP3909() //copied from the MCP3909 ref design
{
output_high(MCP3909_MCLR);
output_low(cs);
delay_ms(4);
spi_write(0b10101100); //according to mcp3909 datasheet sets
//mode to Dual Channel Output Post HPF
//which to the best of my knowledge gives
//1st 8bits: voltage and 2nd 8bits: current
delay_ms(5);
}
void main()
{
output_high(cs);
output_low(MCP3909_MCLR);
setup_spi(spi_master |spi_l_to_h | spi_clk_div_16 );
delay_ms(10);
lcd_init();
usb_cdc_init();
usb_init();
delay_ms(5);
printf(lcd_putc,"Senior Design\nVoltage Read");
while(TRUE)
{
if(usb_cdc_kbhit()){
Hit = usb_cdc_getc(); }
if(Hit=='0'){
readMCP3909();
sendArray();
Hit=0;
}
}
} |
my main issue at this point is that the only thing read into X[50] is 255 from the SDI pin, so I just get an array of 50 '255' printed to the serial monitor. Is there an issue with how I have my code structured? I'm still really new to SPI interface so please feel free to point out any/all flaws in the code. otherwise I'll know this is a potential hardware issue. |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Thu May 09, 2013 2:36 pm |
|
|
your code is a mess -hard to know where to start
'
1- you try to talk to and init the MCp3909 before you init the SPI channel.
2-the init3909 function never raises NOT CS when done !
3-i am not at all certain your setup command for the 3909 is correct
even if you DID handle the enable line correctly
4 got a circuit of what you wired here ? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu May 09, 2013 3:00 pm |
|
|
Quote: |
#include <18F4455.h>
#fuses HS,HSPLL,NOWDT,PUT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN,NOMCLR
#use delay(clock=20000000)
|
You're using two different oscillator fuses. If you do that, the compiler
will use the last one that it sees. Which means that your PIC is running
at 48 MHz, but your #use delay() is set for 20 MHz, so all delays in the
program (hardware or software) will be wrong.
See this post which shows how to setup the PIC to run in HS mode
or in HSPLL mode. Both are with a 20 MHz crystal:
http://www.ccsinfo.com/forum/viewtopic.php?t=42223&start=1 |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Thu May 09, 2013 3:24 pm |
|
|
w/o a schematic to go by , you indicate
Quote: |
CCP2->CLKI
and this
|
B U T
since you never set up timer2
&&
you don't set up the CCP module
i predict CLKI is receiving a precise 0.00 hz signal
ALSO
you read ONE byte - but the datasheet says there are 16 ( or more)
bits of data to be read from the ADC.
have you read and UNDERSTOOD the datasheet for this rather complex part ????
&&
that might JUST have a bearing on your pain too...
|
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Thu May 09, 2013 4:13 pm |
|
|
then we have this link of blast-from-the-past .........
http://www.ccsinfo.com/forum/viewtopic.php?t=32893&highlight=mcp3909
your senior project code has a rather striking similarity - right down to the mistakes -of this previously posted mess.
in my humble opinion
cribbing
init_MCP3909()
from the 2007 post ( which was just as wrong verbatim)
was of no help at all,
as great minds think alike i guess. |
|
|
jambox5
Joined: 06 Mar 2013 Posts: 4 Location: St. Cloud State University
|
|
Posted: Fri May 10, 2013 10:45 am |
|
|
Quote: |
2-the init3909 function never raises NOT CS when done !
3-i am not at all certain your setup command for the 3909 is correct
even if you DID handle the enable line correctly
4 got a circuit of what you wired here ? |
2: please expand
3: advise on how to make it correct?
4: working on it
Quote: |
Quote:
#include <18F4455.h>
#fuses HS,HSPLL,NOWDT,PUT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN,NOMCLR
#use delay(clock=20000000)
You're using two different oscillator fuses. If you do that, the compiler
will use the last one that it sees. Which means that your PIC is running
at 48 MHz, but your #use delay() is set for 20 MHz, so all delays in the
program (hardware or software) will be wrong.
|
Good catch, sloppy coding typo. didn't notice that befor
Quote: |
Quote:
X[i]=spi_read(0);
you read ONE byte - but the datasheet says there are 16 ( or more)
bits of data to be read from the ADC.
have you read and UNDERSTOOD the datasheet for this rather complex part ???? |
I have read this portion of the MCP3909 data sheet, and I understand that there are 32 bits of data to be read (16 voltage, 16 current) BUT from what I gathered about spi_read(0) command is that it collects 8bits at a time. if this is not accurate could you please give me abit more detail on the matter?
Quote: | in my humble opinion
cribbing
init_MCP3909()
from the 2007 post ( which was just as wrong verbatim)
was of no help at all,
as great minds think alike i guess. Very Happy Very Happy |
reason for this is because I'm struggling to understand SPI and how to initiate/read from the MCP3909, it was one of the very few resources I've been able to find that gave some idea as to how I'm supposed to do this in CCS, I know it has flaws but I can't find any examples.
As far as the support and advice from all the responses so far I thank you and will try some of these suggestions, possibly returning with different code and/or a schematic in the next couple days! |
|
|
|
|
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
|