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

PCD: Changing SPI Clock with #use SPI

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



Joined: 25 Nov 2009
Posts: 9
Location: Albuquerque NM

View user's profile Send private message

PCD: Changing SPI Clock with #use SPI
PostPosted: Tue Mar 09, 2010 4:24 pm     Reply with quote

Hi,

I am interfacing an AD7367 ADC to a 24FJ128GA006. I have the code working but I'm getting way too much noise. Its on a breadboard so that is some of it but I want to check if some of it is in the SPI sampling so I need to slow the clock since #use SPI sets the fastest clock. I cannot seem to get the SPICON1 clock bits to change.

I'm running PCD & IDE version 4.105 with a CCS PIC 24 Demo board.

Thanks for your help.

Dave

Code:
#include "C:\Program Files\PICC\24F Exercises\A2D Test7.h"
#include <stdio.h>

#use rs232(UART1, DEBUGGER)
#use spi(SPI1, MASTER, MODE=3, BITS=32, STREAM=A2D_Data)
#use fast_io(D)

//SPI chip select and conversion start pins
#define AD7367_nCS PIN_D0
#define AD7367_nCNVST PIN_D1

#bit SPI_EN = 240.15
#bit SPI_DIV_64 = 242.0
#bit SPI_DIV_16 = 242.1


/* Print n as a binary number */
void printbitssimple(int n) {
    unsigned int i;
    i = 1<<(sizeof(n) * 8 - 1);

    while (i > 0) {
        if (n & i)
            printf("1");
        else
            printf("0");
        i >>= 1;
    }
}

void main()
{
   
    int32 A2D_Result;
    int16 ADC_A_RES, ADC_B_RES, Dummy;
    int8  k=0,n=11;
   
    set_tris_D(0x00);
     
    SPI_EN = 0;
    SPI_DIV_64 = 0;
    SPI_DIV_16 = 0;
    SPI_EN = 1;
   
    while(k < n )
    {
   
    output_low(AD7367_nCNVST);//Pulse to conversion start
    output_high(AD7367_nCNVST);
       
    output_low(AD7367_nCS);//Enable the data line of the A2D
           
    ADC_A_RES = spi_xfer(A2D_Data,Dummy,16);//read the results
   
    ADC_A_RES = ADC_A_RES>>2;
 

    ADC_B_RES = spi_xfer(A2D_Data,Dummy,16);
           
    ADC_B_RES = shift_right(&ADC_B_RES,2,0);
    ADC_B_RES = shift_right(&ADC_B_RES,2,0);
     
    output_high(AD7367_nCS);//Tristate the data line

    printbitssimple(ADC_A_RES);
    printf("\n\r");
   
    delay_ms(1);
   
    k++;
    }//end while

}


A2D Test7.h
Code:
#include<24FJ128GA006.h>

#FUSES NOWDT                     //No Watch Dog Timer
#FUSES NOJTAG                    //JTAG disabled
#FUSES NOPROTECT                 //Code not protected from reading
#FUSES NOWRT                     //Program memory not write protected
#FUSES NODEBUG                   //No Debug mode for ICD
#FUSES ICSP2                     //ICD uses PGC2/PGD2 pins
#FUSES NOWINDIS                  //Watch Dog Timer in Window mode
#FUSES WPRES128                  //Watch Dog Timer PreScalar 1:128
#FUSES WPOSTS16                  //Watch Dog Timer PostScalar 1:32768
#FUSES NOIESO                    //Internal External Switch Over mode disabled
#FUSES FRC_PS                    //Fast RC Oscillator with Post Scaler
#FUSES NOCKSFSM                  //Clock Switching is disabled, fail Safe clock monitor is disabled
#FUSES NOOSCIO                   //OSC2 is clock output
#FUSES NOPR                      //Pimary oscillaotr disabled


#device ICD=TRUE
#fuses HS,PR
#use DELAY (CLOCK=20000000)

_________________
Dave N
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 09, 2010 8:19 pm     Reply with quote

According to the PCD manual, you can use the BAUD parameter in your
#use spi() statement to set the SCLK speed:
Quote:

#USE SPI

Syntax: #use spi (options)

BAUD=n Target bits per second. Default is as fast as possible.


Add the parameter shown in bold below. Fill in the desired baud rate.
Quote:

#use spi(SPI1, MASTER, MODE=3, BITS=32, BAUD=xxxxx, STREAM=A2D_Data)

The compiler will try to get as close as possible to your desired baud
rate, based on the limitations of the SPI module's baud rate generator.
dneale



Joined: 25 Nov 2009
Posts: 9
Location: Albuquerque NM

View user's profile Send private message

PostPosted: Wed Mar 10, 2010 5:27 pm     Reply with quote

Thanks so much. My Nov 2009 manual strikes doesn't have that option. I see there is a newer one. I'll have to download it.

Thanks.

Dave
_________________
Dave N
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