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 - No Data Signals

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



Joined: 10 Mar 2005
Posts: 6

View user's profile Send private message

SPI - No Data Signals
PostPosted: Thu Mar 10, 2005 4:59 pm     Reply with quote

Hi,

I am having serious problems trying to get the SPI to work. I have a PIC 18F4431 microcontroller, and the CCS C Compiler V3.212. All that I am trying to do is write some data out onto the SPI, but no data appears at the output (SD0) pin.

Currently I am using the default pins:

C3 = SCL
C4 = SDI
C7 = SDO

Using a scope, I have been able to determine the following:

1) The SCK signal IS present on pin c3, and is working correctly (i.e. outputs 8 clocks every time that I call spi_write).

2) The data out signal is always high (i.e. 5V), and does not change no matter what I write using spi_write().

3) If I try to use the alternate SPI pins:

D3 = SCL
D2 = SDI
D1 = SDO

then I don't even get the clock signal (on D3) anymore.

Here is the code that I am using:

C File:

void main()
{

setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_CLK_DIV_16);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);

while(1)
{
spi_write(0xF0);
}
}

Header File:

#include <18F4431.h>
#device ICD=TRUE
#device adc=8
#use delay(clock=40000000)
#fuses NOWDT,WDT128,HS, NOPROTECT, IESO, BROWNOUT, BORV27, NOPUT, NOCPD, STVREN, DEBUG, NOLVP, NOWRT, NOWRTD, FCMEN, NOWINEN, T1LOWPOWER, HPOL_HIGH, NOWRTC, NOWRTB, NOEBTR, NOEBTRB, NOCPB, LPOL_HIGH, PWMPIN, NOMCLR, FLTAC1, SSP_RC

This seems like it could be a problem with the compiler.... but I have no idea why or what I can do to fix it. Does anyone know what I am doing wrong?

As well, if I connect the clock signal to the clock pin (C5) on another PIC which is configured as slave, the clock signal voltage drops from 5V peak to about 1V peak...

Thank you,

Andy
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Mar 10, 2005 5:54 pm     Reply with quote

The first thing I see is that your #use delay() statement is set for 40 MHz,
but that implies that you're using either the 4x PLL mode (with a 10 MHz
crystal) or the external clock mode. But then you've got "HS" in your
#fuses statement. You would need either "H4" or "EC" instead.
My suggestion is, don't try anything fancy while in the early stages --
just run it at 10 MHz, in HS mode. Do this: #use delay(10000000)

Also, when you tried the alternate pins on Port D, did you remove the
"SSP_RC" fuse setting ? That setting puts the SSP pins on Port C.
v38zy



Joined: 10 Mar 2005
Posts: 6

View user's profile Send private message

PostPosted: Thu Mar 10, 2005 6:22 pm     Reply with quote

Thanks for the quick reply Smile

I am using a development board which has a 40MHz crsytal on it, I can't change that. The clock is connected to the CLKI pin on the PIC.

I tried changing the fuse from HS to H4 and EC as you mentioned, but it didn't resolve anything. I even tried using the internal oscillator by setting fuse to INTRC and #use delay(8000000)... but I got the same result.

Yes, I did change SSP_RC to SSP_RD when I was trying to use the alternate pins on port D.

Any other suggestions?

Thanks,

Andy
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Mar 10, 2005 6:31 pm     Reply with quote

Is the documentation for your development board available on the net ?
Can you post a link to it ?
Guest








PostPosted: Thu Mar 10, 2005 6:39 pm     Reply with quote

No There is no documentation it is not publically available. It was built for the students to use at the University which I am attending.

There isn't much to it, it is just the PIC device, a power connector, the ICD connector, and a bunch of 10 pin headers which are connected to each of the ports on the PIC.

Andy
v38zy



Joined: 10 Mar 2005
Posts: 6

View user's profile Send private message

PostPosted: Thu Mar 10, 2005 6:40 pm     Reply with quote

The previous post about the board was written by me... not sure why it shows up as Guest.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Mar 10, 2005 7:06 pm     Reply with quote

I don't have your version of the compiler, but I compiled the following
test program with vs. 3.188 and saw a problem with the TRIS setup.
Can you compile this program with your compiler, and post the .LST file ?
Then I can see if there's a problem.

When you post it, don't show your CCS user number. It's the number
right after the version at the top of the .LST file. Example:

CCS PCH C Compiler, Version 3.188, xxxxx

I don't know if giving that away makes it easier to for someone else
to logon and download the compiler, but why risk it ?

Code:
#include <18F4431.h>
#fuses EC, NOPROTECT,PUT,BROWNOUT,NOLVP,NOWDT
#use delay(clock=40000000)

//============================
void main()
{
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_16);

while(1)
  {
   spi_write(0xF0);
  }

}
v38zy



Joined: 10 Mar 2005
Posts: 6

View user's profile Send private message

PostPosted: Thu Mar 10, 2005 7:20 pm     Reply with quote

Ok, I compiled the program using the code that you provided. Here is the .lst file that was produced (minus the user number).

Code:

