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

SPI help
Goto page Previous  1, 2, 3  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ckielstra



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

View user's profile Send private message

Re: SPI Help
PostPosted: Sun Dec 03, 2006 3:59 pm     Reply with quote

das wrote:
Looking at the Mode 0 0 it generates 4 bytes - not three based on the Figure 5-7.
You are right, I reversed 0,0 and 1,1.
das
Guest







SPI Help
PostPosted: Sun Dec 03, 2006 4:11 pm     Reply with quote

Thank you for the help. I am having some issues compiling the low level changes in. I am getting errors on your recommendation. Do these need another specification?
ckielstra



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

View user's profile Send private message

PostPosted: Sun Dec 03, 2006 4:22 pm     Reply with quote

What is the version number of your compiler?
ckielstra



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

View user's profile Send private message

PostPosted: Sun Dec 03, 2006 5:05 pm     Reply with quote

A problem in your program is that you are trying to read the _RDY signal from PIN_C4. This will return undefined values as the pin is used by the SPI module.
Not tested, but could you give the following program a try?

Code:
#include <16F877.h>
#fuses HS, NOWDT, NOPROTECT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#define SPI_MODE_0_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_0_1 (SPI_L_TO_H)
#define SPI_MODE_1_0 (SPI_H_TO_L)
#define SPI_MODE_1_1 (SPI_H_TO_L | SPI_XMIT_L_TO_H)

#define LF  10
#define CR  13

#bit SSPEN=0x14.5


void main()
{
  int8 byte_1, byte_2, byte_3;
  int8 i;
  int32 value;

  output_high(PIN_D0); //Start with CS high
  setup_spi(SPI_MASTER | SPI_MODE_1_1 | SPI_CLK_DIV_4 );

  puts("Initialized"); //Output string for debug
  while(TRUE)
  {
    // Toggle CS in order to start AD conversion
    output_low(PIN_D0);
    output_high(PIN_D0);
   
    // Wait for AD conversion to finish
    SSPEN = 0;  // disable SPI so we can access the data line.
    do
    {
      // The Ready state is latched on each falling edge of CS and will not
      // dynamically update if CS is held low. CS must be toggled high
      // through low.
      output_low(PIN_D0);
      if (input(PIN_C4) == 0) // Check to see if the conversion is completed
        break;                // Break out of the loop, leaving _CS active.
      output_high(PIN_D0);
    } while (TRUE);
    SSPEN = 1;  // Enable SPI again

    // Read value from ADC   
    byte_3 = spi_read(0);
    byte_2 = spi_read(0);
    byte_1 = spi_read(0);

    output_high(PIN_D0);  // De-select ADC

    value = make32(0, byte_3, byte_2, byte_1);
    for (i = 32; i>0; i--)
    {
      putc(bit_test(value, i-1) + '0');
    }
    putc(CR);
    putc(LF);

    delay_ms(10000);
  }
}
Ttelmah
Guest







PostPosted: Mon Dec 04, 2006 3:43 am     Reply with quote

PIN C4, should read OK.
If you look at the 'logic' for the I/O pin, there is a pickoff for read, and a picoff for the SPI, and both should be working. When SPI is enabled, it overrides the logic feeding the 'output' connections, but doesn't affect the input connections in this case.
I have driven the ADC involved here OK, but have only used it in single conversion mode, not continuous conversion mode.
I used a pull up on the SD line, and read PIN C4 fine.

Best Wishes
Ttelmah
Guest







PostPosted: Mon Dec 04, 2006 3:48 am     Reply with quote

I think there is a critical misnderstanding about the useable 'modes' going on here.
If you use the four byte transfer, you cannot read C4, to get the RDY status. When four byte transfer is used, the RDY bit is transferred as the first _bit_ of the fourth byte, and from then he line sits high. To have the bit available to read on pin C4, you need to use the 25bit transfer mode.

Best Wishes
ckielstra



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

View user's profile Send private message

PostPosted: Mon Dec 04, 2006 8:01 am     Reply with quote

Ttelmah wrote:
PIN C4, should read OK.
If you look at the 'logic' for the I/O pin, there is a pickoff for read, and a picoff for the SPI, and both should be working. When SPI is enabled, it overrides the logic feeding the 'output' connections, but doesn't affect the input connections in this case.
You are right. Because the digital output is disabled when using SPI I assumed the same to be true for the input. Instead of only reading the table of PortC functions I should have looked at the diagrams as well.

