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

dsPIC30F4013 SPI Problem

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



Joined: 22 Oct 2009
Posts: 12

View user's profile Send private message

dsPIC30F4013 SPI Problem
PostPosted: Sun Jan 16, 2011 9:35 pm     Reply with quote

Hello I'm using AD7780 with dsPIC30F4013.
I'm always getting 0xFFFFFF as a result.
I've tried with 16B mode but no luck
any one can help?
Config. with 20MHz crystal
Code:

#include <30f4013.h>
#device *=16
#fuses hs2_pll8 
#fuses NOWDT
#use delay(clock=80M)


This is the initializer subroutine
Code:

void initz(){
setup_adc_ports(no_analogs);
setup_adc(adc_off);
set_tris_a(0b0000100000000000);
set_tris_b(0b0001111000000000);
set_tris_c(0x00);
set_tris_d(0b0000001100000000);
set_tris_f(0x00);

setup_spi(spi_master|spi_h_to_l|spi_clk_div_4|spi_ss_disabled);

setup_timer1(tmr_internal|tmr_div_by_1);
setup_timer2(tmr_internal|tmr_div_by_1);
setup_timer3(tmr_internal|tmr_div_by_1);   
   
ext_int_edge(h_to_l);
enable_interrupts(int_ext0);
disable_interrupts(int_ext1);
disable_interrupts(int_ext2);
   
enable_interrupts(int_timer1);
disable_interrupts(int_timer2);
enable_interrupts(int_timer3);               
enable_interrupts(intr_global);
}


This is the reading subroutine
Code:

int32 read_7780(){
int aMSB,aMB,aLSB,aStat;
int32 data;

output_high(spi_clk);
output_high(spi_pdrst);
while(input(spi_di));
aStat=spi_read(0);
aMSB=spi_read(0);
aMB=spi_read(0);
aLSB=spi_read(0);
output_low(spi_pdrst);

aMB<<=8;
aLSB= aMB + aLSB;
data=make32(aMSB,aLSB);

return data;
}


also tried this
Code:

int32 read_7780(){
int8 aMSB,aMB,aLSB,aStat;
int32 data;

output_high(spi_clk);
output_high(spi_pdrst);
while(input(spi_di));
aStat=spi_read(0);
aMSB=spi_read(0);
aMB=spi_read(0);
aLSB=spi_read(0);
output_low(spi_pdrst);

data=make32(aMSB,aMB,aLSB);

return data;
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jan 18, 2011 5:34 pm     Reply with quote

Quote:

setup_spi(spi_master|spi_h_to_l|spi_clk_div_4|spi_ss_disabled);

The parameter in bold is only supposed to be used with an SPI Slave.
If you use it for a Master, it can cause a change in your desired setup.
Delete that parameter.


Quote:

setup_spi(spi_master|spi_h_to_l|spi_clk_div_4|spi_ss_disabled);

According to the AD7780 data sheet (on page 5), SCLK idles at a high
level and samples the data on the rising edge. This is SPI Mode 3:
http://www.totalphase.com/support/kb/10045/#modes
I don't have the PCD compiler, so I don't know this for certain, but your
setup_spi() above may be using SPI Mode 2. You need to change it to
Mode 3.
cbkulatunge



Joined: 22 Oct 2009
Posts: 12

View user's profile Send private message

PostPosted: Tue Jan 18, 2011 10:02 pm     Reply with quote

Thanks for reply bro.

i was able to read data before using 18f452 using config below

Code:

void initz(){
   //ports
   output_a(0);
      setup_adc_ports(NO_ANALOGS);
      setup_adc(ADC_OFF);

   port_b_pullups(true);
   set_tris_a(0b00001011);
   setup_spi(spi_master|spi_h_to_l|spi_clk_div_4|spi_ss_disabled);
   bit_set(trisc,2);
   set_tris_e(0b00000000);
   set_tris_a(0b00001011);
   bit_clear(trisb,3);
   output_low(pin_b3);
   output_high(pin_a2);
   
   set_timer0(0x3d);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   
   disable_interrupts(GLOBAL|INT_TIMER0);

}
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Wed Jan 19, 2011 3:07 am     Reply with quote

To let us know, if your code sets the correct SPI mode or suffers from PCD bugs, you have to tell the PCD version.
cbkulatunge



Joined: 22 Oct 2009
Posts: 12

View user's profile Send private message

PostPosted: Wed Jan 19, 2011 3:46 am     Reply with quote

I'm using PCD 4.093 in MPLAB 8.60 IDE.

I also tried this
Code:

#word SPI1STAT =   0x220
#word SPI1CON =   0x222
#word TRISF =   0x2DE



   bit_set(TRISF,2);
   bit_clear(TRISF,6);
   bit_set(spi1stat,15);
   SPI1CON=0b0000000001111110;
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Jan 20, 2011 1:29 pm     Reply with quote

Quote:
I don't have the PCD compiler, so I don't know this for certain, but your
setup_spi() above may be using SPI Mode 2. You need to change it to
Mode 3.
With older PCD versions,
SPI_H_TO_L refers to mode 3, as required. The meaning has been changed around V4.109 to mode 2 according
to usual definition for 8-Bit PICs.

Code:
   bit_set(TRISF,2);
bit_clear(TRISF,6);
bit_set(spi1stat,15);
SPI1CON=0b0000000001111110;
Quote:

The dsPIC manual clarifies:
Most serial communication peripherals, when enabled, take full control of the I/O pin, so that the input pins associated
with the peripheral cannot be affected through the corresponding PORT registers.

This applies e.g. to SPI. In so far, presetting the clock input has no effect after SPI setup has been executed.

Enabling the SPI port should be always the last step in setup, some control settings may be ignored otherwise.

Apart from this points, I don't see other issues with your code. Normally, one would start hardware debugging at this
point, e.g. check if the SPI clock is operated, if data are send by the ADC, and so on.
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