CCS PCH C Compiler, Version 3.214, xxxxx               10-Mar-05 21:16

               Filename: C:\Documents and Settings\Andy\Desktop\Test3\Test.LST

               ROM used: 50 bytes (0%)
                         Largest free fragment is 16334
               RAM used: 5 (1%) at main() level
                         5 (1%) worst case
               Stack:    0 locations

*
0000:  GOTO   0004
....................  #include <18F4431.h> 
....................  //////// Standard Header file for the PIC18F4431 device //////////////// 
.................... #device PIC18F4431 
.................... #list 
.................... 
.................... #fuses EC, NOPROTECT,PUT,BROWNOUT,NOLVP,NOWDT   
.................... #use delay(clock=40000000)   
....................   
.................... //============================   
.................... void main()   
.................... {   
0004:  CLRF   FF8
0006:  BCF    FD0.7
0008:  CLRF   FEA
000A:  CLRF   FE9
000C:  BCF    FB9.0
000E:  BCF    FC1.6
0010:  BCF    FC1.7
0012:  MOVLW  00
0014:  MOVWF  FB8
.................... setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_16);   
0016:  BCF    FC6.5
0018:  BCF    F94.5
001A:  BCF    F94.3
001C:  MOVLW  21
001E:  MOVWF  FC6
0020:  MOVLW  00
0022:  MOVWF  FC7
....................   
.................... while(1)   
....................   {   
....................    spi_write(0xF0);   
0024:  MOVF   FC9,W
0026:  MOVLW  F0
0028:  MOVWF  FC9
002A:  BTFSS  FC7.0
002C:  BRA    002A
....................   }   
002E:  BRA    0024
....................   
.................... }   
.................... 
0030:  SLEEP

Configuration Fuses:
   Word  1: C400   EC IESO FCMEN
   Word  2: 3E0E   BROWNOUT NOWDT PUT NOWINEN WDT32768
   Word  3: 9D3C   T1LOWPOWER HPOL_HIGH LPOL_HIGH PWMPIN NOMCLR FLTAC1 SSP_RC
   Word  4: 0081   STVREN NODEBUG NOLVP
   Word  5: C00F   NOPROTECT NOCPD NOCPB
   Word  6: E00F   NOWRT NOWRTD NOWRTC NOWRTB
   Word  7: 400F   NOEBTR NOEBTRB


Thanks,

Andy
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Mar 10, 2005 9:35 pm     Reply with quote

There's a bug in the setup_spi() function. It's using the SCK and SDO
pins as if this was a 18F452. The problem is that in the 18F4431,
Microchip moved the SPI signals to different pins.

The code is setting up pins C3 and C5 to be outputs, and you really want
it on pins C5 and C7. So, the minimal fix would be to just set the TRIS
on C7 to be an output. See the revised test program below.

Code:

...... setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_16);   
0016:  BCF    FC6.5
0018:  BCF    F94.5   
001A:  BCF    F94.3  // *** Bug ***  Should be F94.7
001C:  MOVLW  21
001E:  MOVWF  FC6
0020:  MOVLW  00
0022:  MOVWF  FC7


To fix this bug, add the lines shown in bold, below:

#include <18F4431.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)

#byte TRISC = 0xF94
#bit SDO_TRIS = TRISC.7

//============================
void main()
{
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_16);

SDO_TRIS = 0; // Bug fix: Configure the SDO pin as an output

while(1)
{
spi_write(0xF0);
}

}
v38zy



Joined: 10 Mar 2005
Posts: 6

View user's profile Send private message

PostPosted: Thu Mar 10, 2005 10:18 pm     Reply with quote

I can now see the data being sent out on C7!!! Thank you soo much, I was beginning to get pretty frustrated with this problem.

Is this the solution to the problem if I use the alternate pins on port D as well? Here is the .lst file if I add the fuse SSP_RD... Do you know what I should change?

Code:

CCS PCH C Compiler, Version 3.214, xxxxx               11-Mar-05 00:14

               Filename: C:\Documents and Settings\Andy\Desktop\Test3\Test.LST

               ROM used: 50 bytes (0%)
                         Largest free fragment is 16334
               RAM used: 5 (1%) at main() level
                         5 (1%) worst case
               Stack:    0 locations

*
0000:  GOTO   0004
....................  #include <18F4431.h> 
....................  //////// Standard Header file for the PIC18F4431 device //////////////// 
.................... #device PIC18F4431 
.................... #list 
.................... 
.................... #fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP, SSP_RD   
.................... #use delay(clock = 4000000)   
....................   
.................... void main()   
.................... {   
0004:  CLRF   FF8
0006:  BCF    FD0.7
0008:  CLRF   FEA
000A:  CLRF   FE9
000C:  BCF    FB9.0
000E:  BCF    FC1.6
0010:  BCF    FC1.7
0012:  MOVLW  00
0014:  MOVWF  FB8
.................... setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_16);   
0016:  BCF    FC6.5
0018:  BCF    F94.5
001A:  BCF    F94.3
001C:  MOVLW  21
001E:  MOVWF  FC6
0020:  MOVLW  00
0022:  MOVWF  FC7
....................   
.................... while(1)   
.................... {   
....................    spi_write(0xF0); 
0024:  MOVF   FC9,W
0026:  MOVLW  F0
0028:  MOVWF  FC9
002A:  BTFSS  FC7.0
002C:  BRA    002A
.................... }   
002E:  BRA    0024
....................   
.................... } 
.................... 
0030:  SLEEP

