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

Interface AT25F4096 with PIC16F877
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
bal_square



Joined: 05 Dec 2018
Posts: 11

View user's profile Send private message

Interface AT25F4096 with PIC16F877
PostPosted: Wed Dec 05, 2018 11:58 am     Reply with quote

I'm trying to connect PIC controller with AT25F4096 for reading information, but I'm a beginner in this theme and I don't know how to connect it right and how to make a program using CCS, I couldn't find any useful information, please, help me.

[img]https://ibb.co/7kZ5ScT[/img]
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Wed Dec 05, 2018 12:24 pm     Reply with quote

I can't say anything about the programming for now, but here are some things with your physical setup:

1. Both of the SPI data lines (SI and SO) should be connected.
2. MCU data out should be plugged to AT25F4096 data in and vice versa.
3. You need to connect the chip select (CS) pin to a GPIO pin on your MCU.

You should read a tutorial on SPI. This one was good as a primer when I was also a total beginner:
https://learn.sparkfun.com/tutorials/serial-peripheral-interface-spi/all
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Wed Dec 05, 2018 3:07 pm     Reply with quote

There is a big issue.

The AT25F4096, is a 3.3v device. The 16F877 mentioned in your header, is a 5v PIC (4v minimum). You need SPI level buffering to use these two devices together (and two power supply rails). Look at the 'sticky' thread at the head of the forum on interfacing a 3.3v SD card with a 5v PIC. The circuitry you need is the same as for this.
The interface code is actually similar to that used by smaller memories like the 24512, but then needs 8 sectors implemented instead of one.
You really will need a PIC with more RAM than the 16F877. The transfers will normally be in a 256byte page, and honestly it'll be far easier with a PIC that can store this in RAM.
A lot depends on what you actually want to 'do' with the data....
temtronic



Joined: 01 Jul 2010
Posts: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Dec 05, 2018 6:18 pm     Reply with quote

You really should get a current PIC as the 877 is obsolete and NOT suggested for use anymore. the 16F18877 is one possible upgrade..less money, more features INCLUDING being able to run at 3 volts. THAT makes it compatible with the AT device so no need for logic level convertors, so simpler PCB !

Jay
bal_square



Joined: 05 Dec 2018
Posts: 11

View user's profile Send private message

PostPosted: Thu Dec 06, 2018 2:00 pm     Reply with quote

temtronic wrote:
You really should get a current PIC as the 877 is obsolete and NOT suggested for use anymore. the 16F18877 is one possible upgrade..less money, more features INCLUDING being able to run at 3 volts. THAT makes it compatible with the AT device so no need for logic level convertors, so simpler PCB !

Jay


Thank you, I change it to PIC18F4550, I'm using CCS PIC C compiler and trying to get information from AT25F4096, may you explain me the steps of receiving data?

My code:
Code:
   setup_spi(spi_master | spi_l_to_h | spi_clk_div_16);
   output_LOW(SPI_CS);
   temp=spi_read(0);

   output_HIGH(SPI_CS);


Why SCK is always LOW ?
temtronic



Joined: 01 Jul 2010
Posts: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Dec 06, 2018 2:22 pm     Reply with quote

First do basic trouble shooting. cut a program that toggles the SCLK pin,say at 1Hz. Put scope on the pin (or LED+470r) and verify if flashes. If it doesn't then the pin is grounded. Maybe a solder whisker ? Perhaps you've got the wrong pin ?? You've changed PICs so you'll need to confirm you have the correct pin for the SPI port you're using.

Jay
gaugeguy



Joined: 05 Apr 2011
Posts: 303

View user's profile Send private message

PostPosted: Thu Dec 06, 2018 2:25 pm     Reply with quote

That processor is not better. Minimum voltage for the processor is 4.2V.
Maximum voltage for the memory is 3.6V. They cannot both operate from the same supply voltage and so will not have compatible input and output voltages.
How are you attempting to power and connect these devices?
bal_square



Joined: 05 Dec 2018
Posts: 11

View user's profile Send private message

