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

Need Confirmation in Using Pic18F and MCP3551

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



Joined: 12 Feb 2009
Posts: 54

View user's profile Send private message

Need Confirmation in Using Pic18F and MCP3551
PostPosted: Thu Aug 27, 2009 8:26 pm     Reply with quote

Hello...

Here, I need confirmation towards to the project that I'm working recently. I'm using PIC18F252 to retrieve data from the MCP3551 ADC converter by using SPI Mode 0,0 which mean that I will receive 4 bytes of data from it. The MCP3551 is used to measure temperature using RTD sensor. I already tested and I managed to get a constant reading after a minute. Sometimes the readings showed a weird reading before starting to give a constant reading again. The coding that I made only just for testing stage first to see whether the pic able to retrieve a constant reading or not. Another thing that I need to be sure is that should I need to recalculate the reading to display the real value? I used Pic C Compiler 4.084 and the code as below.
Code:

#include <18F252.h>
#device *= 16
#fuses HS,NOWDT,NOPROTECT,NOLVP, NOBROWNOUT, PUT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, stream = PC)

// SPI modes
#define SPI_MODE_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1  (SPI_L_TO_H)
#define SPI_MODE_2  (SPI_H_TO_L)
#define SPI_MODE_3  (SPI_H_TO_L | SPI_XMIT_L_TO_H)

void PC_fputc(char c)
{
   fputc(c, PC);
}

void main()
{
   int8 data0,data1,data2,data3;
   int32 RTD_Reading;

   setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_4);
       
   output_low(PIN_B0);
   delay_ms(1);
   output_high(PIN_B0);
     
   while(True)
   {   
      output_low(PIN_B0);
      delay_ms(1);
      output_high(PIN_B0);
   
      output_low(PIN_B0);
     
      data3 = spi_read(0x00);
      data2 = spi_read(0x00);
      data1 = spi_read(0x00);
      data0 = spi_read(0x00);
         
      output_high(PIN_B0);
     
      RTD_Reading = make32(data3,data2,data1,data0);
     
      delay_ms(500);
     
      output_high(PIN_A4);       
      printf(PC_fputc,"\r\nRTD Value  = %LX\r\n",RTD_Reading);       
      output_low(PIN_A4);
   }
}

Hopefully somebody can give a point of view or confirmation toward the project that I'm working on. Thank you.

To see the hardware connection please visit this site
http://www.flickr.com/photos/41963741@N08/?saved=1
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Aug 28, 2009 5:41 am     Reply with quote

Code:
   output_low(PIN_B0);
   delay_ms(1);
   output_high(PIN_B0);
     
   while(True)
   {   
      output_low(PIN_B0);
      delay_ms(1);
      output_high(PIN_B0);
Two times the same code sequence... some optimization is possible. Problem here is that you don't know if the PIC starts up with an active high or low output. I'd replace the first sequence with an output_high and delay of 100ms to allow the ADC to start up.

Code:
      output_low(PIN_B0);
      delay_ms(1);         <<-- this is overkill, 1us will do (requirement is 50ns minimum).
      output_high(PIN_B0);
                                 <<-- A delay is missing here !!!
      output_low(PIN_B0);
     
      data3 = spi_read(0x00);
      data2 = spi_read(0x00);
      data1 = spi_read(0x00);
      data0 = spi_read(0x00);
         
      output_high(PIN_B0);
It takes the ADC about 76ms to do the conversion. You are reading directly after starting the conversion, this will yield wrong results.
A quick-and-dirty solution is to insert a 100ms delay. Better, and more efficient, is to sample the ADC's _RDY pin.

What is connected to PIN_A4? Note that this is an Open Collector output, you'' have to add a pull-up resistor.

Code:
#device *= 16
This line is only required for PIC16 and smaller PIC types. On a PIC18 it has no effect.
khalis



Joined: 12 Feb 2009
Posts: 54

View user's profile Send private message

PostPosted: Mon Aug 31, 2009 8:08 pm     Reply with quote

Sorry for late reply. Thanks for the comment. PIN A4 is used for the communication. I used RS485 so the PIN A4 does not relate in SPI connection to the MCP 3551. Sorry to ask once again, where do I need to insert a pull-up resistor? Which pins are open collector?
Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Sep 01, 2009 1:27 pm     Reply with quote

Quote:
Which pins are open collector?

Download the 18F252 data sheet:
http://ww1.microchip.com/downloads/en/DeviceDoc/39564c.pdf
Look at the pin descriptions in this table:
Quote:
TABLE 1-2: PIC18F2X2 PINOUT I/O DESCRIPTIONS

Look for pins that are described as "OD" ("Open Drain"). These require
a pull-up resistor if they are used as output pins.
mkuang



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

PostPosted: Tue Sep 01, 2009 1:45 pm     Reply with quote

What does it mean exactly when a pin such as RA4 is ST (Schmitt trigger input)? Does it just mean it can be used as a comparator ?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Sep 01, 2009 2:00 pm     Reply with quote

For the 18F252, it means that, when used as an input pin, the Vil is 0.2 x
Vdd, and ViH is 0.8 x Vdd. So for a PIC running at +5v Vdd, a "logic low"
input must be 1.0 volts or less, and a "logic high" input level must be 4.0
volts or higher.

I can't find a spec for the input hysteresis, but based on other data sheets
from Microchip, it could be a number between 25mv and 100mv.
dyeatman



Joined: 06 Sep 2003
Posts: 1933
Location: Norman, OK

View user's profile Send private message

PostPosted: Tue Sep 01, 2009 2:03 pm     Reply with quote

Here is a reference on how it works:
http://en.wikipedia.org/wiki/Schmitt_trigger
_________________
Google and Forum Search are some of your best tools!!!!
khalis



Joined: 12 Feb 2009
Posts: 54

View user's profile Send private message

PostPosted: Tue Sep 01, 2009 8:35 pm     Reply with quote

Thanks for explanation, so when I use the pins that have open drain so I need to put a pull up resistor. And other pins which not an open drain can be connected directly without a pull up resistor. I also already modified my coding based on your previous post related to time for the conversion and it worked fine. I really appreciate that....
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