View previous topic :: View next topic |
Author |
Message |
bal_square
Joined: 05 Dec 2018 Posts: 11
|
Interface AT25F4096 with PIC16F877 |
Posted: Wed Dec 05, 2018 11:58 am |
|
|
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
|
|
Posted: Wed Dec 05, 2018 12:24 pm |
|
|
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: 19549
|
|
Posted: Wed Dec 05, 2018 3:07 pm |
|
|
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: 9245 Location: Greensville,Ontario
|
|
Posted: Wed Dec 05, 2018 6:18 pm |
|
|
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
|
|
Posted: Thu Dec 06, 2018 2:00 pm |
|
|
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: 9245 Location: Greensville,Ontario
|
|
Posted: Thu Dec 06, 2018 2:22 pm |
|
|
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
|
|
Posted: Thu Dec 06, 2018 2:25 pm |
|
|
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
|
|
Posted: Thu Dec 06, 2018 6:57 pm |
|
|
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
|
|
Posted: Thu Dec 06, 2018 8:16 pm |
|
|
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: 19549
|
|
Posted: Fri Dec 07, 2018 1:48 am |
|
|
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....
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
|
|
Posted: Fri Dec 07, 2018 9:34 am |
|
|
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....
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: 9245 Location: Greensville,Ontario
|
|
Posted: Fri Dec 07, 2018 10:43 am |
|
|
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
|
|
Posted: Fri Dec 07, 2018 10:58 am |
|
|
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: 9245 Location: Greensville,Ontario
|
|
Posted: Fri Dec 07, 2018 11:45 am |
|
|
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: 19549
|
|
Posted: Fri Dec 07, 2018 12:00 pm |
|
|
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. |
|
|
|