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 support@ccsinfo.com

spi problem

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







spi problem
PostPosted: Wed Aug 15, 2007 11:03 pm     Reply with quote

hi
i am new to this ccs
i am now trying to communicate b/w to pics using spi and i have written code , but master is not clk signal
can anyone help
[#include<18F452.h>
//#use spi(FORCE_HW, BITS=8, stream=SPI_STREAM)
#use delay(clock=10000000)
#include<lcd.c>
#fuses HS,NOWDT,NOPROTECT//,NOLVP,PUT
#define CS PIN_A5
#define DA PIN_C5
#define DI PIN_C4
#define CLK PIN_C3
void main()
{
int16 a,b;
SET_TRIS_A(0x00);
SET_TRIS_C(0x10);
SET_TRIS_B(0x00);
b=0;
lcd_init();
lcd_gotoxy(1,1);
//output_high(DA);
// output_high(CLK);
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_16);
while(TRUE)
{
output_high(CS);
spi_write(b);
a=spi_read();
output_low(CS);
printf(lcd_putc,"%lu",a);
b=b+1;
delay_ms(100);
}
}
]
[#include<18f4455.h>
#use delay(clock=10000000)
#include<lcd.c>
#fuses HS,NOWDT,NOPROTECT,NOLVP,PUT
#define CS PIN_A0
#define DA PIN_C7
#define DI PIN_B0
#define CLK PIN_B1
void main()
{
int16 i;
SET_TRIS_A(0x00);
SET_TRIS_C(0x00);
SET_TRIS_B(0x01);
lcd_init();
lcd_gotoxy(1,1);
//output_high(DA);
//output_high(CLK);
// output_high(CS);
setup_spi(SPI_SLAVE | SPI_L_TO_H | SPI_CLK_DIV_16);
while(1)
{
i=spi_read();
spi_write(i);
}
}

]
ckielstra



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

View user's profile Send private message

PostPosted: Thu Aug 16, 2007 1:52 am     Reply with quote

When posting code please use the 'code' buttons in order to preserve the layout.

in your master change
Code:
a=spi_read();
to
Code:
a=spi_read(0);
The reason for this change is that the master is the only device that can generate clock signals on a SPI bus. When you write data to a slave the SPI bus is reading data from the slave at the same time.
Calling spi_read() without a parameter is returning the last read value (no clock signals). Calling spi_read(x) with a parameter will generate clock signals for writing value x and returns you a new value from the slave.


The slave will never generate clock pulses which makes the line below an invalid setup combination
Code:
 setup_spi(SPI_SLAVE | SPI_L_TO_H | SPI_CLK_DIV_16);
change to
Code:
 setup_spi(SPI_SLAVE | SPI_L_TO_H);


Do you have the Chip Select line from the master is connected to PIN_A5 on the slave?
saran
Guest







still no change
PostPosted: Thu Aug 16, 2007 3:38 am     Reply with quote

Thanku for ur reply
still there is no change
the clock is not generating (on seeing on oscilloscope)
I am getting only constant high signal on clk pin
i have the SS pins too
ckielstra



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

View user's profile Send private message

PostPosted: Thu Aug 16, 2007 3:56 am     Reply with quote

What is your compiler version?

Are you sure your processor is running? For example try toggling an I/O pin with a LED connected.

Set the NOLVP fuse again. At least 99% of the people don't use a low voltage programmer and than this is a dangerous feature. With LVP activated a high level voltage on PIN_B5 will cause your processor to stop.
saran
Guest







no problem in programming
PostPosted: Thu Aug 16, 2007 4:30 am     Reply with quote

I have tested the ic a
its working
i dont know , where the problem lies
ckielstra



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

View user's profile Send private message

PostPosted: Thu Aug 16, 2007 4:43 am     Reply with quote

No clock on the SPI bus means the master has a problem.

Try the very simple program below and see if you do get a clock signal.
Code:
#include<18F452.h>
#fuses HS, NOWDT, NOPROTECT, NOLVP, PUT
#use delay(clock=10000000)

void main()
{
  setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_16);
 
  while(TRUE)
  {
    spi_write(0xAA);          // 0xAA has no special meaning but has every bit different.
    delay_ms(100);
    output_toggle(PIN_A5);    // added so you can check the chip is running
  }
}


Besides the missing clock there is a problem with your chip select signal to the slave. The SS line is active low while you have it now designed for active high.

Again, what is your compiler version?
saran
Guest







no change
PostPosted: Fri Aug 17, 2007 1:00 am     Reply with quote

i am not getting the expected value (AA)
i am getting some garbage values
i am using PCWH version
ckielstra



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

View user's profile Send private message

Re: no change
PostPosted: Fri Aug 17, 2007 1:29 am     Reply with quote

saran wrote:
i am not getting the expected value (AA)
i am getting some garbage values
At least this means there is a clock. How are you checking the sent data? The slave code you posted has no method for displaying the received data.

How is your electrical connection between the master and slave? Which pins are connected?
Are both master and slave running at the same voltage?

Quote:
i am using PCWH version
The version number has 4 digits, something like 3.249 or 4.050. You can find this number on top of the list file (*.lst) or by using the 'Compiler version' tool (Start/Programs/PIC-C/Compiler version).
saran
Guest







