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

MCP3424 with pic18f2455

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



Joined: 21 Apr 2015
Posts: 2

View user's profile Send private message

MCP3424 with pic18f2455
PostPosted: Tue Apr 21, 2015 5:43 pm     Reply with quote

I'm working to get data from mcp3424 but i get nothing.
PLZ help me with this !!! I'm stuck on it Shocked Shocked Shocked
Code:

#use i2c(Master, scl=PIN_B1,sda=PIN_B0,slow)
//#use i2c(i2c1,MASTER,SCL=Device_SCL, SDA=Device_SDA,FAST=400000, FORCE_HW)
//#use i2c(master, sda=PIN_J1, scl=PIN_J0,SLOW)
#use rs232(baud=9600 , UART1)

#define ADCRead 0xDB

#define ADCWrite 0xDA
       

void main()
{
     int8 HighBuffer,LowBuffer,Config,ack=0; 
     float value;

        while(1){
            i2c_start();
            ack = i2c_write(ADCWrite); // Address factory set at 1101 = 0xD
            if(ack)
               printf("Feil!!");   
            i2c_write(0x8B); //16bit mode, Gain = 8, one shot sample mode   
            i2c_stop();   
            delay_ms(5);// Wait for conversion to be finished in oneshot mode
            i2c_start();
            i2c_write(ADCRead); //Set Read/Write to 1 for read
            HighBuffer = i2c_read(1);
            LowBuffer = i2c_read(1);
            Config = i2c_read(1);
            printf("Config: %x\n\r",Config);
            while((Config&0x80)){Config = i2c_read(1);}; // Wait for finish
            i2c_stop();            // When conversion finished
            delay_ms(5);

            i2c_start();         // Read new value
            i2c_write(ADCRead); //Set Read/Write to 1 for read
            HighBuffer = i2c_read(1);
            LowBuffer = i2c_read(1);
            Config = i2c_read(0);
            i2c_stop();   
            delay_ms(5);
            HighBuffer = i2c_read(1);
            LowBuffer = i2c_read(1);
            i2c_stop();
            delay_ms(5);

            Value = make16(HighBuffer,LowBuffer);
            printf("Lading Sol:%f A\n\r",value);   

      }
   
   }
Ttelmah



Joined: 11 Mar 2010
Posts: 19499

View user's profile Send private message

PostPosted: Wed Apr 22, 2015 1:09 am     Reply with quote

First, what PIC?.
What voltage is the system running?.
What I2C pull-ups are you using?.

Then before trying to talk to the chip, use PCM_Programmers I2C scanner program in the code library.

Only progress to trying to actually driving the chip once this has seen the device.

The addresses you are using correspond to Adr0, being pulled up to the supply, and Adr1, being left floating. Is this how you have them wired?.
khaled_23



Joined: 21 Apr 2015
Posts: 2

View user's profile Send private message

PostPosted: Wed Apr 22, 2015 4:56 am     Reply with quote

thank you Ttelmah Smile Smile Smile
i use PIC 18F2455 and the voltage 5V and i use 4.9K resistor pull-up
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Apr 22, 2015 10:02 am     Reply with quote

Run the i2c bus scanner program and see if it finds the mcp3424:
http://www.ccsinfo.com/forum/viewtopic.php?t=49713
And see what i2c slave address it reports.


After you confirm the correct mcp3424 slave address is in use,
then fix your code.

The last i2c_read() before the stop always needs to do a NAK.
This means the parameter should be a 0. Fix the line shown below in bold:
Quote:

i2c_start();
i2c_write(ADCRead); //Set Read/Write to 1 for read
HighBuffer = i2c_read(1);
LowBuffer = i2c_read(1);
Config = i2c_read(1);
printf("Config: %x\n\r",Config);
while((Config&0x80)){Config = i2c_read(1);}; // Wait for finish
i2c_stop();

