![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
applecon2000
Joined: 29 Jul 2007 Posts: 31 Location: UK
|
ask for help |
Posted: Tue Aug 21, 2007 2:52 pm |
|
|
Dear all,
I am trying to compile data transfering with mode of SPI, for my master device, the code was shown below:
Code: | #include <16F876.h>
#Fuses XT,NOWDT,NOPROTECT,NOPUT,NOBROWNOUT,NOLVP,NODEBUG
#USE DELAY(clock=4000000)
#USE standard_IO(A)
#USE standard_IO(B)
#USE standard_IO(C)
//#USE I2C(Master,slow, sda=PIN_C4, scl=PIN_C3)
#INT_SSP
void main()
{
setup_spi(SPI_MASTER);
output_low(PIN_B0); // Drive SS as low level
while(!input(PIN_B0)) // Write the letter "xw503" to the slave board.
{
spi_write("xw503");
}
} |
And for my slave device code :
Code: | while(1) // SPI start running
{
setup_spi(SPI_SLAVE);
lcd_putc("\f SPI data: \n");
value3=spi_read();
lcd_putc(value3);
delay_ms(1000);
} //end while of SPI |
These codes were used for driving LCD module display any message stored in master device.
I used two PIC16F876 to execute this job. But when I tried to simulate them in ISIS, the message displayed on screen would always be :
SPI data:
i
whatever I changing the message storing in Master device.
Is there any friend here can help me solve this problem?
By the way, could you please explain following pre-processor code:
Quote: | setup_spi (mode)
SPI_SS_DISABLED
SPI_L_TO_H, SPI_H_TO_L
SPI_CLK_DIV_4, SPI_CLK_DIV_16,
SPI_CLK_DIV_64, SPI_CLK_T2
Constants from each group may be or'ed together with |. |
I do thank you for each help from you!!! _________________ Enjoy our EEE |
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
Re: ask for help |
Posted: Wed Aug 22, 2007 2:53 am |
|
|
Code: | spi_write("xw503"); | You can not pass a string to this function. The strange output you get is the start address of your string.
applecon2000 wrote: | By the way, could you please explain following pre-processor code:
Quote: | setup_spi (mode)
SPI_SS_DISABLED
SPI_L_TO_H, SPI_H_TO_L
SPI_CLK_DIV_4, SPI_CLK_DIV_16,
SPI_CLK_DIV_64, SPI_CLK_T2
Constants from each group may be or'ed together with |. |
I do thank you for each help from you!!! | These configurations values are comming directly from the names as defined in the PIC datasheet. Depending on your processor more or less options will be available, check the header file for your processor.
A very quick introduction:
SPI_SS_DISABLED: Slave Select is the Microchip name for the Chip Select functionality. Only use this option in the slave. When you set this option the SS-pin is not used and the slave will always listen to the data on the SPI bus.
SPI_CLK_DIV_xxx: Sets the clock rate for the SPI bus.
SPI_L_TO_H, SPI_H_TO_L: The SPI bus can be used in four different modes and using these parameters you define which one to use. Motorola names them SPI mode 0 to 3. Make sure the master and slave device use the same mode or communication will fail. |
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Aug 22, 2007 3:14 am |
|
|
Your program has several flaws. Here an example program that will display the values '0' to '9' on the slave display. (not tested)
The hardware configuration must have pin B0 of the master connected to the Slave Select pin (SS) of the slave (pin A5 on a 16F876).
Master Code: | #include <16F876.h>
#fuses XT,NOWDT,NOPROTECT,NOPUT,NOBROWNOUT,NOLVP,NODEBUG
#use delay(clock=4000000)
#define EEPROM_SELECT PIN_B0
void main()
{
int8 i;
setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_16);
while (1)
{
for (i='0'; i<='9'; i++)
{
output_low(EEPROM_SELECT); // Slave select
spi_write( i );
output_high(EEPROM_SELECT); // Slave deselect
delay_ms(1000);
}
}
} |
Slave Code: | #include <16F876.h>
#fuses XT,NOWDT,NOPROTECT,NOPUT,NOBROWNOUT,NOLVP,NODEBUG
#use delay(clock=4000000)
void main()
{
int8 value;
setup_spi(SPI_SLAVE | SPI_H_TO_L);
while (1)
{
value = spi_read(); // Will wait until a character is received.
lcd_putc("\f SPI data: \n");
lcd_putc(value);
}
} |
|
|
![](templates/subSilver/images/spacer.gif) |
applecon2000
Joined: 29 Jul 2007 Posts: 31 Location: UK
|
thanks a lot, but... |
Posted: Wed Aug 22, 2007 7:24 am |
|
|
Thank you very much indeed.
The program compiling was fine but when I simulated it in ISIS, the LCD screen would still display:
SPI data:
i
I also changed the variable of i to other letter and changed the contents of i, but it still did not work.
why the screen just displayed "i"?
Thanks for your help. _________________ Enjoy our EEE |
|
![](templates/subSilver/images/spacer.gif) |
jfk1965
Joined: 21 Oct 2003 Posts: 58
|
|
Posted: Wed Aug 22, 2007 8:30 am |
|
|
Try removing the inverted commas in the for statement I don't think thay are needed.
So
Code: | for (i='0'; i<='9'; i++) |
becomes
Code: | for (i=0; i<=9; i++) |
JFK |
|
![](templates/subSilver/images/spacer.gif) |
applecon2000
Joined: 29 Jul 2007 Posts: 31 Location: UK
|
hello |
Posted: Wed Aug 22, 2007 10:23 am |
|
|
Dear all,
I have changed codes but it still not worked, for my circuit, I just connected master device SCL PIN_C3 and SDO PIN_C5 to slave device SCL PIN_C3 and SDI PIN_C4. I have not put pull_up resistors to those two PINs.
For I2C mode, it was necessary to connect two resistors to both SCK and SDA.
I also configured Port A, B, C as standard I/O and defined #INT_SSP, is there any other thing I should build in my design?
Thanks for your help. _________________ Enjoy our EEE |
|
![](templates/subSilver/images/spacer.gif) |
SET
Joined: 15 Nov 2005 Posts: 161 Location: Glasgow, UK
|
|
Posted: Wed Aug 22, 2007 11:07 am |
|
|
Yes quotes 'are' needed if you want to see the characters 0 through 9 |
|
![](templates/subSilver/images/spacer.gif) |
applecon2000
Joined: 29 Jul 2007 Posts: 31 Location: UK
|
hello |
Posted: Wed Aug 22, 2007 11:16 am |
|
|
I have quoted, but it still did not work, could you please check if my connection was ok.
Or this interface can not be simulated in ISIS at all !? _________________ Enjoy our EEE |
|
![](templates/subSilver/images/spacer.gif) |
SET
Joined: 15 Nov 2005 Posts: 161 Location: Glasgow, UK
|
|
Posted: Wed Aug 22, 2007 11:21 am |
|
|
Sure you havent written:
in the master code? |
|
![](templates/subSilver/images/spacer.gif) |
applecon2000
Joined: 29 Jul 2007 Posts: 31 Location: UK
|
Dear all |
Posted: Wed Aug 22, 2007 11:26 am |
|
|
Except the SPI issue.
My I2C worked, but not well enough. As the program for master device were shown below:
Code: | #include <16F876.h>
#Fuses XT,NOWDT,NOPROTECT,NOPUT,NOBROWNOUT,NOLVP,NODEBUG
#USE DELAY(clock=4000000)
#USE standard_IO(A)
#USE standard_IO(B)
#USE standard_IO(C)
#USE I2C(Master,slow, sda=PIN_C4, scl=PIN_C3)
#INT_SSP
void main()
{
while(1)
{
i2c_start();
i2c_write(0x30);
i2c_write(0x00);
i2c_write('x');
i2c_write('w');
i2c_write('5');
i2c_write('0');
i2c_write('3');
i2c_stop();
}
} |
And the program for Slave device:
Code: | #USE I2C(slave, slow, sda=PIN_C4, scl=PIN_C3, address=0x30, force_hw)
#INT_SSP
...
while(1) // I2C start running
{
lcd_putc("\f I2C data: \n");
i2c_start();
address1=i2c_read();
address2=i2c_read();
value21=i2c_read();
value22=i2c_read();
value23=i2c_read();
value24=i2c_read();
value25=i2c_read();
i2c_stop();
lcd_putc(value21);
lcd_putc(value22);
lcd_putc(value23);
lcd_putc(value24);
lcd_putc(value25);
delay_ms(1000);
} //end while of I2C |
When I run my program in ISIS, it would display:
I2C data:
w503
and the second round would display:
I2C data:
¬ `
which could not be understood at all.
Is there any lady and gentleman can give me your suggestion?
I do thank you for your care. _________________ Enjoy our EEE |
|
![](templates/subSilver/images/spacer.gif) |
applecon2000
Joined: 29 Jul 2007 Posts: 31 Location: UK
|
my reply for SPI issue |
Posted: Wed Aug 22, 2007 11:34 am |
|
|
Of course I haven't.
The program I wrote in Master device was shown below:
Code: | #include <16F876.h>
#fuses XT,NOWDT,NOPROTECT,NOPUT,NOBROWNOUT,NOLVP,NODEBUG
#use delay(clock=4000000)
#define EEPROM_SELECT PIN_B0
#USE standard_IO(A)
#USE standard_IO(B)
#USE standard_IO(C)
#INT_SSP
void main()
{
int8 i;
setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_16);
while (1)
{
for (i='0'; i<='9'; i++)
{
output_low(EEPROM_SELECT); // Slave select
spi_write(i);
output_high(EEPROM_SELECT); // Slave deselect
delay_ms(1000);
}
}
} |
For the slave devcie, the portion of codes were:
Code: | setup_spi(SPI_SLAVE | SPI_H_TO_L);
while (1) // SPI start running
{
value3 = spi_read(); // Will wait until a character is received.
lcd_putc("\f SPI data: \n");
lcd_putc(value3);
delay_ms(1000);
} |
I cannot find any flaws within the codes above.
Waiting for your suggestion.
By the way, if you can, please give me your suggestion about my I2C issure as well.
Thanks a lot. _________________ Enjoy our EEE |
|
![](templates/subSilver/images/spacer.gif) |
SET
Joined: 15 Nov 2005 Posts: 161 Location: Glasgow, UK
|
|
Posted: Wed Aug 22, 2007 11:54 am |
|
|
Why do you have
Your code doesn't use interrupts - and I agree, it looks like it should work. |
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Aug 22, 2007 12:11 pm |
|
|
The slave is receiving something, so the Slave Select and clock lines are connected correctly.
Do you have a cross in the SDI and SDO lines? SDO of the master must be connected to the SDI of the slave and vice versa.
Also remove the delay_ms() in the slave, it is not required and can mess up the synchronization between the units. Most likely this is why your I2C version is messed up the second round.
In the I2C slave remove the i2c_start and i2c_stop() calls, these are master only commands. |
|
![](templates/subSilver/images/spacer.gif) |
Tim Bobbins Guest
|
|
Posted: Wed Aug 22, 2007 7:14 pm |
|
|
Also,
Shouldnt the #use delay line refer to 20,000,000 or less since the 876 doesnt support freq > 20Mhz.
Quote: | #use delay(clock=4000000) |
Quote: | #use delay(clock=20M) |
|
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Aug 23, 2007 2:31 am |
|
|
Tim Bobbins wrote: | Shouldnt the #use delay line refer to 20,000,000 or less since the 876 doesnt support freq > 20Mhz. | There is no problem in the code. It is 4M, not 40M |
|
![](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
|