|
|
View previous topic :: View next topic |
Author |
Message |
temopic
Joined: 20 Feb 2008 Posts: 27 Location: mexico
|
SPI Pic to Pic Problem (Master can't read Slave) |
Posted: Wed Feb 20, 2008 12:59 pm |
|
|
this is an app SMMS (Single Master-Multiple Slave) (one Master and 6 Slaves) All Pic18f2520.
I succesfully communicate the PC RS-232-to-Master and Master-to-Slave
But need help to communicate the Slave-to-Master. I cant make the Master to read from the Slave
=====================================
=========Master========================
=====================================
//Master Main Program
#include <18F2520.h>
#include <USART1.c>
#include <SPIMASTER.c>
#include <portinimaster.c>
#BYTE sbuffer = 0xFc9
#include <string.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
void main() {
int b=0;
char data;
char a;
char z;
port_initialize();
enable_interrupts(global);
enable_interrupts(int_rda);
enable_interrupts(INT_SSP);
delay_ms(255);
output_low(pin_c2); //test led On
delay_ms(500);
output_high(pin_c2); //test led Off
delay_ms(500);
do {
a=bgetc();
bputc("\n\r");
bputc(a);
spi_write(a);
delay_ms(20);
z=spi_read(0)
bputc(z); //Print "z" in Hyperterminal
z=spi_read(0)
bputc(z);
z=spi_read(0)
bputc(z);
z=spi_read(0)
bputc(z);
}
}
//------------------------------------------------------------------------------
void port_initialize(){
set_tris_b(0b00000101);
set_tris_c(0b10010011);
set_tris_a(0b10100000);
//setup_spi(spi_slave | spi_H_to_l | spi_clk_div_16);
setup_spi(spi_master | spi_l_to_h | spi_clk_div_16);
//----------------------------------------------------
//SPI Master
char c;
char mensaje[8] = {0x30,0x31,0x32,0x33,0x34,0x35,0x36};
#int_SSP
void SPIMANGER() {
}
=====================================
=========SLAVE========================
=====================================
#include <18F2520.h>
#include <USART1.c>
#include <SPISLAVE.c>
#include <portinislave.c>
#include <string.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
void main() {
int b=0;
char data;
char a;
byte d;
d =0x41;
port_initialize();
enable_interrupts(global);
enable_interrupts(int_rda);
enable_interrupts(INT_SSP);
output_low(pin_c2); //test led On
delay_ms(500);
output_high(pin_c2); //test led Off
delay_ms(500);
do {
output_low(pin_c2); //test led On
delay_ms(50);
output_high(pin_c2); //test led Off
delay_ms(50);
a=bgetc();
bputc("\n\r");
bputc(a); //Print "a" in the Hyperterminal
}
}//end main
-------------------------------------------
char c;
int8 index;
int8 SPIData;
long i=0;
char mensaje[30] = {""};
#int_SSP
void SPIMANGER() {
if( spi_data_is_in() )
c = spi_read();
bputc(c); //Print "c" in the hyperterminal
//spi_write(c);
}
--------------------------------
void port_initialize(){
output_low(pin_b7);
output_high(pin_c0);
set_tris_b(0x20); //make port b all outputs
set_tris_D(0x40); // make d7 input all other output
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
What I am doing wrong?
I am working with PCWH compiler
ide version 3.212
pcb 3.212
pcm 3.212
pcm 3.212 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Feb 20, 2008 1:29 pm |
|
|
1. Get rid of 90% of your code and write small test programs for both
master and slave.
2. Don't try to make it work with multiple slaves. Just make it work
with one slave for this first test.
3. Don't set the TRIS. Use standard i/o mode and let the compiler
do it for you automatically. (Standard i/o is the default mode of the
compiler. You don't have to do anything to enable it).
4. Look at the CCS example code for an SPI slave.
Quote: | c:\program files\picc\examples\ex_spi_slave.c |
Look at other examples in the forum archives:
http://www.ccsinfo.com/forum/viewtopic.php?t=26888 |
|
|
temopic
Joined: 20 Feb 2008 Posts: 27 Location: mexico
|
|
Posted: Wed Feb 20, 2008 5:42 pm |
|
|
=======Master Code===========
Code: | #include <18F2520.h>
#include <USART1.c>
#include <string.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
void main() {
char a;
char b;
int i=0;
delay_ms(300);
set_tris_b(0b00000101);
set_tris_c(0b10010011);
set_tris_a(0b10100000);
setup_spi(spi_master | spi_l_to_h | spi_clk_div_16);
output_low(pin_b7); //test pin 2
output_low(pin_b6); //test pin 1
output_low(pin_b5);
output_low(pin_b4);
output_low(pin_a3);
output_low(pin_a2);
output_low(pin_a1);
output_low(pin_a0);
enable_interrupts(global);
enable_interrupts(int_rda);
enable_interrupts(INT_SSP);
bputc("\n\r");
printf("Hello World");
bputc("\n\r");
output_high(pin_c2); //test led
delay_ms(1000);
output_low(pin_c2); //test led
do {
output_high(pin_c2); //test led
delay_ms(50);
a=bgetc();
bputc("\n\r");
printf("%C escribiste", a);
spi_write(a); // or whatever
delay_ms(50);
for (i=0; i<8; ++i)
{
b=spi_read(0);
bputc(b);
bputc("\n\r");
delay_ms(50);
}
output_low(pin_c2); //test led
}
while(1);
}//end main
#int_SSP
void SPIMANGER() {
} |
===================Slave1 Code=============
Code: | #include <18F2520.h>
#include <USART1.c>
#include <string.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
void main() {
char a;
set_tris_b(0b00000101);
set_tris_c(0b10011011);
set_tris_a(0b10100000);
setup_spi(spi_slave | spi_L_to_H);
output_low(pin_b7); //test pin 2
output_low(pin_b6); //test pin 1
output_low(pin_b5);
output_low(pin_b4);
output_low(pin_a3);
output_low(pin_a2);
output_low(pin_a1);
output_low(pin_a0);
output_low(pin_c2);
enable_interrupts(global);
enable_interrupts(int_rda);
enable_interrupts(INT_SSP);
printf(bputc, "Se escribira lo que recibes por SPI");
do {
output_high(pin_c2); //test led
delay_ms(100);
a=bgetc();
bputc("\n\r");
bputc(a);
output_low(pin_c2); //test led
}
while (1);
}//end main
char c;
#int_SSP
void SPIMANGER() {
c = spi_read();
bputc(c);
spi_write(c);
spi_write(c+1);
spi_write(c+2);
spi_write(c+3);
spi_write(c+4);
spi_write(c+5);
spi_write(c+6);
spi_write(c+7);
} |
==============================
==============================
==============================
Thank you PCM programmer
your recommendations 1 and 2 fixed my problem
One more question:
Have somebody implement some kind of handshake-protocol-crc for SingleMaster-MultipleSlaves? |
|
|
|
|
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
|