![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
Forest Guest
|
SPI 16f877 to 25AA320/040 problems |
Posted: Mon Apr 21, 2003 4:40 am |
|
|
Help again!! - thanks to Dave Yeatman and CCS for their answers.
This time I'm having trouble interfacing a 25AA320 EEPROM to my 16f877. Below is my very basic code to just get the thing working. Every time I compile and run - SIOW stops. By putting in some printf's I figured out that it is getting to the first SPI_WRITE (0x03) and stopping, the PIC is not sending data out on RC5. Have looked in the forum for answers and have found something about problems with SPI modes. tried fixes with no joy.
Any suggestions?
Thanks in advance.
#include <16F877.h>
#fuses hs,nowdt,noprotect,noput,nowrt,nolvp,nobrownout
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#define EEPROM_SELECT PIN_D2
#define EEPROM_DI PIN_C5
#define EEPROM_DO PIN_C4
#define EEPROM_CLK PIN_C3
void main() {
byte data;
byte writecom; //initialise write command
byte value,address;
address = 0001;
value = (01010101);
writecom = (0x02);
setup_spi(SPI_MASTER|SPI_L_TO_H | SPI_CLK_DIV_16|SPI_SS_DISABLED|SPI_XMIT_L_TO_H);
setup_adc_ports(NO_ANALOGS);
setup_psp(PSP_DISABLED);
// write to SPI
printf("\n\rWriting....");
output_high(EEPROM_SELECT);
output_low(EEPROM_SELECT); // enable eeprom
spi_write(0x06); // WREN command
output_high(EEPROM_SELECT); // cycle chip select
output_low(EEPROM_SELECT);
spi_write(writecom); // Write command
spi_write(0x00); // Address to write to
spi_write(0x00);
spi_write(10101010); // Write example data
output_high(EEPROM_SELECT); // disable eeprom
delay_ms(1500);
// read from SPI
printf("\n\rReading....");
output_low(EEPROM_SELECT);
spi_write(0x03);
spi_write(0x00);
spi_write(0x00);
data=spi_read();
output_high(EEPROM_SELECT);
printf("\r\nData:... \%4X", data);
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 13859 |
|
![](templates/subSilver/images/spacer.gif) |
TSchultz
Joined: 08 Sep 2003 Posts: 66 Location: Toronto, Canada
|
RE:SPI 16f877 to 25AA320/040 problems |
Posted: Mon Apr 21, 2003 5:53 am |
|
|
:=Help again!! - thanks to Dave Yeatman and CCS for their answers.
:=
:=This time I'm having trouble interfacing a 25AA320 EEPROM to my 16f877. Below is my very basic code to just get the thing working. Every time I compile and run - SIOW stops. By putting in some printf's I figured out that it is getting to the first SPI_WRITE (0x03) and stopping, the PIC is not sending data out on RC5. Have looked in the forum for answers and have found something about problems with SPI modes. tried fixes with no joy.
:=
:=Any suggestions?
:=
:=Thanks in advance.
:=
:=
:=#include <16F877.h>
:=#fuses hs,nowdt,noprotect,noput,nowrt,nolvp,nobrownout
:=#use delay(clock=4000000)
:=#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
:=
:=
:=#define EEPROM_SELECT PIN_D2
:=#define EEPROM_DI PIN_C5
:=#define EEPROM_DO PIN_C4
:=#define EEPROM_CLK PIN_C3
:=
:=
:=void main() {
:= byte data;
:= byte writecom; //initialise write command
:= byte value,address;
:= address = 0001;
:= value = (01010101);
:= writecom = (0x02);
:=
:=setup_spi(SPI_MASTER|SPI_L_TO_H | SPI_CLK_DIV_16|SPI_SS_DISABLED|SPI_XMIT_L_TO_H);
:=setup_adc_ports(NO_ANALOGS);
:=setup_psp(PSP_DISABLED);
:=
:=// write to SPI
:=printf("\n\rWriting....");
:= output_high(EEPROM_SELECT);
:= output_low(EEPROM_SELECT); // enable eeprom
:=
:= spi_write(0x06); // WREN command
:= output_high(EEPROM_SELECT); // cycle chip select
:= output_low(EEPROM_SELECT);
:= spi_write(writecom); // Write command
:= spi_write(0x00); // Address to write to
:= spi_write(0x00);
:= spi_write(10101010); // Write example data
:= output_high(EEPROM_SELECT); // disable eeprom
:= delay_ms(1500);
:=
:=// read from SPI
:=
:=printf("\n\rReading....");
:= output_low(EEPROM_SELECT);
:= spi_write(0x03);
:= spi_write(0x00);
:= spi_write(0x00);
:= data=spi_read();
:= output_high(EEPROM_SELECT);
:=
:=printf("\r\nData:... \%4X", data);
:=
:=
:=}
Try changing the spi_read() to spi_read(0). The spi_read function will not generate the necessary serial clock unless you pass it a word to write. Using spi_read() will only provide to last clocked in value.
-Troy
___________________________
This message was ported from CCS's old forum
Original Post ID: 13860 |
|
![](templates/subSilver/images/spacer.gif) |
Forest Guest
|
Re: RE:SPI 16f877 to 25AA320/040 problems |
Posted: Mon Apr 21, 2003 7:30 am |
|
|
:=:=Help again!! - thanks to Dave Yeatman and CCS for their answers.
:=:=
:=:=This time I'm having trouble interfacing a 25AA320 EEPROM to my 16f877. Below is my very basic code to just get the thing working. Every time I compile and run - SIOW stops. By putting in some printf's I figured out that it is getting to the first SPI_WRITE (0x03) and stopping, the PIC is not sending data out on RC5. Have looked in the forum for answers and have found something about problems with SPI modes. tried fixes with no joy.
:=:=
:=:=Any suggestions?
:=:=
:=:=Thanks in advance.
:=:=
:=:=
:=:=#include <16F877.h>
:=:=#fuses hs,nowdt,noprotect,noput,nowrt,nolvp,nobrownout
:=:=#use delay(clock=4000000)
:=:=#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
:=:=
:=:=
:=:=#define EEPROM_SELECT PIN_D2
:=:=#define EEPROM_DI PIN_C5
:=:=#define EEPROM_DO PIN_C4
:=:=#define EEPROM_CLK PIN_C3
:=:=
:=:=
:=:=void main() {
:=:= byte data;
:=:= byte writecom; //initialise write command
:=:= byte value,address;
:=:= address = 0001;
:=:= value = (01010101);
:=:= writecom = (0x02);
:=:=
:=:=setup_spi(SPI_MASTER|SPI_L_TO_H | SPI_CLK_DIV_16|SPI_SS_DISABLED|SPI_XMIT_L_TO_H);
:=:=setup_adc_ports(NO_ANALOGS);
:=:=setup_psp(PSP_DISABLED);
:=:=
:=:=// write to SPI
:=:=printf("\n\rWriting....");
:=:= output_high(EEPROM_SELECT);
:=:= output_low(EEPROM_SELECT); // enable eeprom
:=:=
:=:= spi_write(0x06); // WREN command
:=:= output_high(EEPROM_SELECT); // cycle chip select
:=:= output_low(EEPROM_SELECT);
:=:= spi_write(writecom); // Write command
:=:= spi_write(0x00); // Address to write to
:=:= spi_write(0x00);
:=:= spi_write(10101010); // Write example data
:=:= output_high(EEPROM_SELECT); // disable eeprom
:=:= delay_ms(1500);
:=:=
:=:=// read from SPI
:=:=
:=:=printf("\n\rReading....");
:=:= output_low(EEPROM_SELECT);
:=:= spi_write(0x03);
:=:= spi_write(0x00);
:=:= spi_write(0x00);
:=:= data=spi_read();
:=:= output_high(EEPROM_SELECT);
:=:=
:=:=printf("\r\nData:... \%4X", data);
:=:=
:=:=
:=:=}
:=
:=
:=Try changing the spi_read() to spi_read(0). The spi_read function will not generate the necessary serial clock unless you pass it a word to write. Using spi_read() will only provide to last clocked in value.
:=
:=-Troy
Thanks Troy - will add that suggestion. Still doesn't help with my original problem - program won't get past first SPI_WRITE.
___________________________
This message was ported from CCS's old forum
Original Post ID: 13862 |
|
![](templates/subSilver/images/spacer.gif) |
chas Guest
|
Re: SPI 16f877 to 25AA320/040 problems |
Posted: Mon Apr 21, 2003 8:41 am |
|
|
I've inserted a couple of notes in your code below:
:=Help again!! - thanks to Dave Yeatman and CCS for their answers.
:=
:=This time I'm having trouble interfacing a 25AA320 EEPROM to my 16f877. Below is my very basic code to just get the thing working. Every time I compile and run - SIOW stops. By putting in some printf's I figured out that it is getting to the first SPI_WRITE (0x03) and stopping, the PIC is not sending data out on RC5. Have looked in the forum for answers and have found something about problems with SPI modes. tried fixes with no joy.
:=
:=Any suggestions?
:=
:=Thanks in advance.
:=
:=
:=#include <16F877.h>
:=#fuses hs,nowdt,noprotect,noput,nowrt,nolvp,nobrownout
:=#use delay(clock=4000000)
:=#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
:=
:=
:=#define EEPROM_SELECT PIN_D2
:=#define EEPROM_DI PIN_C5
:=#define EEPROM_DO PIN_C4
:=#define EEPROM_CLK PIN_C3
:=
:=
:=void main() {
:= byte data;
:= byte writecom; //initialise write command
:= byte value,address;
:= address = 0001;
:= value = (01010101);
:= writecom = (0x02);
:=
*** Verify that this is setting up the proper mode!
:=setup_spi(SPI_MASTER|SPI_L_TO_H | SPI_CLK_DIV_16|SPI_SS_DISABLED|SPI_XMIT_L_TO_H);
:=setup_adc_ports(NO_ANALOGS);
:=setup_psp(PSP_DISABLED);
:=
:=// write to SPI
:=printf("\n\rWriting....");
:= output_high(EEPROM_SELECT);
:= output_low(EEPROM_SELECT); // enable eeprom
:=
***
Check the listing to see if the following command just writes the data to the SSP buffer and then moves on to the next commands. If so, you should wait for the write to complete by monitoring the SSPIF flag then reselect the EEPROM.
:= spi_write(0x06); // WREN command
*** (Add a wait for SSPIF here?)
:= output_high(EEPROM_SELECT); // cycle chip select
:= output_low(EEPROM_SELECT);
:= spi_write(writecom); // Write command
:= spi_write(0x00); // Address to write to
:= spi_write(0x00);
:= spi_write(10101010); // Write example data
*** (Add a wait here also?)
:= output_high(EEPROM_SELECT); // disable eeprom
:= delay_ms(1500);
:=
:=// read from SPI
:=
:=printf("\n\rReading....");
:= output_low(EEPROM_SELECT);
:= spi_write(0x03);
:= spi_write(0x00);
:= spi_write(0x00);
:= data=spi_read();
:= output_high(EEPROM_SELECT);
:=
:=printf("\r\nData:... \%4X", data);
:=
:=
:=}
___________________________
This message was ported from CCS's old forum
Original Post ID: 13866 |
|
![](templates/subSilver/images/spacer.gif) |
R.J.Hamlett Guest
|
Re: SPI 16f877 to 25AA320/040 problems |
Posted: Mon Apr 21, 2003 8:45 am |
|
|
:=Help again!! - thanks to Dave Yeatman and CCS for their answers.
:=
:=This time I'm having trouble interfacing a 25AA320 EEPROM to my 16f877. Below is my very basic code to just get the thing working. Every time I compile and run - SIOW stops. By putting in some printf's I figured out that it is getting to the first SPI_WRITE (0x03) and stopping, the PIC is not sending data out on RC5. Have looked in the forum for answers and have found something about problems with SPI modes. tried fixes with no joy.
:=
:=Any suggestions?
:=
:=Thanks in advance.
:=
:=
:=#include <16F877.h>
:=#fuses hs,nowdt,noprotect,noput,nowrt,nolvp,nobrownout
:=#use delay(clock=4000000)
:=#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
:=
:=
:=#define EEPROM_SELECT PIN_D2
:=#define EEPROM_DI PIN_C5
:=#define EEPROM_DO PIN_C4
:=#define EEPROM_CLK PIN_C3
:=
:=
:=void main() {
:= byte data;
:= byte writecom; //initialise write command
:= byte value,address;
:= address = 0001;
:= value = (01010101);
:= writecom = (0x02);
:=
:=setup_spi(SPI_MASTER|SPI_L_TO_H | SPI_CLK_DIV_16|SPI_SS_DISABLED|SPI_XMIT_L_TO_H);
:=setup_adc_ports(NO_ANALOGS);
:=setup_psp(PSP_DISABLED);
:=
:=// write to SPI
:=printf("\n\rWriting....");
:= output_high(EEPROM_SELECT);
:= output_low(EEPROM_SELECT); // enable eeprom
:=
:= spi_write(0x06); // WREN command
:= output_high(EEPROM_SELECT); // cycle chip select
:= output_low(EEPROM_SELECT);
:= spi_write(writecom); // Write command
:= spi_write(0x00); // Address to write to
:= spi_write(0x00);
:= spi_write(10101010); // Write example data
:= output_high(EEPROM_SELECT); // disable eeprom
:= delay_ms(1500);
:=
:=// read from SPI
:=
:=printf("\n\rReading....");
:= output_low(EEPROM_SELECT);
:= spi_write(0x03);
:= spi_write(0x00);
:= spi_write(0x00);
:= data=spi_read();
:= output_high(EEPROM_SELECT);
:=
:=printf("\r\nData:... \%4X", data);
:=
:=
:=}
First comment. Presumably a 'mistype', but you refer to nothing being sent on RC5, but you are not using RC5.
Second comment. What compiler version?.
There was a problem with SPI, some time ago. At this time I went 'DIY' to solve this.
I must admit, that I don't 'like' the way the standard SPI functions work, since I prefer to be able to tell the status of the hardware, seperately from the act of clocking data in/out. My problems were with the 18F877, so may apply to you. The replacement operations I used were:
#byte SSPBUF = 0x13
#byte SSPCON = 0x14
#byte SSPSTAT = 0x94
#DEFINE READ_SSP() (SSPBUF)
#DEFINE WAIT_FOR_SSP() while((SSPSTAT & 1)==0)
#DEFINE WRITE_SSP(chr) SSPBUF=(chr)
#DEFINE CLEAR_WCOL() SSPCON=SSPCON & 0x3F
These function differently from the supplied functions, in that 'WRITE_SSP(chr)', just puts the character into the sending hardware. Hence if you need to wait before sending the next character, you then have to use:
WRITE_SSP(chr);
WAIT_FOR_SSP();
The 'good thing' about this approach, is that I can use interrupt driven SPI, and simply read the last character received, and write the next character to send, in the interrupt routine, since the interrupt will only occur when the last transfer has completed. :-)
Obviously if you are using fast_io, you have to remember to set the TRIS bits correctly.
Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 13867 |
|
![](templates/subSilver/images/spacer.gif) |
R.J.Hamlett Guest
|
Re: SPI 16f877 to 25AA320/040 problems |
Posted: Mon Apr 21, 2003 9:02 am |
|
|
:=:=Help again!! - thanks to Dave Yeatman and CCS for their answers.
:=:=
:=:=This time I'm having trouble interfacing a 25AA320 EEPROM to my 16f877. Below is my very basic code to just get the thing working. Every time I compile and run - SIOW stops. By putting in some printf's I figured out that it is getting to the first SPI_WRITE (0x03) and stopping, the PIC is not sending data out on RC5. Have looked in the forum for answers and have found something about problems with SPI modes. tried fixes with no joy.
:=:=
:=:=Any suggestions?
:=:=
:=:=Thanks in advance.
:=:=
:=:=
:=:=#include <16F877.h>
:=:=#fuses hs,nowdt,noprotect,noput,nowrt,nolvp,nobrownout
:=:=#use delay(clock=4000000)
:=:=#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
:=:=
:=:=
:=:=#define EEPROM_SELECT PIN_D2
:=:=#define EEPROM_DI PIN_C5
:=:=#define EEPROM_DO PIN_C4
:=:=#define EEPROM_CLK PIN_C3
:=:=
:=:=
:=:=void main() {
:=:= byte data;
:=:= byte writecom; //initialise write command
:=:= byte value,address;
:=:= address = 0001;
:=:= value = (01010101);
:=:= writecom = (0x02);
:=:=
:=:=setup_spi(SPI_MASTER|SPI_L_TO_H | SPI_CLK_DIV_16|SPI_SS_DISABLED|SPI_XMIT_L_TO_H);
:=:=setup_adc_ports(NO_ANALOGS);
:=:=setup_psp(PSP_DISABLED);
:=:=
:=:=// write to SPI
:=:=printf("\n\rWriting....");
:=:= output_high(EEPROM_SELECT);
:=:= output_low(EEPROM_SELECT); // enable eeprom
:=:=
:=:= spi_write(0x06); // WREN command
:=:= output_high(EEPROM_SELECT); // cycle chip select
:=:= output_low(EEPROM_SELECT);
:=:= spi_write(writecom); // Write command
:=:= spi_write(0x00); // Address to write to
:=:= spi_write(0x00);
:=:= spi_write(10101010); // Write example data
:=:= output_high(EEPROM_SELECT); // disable eeprom
:=:= delay_ms(1500);
:=:=
:=:=// read from SPI
:=:=
:=:=printf("\n\rReading....");
:=:= output_low(EEPROM_SELECT);
:=:= spi_write(0x03);
:=:= spi_write(0x00);
:=:= spi_write(0x00);
:=:= data=spi_read();
:=:= output_high(EEPROM_SELECT);
:=:=
:=:=printf("\r\nData:... \%4X", data);
:=:=
:=:=
:=:=}
:=First comment. Presumably a 'mistype', but you refer to nothing being sent on RC5, but you are not using RC5.
Ignore this, I was thinking of another chip. Embarassed grin...).
Best Wishes
:=Second comment. What compiler version?.
:=There was a problem with SPI, some time ago. At this time I went 'DIY' to solve this.
:=I must admit, that I don't 'like' the way the standard SPI functions work, since I prefer to be able to tell the status of the hardware, seperately from the act of clocking data in/out. My problems were with the 18F877, so may apply to you. The replacement operations I used were:
:=#byte SSPBUF = 0x13
:=#byte SSPCON = 0x14
:=#byte SSPSTAT = 0x94
:=#DEFINE READ_SSP() (SSPBUF)
:=#DEFINE WAIT_FOR_SSP() while((SSPSTAT & 1)==0)
:=#DEFINE WRITE_SSP(chr) SSPBUF=(chr)
:=#DEFINE CLEAR_WCOL() SSPCON=SSPCON & 0x3F
:=
:=These function differently from the supplied functions, in that 'WRITE_SSP(chr)', just puts the character into the sending hardware. Hence if you need to wait before sending the next character, you then have to use:
:=WRITE_SSP(chr);
:=WAIT_FOR_SSP();
:=
:=The 'good thing' about this approach, is that I can use interrupt driven SPI, and simply read the last character received, and write the next character to send, in the interrupt routine, since the interrupt will only occur when the last transfer has completed. :-)
:=Obviously if you are using fast_io, you have to remember to set the TRIS bits correctly.
:=
:=Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 13870 |
|
![](templates/subSilver/images/spacer.gif) |
Forest Guest
|
Dooh!! Thanks to T.Schultz and R.J.Hamlett |
Posted: Tue Apr 22, 2003 5:58 pm |
|
|
:=:=:=Help again!! - thanks to Dave Yeatman and CCS for their answers.
:=:=:=
:=:=:=This time I'm having trouble interfacing a 25AA320 EEPROM to my 16f877. Below is my very basic code to just get the thing working. Every time I compile and run - SIOW stops. By putting in some printf's I figured out that it is getting to the first SPI_WRITE (0x03) and stopping, the PIC is not sending data out on RC5. Have looked in the forum for answers and have found something about problems with SPI modes. tried fixes with no joy.
:=:=:=
:=:=:=Any suggestions?
:=:=:=
:=:=:=Thanks in advance.
:=:=:=
:=:=:=
:=:=:=#include <16F877.h>
:=:=:=#fuses hs,nowdt,noprotect,noput,nowrt,nolvp,nobrownout
:=:=:=#use delay(clock=4000000)
:=:=:=#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
:=:=:=
:=:=:=
:=:=:=#define EEPROM_SELECT PIN_D2
:=:=:=#define EEPROM_DI PIN_C5
:=:=:=#define EEPROM_DO PIN_C4
:=:=:=#define EEPROM_CLK PIN_C3
:=:=:=
:=:=:=
:=:=:=void main() {
:=:=:= byte data;
:=:=:= byte writecom; //initialise write command
:=:=:= byte value,address;
:=:=:= address = 0001;
:=:=:= value = (01010101);
:=:=:= writecom = (0x02);
:=:=:=
:=:=:=setup_spi(SPI_MASTER|SPI_L_TO_H | SPI_CLK_DIV_16|SPI_SS_DISABLED|SPI_XMIT_L_TO_H);
:=:=:=setup_adc_ports(NO_ANALOGS);
:=:=:=setup_psp(PSP_DISABLED);
:=:=:=
:=:=:=// write to SPI
:=:=:=printf("\n\rWriting....");
:=:=:= output_high(EEPROM_SELECT);
:=:=:= output_low(EEPROM_SELECT); // enable eeprom
:=:=:=
:=:=:= spi_write(0x06); // WREN command
:=:=:= output_high(EEPROM_SELECT); // cycle chip select
:=:=:= output_low(EEPROM_SELECT);
:=:=:= spi_write(writecom); // Write command
:=:=:= spi_write(0x00); // Address to write to
:=:=:= spi_write(0x00);
:=:=:= spi_write(10101010); // Write example data
:=:=:= output_high(EEPROM_SELECT); // disable eeprom
:=:=:= delay_ms(1500);
:=:=:=
:=:=:=// read from SPI
:=:=:=
:=:=:=printf("\n\rReading....");
:=:=:= output_low(EEPROM_SELECT);
:=:=:= spi_write(0x03);
:=:=:= spi_write(0x00);
:=:=:= spi_write(0x00);
:=:=:= data=spi_read();
:=:=:= output_high(EEPROM_SELECT);
:=:=:=
:=:=:=printf("\r\nData:... \%4X", data);
:=:=:=
:=:=:=
:=:=:=}
:=:=
:=:=
:=:=Try changing the spi_read() to spi_read(0). The spi_read function will not generate the necessary serial clock unless you pass it a word to write. Using spi_read() will only provide to last clocked in value.
:=:=
:=:=-Troy
:=
:=Thanks Troy - will add that suggestion. Still doesn't help with my original problem - program won't get past first SPI_WRITE.
Hmmm -12:00am and it occured to me that that wire connecting RC5 shouldn't be there, damn damn damn Dooooohhh!!! (slaps forehead). Thanks to T.Schultz and R.J.Hamlett for their help.
___________________________
This message was ported from CCS's old forum
Original Post ID: 13932 |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|