Hello hi everyone... i got a problem to read and write from PIC16877 EEPROM. Here i attached together my code using PIC C LITE compiler...I hope all of u can help me. Thank You very much.
// Project 8.1 We are Online
#include <pic.h> // standard pic header file
/* Xtal frequency */
#define XTAL 4000000
// Hardware Notes:
// PIC16F627 Running at 4MHz
// Configuration Fuses
#if defined(_16F877A)
#warning PIC16F877A Selected
__CONFIG(0x3F61);
#error Unsupported PICmicro MCU Selected
#endif
// Global Variables
#define TRISLED1 TRISB0
#define TRISLED2 TRISB1
#define TRISLED3 TRISB2
#define TRISLED4 TRISB3
#define TRISLED5 TRISB4
#define TRISLED6 TRISB5
#define TRISLED7 TRISB6
#define TRISLED8 TRISB7
#define LED1 RB0
#define LED2 RB1
#define LED3 RB2
#define LED4 RB3
#define LED5 RB4
#define LED6 RB5
#define LED7 RB6
#define LED8 RB7
volatile unsigned CMCONNEW @ (unsigned) &CMCON; // LED Physical Bits
ANSEL = 0x00; // Digital I/O
// ---------------------------- Delay Function -------------------------------------------------
void Delay500Us() //500 Usec Delay Routine
{
unsigned char cnt500Us = 165; // Delay Cycle to achieve 500Us delay
while(--cnt500Us != 0) // Delay Timing is approximately 3 usec per loop
continue; // Note this routine is for 4MHz crystal frequency
}
void Delay100Ms() //100 msec Delay Routine
{
unsigned char cnt100Ms = 200; // 200 * 500 Usec = 100 msec
do
{
Delay500Us();
}while(--cnt100Ms != 0);
}
void Delay1s() // 1 sec Delay Routine
{
unsigned char cnt1Ms = 1; //10 * 100 msec = 1 sec
do
{
Delay100Ms();
}while(--cnt1Ms != 0);
}
// ---------------------------- Delay Function -------------------------------------------------
unsigned char ReadByteFromEE(unsigned char address)
{
unsigned char byte; // Variable hold the data that is read
EEADR = address; // Read from this address
RD = 1; // Initiate a read cycle
byte = EEDATA; // Fetch byte from dataregister
return byte; // Return the read byte
}
void WriteByteToEE(unsigned char data, unsigned char address)
{
EEADR = address; // Address to write to 0x00 to 0xFF
EEDATA = data; // Data to write
WREN = 1; // Enable writes to the EEProm
while(GIE)
GIE = 0; // Disable interrupts during write
EECON2 = 0x55; // Register not implemented on 16F684
EECON2 = 0xAA; // strange that this is required
WR = 1; // Initiate a write cycle
while(!EEIF); // Wait for write to complete
WREN = 0; // Disable writes to EEProm
EEIF = 0; // Clear "write complete" flag
GIE = 1; // Reenable interrupts
}
// ---------------------------- main program ---------------------------------------------------
void main()
{
TRISLED1 = 0;
TRISLED2 = 0;
TRISLED3 = 0;
TRISLED4 = 0;
TRISLED5 = 0;
TRISLED6 = 0;
TRISLED7 = 0;
TRISLED8 = 0;
LED4 = 0;
LED3 = 0;
LED2 = 0;
LED1 = 0;
LED5 = 0;
LED6 = 0;
LED7 = 0;
LED8 = 0;
CMCONNEW = 0b00000111; // Disabled analog function at port A
while (1 == 1) // Loop forever
{
WriteByteToEE(0x0A, 0x00);
PORTB = ReadByteFromEE(0x00);
Delay1s();
}
}
// Project 8.1 We are Online
#include <pic.h> // standard pic header file
/* Xtal frequency */
#define XTAL 4000000
// Hardware Notes:
// PIC16F627 Running at 4MHz
// Configuration Fuses
#if defined(_16F877A)
#warning PIC16F877A Selected
__CONFIG(0x3F61);
#error Unsupported PICmicro MCU Selected
#endif
// Global Variables
#define TRISLED1 TRISB0
#define TRISLED2 TRISB1
#define TRISLED3 TRISB2
#define TRISLED4 TRISB3
#define TRISLED5 TRISB4
#define TRISLED6 TRISB5
#define TRISLED7 TRISB6
#define TRISLED8 TRISB7
#define LED1 RB0
#define LED2 RB1
#define LED3 RB2
#define LED4 RB3
#define LED5 RB4
#define LED6 RB5
#define LED7 RB6
#define LED8 RB7
volatile unsigned CMCONNEW @ (unsigned) &CMCON; // LED Physical Bits
ANSEL = 0x00; // Digital I/O
// ---------------------------- Delay Function -------------------------------------------------
void Delay500Us() //500 Usec Delay Routine
{
unsigned char cnt500Us = 165; // Delay Cycle to achieve 500Us delay
while(--cnt500Us != 0) // Delay Timing is approximately 3 usec per loop
continue; // Note this routine is for 4MHz crystal frequency
}
void Delay100Ms() //100 msec Delay Routine
{
unsigned char cnt100Ms = 200; // 200 * 500 Usec = 100 msec
do
{
Delay500Us();
}while(--cnt100Ms != 0);
}
void Delay1s() // 1 sec Delay Routine
{
unsigned char cnt1Ms = 1; //10 * 100 msec = 1 sec
do
{
Delay100Ms();
}while(--cnt1Ms != 0);
}
// ---------------------------- Delay Function -------------------------------------------------
unsigned char ReadByteFromEE(unsigned char address)
{
unsigned char byte; // Variable hold the data that is read
EEADR = address; // Read from this address
RD = 1; // Initiate a read cycle
byte = EEDATA; // Fetch byte from dataregister
return byte; // Return the read byte
}
void WriteByteToEE(unsigned char data, unsigned char address)
{
EEADR = address; // Address to write to 0x00 to 0xFF
EEDATA = data; // Data to write
WREN = 1; // Enable writes to the EEProm
while(GIE)
GIE = 0; // Disable interrupts during write
EECON2 = 0x55; // Register not implemented on 16F684
EECON2 = 0xAA; // strange that this is required
WR = 1; // Initiate a write cycle
while(!EEIF); // Wait for write to complete
WREN = 0; // Disable writes to EEProm
EEIF = 0; // Clear "write complete" flag
GIE = 1; // Reenable interrupts
}
// ---------------------------- main program ---------------------------------------------------
void main()
{
TRISLED1 = 0;
TRISLED2 = 0;
TRISLED3 = 0;
TRISLED4 = 0;
TRISLED5 = 0;
TRISLED6 = 0;
TRISLED7 = 0;
TRISLED8 = 0;
LED4 = 0;
LED3 = 0;
LED2 = 0;
LED1 = 0;
LED5 = 0;
LED6 = 0;
LED7 = 0;
LED8 = 0;
CMCONNEW = 0b00000111; // Disabled analog function at port A
while (1 == 1) // Loop forever
{
WriteByteToEE(0x0A, 0x00);
PORTB = ReadByteFromEE(0x00);
Delay1s();
}
}