CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Problem with memory SPI

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ad



Joined: 24 Mar 2015
Posts: 14

View user's profile Send private message

Problem with memory SPI
PostPosted: Fri Jun 19, 2015 2:54 am     Reply with quote

I have a N25Q064 memory with communication SPI, my problem is that not read correctly. It always reads 0. I don't know because it happens.

My code:

Code:


#include <18F4550.h>
//!#fuses XT               //Cristal de cuarzo de 8 MHz
//!#fuses NOWDT            //Perro guardian deshabilitado
//!#fuses NOPROTECT        //Inhabilitado la proteccion contra la lectura
//!#fuses NOLVP   
//!#fuses MCLR
//#fuses INTHS                  //Oscilador Interno

#use delay(clock=20MHz)
//#use delay(internal=8MHz)       //Velocidad Oscilador Interno
#use spi(master,DI=PIN_C7,DO=PIN_B0,CLK=PIN_B1,ENABLE=PIN_A5)                                 //Comunicacion PIC-MEMORIA SPI
#include "C:\Users\x\PROGRAMA_DEFINITIVO\flex_lcd420.c"       //LCD
//!#include <math.h>                                   
//!#include "C:\Users\x\PROGRAMA_DEFINITIVO\kbd.c"               //Teclado
//!#include <stdlib.h>

//Variables
int dato;
int direccion;

void main(){

//Configuracion de la memoria
setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_16);
//Inicializar LCD
lcd_init();
//Habilitar Escritura
output_low(PIN_A5);            //Habilitar dispositivo
spi_write(6);                    //Habilitar la escritura de la memoria
output_high(PIN_A5);            //Deshabilitar el dispositivo
//Escribir Memoria
output_low(PIN_A5);            //Habilitar dispositivo
spi_write(2);                    //Instruccion de escritura de la memoria
spi_write(0);                    //Direccion de la memoria
lcd_putc("E");
for (dato=0;dato<7;dato++){               //Dato
spi_write(dato);                    //Valor del dato
printf(lcd_putc,"%u",dato);         //Muestra el valor del dato en LCD
delay_ms(500);                   //Tiempo de espera
}
//Leer
output_high(PIN_A5);            //Deshabilita escritura
delay_ms(1000);
lcd_putc("\nL");
output_low(PIN_A5);            //Habilitar dispositivo
spi_write(3);                     //Habilitar la lectura de la memoria
spi_write(0);
for (direccion=0;direccion<7;direccion++){
dato=(spi_read(direccion));                 //Lee el dato
printf(lcd_putc,"%u",dato);          //Muestra el valor del dato en LCD
delay_ms(500);
}
output_high(PIN_A5);            //Deshabilita escritura
}


Ttelmah



Joined: 11 Mar 2010
Posts: 19499

View user's profile Send private message

PostPosted: Fri Jun 19, 2015 8:02 am     Reply with quote

First thing. How are you handling voltages?. You have a 5v PIC. The 4550, requires 4.2v minimum. Your memory supports 3.6v maximum. The PIC SPI running requires signals to go up to 4v. The memory can't do this without a level translator between the chips, and if you are feeding the PIC SPI outputs directly into the chip you risk damaging it.....

Second comment. It is surprising your chip is actually working. Fuses are needed. You may just be lucky, and the erased state of the fuses is defaulting to a working combination, but this is _luck_.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sat Jun 20, 2015 8:00 am     Reply with quote

And then the other issues:
Code:
#use spi(master,DI=PIN_C7,DO=PIN_B0,CLK=PIN_B1,ENABLE=PIN_A5)
...
setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_16);
The CCS compiler supports two sets of SPI-functions:
1) The old set, a combination of the functions setup_spi(), spi_write() and spi_read().
These functions can only be used with the hardware SPI module in the processor.
2) The newer functions consisting of '#use SPI' and spi_xfer().
These newer functions are more flexible and can generate a software UART when you use pins not connected to a hardware SPI module.

BEWARE: you can't mix the two function sets in your code! It is undefined how your SPI module will be configured when you mix the functions of both sets as you do in your code.

Now, to which pins is your external device connected?
In the #use SPI setup you define C7 for SPI input, but in the SPI hardware from the processor it is a SPI output. The compiler solves this by generating a software SPI module instead. Software SPI will work, but it is a waste to use the correct hardware pins for hardware SPI and then have them connected wrong.

SPI can work in one of four communication modes. You didn't specify this 'mode' parameter. 75% chance the compiler default is wrong for your chip...
ad



Joined: 24 Mar 2015
Posts: 14

View user's profile Send private message

PostPosted: Mon Jun 29, 2015 2:49 am     Reply with quote

Realized I was mixing the communication protocols of hardware with software. I have established that the software will therefore be used:

#use spi (master, DI = PIN_C7, DO = PIN_B0, CLK = PIN_B1, ENABLE = PIN_A5) for configuration.

spi_xfer (stream DATA) for the issue of communicating.

Now that I have found this error I'm a little lost, such as saving of the values obtained of an NTC temperature for example every 5 seconds, for an hour.

We know that #task ( rate = 5s , max = 1ms ) NTC void ();

NTC is read within 5 seconds.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Jun 29, 2015 4:03 am     Reply with quote

ckielstra wrote:
SPI can work in one of four communication modes. You didn't specify this 'mode' parameter. 75% chance the compiler default is wrong for your chip...
ad wrote:
#use spi (master, DI = PIN_C7, DO = PIN_B0, CLK = PIN_B1, ENABLE = PIN_A5)
Rolling Eyes
Never mind, I'm out.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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