PostPosted: Thu Dec 06, 2018 6:57 pm     Reply with quote

gaugeguy wrote:
That processor is not better. Minimum voltage for the processor is 4.2V.
Maximum voltage for the memory is 3.6V. They cannot both operate from the same supply voltage and so will not have compatible input and output voltages.
How are you attempting to power and connect these devices?


I change it to PIC24FJ128GA006 is it better?
bal_square



Joined: 05 Dec 2018
Posts: 11

View user's profile Send private message

PostPosted: Thu Dec 06, 2018 8:16 pm     Reply with quote



Code:
#include <24FJ128GA006.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOJTAG                   //JTAG disabled
#FUSES CKSFSM                   //Clock Switching is enabled, fail Safe clock monitor is enabled

#device ICSP=1
#use delay(crystal=4000000)
#use rs232(UART2, baud=9600, stream=UART_PORT2)
#define SPI_CS pin_B2

void init(void){
   setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_XMIT_L_TO_H | SPI_CLK_DIV_4);
}

int8 temp;
void main(){
   init();
   printf("\nInit OK");
   output_LOW(SPI_CS);
   temp=spi_read(0);
   printf(temp);
   output_HIGH(SPI_CS);
   
}


I tried this code and scheme, but SCK and SI are always low, I can't understand, It's so hard for me... Can someone explain to me what I should make to read data from this memory? If I understand correctly, the first step is spi_write(instruction from the datasheet "0000 X011 - Read Data from Memory Array"), the second step is spi_read(address of memory cell), isn't it?
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Fri Dec 07, 2018 1:48 am     Reply with quote

No.

You have to send a read command first.

