Guess who’s back ?!?!?!?! Ok, right it’s just me. In this practice I’m going to write with one pic and read with the other pic.
We already Spoke about what SPI is.
This is my practice when it was totally mounted.
I have two programs, one for the writer pic, and other for the reader pic.
The writter pic:
////////////////////////////////////////////////////////////////////////////////////
// AUTOR: Oscar Alsina Merino Fecha Jueves 30/10/2013 ///
////////////////////////////////////////////////////////////////////////////////////
// PROGRAMA: Estudio memoria eeprom SPI VERSIÓN: 1.0
// DISPOSITIVO: PIC 16F84550 COMPILADOR: CCS vs4.140
// Entorno IDE: MPLAB IDEv8.89 SIMULADOR: Proteus 7.8
// TARJETA DE APLICACIÓN: Ninguna DEBUGGER: Ninguno
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//*******Estudio del SPI**////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
// CABECERA ////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
#include <18f4550.h>
#fuses INTHS,MCLR
#use delay(internal=8mhz)
#use SPI(MASTER, CLOCK_HIGH= 1, DI= PIN_D1, DO= PIN_D2, CLK= PIN_D3, MODE= 0, BITS= 8, IDLE= 0)
//#use spi(DI=PIN_D1, DO=PIN_D2, CLK=PIN_D3, BITS=16)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#include <LCD_flexible.c>
////////////////////////////////////////////////////////////////////////////////////
// VARIABLES GLOBALES //////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
char mensaje;
int dir = 0;
////////////////////////////////////////////////////////////////////////////////////
// INTERRUPCION ////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
#INT_EXT
void RB0(void)
{
//setup_spi(spi_master | SPI_L_TO_H |spi_clk_div_16 | SPI_XMIT_L_TO_H );
puts(«Escriba su mensaje para guardar en la memoria»);
output_high(PIN_A0);
while(1)
{
//output_high(PIN_A0);
mensaje=getc();
putc(mensaje);
output_low(PIN_A0);
spi_xfer(0b00000110);
output_high(PIN_A0);
delay_ms(5);
output_low(PIN_A0);
spi_xfer(0b00000010);
spi_xfer(dir);
spi_xfer(mensaje);
output_high(PIN_A0);
dir++;
if(mensaje == 0x0d)
{
puts(«Mensaje guardado»);
output_low(PIN_A0);
spi_xfer(0b00000100);
output_high(PIN_A0);
delay_ms(20);
output_high(PIN_B1);
for(;;){}
//outpud_high(PIN_B0);
}
output_low(PIN_A0);
spi_xfer(0b00000100);
output_high(PIN_A0);
}
}
////////////////////////////////////////////////////////////////////////////////////
// PROGRAMA ////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
void main(void)
{
enable_interrupts(int_ext); //activar interrupcion rb4:7
enable_interrupts(GLOBAL); //activar interrupciones
output_low(PIN_B1);
while(1);
}
and the reader pic:
////////////////////////////////////////////////////////////////////////////////////
// AUTOR: Oscar Alsina Merino Fecha Jueves 30/10/2013 ///
////////////////////////////////////////////////////////////////////////////////////
// PROGRAMA: Estudio memoria eeprom SPI VERSIÓN: 1.0
// DISPOSITIVO: PIC 16F84550 COMPILADOR: CCS vs4.140
// Entorno IDE: MPLAB IDEv8.89 SIMULADOR: Proteus 7.8
// TARJETA DE APLICACIÓN: Ninguna DEBUGGER: Ninguno
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//*******Estudio del SPI**////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
// CABECERA ////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
#include <18f4550.h>
#fuses INTHS,MCLR
#use delay(internal=8mhz)
//#use SPI(MASTER, CLOCK_HIGH= 1, DI= PIN_D1, DO= PIN_D2, CLK= PIN_D3, MODE= 0, BITS= 8, IDLE= 0)
//#use spi(DI=PIN_D1, DO=PIN_D2, CLK=PIN_D3, BITS=16)
//#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#include <LCD_flexible.c>
////////////////////////////////////////////////////////////////////////////////////
// VARIABLES GLOBALES //////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
char data1[40];
int dir = 0;
int inc=0;
int cuenta=0;
////////////////////////////////////////////////////////////////////////////////////
// INTERRUPCION ////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
#INT_EXT2
void RB0()
{
output_low(PIN_C5);
setup_spi(spi_master | SPI_L_TO_H |spi_clk_div_16 | SPI_XMIT_L_TO_H );
lcd_init();
output_high(PIN_A0);
delay_ms(5);
while(cuenta <= 40)
{
output_low(PIN_A0);
spi_read(0b00000011);
spi_read(dir);
data1[inc] = spi_read(dir);
output_high(PIN_A0);
inc++;
cuenta++;
dir++;
delay_ms(20);
}
lcd_gotoxy(1,1);
printf(lcd_putc,»%s»,data1);
for(;;){}
}
////////////////////////////////////////////////////////////////////////////////////
// PROGRAMA PRINCIPAL //////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
void main(void)
{
output_high(PIN_C5);
enable_interrupts(int_ext2); //activar interrupcion rb4:7
enable_interrupts(GLOBAL); //activar interrupciones
while(1);
}
Remember that you can download this program from the link below:
http://www.mediafire.com/download/ayaclpelf29l56n/Estudio_memoria_eeprom_SPI.zip