View previous topic :: View next topic |
Author |
Message |
theMagni
Joined: 21 May 2004 Posts: 48 Location: Victoria, BC
|
I require help with SPI. |
Posted: Thu Jun 03, 2004 3:25 pm |
|
|
Hello, forum.
I'm having trouble using the SPI interface. I'm using a 16F87 as the PIC, and trying to connect with a RTC chip (the 4574A to be precise).
One of my problems stems from the fact that some joker over-wrote the ex_spi.c file in the examples directory, so I haven't got the slightest idea how I'm supposed to set it up. I lookd through the examples on the forum, but I can't see how to decide the clock divisions or the edge that I should be interested in.
Further, I don't think the RTC has SPI. Can I still use SPI? There's an EEPROM with SPI on the same data wire, and I'd hate to have to send the boards back to production. (I didn't design the boards.)
Thanks for your help. |
|
![](templates/subSilver/images/spacer.gif) |
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
|
Posted: Thu Jun 03, 2004 3:31 pm |
|
|
Install the compiler version you wish to use again. It will over write all files supplied by CCS. Because of this you should never modify the files supplied by CCS unless you first change the file name. |
|
![](templates/subSilver/images/spacer.gif) |
theMagni
Joined: 21 May 2004 Posts: 48 Location: Victoria, BC
|
|
Posted: Fri Jun 04, 2004 4:35 pm |
|
|
I'm not really interested in reinstalling the software. I'm trying to find out how to initialize the SPI settings on a PIC16F87. Although the traget chip doesn't specifically support SPI, I'll be able to trick it into accepting the data, as long as I can send data down to the chip.
An oscilloscope shows that there's a pulse on the SCK, but there's nothing going down the data pipe. Do I have to initialize the pins, or does the compiler automagically know based on my chip?
Thanks.
Here's what I've got so far (snippets, of course):
Code: |
SSPCON = 0b00100000;
SSPSTAT = 0b10000000;
setup_spi( SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_64 );
delay_ms(1000);
//The point of this is to write the seconds, read the seconds,
//then read the seconds (and print them) every 5 seconds for
//15 seconds. Hopefully I'll see an increase.
if( write_RTC_data( SEC, 5 ) )
{
printf("SEC: %u (default)\n\r", returned );
returned = read_RTC_data( SEC );
printf("SEC: %u\n\r", returned );
returned = read_RTC_data( SEC );
printf("SEC: %u\n\r", returned );
}
int1 write_RTC_data( int address, int data )
{
int data_out = 0;
//turn the RTC chip on.
CS_RTC = HIGH;
//There is an 800ns CS delay. Refer to application notes.
delay_us(1);
//Takes the RTC_WRITE signal
data_out = RTC_WRITE;
//and moves it to the front of the outgoing signal.
data_out = data_out << 4;
//The first step is to disable the clock while it's being updated.
//This is strongly recommended by the manufacturer. CONTROL2 is
//the control register with the hold/reset/stop.
//MAKE CERTAIN THAT BIT SIX IN THIS REGISTER IS ALWAYS LOW.
data_out = data_out | 0b00001111; //0x0f is the CTRL2 register.
printf("WRITING: %u\n\r", data_out );
spi_write( data_out );
spi_write( STOP_CLOCK );
//In order to start writing again, we have to turn the CS on and
//off. It's weird, but I couldn't see another way.
CS_RTC = LOW;
delay_us(1);
CS_RTC = HIGH;
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
Kenny
Joined: 07 Sep 2003 Posts: 173 Location: Australia
|
|
Posted: Fri Jun 04, 2004 6:42 pm |
|
|
The data sheet shows that the clock needs to have a rising edge at mid-bit time. Also, the clock period minimum is 350nS so there's no need to divide down the pic clock frequency much.
setup_spi( SPI_MASTER | SPI_L_TO_H |SPI_CLK_DIV_16);
(leave out the SSPCON and SSPSTAT statements unless you need to change them as follows, in which case you would OR them with the the additional bits after the setup_spi directive).
The idle state of the clock is high according to the data sheet, so you may need to set the CKP bit as well since the compiler doesn't give this option I think (I'm not at work to check what I'm saying).
I would write a small program to test the spi interface on it's own to make sure that it's doing the right thing (you have an oscilloscope).
while(1)
{
spi_write(0x55); // send alternating ones and zeros
} |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
Re: I require help with SPI. |
Posted: Thu Oct 06, 2005 8:50 am |
|
|
theMagni wrote: | Hello, forum.
I'm having trouble using the SPI interface. I'm using a 16F87 as the PIC, and trying to connect with a RTC chip (the 4574A to be precise).
One of my problems stems from the fact that some joker over-wrote the ex_spi.c file in the examples directory, so I haven't got the slightest idea how I'm supposed to set it up. I lookd through the examples on the forum, but I can't see how to decide the clock divisions or the edge that I should be interested in.
Further, I don't think the RTC has SPI. Can I still use SPI? There's an EEPROM with SPI on the same data wire, and I'd hate to have to send the boards back to production. (I didn't design the boards.)
Thanks for your help. |
|
|
![](templates/subSilver/images/spacer.gif) |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Oct 06, 2005 9:05 am |
|
|
Must be the same DA that was quoting old posts in another topic ![Razz](images/smiles/icon_razz.gif) |
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Oct 06, 2005 9:33 am |
|
|
I couldn't find the datasheet for the RTC-4574A. Is the RTC-4574JE similar? http://www.eea.epson.com/go/Prod_Admin/Categories/EEA/QD/Real_Time_Clocks/displayItem?itemId=RTC-4574JE&categoryId=EEA.QD.Real_Time_Clocks
Quote: | Further, I don't think the RTC has SPI. Can I still use SPI? There's an EEPROM with SPI on the same data wire, and I'd hate to have to send the boards back to production. (I didn't design the boards.) |
Assuming the RTC-4574JE is similar to the RTC-4574A you have a problem. The chip has a bidirectional data bus while SPI has seperate data lines for input and output. I've seen simple solutions to make this compatible to SPI but don't know the links anymore. The EEPROM on the same bus could make things complicated though. Maybe someone with more electronics knowledge can jump in here? |
|
![](templates/subSilver/images/spacer.gif) |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Oct 06, 2005 9:35 am |
|
|
I wouldn't waste my time looking. The post is a year old and no one really asked a question! |
|
![](templates/subSilver/images/spacer.gif) |
|