1

我有 PIC18F87J11 和 25LC1024 外部 EEPROM,我想在上面存储一些数据,以便以后读取。我做了一些研究,但不幸的是,我找不到使用与我类似的板的教程。我正在使用带有 C18 编译器的 MPLAB IDE。

PIC18F87J11

注意:另外两个链接写为下面的评论。

这就是我的问题所在...

为了写入 25LC1024 外部 EEPROM,我按照微芯片的教程进行操作。第一个问题是这个 tut 是为 PIC18F1220 编写的,而我使用的是 PIC18F87J11。因此,在打开项目时,我得到两个文件未找到错误,但我只是忽略了它们。

图片

我将文件AN1018.hAN1018_SPI.c复制到我正在处理的项目中,并从AN1018.c文件中复制了一些代码。

AN1018.c 文件中的代码

void main(void)
{
#define PAGESIZE    16  
static unsigned char data[PAGESIZE];        // One-page data array
static unsigned char i;

    init();                                     // Initialize PIC

    data[0] = 0xCC;                             // Initialize first data byte

    /* Low-density byte function calls */
    LowDensByteWrite(data[0], 0x133);           // Write 1 byte of data at 0x133
    data[0] = 0xFF;
    LowDensByteRead(data, 0x133);      
    printf("%x",data);
    while(1){};  
}
void init(void)
{
    ADCON1 = 0x7F;                      // Configure digital I/O
    PORTA = 0x08;                       // Set CS high (inactive)
    TRISA = 0b11110111;                 // Configure PORTA I/O
    PORTB = 0;                          // Clear all PORTB pins
    TRISB = 0b11111100;                 // Configure PORTB I/O
}

我的第二个问题是输出消息始终是1e0。换句话说,我不知道写入是否成功。我也不确定我可能会错过什么。

如果我能得到某种帮助,我将不胜感激。总而言之,我想将数据存储到我的外部 EEPROM 并在需要时保留它。请知道我是微控制器编程的初学者。

4

3 回答 3

1

作为第一步(在读取和写入之前),您必须确保您的 SPI 接口(硬件和软件)配置正确。要检查此步骤,您可以从 25LC1024 中读取“状态寄存器”。查看“RDSR”的数据表,发送到 eeprom 的指令应该是 0b00000101 所以(int)5。

这里有一些 18F* + 25LC* 的代码,写在一个非常古老的项目的 sdcc 中。该代码非常基本,没有使用外部库,您只需为您的图片替换寄存器变量名和初始化配置。

一些代码来自这里,感谢bitberzerkir

spi.c

#ifndef SPI_HH
#define SPI_HH

#define SpiWrite(x) spiRW(x)
#define SpiRead()   spiRW(0)

unsigned char spiRW(unsigned char data_){
    SSPBUF = data_;
    while(!PIR1bits.SSPIF);
    PIR1bits.SSPIF = 0;
    return SSPBUF;
}

void SpiInit() {
    SSPSTAT = 0x40; // 01000000
    SSPCON1 = 0x20; // 00100000
    PIR1bits.SSPIF = 0;
}

#endif

eeprom.c

注意:由于 25LC1024 的地址是 3x8 位,请确保您的编译器“长”类型至少有 24 位

#ifndef EEPROM_HH
#define EEPROM_HH

#include "spi.c"

#define CS PORTCbits.RC2

void EepromInit() {
    SpiInit();
    CS = 1;
}

unsigned char EReadStatus () {
    unsigned char c;
    CS = 0;
    SpiWrite(0x05);
    c = SpiRead();
    CS = 1;
    return c;
}

unsigned char EWriting() {
    unsigned char c;
    CS = 0;
    SpiWrite(0x05);
    c = SpiRead();
    CS = 1;
    return c & 1;
}

