📄 eetest.c
字号:
#include <pic18.h>
#include "eedemo.h"
#include <stdio.h>
/* NOTE: This project shall not function for PIC18Cxxx devices. */
/* fill the EEPROM with initial values */
__EEPROM_DATA(0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07);
__EEPROM_DATA(0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F);
__EEPROM_DATA(0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17);
__EEPROM_DATA(0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F);
__EEPROM_DATA(0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27);
__EEPROM_DATA(0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F);
__EEPROM_DATA(0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37);
__EEPROM_DATA(0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F);
__EEPROM_DATA(0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47);
__EEPROM_DATA(0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F);
__EEPROM_DATA(0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57);
__EEPROM_DATA(0x58,0x59,0x5A,0x5B,0x5C,0x5D,0x5E,0x5F);
__EEPROM_DATA(0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67);
__EEPROM_DATA(0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F);
__EEPROM_DATA(0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77);
__EEPROM_DATA(0x78,0x79,0x7A,0x7B,0x7C,0x7D,0x7E,0x7F);
void interrupt ISR(void);
void init(void);
void write(void);
unsigned char read(void);
void putch(unsigned char);
volatile unsigned char data=0;
unsigned char eeprom_size;
void main(void)
{
eeprom_size=0xFF;
init();
while(1)
{
if(TMR1IF) /* a new action begins each time timer 1 overflows */
{
TMR1IF=0;
LED=(LED^1); /* toggle LED to indicate a new action has begun */
printf("\rAddress %x was ",EEADR); /* output the current address */
if(READ)
{
data=read();
printf("read, value = %x. ",data); /* output the action */
}
else
{
EEDATA=BLUE_DIP;
write();
printf("written with %x. ",EEDATA); /* output the action */
}
if(EEADR == eeprom_size) /* after the last EEPROM address */
EEADR=0; /* return to the first */
else
EEADR++; /* increment EEPROM address pointer */
}
}
}
void init (void)
{
T1CON=0x91; /* Timer 1 is enabled, 1:2 prescale timer */
TMR1IE=0;
TMR1IF=0;
TRISA=0x10; /* read/write select switch connect to PORTA4 */
TRISB=0xFF; /* PORTB0:3 = RED_DIP1, PORTB5 = pushbutton in */
TRISC=0x0F; /*RED_DIP2 switches connect to PORTC */
TRISD=0xFF; /* Data (blue) DIP switches connect to PORTD */
RBIE=1; /* PORTB interrupt on change is enabled */
/* The EEPROM address can be reloaded by */
RBIF=0; /* the pushbutton on PORTB5 */
GIE=1;
IPEN=0; /* interrupt priorities are not enabled */
SPEN=1; /* enable serial port */
TXEN=1; /* serial transmit enabled */
TXIE=0; /* not interrupt driven */
EEADR=0;
}
void write(void) /* write a byte of data to EEPROM */
{
GIE=0; // disable interrupts
WREN=1; // enable writes
EECON2=0x55; // required sequence for EEPROM update
EECON2=0xAA;
WR=1;
while(WR)continue;
EEIF=0;
WREN=0;
GIE=1; // re-enable interrupts
}
unsigned char read(void) /* read a byte of data from EEPROM */
{
unsigned char eeprom_data; // in PIC18Fxx2 devices, the data needs
// to be collected in the next instruction
GIE=0; // following RD=1. A temporary variable is
RD=1; // created to do this.
eeprom_data=EEDATA;
GIE=1;
return eeprom_data;
}
void putch(unsigned char c)
{
TXREG=c;
while(!TXIF)continue;
TXIF=0;
}
void interrupt ISR(void) /* interrupt triggered by the pushbuttons */
{
if((RBIE)&&(RBIF))
{
RBIF=0;
if(ADDRESS_RESET) /* if the pushbutton is depressed */
EEADR=RED_DIP; /* load the red DIP switch to the EEPROM address */
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -