//********************************************************************** /*   『EEPROMリーダ&ライタ(SDカード対応)』  ■概要   ・EEPROMのデータをSDカードにファイルとして保存する。     →"eep2sdc.txt"   ・SDカードにあるファイルをEEPROMへ書き込む。     →"sdc2eep.txt" */ //********************************************************************** #define LED PORTA.F6 #define SW_MODE PORTA.F3 //EEPROM←→SDC #define SW_START PORTA.F4 //開始 #define SW_STOP PORTA.F5 //停止 #define ON 1 #define OFF 0 #define CR 0x0d #define LF 0x0a #define ACK 1 #define NO_ACK 0 #define DATA_SIZE 16 //********************************************************************** static unsigned short dataBuf[DATA_SIZE]; //********************************************************************** void EEPROM_24LC1025_Init() { Soft_I2C_Config(&PORTB, 3, 2); } //********************************************************************** void EEPROM_24LC1025_Page_Read(unsigned long addr, unsigned short *buf, unsigned short len) { unsigned short cnt; // Soft_I2C_Start(); if ((addr & 0x10000) == 0) Soft_I2C_Write(0xA0); else Soft_I2C_Write(0xA8); Soft_I2C_Write((addr >> 8) & 0xFF); Soft_I2C_Write(addr & 0xFF); Soft_I2C_Start(); if ((addr & 0x10000) == 0) Soft_I2C_Write(0xA1); else Soft_I2C_Write(0xA9); for (cnt = 0; cnt < (len - 1); cnt++) { buf[cnt] = Soft_I2C_Read(ACK); //acknowledge } buf[cnt] = Soft_I2C_Read(NO_ACK); //not acknowledge Soft_I2C_Stop(); } //********************************************************************** void EEPROM_24LC1025_Page_Write(unsigned long addr, unsigned short *buf, unsigned short len) { unsigned short cnt; // Soft_I2C_Start(); if ((addr & 0x10000) == 0) Soft_I2C_Write(0xA0); else Soft_I2C_Write(0xA8); Soft_I2C_Write((addr >> 8) & 0xFF); Soft_I2C_Write(addr & 0xFF); for (cnt = 0; cnt < len; cnt++) { Soft_I2C_Write(buf[cnt]); } Soft_I2C_Stop(); // Delay_ms(100); } //********************************************************************** void eeprom2sdc() { unsigned long addr; unsigned short cnt; //MMCのファイルのオープン Mmc_Fat_Assign("eep2sdc.txt", 0xA0); Mmc_Fat_Rewrite(); // addr = 0; for (addr = 0; addr < 0x20000; addr += DATA_SIZE) { LED = ON; EEPROM_24LC1025_Page_Read(addr, dataBuf, DATA_SIZE); LED = OFF; for (cnt = 0; cnt < DATA_SIZE; cnt++) Lcd_Custom_Chr(2, cnt + 1, dataBuf[cnt]); Mmc_Fat_Write(dataBuf, DATA_SIZE); // LongToStr(addr + DATA_SIZE, dataBuf); Lcd_Custom_Out(1, 11, &dataBuf[5]); // if (SW_STOP == 0) return; } } //********************************************************************** void sdc2eeprom() { static unsigned long fsize, addr; static unsigned int cnt1, cnt2, round; static unsigned short character, fraction; //MMCのファイルのオープン Mmc_Fat_Assign("sdc2eep.txt", 0); Mmc_Fat_Reset(&fsize); // fsize = Mmc_Fat_Get_File_Size(); round = fsize / DATA_SIZE; fraction = fsize % DATA_SIZE; // addr = 0; for (cnt1 = 0; cnt1 < round; cnt1++) { LED = ON; for (cnt2 = 0; cnt2 < DATA_SIZE; cnt2++) { Mmc_Fat_Read(&character); dataBuf[cnt2] = character; Lcd_Custom_Chr(2, cnt2 + 1, character); } LED = OFF; EEPROM_24LC1025_Page_Write(addr, dataBuf, DATA_SIZE); addr += DATA_SIZE; // LongToStr(addr, dataBuf); Lcd_Custom_Out(1, 11, &dataBuf[5]); // if (SW_STOP == 0) return; } // if (fraction > 0) { LED = ON; for (cnt2 = 0; cnt2 < fraction; cnt2++) { Mmc_Fat_Read(&character); dataBuf[cnt2] = character; } LED = OFF; EEPROM_24LC1025_Page_Write(addr, dataBuf, fraction); // LongToStr(addr + fraction, dataBuf); Lcd_Custom_Out(2, 1, dataBuf); } } //********************************************************************** void main() { //変数の定義 unsigned short cnt; // OSCCON.IRCF2 = 1; OSCCON.IRCF1 = 1; OSCCON.IRCF0 = 1; OSCCON.SCS1 = 1; OSCCON.SCS0 = 1; //A/D変換の設定 ADCON1.PCFG3 = 1; ADCON1.PCFG2 = 1; ADCON1.PCFG1 = 1; ADCON1.PCFG0 = 1; //ポートの設定 TRISA = 0b10111111; TRISB = 0b00000000; TRISC = 0b00000001; //変数の初期化 //LCDの初期化 Lcd_Custom_Config(&PORTB,7,6,5,4,&PORTA,2,1,0); Lcd_Custom_Cmd(LCD_CURSOR_OFF); Lcd_Custom_Cmd(LCD_CLEAR); for (cnt = 1; cnt <= 16; cnt++) { Lcd_Custom_Chr(1, cnt, 0xFF); LED = ON; Delay_ms(50); LED = OFF; Delay_ms(50); } for (cnt = 1; cnt <= 16; cnt++) { Lcd_Custom_Chr(2, cnt, 0xFF); LED = ON; Delay_ms(50); LED = OFF; Delay_ms(50); } Lcd_Custom_Cmd(LCD_CLEAR); //MMCの初期化 LED = ON; Spi_Init_Advanced(MASTER_OSC_DIV64, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH); if (Mmc_Fat_Init(&PORTC, 2)) { Lcd_Custom_Out(1, 1, "MMC error!"); while (1) { LED = ON; Delay_ms(50); LED = OFF; Delay_ms(50); } } Spi_Init_Advanced(MASTER_OSC_DIV16, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH); LED = OFF; // EEPROM_24LC1025_Init(); // while (1) { Lcd_Custom_Out(1, 1, "EEPROM <-> SDC"); //開始スイッチが押されるのをチェックする。 while (SW_START == 1) { Delay_ms(10); } Lcd_Custom_Cmd(LCD_CLEAR); // if (SW_MODE == 1) { Lcd_Custom_Out(1, 1, "PROM->SDC"); eeprom2sdc(); Delay_ms(1000); Lcd_Custom_Cmd(LCD_CLEAR); } else { Lcd_Custom_Out(1, 1, "PROM<-SDC"); sdc2eeprom(); Delay_ms(1000); Lcd_Custom_Cmd(LCD_CLEAR); } } }//~! //**********************************************************************