The read command is 0x03. Then you then have to send a 24bit address
(A19 to 23 are don't care), so 19 bits that matter. Then you send one more
byte, which returns the byte you have addressed.

Use the mode number and #USE SPI.

So:
Code:

#include <24FJ128GA006.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOJTAG                   //JTAG disabled
#FUSES CKSFSM                   //Clock Switching is enabled, fail Safe clock monitor is enabled

#device ICSP=1
#use delay(crystal=4000000)
#use rs232(UART2, baud=9600, stream=UART_PORT2)
#define SPI_CS pin_B2
#define READ 0x3
#define ERASE_SECTOR 0x52
#define WREN 0x6
#define WRDI 0x4
#define PROGRAM 0x2

#USE SPI(SPI1, MASTER, MODE=0, STREAM=AT25F4096)

int8 readbyteAT25(int32 address)
{
    unsigned int8 dummy;
    output_low(SPI_CS);
    dummy=spi_xfer(AT25F4096, READ, 8); //issue read command
    spi_xfer(AT25F4096, address, 24); //send 24bit address
    dummy=spi_xfer(AT25F4096, 0, 8); //clock out a zero byte to get reply
    output_high(SPI_CS);
    return dummy; //and return reply
}
   

void main(){
   unsigned int8 temp;
   
   temp=readbyteAT25(0x00000); //read byte from address 0
   printf(temp);
   while (TRUE)
      ;   
}

As shown, a real chip cannot run without Vcap connected, or (since you
specify a crystal), without the crystal. Do _not_ rely on what Proteus
tells you. Read the 'sticky' at the top of the forum about Proteus.... Sad

Your code was actually selecting SPI mode 3, not mode 0, so not surprising
it wouldn't work.....
bal_square



Joined: 05 Dec 2018
Posts: 11

View user's profile Send private message

PostPosted: Fri Dec 07, 2018 9:34 am     Reply with quote

Ttelmah wrote:
No.

You have to send a read command first.

The read command is 0x03. Then you then have to send a 24bit address
(A19 to 23 are don't care), so 19 bits that matter. Then you send one more
byte, which returns the byte you have addressed.

Use the mode number and #USE SPI.

So:
Code:

#include <24FJ128GA006.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOJTAG                   //JTAG disabled
#FUSES CKSFSM                   //Clock Switching is enabled, fail Safe clock monitor is enabled

#device ICSP=1
#use delay(crystal=4000000)
#use rs232(UART2, baud=9600, stream=UART_PORT2)
#define SPI_CS pin_B2
#define READ 0x3
#define ERASE_SECTOR 0x52
#define WREN 0x6
#define WRDI 0x4
#define PROGRAM 0x2

#USE SPI(SPI1, MASTER, MODE=0, STREAM=AT25F4096)

int8 readbyteAT25(int32 address)
{
    unsigned int8 dummy;
    output_low(SPI_CS);
    dummy=spi_xfer(AT25F4096, READ, 8); //issue read command
    spi_xfer(AT25F4096, address, 24); //send 24bit address
    dummy=spi_xfer(AT25F4096, 0, 8); //clock out a zero byte to get reply
    output_high(SPI_CS);
    return dummy; //and return reply
}
   

void main(){
   unsigned int8 temp;
   
   temp=readbyteAT25(0x00000); //read byte from address 0
   printf(temp);
   while (TRUE)
      ;   
}

As shown, a real chip cannot run without Vcap connected, or (since you
specify a crystal), without the crystal. Do _not_ rely on what Proteus
tells you. Read the 'sticky' at the top of the forum about Proteus.... Sad

Your code was actually selecting SPI mode 3, not mode 0, so not surprising
it wouldn't work.....




Unfortunately, It's still not working. I can't understand why SO is always grey. I tried to add voltage, but it did not help.
temtronic



Joined: 01 Jul 2010
Posts: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Dec 07, 2018 10:43 am     Reply with quote

OK, you're using Proteus NOT real parts ???
THAT explains it.. as we all KNOW Proteus is full of bugs, errors and faulty DRCs.
You should be aware that if you used real hardware, connected as shown in the 'schematic' that even the real hardware will NOT function !
You've tried 3 PICs.. an obsolete one, one with internal USB and a powerhouse PIC so I have to assume you're just playing with Proteus and not real PICs. THIS you need to tell us, from the start.

Jay
bal_square



Joined: 05 Dec 2018
Posts: 11

View user's profile Send private message

PostPosted: Fri Dec 07, 2018 10:58 am     Reply with quote

temtronic wrote:
OK, you're using Proteus NOT real parts ???
THAT explains it.. as we all KNOW Proteus is full of bugs, errors and faulty DRCs.
You should be aware that if you used real hardware, connected as shown in the 'schematic' that even the real hardware will NOT function !
You've tried 3 PICs.. an obsolete one, one with internal USB and a powerhouse PIC so I have to assume you're just playing with Proteus and not real PICs. THIS you need to tell us, from the start.

Jay


Yes, I'm a student and I make my home task.
It's the second time when I use this program to simulate something. My task is to connect this type of flash memory with PIC controller. I do not have a good description of how to do this, so I asked for help here.
temtronic



Joined: 01 Jul 2010
Posts: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Dec 07, 2018 11:45 am     Reply with quote

Your problem is that Proteus is defective ! You cannot accomplish your task with that broken tool. Perhaps your teacher will CONFIRM that he has done exactly what you're supposed to do with the same version of Proteus ?
We get thousands of Proteus related posts here over the past years and NONE of them could actually function using real parts. While Proteus might 'work' for a simple program using basic I/O it is seriously flawed and unreliable.

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Fri Dec 07, 2018 12:00 pm     Reply with quote

You are still not showing a crystal in the circuit. Also the ENVREG pin needs
to be tied to Vdd.
Also look at Figure 2-1. This shows the minimum connections required for the PIC to work.
You need to check that the chips are both receiving the right supply. The way that Proteus hides the supply pins and can then connect the wrong supply (with no standard for voltage levels and names existing), can easily lead to a chip not being powered,
Big question is whether the Proteus model of the memory actually works....
Unfortunately they often don't. I'd say at least 10% of parts have significant
issues in Proteus.
It is a very poor simulator, and people relying on it are most of the time
wasting their time...
The SO pin will not be driven till CS is low.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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