Quote:
I think there is a critical misnderstanding about the useable 'modes' going on here.
If you use the four byte transfer, you cannot read C4, to get the RDY status. When four byte transfer is used, the RDY bit is transferred as the first _bit_ of the fourth byte, and from then he line sits high. To have the bit available to read on pin C4, you need to use the 25bit transfer mode.
Sorry, I don't agree with you here. As I read Figure 5-7 the RDY bit is in the first bit of the _first_ byte. At first I thought we had a misunderstanding in numbering the bytes, which one is first and which one fourth? But then you wrote 'from then the line sits high', this is only true for the byte most right in the picture which I also see as fourth. The fourth byte contains the LSB data bit, not the RDY status bit !

Besides the 3 or 4 byte transfer 'mode' there is also the choice of 'single conversion' and 'continuous conversion' mode. The behaviour of the RDY pin is different in both modes:
1) In the continuous mode the CS pin is always low and the RDY pin dynamically changes depending on the state of the internal conversion process.
2) In 'single conversion' mode the RDY pin shows the Ready state at the time of falling _CS edge. There are no dynamic updates of the RDY pin, in order to get a status update CS must be toggled high through low.

So, in 'single conversion' mode you can always read the RDY status by toggling the CS line, this is independent of reading the data with 3 or 4 byte transfers.

Although my posted code can be simplified by removing all lines referring to SSPEN, I guess it should work as it is. I don't have access to a MCP3551 chip to confirm this, so shoot and tell me if I'm wrong. Smile
das
Guest







SPI Help
PostPosted: Mon Dec 04, 2006 8:13 am     Reply with quote

I can change it to single conversion mode... I just need it to work ASAP. Can I see your code that you used to drive it in single conversion mode?
Ttelmah
Guest







PostPosted: Mon Dec 04, 2006 8:16 am     Reply with quote

The more I read this part of the sheet, the more I am inclined to go back to my original statement, that you cannot (sensibly) use the 4 byte mode, for continuous conversions!.
The diagram that shows the status in the first bit, is from a mode with CS enabled, where the bit becomes available when CS is lowered, and is then clocked in as the first bit of the first byte. The description text for the two wire transfer (without CS), says that the RDY line activates one clock after 24 bits are transferred, and a transfer cannot be interrupted. As such then, four byte mode cannot be used this way. Look at paragraph 5.4.1. It is the only one that refers to the mode being used, and makes no mention of using a four byte transfer.

Best Wishes
das
Guest







SPI Help
PostPosted: Mon Dec 04, 2006 9:47 am     Reply with quote

Does anyone have code that I can test? I tried ckielstra's code (above), but it still yields zero...
ckielstra



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

View user's profile Send private message

PostPosted: Mon Dec 04, 2006 9:58 am     Reply with quote

Back to the basics:
1) What is your compiler version (check the top of the *.lst file)
2) Which processor are you using?
3) What frequency and voltage are you running the processor at?
4) How is the MCP3551 connected? Pull-up / pull-down resistors, etc.. Schematic picture if possible.
5) Other connected hardware?
6) Do you want to run the A/D in continuous or single conversion mode?
7) Provide any other information that might slightly be important to us.
das
Guest







PostPosted: Mon Dec 04, 2006 10:05 am     Reply with quote

Back to the basics:
1) What is your compiler version (check the top of the *.lst file)
The compiler is new... the latest compiler version.
2) Which processor are you using?
PIC16F877A
3) What frequency and voltage are you running the processor at?
4MHz
4) How is the MCP3551 connected? Pull-up / pull-down resistors, etc.. Schematic picture if possible.
Temporary pull up on CS, otherwise no pullups. C3 is the clock, D0 is the CS, DI is C4. All have 1K resistors inline.
5) Other connected hardware?
The 3551 is routed thru a CS3002 dual Op-amp per the weight application
6) Do you want to run the A/D in continuous or single conversion mode?
It does not matter if I run in continuos or single. I just thought it was easier to run in continuous. If it is not I will run in single.
7) Provide any other information that might slightly be important to us
ckielstra



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

View user's profile Send private message

PostPosted: Mon Dec 04, 2006 10:21 am     Reply with quote

Quote:
The compiler is new... the latest compiler version.
If you mean it is a compiler in the 4.xxx series than I have to suggest you stop using it and return to v3.249 (also available for download on the CCS website). The v4.xxx compilers are to be considered Beta versions, do a search on this forum for more information.
das
Guest







SPI Help
PostPosted: Mon Dec 04, 2006 12:36 pm     Reply with quote

This is the downloaded Demo version. I would like to make sure that this will work before I will buy it.
das
Guest







SPI Help
PostPosted: Mon Dec 04, 2006 12:38 pm     Reply with quote

ckielstra's code still yielded zeros.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3  Next
Page 2 of 3

 
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