[英]Saving data to External EEPROM with PIC18
我有带有25LC1024外部EEPROM的PIC18F87J11,我想在上面存储一些数据,以后可以读取。 我已经进行了一些研究,但不幸的是,我找不到使用与我的主板类似的教程。 我正在将MPLAB IDE与C18编译器一起使用。
注意:下面另写了两个链接作为注释。
这是我的问题所在...
为了写入25LC1024外部EEPROM,我遵循了微芯片上的教程。 第一个问题是该Tut是为PIC18F1220编写的,而我正在使用PIC18F87J11。 因此,在打开项目时,我得到两个文件未找到错误,但我只是忽略了它们。
我将文件AN1018.h和AN1018_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中,并在需要时保留它们。 请知道我是微控制器编程的初学者。
第一步(读取和写入之前),必须确保正确配置了SPI接口(硬件和软件)。 要检查此步骤,您可以从25LC1024中读取“状态寄存器”。 在数据表中查找“ RDSR”,发送给eeprom的指令应为0b00000101 so(int)5。
这是一个非常老的项目的sdcc中18F * + 25LC *的代码。 该代码非常基础,无需使用任何外部库,您只需要为图片替换寄存器变量名称和init config。
感谢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
main.c中
根据数据表设置您的初始化
#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
我认为在直接使用AN1018.h / AN1018_spi.c之前,您需要验证它是否与您的微控制器兼容。 我建议检查两个微控制器的数据表,并查看专用于SPI模块的差异,因为您使用的外部EEPROM将连接到SPI总线。 如果这两个微控制器具有相同的SPI寄存器配置/模块,则可以使用它,否则您将必须自己编写驱动程序。 您可以使用AN1018_spi.c作为参考,我想您只需要更改一些寄存器即可。 然后,在您的init函数中,您没有初始化SPI模块,您将需要根据外部设备指定正确的SPI时钟,SPI模式。 正确初始化SPI模块后。 您将需要编写EEPROM_Read / EEPROM_Write函数。 在其中,您将必须遵循外部设备的数据表中给出的协议,才能从使用设备发送/接收数据。
您好,我用Google搜索并获得了一个很好的网站,在那里我找到了通过FM24C64通过i2c协议将外部EEPROM与PIC微控制器接口的文章,以及他们在文章中给出的经过我测试和正常工作的代码。 我给那个链接可能对您有帮助。 http://www.nbcafe.in/interfacing-external-eeprom-with-pic-microcontroller/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.