|
|
View previous topic :: View next topic |
Author |
Message |
Markdem
Joined: 24 Jun 2005 Posts: 206
|
MCP3909 |
Posted: Mon Dec 03, 2007 4:36 am |
|
|
Hi All, I am tring to write a driver for the microchip MCP3909. After reading the datasheet and having a look that the refrence design, i am a bit confused.
When i check the datasheet, it says that the CS pin should be pulled low when I am writing to the MCP3909, but if i look that the asm file that comes with the refrence design, they are pulling the CS high. There must be somthing that I am missing. I have tried it both ways with the following code, but still get all zeros out from the MCP3909, nomatter what voltage i have on the inputs of the MCP3909.
Code: |
#include <18F2680.h>
#fuses HS,WDT,PUT,NOPROTECT,NOLVP,NOMCLR
#use delay(clock=20000000)
#include <LCD420.c>
#define CS PIN_A1
#define MCP3909_MCLR PIN_A2
int data1, data2, data3, data4;
void readMCP3909()
{
spi_read(data1);
spi_read(data2);
spi_read(data3);
spi_read(data4);
lcd_gotoxy(1,2);
printf(lcd_putc,"%U:%U",data1,data2);
lcd_gotoxy(1,3);
printf(lcd_putc,"%U:%U",data3,data4);
}
void init_MCP3909() //copied from the MCP3909 ref design
{
output_high(MCP3909_MCLR);
output_low(cs);
delay_ms(4);
spi_write(0b10101100);
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(100);
lcd_init();
init_MCP3909();
while(TRUE)
{
readMCP3909();
delay_ms(2000);
}
}
|
I have made sure that all my connections on the board are OK, and the MCP is getting power and has a osc connected to it.
Can any one see any problems with this code??
Thanks you
Mark |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Dec 03, 2007 1:35 pm |
|
|
Quote: | #include <18F2680.h>
#fuses HS,WDT,PUT,NOPROTECT,NOLVP,NOMCLR |
You have enabled the WatchDog Timer, but you don't have any lines of
code to call the restart_wdt() function. This is not correct. Change the
fuse to NOWDT.
Quote: |
When i check the datasheet, it says that the CS pin should be pulled low
when I am writing to the MCP3909, but if i look that the asm file that
comes with the refrence design, they are pulling the CS high. |
Not true. They set \CS low to enable it. In their sample driver,
the code to read the SPI is in this routine:
Code: | LowPriorityInterrupts |
They use a BCF instruction to set the chip select pin low, and a BSF
instruction to set it high. You can use output_low(CS) and
output_high(CS) to do the same thing.
Quote: |
#ifdef ADC_MCP3909
; Enable phase A I/O so that next data ready pulse can be seen
bsf MPU_ADC_CSC,A ;Disable phase C I/O by setting its CS* HIGH
bcf MPU_ADC_CSA,A ;and enable phase A I/O by setting its CS* LOW
#endif
|
Your code has the CS control statements in the wrong place.
They need to surround the spi_read() statements with the \CS
statements, as shown below. That's how they do it in the sample code.
Also, your SPI read statements are incorrect. You need to put the
data into a variable, as shown below. Also the SPI read statements
must have a 0 parameter to generate the clock.
Code: |
void readMCP3909()
{
output_low(CS);
data1 = spi_read(0);
data2 = spi_read(0);
data3 = spi_read(0);
data4 = spi_read(0);
output_high(CS);
lcd_gotoxy(1,2);
printf(lcd_putc,"%U:%U",data1,data2);
lcd_gotoxy(1,3);
printf(lcd_putc,"%U:%U",data3,data4);
}
|
Do the same thing for the SPI write statement:
Quote: |
void init_MCP3909() //copied from the MCP3909 ref design
{
output_high(MCP3909_MCLR);
delay_ms(4);
output_low(CS);
spi_write(0b10101100);
output_high(CS);
delay_ms(5);
}
|
Also, I didn't see any place in the sample ASM code where they send
0b10101100 to the MCP3909. I could have missed it, but where did
you see this ? |
|
|
Markdem
Joined: 24 Jun 2005 Posts: 206
|
|
Posted: Mon Dec 03, 2007 3:33 pm |
|
|
Hi PCM, I was thinking i had to make the CS pin low, just was not sure.
In regards to making the CS pin high after i have finnished reading or writing to the MCP3909, why do we need to do this? I am only going to use this one device on the SPI bus. I was thinking i could just leave it low all the time.
In the init code, i need to send spi_write(0b10101100) to enable the serial mode in the MCP3909 and set it up to be able to read the V and I values from it. This is both in the datasheet and in the sample ASM.
I will try the changes tonight and see how i go.
Thanks
Mark |
|
|
aaronik19
Joined: 25 Apr 2011 Posts: 297
|
MCP3909 |
Posted: Wed Jun 08, 2011 9:37 am |
|
|
Dear All,
Do you have an application schematic of the MCP3909 please which I can use to get started with this MCP3909? I am trying to download the datasheet but for some reasons, it can't be opened. Thanks for your feedback. |
|
|
|
|
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
|