This time we use two PIC 18F4550 with one 25aa020a write the memory and the other one we read, we will use a switch to coordinate reading and writing.
for this program we will need to set the spi interface. For this there are two ways:
1 – we can use the setup_spi instruction.
ej.setup_spi (spi_MASTER | spi_L_to_H | spi_clk_div_16 | SPI_XMIT_H_TO_L)
Here we configure the micro as a master, the communication starts at the falling edge, the transmission speed is 16 times less than the clock speed.
with this option, we can use the spi_read guidelines and spi_write to configure the program chosen.
2–way choice for us is to use spi settings.
eg. # use SPI (MASTER, DI = PIN_D1, DO = PIN_D2, CLK = PIN_D3, MODE = 0, BITS = 8, IDLE = 0).
Here we configure the micro as a master, the pins used for data transmission, data input DI, DO output data, clock, transmit bits 8 and that will break the clock.With this configuration you can only use the command to write spi_xfer so as to read
First configure the micro writing, in this case the switch is pressed.
We write in the Hyperterminal desired phrase, no more than 32 characters is the maximum that showing alphanumeric lcd.
First configure the micro writing, in this case the switch is pressed.
We write in the Hyperterminal desired phrase, no more than 32 characters is the maximum that showing alphanumeric lcd.
The program will see the first thing we do is delete the first 33 characters of writing to memory ff.
///////////////////////////////////////////////////////////////////////////////////////////
// AUTOR: sergio saiz Octubre/2013
///////////////////////////////////////////////////////////////////////////////////////////
// PROGRAMA: MAESTRO esclavo SPI VERSIÓN: 1.0
// DISPOSITIVO: PIC 18F4550 COMPILADOR: CCS vs4.023
// Entorno IDE: MPLAB v8.89 SIMULADOR: Proteus 8.0 sp1
// TARJETA DE APLICACIÓN: Trainer DEBUGGER: ICD3
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
// Estudio de la comunicación del bus serie SPI //
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
// CABECERA ///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
#include <18f4550.h>
#FUSES INTHS, MCLR //reloj de alta velocidad
#use delay(internal=8Mhz)// Selecciona la velocidad del oscilador interno
#use SPI(MASTER, CLOCK_HIGH= 1, DI= PIN_D1, DO= PIN_D2, CLK= PIN_D3, MODE= 0, BITS= 8, IDLE= 0)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)//configuracion del rs232
#include<C:\Users\troner\Desktop\sjd2\Desarrolo de prototipos\sistema _mc_2014\proyectos mplab\sexto proyecto leer y escribir spi\LCD_flexible.c>
////////////////////////////////////////////////////////////////////////////////////
// VARIABLES GLOBALES //////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
char dato;
char frase[15];//string de 15 caracteres que usaremos para sacarlo x la primera linea del lcd
char sfrase[15];//usamos para segunda linea
int i=0;//inicializamos a 0 la variable numerica
int borrado=0;
int pos=0;
int spos=0;
////////////////////////////////////////////////////////////////////////////////////
// FUNCIONES ///////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
// PRINCIPAL ///////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
void main()
{
lcd_init();
output_high(PIN_A0);//ponemos cs en alto desactivando la eeprom
puts(«ESPERA UNOS SEGUNDOS MIENTRAS SE BORRA LA MEMORIA…\n»);
for(borrado=0;borrado<=33;borrado++)//reescribimos 33 datos de memoria porque son los maximos que mostramos por el lcd
{
output_low(PIN_A0);//escucha(ponemos el cs en bajo seleccionando la eeprom)
spi_xfer(6);//configuracion wren proteccion escritura QUITADA wren 0b00000110(6)
output_high(PIN_A0);//ponemos el cs en alto para desactivar el cs y que se grave la eeprom con la instruccion
delay_ms(10);//tiempo minimo de grabacion 5ms aunq con el reloj a 8mhz tarda un ciclo maquina 0,5ms
output_low(PIN_A0);//escucha
spi_xfer(2);//comando escritura 0b00000010
spi_xfer(i);//direccion escritura
spi_xfer(0xff);//dato a escribir,borrar dato con ff 0b11111111
output_high(PIN_A0);
i++;//incrementa la variable entera que usamos para la direccion de memoria
output_low(PIN_A0);
spi_xfer(0b00000100);//reestablece la proteccion de la escritura
output_high(PIN_A0);
delay_ms(10);
}
i=0;//si la variable que nos indica la direccion de la memoria es la primera
puts(«…BORRADO COMPLETADO…\n»);
puts(«Escriba los caracters a transmitir en la memoria\n»);
puts(«para finalizar pulse enter…\n»);
while(1)
{
dato=getc();//char dato=dato que esta en el buffer del teclado
putc(dato);//escribe en el hiperterminal el char dato
if(spos<16)//si la variable spos vale menos de 16 por que 16 son los byte maximos que se ven en el lcd
{
if(pos<15)//si la variable pos vale menos de 15
{
frase[pos]= dato;//el caracter frase se escribe con el valor del dato en cada posicion de la variable entera pos
pos++;//aumenta la posicion de la variable pos que usaremos para la siguiente posicion del char frase
}
else//si la variable pos vale mas de 15
{
sfrase[spos]= dato;//sfrase se escribe con el valor de dato en cada posicion de spos
spos++; //aumenta la posicion de spos que usaremos para la siguiente posicion del char sfrase
}
}
output_low(PIN_A0);//escucha
spi_xfer(6);//configuracion wren proteccion escritura QUITADA wren
output_high(PIN_A0);//graba
delay_ms(5);
output_low(PIN_A0);//escucha
spi_xfer(2);//comando escritura
spi_xfer(i);//direccion escritura
spi_xfer(dato);//dato a escribir
output_high(PIN_A0);
i++;//incrementa direccion
output_low(PIN_A0);
spi_xfer(0b00000100);//proteccion escritura
output_high(PIN_A0);
if(dato == 0x0D)//si el dato es igual a enter
{
lcd_gotoxy(1,1);//escribe en la primera linea del lcd
printf(lcd_putc,»%s»,frase);//escribe el string frase que creamos antes con el algoritmo
lcd_gotoxy(1,2);//escribe en la segunda linea del lcd
printf(lcd_putc,»%s»,sfrase);//escribe el string
puts(«FIN DE LA TRANSMISION»);
for(;;); //mantente en espera
}
}
}
In the case of reading from memory, the switch must be without pressing, so after making the writing part, got the switch and automatically read the memory and the data is written to the lcd.
///////////////////////////////////////////////////////////////////////////////////////////
// AUTOR: sergio saiz Octubre/2013
///////////////////////////////////////////////////////////////////////////////////////////
// PROGRAMA: MAESTRO esclavo lectura SPI VERSIÓN: 1.0
// DISPOSITIVO: PIC 18F4550 COMPILADOR: CCS vs4.023
// Entorno IDE: MPLAB v8.89 SIMULADOR: Proteus 8.0 sp1
// TARJETA DE APLICACIÓN: Trainer DEBUGGER: ICD3
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
// Estudio de la comunicación del bus serie SPI //
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
// CABECERA ///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
#include <18f4550.h>
#FUSES INTHS, MCLR //reloj de alta velocidad
#use delay(internal=8Mhz)// Selecciona la velocidad del oscilador interno
#use SPI(MASTER, CLOCK_HIGH= 1, DI= PIN_D1, DO= PIN_D2, CLK= PIN_D3, MODE= 0, BITS= 8, IDLE= 0)
#include<C:\Users\troner\Desktop\sjd2\Desarrolo de prototipos\sistema _mc_2014\proyectos mplab\sexto proyecto parte lectura spi\LCD_flexible.c>
////////////////////////////////////////////////////////////////////////////////////
// VARIABLES GLOBALES //////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
char dato;
int i=0;//inicializamos a 0 la variable numerica
int pos=0;
int spos=0;
int x=0;
////////////////////////////////////////////////////////////////////////////////////
// FUNCIONES ///////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
// PRINCIPAL ///////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
void main()
{
lcd_init();
lcd_putc(‘\f’);
while(1)
{
if(spos<16)//si la variable spos vale menos de 16 por que 16 son los byte maximos que se ven en el lcd
{
if(pos<15)//si la variable pos vale menos de 16
{
pos++;
printf(lcd_putc,»%c»,dato);
}
else//si la variable pos vale mas de 15
{
lcd_gotoxy(x,2);
printf(lcd_putc,»%c»,dato);
spos++; //aumenta la posicion de spos que usaremos para la siguiente posicion del char sfrase
x++;
}
}
output_high(PIN_A0);termina la opcion de lectura poniendo el cs en alto
output_low(PIN_A0);//escucha pon el cs a bajo y comienza la lectura
spi_xfer(0b00000011);//configuracion lectura
delay_ms(5);
spi_xfer(i);//direccion lectura que empieza en la direccion 0 e incrementa con la i
dato=spi_xfer();//dato leido es igual a la vairable dato
if(dato == 0x0D)//si el dato es igual a enter
{
for(;;); //mantente en espera
}
i++;//incrementa direccion
}
}