//Comunicar 2 PICs 18F4550 utilizando el protocolo SPI.
//
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
//CABECERA///////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
#include <18f4550.h> //pic a utilizar
#fuses inths //oscilador interno
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#FUSES BORV20 //Brownout reset at 2.0V
#FUSES NOPUT //No Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES NOSTVREN //Stack full/underflow will not cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES NOIESO //Internal External Switch Over mode disabled
#FUSES NOFCMEN //Fail-safe clock monitor disabled
#FUSES NOPBADEN //PORTB pins are configured as digital I/O on RESET
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES NOCPB //No Boot Block code protection
#FUSES NOLPT1OSC //Timer1 configured for higher power operation
#FUSES NOMCLR //Master Clear pin used for I/O
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#use delay(internal=8Mhz) //frecuencia del oscilador
/////////////////////////////////////////////////////////////////////////////////////
//VARIABLES GLOBALES////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
//FUNCIONES/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
//PRINCIPAL/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
void main( )
{
setup_spi(spi_master | spi_l_to_h | spi_clk_div_16); //configurar spi como maestro
output_high(PIN_B2);
delay_ms(50);
while(1)
{
spi_write(1); //escribir en el pin ra1
output_low(PIN_B2);
delay_ms(50);
spi_write(0); //escribir en el pin ra1
output_high(PIN_B2);
delay_ms(50);
}
}
//Comunicar 2 PICs 18F4550 utilizando el protocolo SPI. Cuando hay un "1" lógico
//en el pin ra1 se enciende un diodo led.
//
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
//CABECERA///////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
#include <18f4550.h> //pic a utilizar
#fuses inths //oscilador interno
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#FUSES BORV20 //Brownout reset at 2.0V
#FUSES NOPUT //No Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES NOSTVREN //Stack full/underflow will not cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES NOIESO //Internal External Switch Over mode disabled
#FUSES NOFCMEN //Fail-safe clock monitor disabled
#FUSES NOPBADEN //PORTB pins are configured as digital I/O on RESET
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES NOCPB //No Boot Block code protection
#FUSES NOLPT1OSC //Timer1 configured for higher power operation
#FUSES NOMCLR //Master Clear pin used for I/O
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#use delay(internal=8Mhz) //frecuencia del oscilador
/////////////////////////////////////////////////////////////////////////////////////
//VARIABLES GLOBALES////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
//FUNCIONES/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
//PRINCIPAL/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
void main()
{
output_c(0x00);
setup_spi(spi_slave | spi_l_to_h | spi_clk_div_16); //configurar spi como esclavo
for(;;)
{
/* output_low(PIN_C0);
delay_ms(500);
output_high(PIN_C0);
delay_ms(500);
*/
if(spi_data_is_in()) //si hay un dato en el spi
{
//output_d(spi_read());
output_c(spi_read());
}else{
}
}
}
//Comunicar 2 PICs 18F4550 utilizando el protocolo SPI. Cuando hay un "1" lógico
//en el pin ra1 se enciende un diodo led.
//
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
//CABECERA///////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
#include <18f4550.h> //pic a utilizar
#fuses inths //oscilador interno
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#FUSES BORV20 //Brownout reset at 2.0V
#FUSES NOPUT //No Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES NOSTVREN //Stack full/underflow will not cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES NOIESO //Internal External Switch Over mode disabled
#FUSES NOFCMEN //Fail-safe clock monitor disabled
#FUSES NOPBADEN //PORTB pins are configured as digital I/O on RESET
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES NOCPB //No Boot Block code protection
#FUSES NOLPT1OSC //Timer1 configured for higher power operation
#FUSES NOMCLR //Master Clear pin used for I/O
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#use delay(internal=8Mhz) //frecuencia del oscilador
/////////////////////////////////////////////////////////////////////////////////////
//VARIABLES GLOBALES////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
//FUNCIONES/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
//PRINCIPAL/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
void main()
{
output_c(0x00);
setup_spi(spi_slave | spi_l_to_h | spi_clk_div_16); //configurar spi como esclavo
for(;;)
{
/* output_low(PIN_C0);
delay_ms(500);
output_high(PIN_C0);
delay_ms(500);
*/ if(spi_data_is_in()) //si hay un dato en el spi
{
//output_d(spi_read());
//output_high(PIN_C0);
output_e(spi_read());
output_toggle(PIN_C0);
delay_ms(50);
}else{
}
}
}
lo estas haciendo con pics reales o una simulacion en proteus??
Usas el oscilador interno? Hay veces que el oscilador interno dá muchos problemas en las comunicaciones, me ha pasado alguna que otra vez
Has probado usando un cristal externo?
A mi me parece que el error puede ser que estás demorando la recepción de datos más de lo que demoran en llegar, y produciendo un overflow del buffer de recepción.
Maestro:Código: C#
///////////////////////////////////////////////////////////////////////////////////// //PRINCIPAL///////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////// void main( ) { setup_spi(spi_master | spi_l_to_h | spi_clk_div_16); //configurar spi como maestro output_high(PIN_B2); delay_ms(50); while(1) { spi_write(1); //escribir en el pin ra1 output_low(PIN_B2); delay_ms(1000); spi_write(0); //escribir en el pin ra1 output_high(PIN_B2); delay_ms(1000); } }
Esclavo:Código: C#
///////////////////////////////////////////////////////////////////////////////////// //PRINCIPAL///////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////// void main() { output_c(0x00); setup_spi(spi_slave | spi_l_to_h | spi_clk_div_16); //configurar spi como esclavo for(;;) { if(spi_data_is_in()) //si hay un dato en el spi output_d(spi_read()); } }
y no olvides deshabilitar el Watch Dog Timer en el Maestro, que veo has omitido su FUSE.
Saludos.
/* output_low(PIN_C0);
delay_ms(500);
output_high(PIN_C0);
delay_ms(500);
*/
en los fuses no declaras el tipo de comunicacion spi que deseas.... usas el spi por software o hardware?
prueba insertando estas lineas despues del #use delay()
#use spi(MASTER,BITS=8,FORCE_HW)
Podrías limpiar el bit de la interrupción spi poniendo a 0 el bit respectivo
que tipo de sensor? usando entrada analogica esta conectado ese sensor? si es asi... que resolucion tienes en las entradas ADC? que tipo de dato quieres visualizar? o mejor dicho... como lo quieres visualizar?