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

#USE SPI issue

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



Joined: 27 Mar 2015
Posts: 1

View user's profile Send private message

#USE SPI issue
PostPosted: Thu Jul 16, 2015 8:25 am     Reply with quote

Hello All,

Hopefully a quick question. I'm working with a dsPIC33FJ256MC710A and Analog Device's AD7655 16 bit ADC.

My CCS C compiler version is: 5.026 with MPLAB 2.35

I'm using #USE SPI to setup the SPI2 Hardware on the dsPIC.

When I turn on my system it seems to print 0x0000 for roughly a minute or two then reports valid data. Is there a known bug with the #USE SPI for this compiler version, or am I just doing something incredibly stupid?

The Analog Devices' guy is pushing an SPI timing issue. but once it starts printing out valid data its stays valid for days - so i'm not wholly convinced.

Thanks in advance.

Code:

#include <33FJ256MC710A.h>
#include <STDlib.h>
#USE delay(internal=40MHz)
#USE RS232(BAUD=115200,XMIT=PIN_F3,RCV=PIN_F2,ERRORS)           
#USE SPI(SPI2,FORCE_HW,MASTER,DI=PIN_G7,CLK=PIN_G6,MODE=1,BAUD=56200,XFER16)              //ADC SPI Interface

//Rev. B Boards:
#define ADC_A0          PIN_B0      //Pin Definition for AD7655 MUX Select
#define ADC_CNVST       PIN_E5      //Pin Definition for AD7655 Conversion Start
#define ADC_CS          PIN_E6      //Pin Definition for AD7655 Chip Select
#define ADC_BUSY        PIN_E7      //Pin Definition for AD7655 Busy Signal
int16 chanA = 0;                    //Channel A ADC Reading
int16 chanB = 0;                    //Channel B ADC Reading

void main(){

  delay_ms(5000);
  output_high(ADC_CS);
  output_high(ADC_A0);
  output_high(ADC_CNVST);
  delay_ms(1000);
  output_low(ADC_CS);
  printf("TEST...TEST...TEST..");
  delay_ms(2000);
   
    while(TRUE){
                        output_low(ADC_CNVST);                      //Pulls Conversion Pulse low
         delay_us(1);                                //Delay for Conversion Pulse
         output_high(ADC_CNVST);
         while(input_state(ADC_BUSY) == 1);          //Waits for ADC to respond with end of conversion
                        delay_us(1);
                        delay_us(2);
         chanA = SPI_READ2_16(0x0000);               //Sends clock for SPI read (16 bit wide)
         chanB = SPI_READ2_16(0x0000);               //Sends clock for SPI read (16 bit wide)
                        delay_ms(1);
                        printf("%4X  %4X",chanA,chanB);
                        chanA =0;
                        chanB =0;
                        delay_ms(2000);
                       
                }
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19451

View user's profile Send private message

PostPosted: Thu Jul 16, 2015 10:55 am     Reply with quote

When using #use spi, you _must_ use spi_xfer, not spi_read or spi_write. It'll 'sort of work - sometimes' if you mix the two usages.

That you are doing a define to handle the 16bit transfer, suggests you are not using spi_xfer (spi_xfer can do a 16bit transfer). You don't show us though....

However, set the SPI up differently. Just use SPI2 (you don't need the pin numbers when this is used - selecting both is pointless). Also if the hardware is specified with SPI2, FORCE_HW is also pointless. Get rid of the XFER16 definition (not needed the default setting allows up to four bytes to be transferred), give the stream a name, and add the CS pin to the setup.
#USE SPI(SPI2, MASTER, MODE=1, BAUD=56200, ENABLE=PIN_E6, STREAM=ADC)

Then define the value as a 32bit variable, and

value = spi_xfer(ADC, value, 32);

will automatically operate the CS, and transfer 32bits as one transaction
guy



Joined: 21 Oct 2005
Posts: 297

View user's profile Send private message Visit poster's website

PostPosted: Sat Jul 18, 2015 9:47 am     Reply with quote

Wild guess: the #delay doesn't fit the actual speed in which the chip is actually running, this resulting in the delays (delay_ms(5000) etc.) being much longer than excepted...?
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