spi...
PostPosted: Fri Aug 17, 2007 3:12 am     Reply with quote

CCS PCH C Compiler, Version 3.249, 28193 .
This is the compiler version ,i am using
I am same power supply for both the PICs
master 18f452
slave 18f4455
both the ss are connected
SDO of master is connected to SDI of slave
SDI of master is connected to SDO of slave
both the clocks are connected




Here is my modified code
Code:

#include<18F452.h>
#use delay(clock=10000000)
#include<lcd.c>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#define CS PIN_A5
#define DA PIN_C5
#define DI PIN_C4
#define CLK PIN_C3
void main()
{
    int16 a,b;
   SET_TRIS_A(0x00);
   SET_TRIS_C(0x10);
   SET_TRIS_B(0x00);
    b=0;
        lcd_init();
         lcd_gotoxy(1,1);
         lcd_putc('T');
        lcd_putc('X');
         lcd_gotoxy(1,2);
         lcd_putc('R');
        lcd_putc('X');
    setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_4);
    while(TRUE)
        {
         lcd_gotoxy(4,1);
         printf(lcd_putc,"%lu",b);
         output_high(CS);
         spi_write(b);
         a=spi_read(0);
         output_low(CS);
         lcd_gotoxy(4,2);
         printf(lcd_putc,"%lu",a);
         b=b+1;
        delay_ms(500);
        }
}




slave code
Code:

#include<18f4455.h>
#use delay(clock=10000000)
#fuses HS,NOWDT,NOPROTECT,NOLVP,PUT
#define CS PIN_A5
#define DA PIN_C7
#define DI PIN_B0
#define CLK PIN_B1
void main()
{
    int16 i,a=999;
   SET_TRIS_A(0x21);
   SET_TRIS_C(0x00);
   SET_TRIS_B(0x03);
    setup_spi(SPI_SLAVE | SPI_H_TO_L );
    while(1)
      {
       i=spi_read(0);
      // delay_ms(50);
       spi_write(i);
       delay_ms(400);
       //a--;
      }
}
[/quote]
ckielstra



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

View user's profile Send private message

Re: no change
PostPosted: Fri Aug 17, 2007 3:19 am     Reply with quote

saran wrote:
i am not getting the expected value (AA)
i am getting some garbage values
At least this means there is a clock. How are you checking the sent data? The slave code you posted has no method for displaying the received data.

How is your electrical connection between the master and slave? Which pins are connected?
Are both master and slave running at the same voltage?

Quote:
i am using PCWH version
The version number has 4 digits, something like 3.249 or 4.050. You can find this number on top of the list file (*.lst) or by using the 'Compiler version' tool (Start/Programs/PIC-C/Compiler version).
saran
Guest







nc
PostPosted: Fri Aug 17, 2007 3:38 am     Reply with quote

CCS PCH C Compiler, Version 3.249, 28193 .
This is the compiler version ,i am using
I am same power supply for both the PICs
master 18f452
slave 18f4455
both the ss are connected
SDO of master is connected to SDI of slave
SDI of master is connected to SDO of slave
both the clocks are connected




Here is my modified code
Master code\

Code:

#include<18F452.h>
#use delay(clock=10000000)
#include<lcd.c>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#define CS PIN_A5
#define DA PIN_C5
#define DI PIN_C4
#define CLK PIN_C3
void main()
{
    int16 a,b;
   SET_TRIS_A(0x00);
   SET_TRIS_C(0x10);
   SET_TRIS_B(0x00);
    b=0;
        lcd_init();
         lcd_gotoxy(1,1);
         lcd_putc('T');
        lcd_putc('X');
         lcd_gotoxy(1,2);
         lcd_putc('R');
        lcd_putc('X');
    setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_4);
    while(TRUE)
        {
         lcd_gotoxy(4,1);
         printf(lcd_putc,"%lu",b);
         output_high(CS);
         spi_write(b);
         a=spi_read(0);
         output_low(CS);
         lcd_gotoxy(4,2);
         printf(lcd_putc,"%lu",a);
         b=b+1;
        delay_ms(500);
        }
}
 




slave code

Code:

#include<18f4455.h>
#use delay(clock=10000000)
#fuses HS,NOWDT,NOPROTECT,NOLVP,PUT
#define CS PIN_A5
#define DA PIN_C7
#define DI PIN_B0
#define CLK PIN_B1
void main()
{
    int16 i,a=999;
   SET_TRIS_A(0x21);
   SET_TRIS_C(0x00);
   SET_TRIS_B(0x03);
    setup_spi(SPI_SLAVE | SPI_H_TO_L );
    while(1)
      {
       i=spi_read(0);
      // delay_ms(50);
       spi_write(i);
       delay_ms(400);
       //a--;
      }
}
ckielstra



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

View user's profile Send private message

PostPosted: Fri Aug 17, 2007 8:09 am     Reply with quote

You are using a stable compiler version and the hardware setup looks correct.

Quote:
... there is a problem with your chip select signal to the slave. The SS line is active low while you have it now designed for active high.
This bug is still present causing your slave never to become active when you are writing data. Reverse the output levels of the CS pin.

In the slave
Code:
spi_write(i);
I haven't looked at the assembly code for this, but all example code is only using the spi_read() functions in the slave.
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