The mcp3424 data sheet shows this on page 23.
http://ww1.microchip.com/downloads/en/DeviceDoc/22088c.pdf
It says (regarding continuing to read bytes):
Quote:
To continue: ACK by Master
To end: NAK by Master



And then what is this ? You have some fragments of an i2c operation.
You don't have a start, and you don't send the slave address, and you
don't have a NAK:
Quote:
HighBuffer = i2c_read(1);
LowBuffer = i2c_read(1);
i2c_stop();
lucasromeiro



Joined: 27 Mar 2010
Posts: 167

View user's profile Send private message

Help
PostPosted: Thu May 07, 2015 9:18 pm     Reply with quote

I'm having the same problem, I need to use this ADC but I found library.
I could not make it work!
Can you help me?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu May 07, 2015 11:41 pm     Reply with quote

Look for the mcp342x.c driver file in your CCS directory:
Code:
c:\program files\picc\drivers\mcp342x.c    1/28/2013

Based on the file date, I think it probably first came out with vs. 4.141 of
the compiler. It's also in vs. 5.045. The start of the file says:
Code:

////    Driver for MCP342X Delta-Sigma A/D Converter   ////
////
////  Driver for Microchip's MCP3421, MCP3422, MCP3423, MCP3424,
////  MCP3425, MCP3426, MCP3427 and MCP3428 Delta-Sigma A/D
////  Converters.                                                   
lucasromeiro



Joined: 27 Mar 2010
Posts: 167

View user's profile Send private message

HELP
PostPosted: Fri May 08, 2015 9:56 am     Reply with quote

PCM programmer wrote:
Look for the mcp342x.c driver file in your CCS directory:
Code:
c:\program files\picc\drivers\mcp342x.c    1/28/2013

Based on the file date, I think it probably first came out with vs. 4.141 of
the compiler. It's also in vs. 5.045. The start of the file says:
Code:

////    Driver for MCP342X Delta-Sigma A/D Converter   ////
////
////  Driver for Microchip's MCP3421, MCP3422, MCP3423, MCP3424,
////  MCP3425, MCP3426, MCP3427 and MCP3428 Delta-Sigma A/D
////  Converters.                                                   



Hello Friend!
Thank you for your help !!
I managed to find the library version (5008).
I do not know if this version has problems, but I have a big problem.
The library works well for converting 12bits.
When set up for converting 16 or 18bits generates much delay in processing when my sim to make the conversion (read_adc_mcp ();).
In Proteus gives notice: (Simulation is not running in real time due to excessive CPU load)
In converting 12bits this error does not appear and generates no delay in my code.

My code is simple.
I did only one test to convert and display the serial!
Code:

set_adc_channel_mcp (0);
fo = read_adc_mcp ();
printf ("\ r \ n 1:% ld", fo);

Would you help me?
I do not understand the reason for this problem. Does the library version?
I noticed that the library is incomplete, missing the command: read_adc_value_mcp ()
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri May 08, 2015 12:26 pm     Reply with quote

Quote:
set_adc_channel_mcp (0);
fo = read_adc_mcp ();
printf ("\ r \ n 1:% ld", fo);

This code is not sufficient to show what you are doing.

Post a complete test program that compiles without errors.
The program should have an:
#include for the PIC, #fuses, #use delay, other #use() statements,
then any global variables,
then #define statements to setup the mcp342x configuration,
then the #include <mcp342x.c> line,
then main(),
and then inside main(), call the routines in the mcp342x.c driver file.

Do not post the mcp342x.c driver. I already have it.
lucasromeiro



Joined: 27 Mar 2010
Posts: 167

View user's profile Send private message

Complete code:
PostPosted: Fri May 08, 2015 12:30 pm     Reply with quote

PCM programmer wrote:
Quote:
set_adc_channel_mcp (0);
fo = read_adc_mcp ();
printf ("\ r \ n 1:% ld", fo);

This code is not sufficient to show what you are doing.