unsigned char EReadCh (unsigned long addr) {
    unsigned char c;
    // Send READ command and addr, then read data
    CS = 0;
    SpiWrite(0x03);
    // Address in 3x8 bit mode for 25lc1024
    SpiWrite(addr>>16);
    SpiWrite(addr>>8);
    SpiWrite((unsigned char) addr);
    c = SpiRead();
    CS = 1;
    return c;
}

void EWriteCh (unsigned char c, unsigned long addr) {
    // Enable Write Latch
    CS = 0;
    SpiWrite(0x06);
    CS = 1;

    // Send WRITE command, addr and data
    CS = 0;
    SpiWrite(0x02);
    SpiWrite(addr>>16);
    SpiWrite(addr>>8);
    SpiWrite((unsigned char) addr);
    SpiWrite(c);
    CS = 1;
}

#endif

主程序

根据数据表设置您的初始化

#include <pic18fregs.h>
#include "eeprom.c"

void main(void) {
    char out;
    TRISB = 0x01;
    TRISC = 0x00;
    PORTB = 0x00;
    PORTC = 0x00;
    EepromInit();

    EWriteCh('a', 0x00);    

    out = EReadCh(0x00);

    while(1);
}

如果您想读/写缓冲区,请注意分页。例如这里:

// Page byte size, 64 for 25lc256 and 256 for 25lc1024
#define PSIZE 256
// Addr mem limit 7FFF for 25lc256, 1FFFF for 25lc1024
#define MLIMIT 0x1FFFF


void EReadBuff (unsigned char c[], unsigned long dim, unsigned long addr) {
    unsigned int i;
    // Send READ command and addr, then read data
    CS = 0;
    SpiWrite(0x03);
    SpiWrite(addr>>16);
    SpiWrite(addr>>8);
    SpiWrite((unsigned char) addr);
    for(i = 0; i < dim; ++i)
        c[i] = SpiRead();
    CS = 1;
}

void EWriteBuff (unsigned char c[], unsigned long dim, unsigned long addr) {
    unsigned char i;
    unsigned int begin = 0;
    unsigned int end = dim > PSIZE ? PSIZE : dim;
    while (end > begin && addr + end <= MLIMIT) {  // check if addr is a siutable address [0, MLIMIT]
        // Enable Write Latch
        CS = 0;
        SpiWrite(0x06);
        CS = 1;

        // Send WRITE command, addr and data
        CS = 0;
        SpiWrite(0x02);
        SpiWrite(addr>>8);
        SpiWrite((unsigned char) addr);
        for(i = begin; i < end; ++i)
            SpiWrite(c[i]);
        CS = 1;
        while(EWriting());
        dim -= PSIZE;
        begin += PSIZE;
        addr += PSIZE;
        end = begin + (dim > PSIZE ? PSIZE : dim);
    }
}

#endif
于 2013-05-28T18:23:39.863 回答
0

我认为在直接使用 AN1018.h/AN1018_spi.c 之前,您需要验证它是否与您的微控制器兼容。我建议检查两个微控制器的数据表,并查看专门针对 SPI 模块的差异,因为您使用的外部 EEPROM 将连接到 SPI 总线。如果这两个微控制器具有相同的 SPI 寄存器配置/模块,那么您可以使用它,否则您必须自己编写驱动程序。您可以使用 AN1018_spi.c 作为参考,我想您只需要根据需要更改一些寄存器。然后在你的初始化函数中,你没有初始化 SPI 模块,你需要根据你的外部设备指定正确的 SPI 时钟,SPI 模式。正确初始化 SPI 模块后。您将需要编写 EEPROM_Read/EEPROM_Write 函数。

于 2013-05-28T12:00:21.537 回答
0

嗨,我用谷歌搜索并获得了一个非常好的网站,在那里我找到了通过 i2c 协议与 PIC 微控制器连接外部 EEPROM 与 FM24C64 的帖子以及他们在帖子中给出的代码,我测试并运行良好。我给那个链接可能对你有帮助。http://www.nbcafe.in/interfacing-external-eeprom-with-pic-microcontroller/

于 2014-02-21T15:28:04.077 回答