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 Problem (writing to a ROM)

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







SPI Problem (writing to a ROM)
PostPosted: Tue May 02, 2006 3:22 pm     Reply with quote

Hi all

My first post, and my first go at using CCS, so if I get something wrong
or am not posting enough info, please don't bite my head
off!

I'm using CCS C (v3.239) compiler with a PIC16F690
programming with the PicKit2.
http://ww1.microchip.com/downloads/en/DeviceDoc/41262A.pdf

I'm trying to get my PIC to program the ROM in a PCM2705
http://focus.ti.com/lit/ds/symlink/pcm2705.pdf (page
23-25)

I have the PCM2705 hooked up to the hardware SPI Port
directly, and SPI select connected directly to PortC3

The porogram process is a simple matter of downloading
a Product ID, Vendor ID, Device Name string etc.

The issue is that no matter how I try it I either get
the default strings, or scrambled nonsense.
Interestingly I can send single commands to the
PCM2705 such as volume up.

I have had difficulty interrpretting the datasheet, so
I might be going about the ROM writing process the
wrong way.

Here's my sources
Code:

============ Main ======================

#include "C:\Documents and Settings\Ed\MyDocuments\DivaProgrammer\version1b.h"
#use delay(clock=1000000)
#DEFINE SSPCON 0x14
#DEFINE ADDR_HIGH 2
#DEFINE ST_HIGH 8
#BIT CKP=SSPCON.4

int8 const ROM_DATA[57] = {0xf3, 0x18, 0x10, 0x00,'T', 'h', 'i', 's', ' ', 'i', 's', 'a', ' ', 'T', 'e', 's', 't', ' ', ' ', ' ', 'V', 'e', 'n','d','o','r',' ','S','t','a','r','t','s',' ','H','e','r','e',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', 0xc0, 0x0a, 0x0a, 0x93, 0x01};

int8 count;

void main()
{
   setup_oscillator(OSC_INTRC);
   setup_oscillator(OSC_1MHZ);
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);

   output_high(SELECT); // Set select pin high
   output_high(PIN_C7);
   output_high(PIN_B6);
   CKP=1;
   setup_spi(SPI_MASTER | SPI_CLK_DIV_64 | SPI_H_TO_L);  //Data on High to low!?
   CKP=1;

   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE|0);

   output_high(SELECT); // Set select pin high

   delay_ms(100); // Let everything Settle


   WHILE(1){

   while(input(PIN_A3))
   {
      output_toggle(PIN_C0);     //FLASH LED
      delay_ms(200);
   }
   while(!input(PIN_A3));        // until button is pressed, and released

   output_low(SELECT); // Set select pin low
   delay_us(50);
   spi_write(ADDR_HIGH);  // Send ADDR command
   spi_write(0x00);
   delay_us(50);
   output_high(SELECT); // Set select pin low

   delay_us(500);

   output_low(SELECT); // Set select pin low

   count=0;
   while(count < 57)                // 57 bytes (456bits) to be written
   {
      delay_us(50);
      spi_write(ST_HIGH);           // WIth ST bit set
      spi_write(ROM_DATA[count]);   // Send ROM data
      output_toggle(PIN_C1);        // Strobe LED while writing
      count++;
   }

   delay_us(50);
   output_high(SELECT); // Set select pin high
   delay_ms(100);       // should be done
   }
}

Code:

============ Header File ===================

#include <16F690.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC_IO                 //Internal RC Osc, noCLKOUT
#FUSES NOPROTECT                //Code not protectedfrom reading
#FUSES NOBROWNOUT                 //
#FUSES NOMCLR                     //Master Clear
#FUSES NOCPD                    //No EE protection
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOIESO                     //Internal ExternalSwitch Over mode disabled
#FUSES NOFCMEN                    //Fail-safe clock monitor disabled

#use delay(clock=1000000)
#define BUTTON   PIN_A3
#define LED1   PIN_C0
#define LED2   PIN_C1
#define LED3   PIN_C2
#define SELECT PIN_C3
//#define SPI_CLK PIN_B6
//#define SPI_SDO PIN_C7


Anything obvious?

Thanks!

Ed
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed May 03, 2006 2:12 pm     Reply with quote

There are several things wrong with your code.

1. You have two setup_oscillator() statements. That's wrong.
There should only be one.

2. You have several "magic" pin numbers, that you use in pin i/o
statements. You should refer to the pins by their named constants
that you have in your Version1b.h file.

3. I think you're using the wrong SPI mode.

On page 24 of the PCM2705 data sheet, it shows the SPI write timing.
Go this page, and scroll down near the bottom.
http://www.totalphase.com/support/articles/article03/
It shows a diagram of the clock to data phase relationships for each
SPI mode.

In the Acrobat reader, zoom in on the left side of the timing diagram
for "Single Write Operation" in the PCM2705 data sheet. Notice the idle
state of the clock is a high level. So that means you're going to be in
either Mode 2 or 3, according to the Total Phase website diagram.

Now look closely at the clock to data relationship in the data sheet.
The data changes on the falling edge of clock and is sampled in the
middle of the data cell by the rising edge of clock. So on the Total Phase
website, that's Mode 3.

Now set the SPI mode, according the method shown in this post:
http://www.ccsinfo.com/forum/viewtopic.php?t=26888
Code:
setup_spi(SPI_MASTER | SPI_MODE_1_1 | SPI_CLK_DIV_64); 

Get rid of all the manual "CKP = 1" code. Just use the line above.
edpgc
Guest







PostPosted: Wed May 03, 2006 7:57 pm     Reply with quote

Cheers PCM! I'll look into this tomorrow, I've had a hard time interpreting the TI documentation, to be honest i"m not totally sure of the sequence of the write process.

I'm used to jsut coding in good old assembly, so I wasn't really sure if CCS was setting the CKP bit, got a bit carried away there.

I'll let you know how I get on.

Ed
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