MCP3909 & pic 16F877

Find out how to setup your programmer's software and how to solve many common problems.

Moderator: phalanx

Post Reply
Jongnonkuk
Posts: 2
Joined: Tue Jan 25, 2011 2:43 am

MCP3909 & pic 16F877

Post by Jongnonkuk » Tue Jan 25, 2011 2:56 am

Dear, everyone.

I try to connect MCP3909 with SPI function but it isn't work! So,I do basic code but it not work again -.- (My project's Energy meter.)

I link Avdd, Dvdd pin to +5V and AGND, DGND pin to 0V. Is it true?

And link G0,G1 to 0V.


link datasheet : http://www.microchip.com/wwwproducts/De ... e=en520376

Code:

#include "16f877.h"

#fuses HS,NOWDT,PROTECT,NOLVP
#use delay(clock=10000000)
#use rs232(baud=9600,xmit=PIN_A0,STREAM = LCD,INVERT)

#define CS PIN_C0
#define SCK PIN_C1
#define SDO PIN_C2
#define SDI PIN_C3
#define MCLR PIN_C4


void clockdata(unsigned char bits_in);
void writedata(unsigned char data_in);


void main(void)
{

char TextBuff[16];
int16 In;
int16 ch0=0,ch1=0;
char i,x=1;

set_tris_c(0b00001000); //set port

output_bit(CS,1); // Disable chip select
output_bit(MCLR,1);

writedata(0b10101100);



while(1)
{

output_bit(CS,0); // Active chip select
In = 0;
output_bit(SCK,1); // make clock high first

for(i=1;i<=16;i++)
{
output_bit(SCK,0);

In <<=1;
In = In | (input(SDI) & 0x01);

output_bit(SCK,1);

}
ch0 = In;

In = 0;
for(i=1;i<=16;i++)
{
output_bit(SCK,0);

In<<=1;
In = In | (input(SDI) & 0x01);

output_bit(SCK,1);

}
ch1 = In;
output_bit(CS,1); // Disable chip select



fputc(0xFE,LCD);fputc(0x01,LCD);fprintf(LCD,"Earth");
fputc(0xFE,LCD);fputc(0xC0,LCD);fprintf(LCD,"ch0: %Ld",ch0);
fputc(0xFE,LCD);fputc(0x94,LCD);fprintf(LCD,"ch1: %Ld",ch1);

delay_ms(1000);

}//while(1) ...end



}


void writedata(unsigned char data_in)
{
output_bit(MCLR,0); //chip Reset
delay_us(2);
output_bit(MCLR,1);
delay_us(1);

output_bit(CS,0); // Active chip select
output_bit(SCK,0); // make clock low first

clockdata(data_in); // Clock in data bits.
output_bit(CS,1); // Disable chip select
}

void clockdata(unsigned char bits_in)
{
int bitcnt;
for (bitcnt=8; bitcnt>0; bitcnt--)
{
output_bit(SCK,1); // clock // Set Clock Idle level LOW.
if ((bits_in&0x80)==0x80) {output_bit(SDO,1);} // PCD8544 clocks in the MSb first.
else {output_bit(SDO,0);}
output_bit(SCK,0); // clock // Data is clocked on the rising edge of SCK.
bits_in=bits_in<<1; // Logical shift data by 1 bit left.
}
}

//************************
Result on LCD is

"Earth"
"ch0: 0"
"ch1: 0"

waltr
Support Volunteer
Posts: 2823
Joined: Tue Sep 08, 2009 12:07 pm
Location: Philadelphia, USA

Re: MCP3909 & pic 16F877

Post by waltr » Tue Jan 25, 2011 7:03 am

The MCP3909 seems to be a tricky device to use. Try these threads in the Microchip Forum:
http://www.microchip.com/forums/search. ... gh=MCP3909

Tine Boyi
Posts: 2
Joined: Thu Sep 25, 2014 3:14 am

Re: MCP3909 & pic 16F877

Post by Tine Boyi » Thu Sep 25, 2014 3:33 am

Is it possible for you to post the analogue front end circuitry for the mcp3909. My design project is based on the single phase energy meter.

I have used the data sheet but it's not helping me much. Could you probably help me out

waltr
Support Volunteer
Posts: 2823
Joined: Tue Sep 08, 2009 12:07 pm
Location: Philadelphia, USA

Re: MCP3909 & pic 16F877

Post by waltr » Thu Sep 25, 2014 6:53 am

I've never used this device but have seen many posts on the Microchip User's forum on it so that is the best place for you to go. Also do look at the Microchip App Notes for additional info on using this device.

Tine Boyi
Posts: 2
Joined: Thu Sep 25, 2014 3:14 am

Re: MCP3909 & pic 16F877

Post by Tine Boyi » Thu Sep 25, 2014 11:19 am

Thanks a lot. Will see how it goes. If I have any problems will contact you.

Post Reply