Post a complete test program that compiles without errors.
The program should have an:
#include for the PIC, #fuses, #use delay, other #use() statements,
then any global variables,
then #define statements to setup the mcp342x configuration,
then the #include <mcp342x.c> line,
then main(),
and then inside main(), call the routines in the mcp342x.c driver file.

Do not post the mcp342x.c driver. I already have it.


The complete code:
Code:

#include <18F2685.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES HS                       //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPROTECT                //Code not protected from reading
#FUSES BROWNOUT                 //Reset when brownout detected
//#FUSES BORV20                   //Brownout reset at 2.0V
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES LVP                      //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES PBADEN                   //PORTB pins are configured as analog input channels on RESET
#FUSES BBSIZ4K                  //4K words Boot Block size
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOCPB                    //No Boot Block code protection
#FUSES LPT1OSC                  //Timer1 configured for low-power operation
#FUSES NOMCLR                   //Master Clear pin used for I/O
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)

#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

#include "mcp3424.c"


int32 flo=0;

void main()
{

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF|ADC_TAD_MUL_0);
   setup_spi(SPI_SS_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   setup_vref(FALSE);
   
   adc_init();
   printf("inicio");
   While(1){
   //flo=read_adc_value_mcp(2.048);
   set_adc_channel_mcp(0);
   flo=read_adc_mcp();
   printf("\r\n 1: %ld",flo);//Exibir
   delay_ms(1000);
   set_adc_channel_mcp(1);
   flo=read_adc_mcp();
   printf("\r\n 2: %ld",flo);//Exibir
   delay_ms(1000);
   set_adc_channel_mcp(2);
   flo=read_adc_mcp();
   printf("\r\n 3: %ld",flo);//Exibir
   delay_ms(1000);
   set_adc_channel_mcp(3);
   flo=read_adc_mcp();
   printf("\r\n 4: %ld",flo);//Exibir
   delay_ms(1000);
   }
}
lucasromeiro



Joined: 27 Mar 2010
Posts: 167

View user's profile Send private message

PostPosted: Fri May 08, 2015 1:01 pm     Reply with quote

You need something else?
I think the problem is in the library (mcp3424.c) of Mihas compiler version (5008).
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri May 08, 2015 1:25 pm     Reply with quote

Quote:

In Proteus gives notice: (Simulation is not running in real time due to excessive CPU load)

This is a common problem with the Isis simulator. Google for this:
Quote:
Proteus OR Isis "in real time due to excessive CPU load"

I suggest that you test your project in real hardware.
lucasromeiro



Joined: 27 Mar 2010
Posts: 167

View user's profile Send private message

PostPosted: Fri May 08, 2015 1:32 pm     Reply with quote

PCM programmer wrote:
Quote:

In Proteus gives notice: (Simulation is not running in real time due to excessive CPU load)

This is a common problem with the Isis simulator. Google for this:
Quote:
Proteus OR Isis "in real time due to excessive CPU load"

I suggest that you test your project in real hardware.


I got it.
but when I read 12bits is not it.
Occurs only when phaco reading of 16 or 18bits.
I found it very strange.
In other simulations using i2c other devices that does not occur.
The library may have a problem.
Because it already found a problem, not shown to function: read_adc_value_mcp ()
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri May 08, 2015 2:14 pm     Reply with quote

Quote:
I think the problem is in the library (mcp3424.c) of my compiler version (5008).

I compared the mcp342x.c files between compiler versions 5.008 and
5.045 and they are identical.

Quote:
The library may have a problem.

Or the Isis model for the mcp3424 may have a problem in 16-bit or
18-bit mode. We can not work on Isis problems on the CCS forum.
Please use real hardware.
Ttelmah



Joined: 11 Mar 2010
Posts: 19499

View user's profile Send private message

PostPosted: Sat May 09, 2015 7:34 am     Reply with quote

The warning is common with Isis, if maths gets complex.
It's a warning, not an error. Just says that timings will be wrong.

When you switch up to 18bit mode, the amount of data being moved increases, the variable sizes double, and the amount of maths increases. Isis just can't cope.
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