Configuration Fuses:
   Word  1: C100   XT IESO FCMEN
   Word  2: 3E0E   BROWNOUT NOWDT PUT NOWINEN WDT32768
   Word  3: 993C   T1LOWPOWER HPOL_HIGH LPOL_HIGH PWMPIN NOMCLR FLTAC1 SSP_RD
   Word  4: 0081   STVREN NODEBUG NOLVP
   Word  5: C00F   NOPROTECT NOCPD NOCPB
   Word  6: E00F   NOWRT NOWRTD NOWRTC NOWRTB
   Word  7: 400F   NOEBTR NOEBTRB


Thanks again,

Andy
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Mar 10, 2005 11:53 pm     Reply with quote

Quote:
Is this the solution to the problem if I use the alternate pins on port D as well?

The ASM code is wrong for Port D. It's setting TRIS for Port C.
The compiler isn't looking at the #fuse setting, in order to set the
TRIS for the correct port.
----------
If you want to use either port, here's a more general solution, as shown
below. You just comment out the top #define statement if you want to
use port C for SPI, and un-comment it for Port D.

The TRIS settings for each port are now in constants, so you can easily
change them if you want to. If you use standard i/o (which is the default)
then you can use "output_low(PIN_D0);" for example, and the compiler
will set Pin D0 as an output without affecting the TRIS for the SPI pins.
Code:

#include <18F4431.h>

#define SSP_PORT_D  1    // Comment out if using SSP on Port C

#ifdef SSP_PORT_D
  #fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP, SSP_RD
#else
  #fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP, SSP_RC
#endif

#use delay(clock = 4000000)

#define PORT_C_TRIS  0x5F  // Port C SPI:  Bits 5 and 7 must be outputs
#define PORT_D_TRIS  0xF5  // Port D SPI:  Bits 1 and 3 must be outputs


//============================
void main()
{
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_16);

// Fix the bug with the TRIS settings on the SPI pins.
#ifdef SSP_PORT_D
  set_tris_d(PORT_D_TRIS);
#else
  set_tris_c(PORT_C_TRIS);
#endif


while(1)
  {
   spi_write(0xF0);
  }

}
v38zy



Joined: 10 Mar 2005
Posts: 6

View user's profile Send private message

PostPosted: Fri Mar 11, 2005 7:40 am     Reply with quote

Great! It works perfectly now Smile

I really really appreciate your help PCM programmer, I never would have figured that problem out myself. People like yourself are what make these forums so useful Smile
v38zy



Joined: 10 Mar 2005
Posts: 6

View user's profile Send private message

PostPosted: Fri Mar 11, 2005 8:08 am     Reply with quote

One final question:

When I set up the slave, I assume that I will have to make similar modifications... Here is the .lst file for the slave, and I would like to use port D... Is there somewhere that I can get a listing of all the registers within the PIC so that I can make the necessary changes?

Code:

....................    setup_spi(SPI_SLAVE|SPI_SS_DISABLED); 
0030:  BCF    FC6.5
0032:  BCF    F94.5
0034:  BSF    F92.5
0036:  BSF    F94.3
0038:  MOVLW  25
003A:  MOVWF  FC6
003C:  MOVLW  00
003E:  MOVWF  FC7


Thanks,

Andy
dyeatman



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

View user's profile Send private message

PostPosted: Fri Mar 11, 2005 8:28 am     Reply with quote

The listing of all the registers (and LOTS of other good info) is found in the PIC datasheet.

You get the datasheet from the Microchip website: www.microchip.com
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

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

PostPosted: Fri Mar 11, 2005 8:31 am     Reply with quote

v38zy wrote:
One final question:

When I set up the slave, I assume that I will have to make similar modifications... Here is the .lst file for the slave, and I would like to use port D... Is there somewhere that I can get a listing of all the registers within the PIC so that I can make the necessary changes?

Code:

....................    setup_spi(SPI_SLAVE|SPI_SS_DISABLED); 
0030:  BCF    FC6.5
0032:  BCF    F94.5
0034:  BSF    F92.5
0036:  BSF    F94.3
0038:  MOVLW  25
003A:  MOVWF  FC6
003C:  MOVLW  00
003E:  MOVWF  FC7


Thanks,

Andy


Goto Microchip's website and download the datasheet for that PIC. It has all the registers and even tells you how to setup the SPI without using CCS's functions. The problem that you are experiencing is the reason why I usually do not use the "special" CCS functions. They are prone to errors of this sort especially when support is added for the newer chips. Sometimes these functions save time but as you can see sometimes they make your life miserable. After coding for a while, you will develop a library of code snippets that you can reuse and you will find that reading the datasheet and understanding the chip will make you a more informed programmer.
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