/* ** ################################################################### ** Filename : EPROM_test.c ** DataSheet: Microchip 24AA256/24LC256/24FC256 256K IIC Serial EEPROM ** REV: 2004 ** Compiler : CodeWarrior compiler ** Processor : MC68HC908JL16CSPE ** Date/Time : 3.11.2008 ** Abstract : ** This program tests the 24LC256 32Kx8 (256K) IIC EPROM ** with a HG25504 LCD ** ** ** Omair Khan ** University of Southern California ** www.omair-khan.com ** ** Referenced from Allan Weber ** http://ee.usc.edu/library/ee459/ ** ** Revisions: ** none ** ** ################################################################### */ #include /* for EnableInterrupts macro */ #include #include #include "derivative.h" /* include peripheral declarations */ #include "24LC256.h" /* The following puts the dummy interrupt service routine at location MY_ISR_ROM which is defined in the PRM file as the start of the FLASH ROM */ #pragma CODE_SEG MY_ISR_ROM #pragma TRAP_PROC void dummyISR(void) { } /* This pragma sets the code storage back to default area of ROM as defined in the PRM file. */ #pragma CODE_SEG DEFAULT void init_uP(void); void blink_LED(int x); void del1m(int); void init_LCD(void); void writeLCDcmd(unsigned char data,int a); void writeSTR(unsigned char a[]); void IIC_trans_start(unsigned char devAddress,unsigned char regAddrLSB); void IIC_trans_ack(unsigned char nextNextData); void IIC_rec_start(unsigned char devAddress); void IIC_rec_wait(void); void IIC_rec_disableAck(void); void IIC_stop(void); void rdprom(unsigned char *, int, word); void wrprom(unsigned char *, int, word); /* Addresses where we store test data in the EEPROM */ #define ADDR1 40 #define ADDR2 500 #pragma DATA_SEG __SHORT_SEG MY_ZEROPAGE unsigned char rc; unsigned char delay0; unsigned char delay1; const unsigned char str1[] = "EE459L!!"; const unsigned char str2[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; unsigned char rdata1[12]; unsigned char rdata2[30]; /* ********************************** * * * Main * * * ********************************** */ void main(void) { init_uP(); init_LCD(); writeLCDcmd(0x46,1); //set cursor address to top of text block (addr 0x0000) writeLCDcmd(0x00,0); writeLCDcmd(0x00,0); writeLCDcmd(0x42,1); //send mwrite cmd writeSTR("writing data "); writeSTR(str1); wrprom((unsigned char *) str1, 8, (word) ADDR1); writeSTR("writing data "); writeSTR(str2); wrprom((unsigned char *) str2, 26, (word) ADDR2); // Read the data at the first address memset(rdata1, 0, 8); rdprom(rdata1, 8, (word) ADDR1); rdata1[8] = 0; writeSTR("Read: "); writeSTR(rdata1); // Read the data at the second address memset(rdata2, 0, 26); rdprom(rdata2, 26, (word) ADDR2); rdata2[26] = 0; writeSTR("Read: "); writeSTR(rdata2); while (1) { } /* please make sure that you never leave this function */ } /* ********************************************************************************* * * * Functions * * * ********************************************************************************* */ void init_uP(){ CONFIG1_COPD = 1; // disable COP reset CONFIG2_IICSEL=1; //Set MMIIC SDA on PTA2 & SCL on PTA3 DDRA_DDRA5=1; PTA_PTA5=0; // reset lcd DDRB = 0xFF; //set port B for output (LCD DATABUS) DDRA_DDRA0=1; //LED (NIGHTLIGHT) DDRA_DDRA1=1; //LCD /WR PTA_PTA1=1; DDRA_DDRA4=1; //LCD A0 PTA_PTA4=1; DDRD_DDRD1=1; //LCD /CS PTD_PTD1=1; MIMCR_MMBR2=0; //Set Baudrate to (internal clk / 32) MIMCR_MMBR1=1; //10MHZ/4 = 2500000 hz MIMCR_MMBR0=0; //2500000/32 = 78125 hz MMCR_MMEN=1; //Enable MMIIC } /* ********************************** * * * LCD * * * ********************************** */ void init_LCD() { int ra=0; PTA_PTA5=1; // deactivate reset writeLCDcmd(0x58,1); //turn off display writeLCDcmd(0x40,1); //initialize LCD writeLCDcmd(0x30,0); //Int CG, 32chr CGRAM, 8lines/char, single pane, No invert, lcd, normal shift clock writeLCDcmd(0x87,0); //8 pixel wide characters, 2 frame AC drive writeLCDcmd(0x07,0); //vertical char size=8 pixels writeLCDcmd(0x1F,0); //32 display bytes per line writeLCDcmd(0x23,0); //tot. addr range per line (4 extra for horz blk) writeLCDcmd(0x7F,0); //128 display lines writeLCDcmd(0x20,0); //low byte of virtual screen width writeLCDcmd(0x00,0); //high byte of virtual screen width writeLCDcmd(0x44,1); //send scroll cmd writeLCDcmd(0x00,0); //set layer 1 home addr to 0x0000 writeLCDcmd(0x00,0); writeLCDcmd(0x7F,0); //128 lines for layer 1 writeLCDcmd(0x00,0); //set layer 2 (graphic) home address to 0x1000 writeLCDcmd(0x10,0); writeLCDcmd(0x7F,0); //128 lines for layer 2 //layers 3 and 4 not used writeLCDcmd(0x5A,1); //set hor scroll cmd writeLCDcmd(0x00,0); //no hor scroll adjustment writeLCDcmd(0x5B,1); //set overlay selections cmd writeLCDcmd(0x00,0); //OR layers, text block 1, 2 layer (layer 2 can only be graphic) writeLCDcmd(0x4C,1); //auto cursor increment +1 cmd writeLCDcmd(0x46,1); //cursor write cmd writeLCDcmd(0x00,0); //0x00 addr writeLCDcmd(0x00,0); writeLCDcmd(0x42,1); //send mwrite cmd //for a total of 512 ascii spaces (0x20) for(ra=1;ra<512;++ra) writeLCDcmd(0x20,0); //send 7168 zeros of data to clr out CGRAM area //(28 x 256 = 7168) //from 0400h to 0FFFh and the graphics area from //1000h to 1FFFh for(ra=0; ra<3584; ++ra) writeLCDcmd(0x00,0); for(ra=0; ra<4096; ++ra) writeLCDcmd(0x00,0); writeLCDcmd(0x5D,1); //set cursor format writeLCDcmd(0x04,0); //five pixels wide writeLCDcmd(0x86,0); //vertical cursor size=7 pixels block cursor writeLCDcmd(0x59,1); //display ON writeLCDcmd(0b00010100,0); //(0b00010100) no layer 3, show layer 2, show layer 1, no cursor writeLCDcmd(0x5C,1); //set CGRAM addr cmd writeLCDcmd(0x00,0); //CGRAM addr = 0x0400 writeLCDcmd(0x04,0); //----- DISPLAY IS CLEARED AND ON ------ } void writeLCDcmd(unsigned char data,int a) { PTB = data; //put data on databus if(a==1) PTA_PTA4 = 1; //write to (0 = data, 1 = command) register if(a==0) PTA_PTA4 = 0; PTD_PTD1 = 0; //enable /CS PTA_PTA1 = 0; //enable /WR PTA_PTA1 = 1; //disable /WR PTD_PTD1 = 1; //disable /CS } void writeSTR(unsigned char a[]){ for(rc=0; a[rc]!='\0'; rc++) writeLCDcmd(a[rc],0); } void blink_LED(int x){ int ra=0,rc=0; //BLINK LED ON AND OFF x TIMES for(ra=0; ra 0) { // We can write up to the next 64 byte boundary, // but no more than is left to write maxw = 64 - (a % 64); // Max for this page if (n < maxw) maxw = n; // Number left to write in page IIC_trans_start(EPROM_IIC_ADDR,EPROM_ADDR_ADDRL); IIC_trans_ack(EPROM_ADDR_ADDRH); IIC_trans_ack(*p++); while (n > 0) { n--; // Decrement count if (n == 0) // Is this the last byte? IIC_trans_ack(0xFF); //dummy data else IIC_trans_ack(*p++); } IIC_stop(); del1m(5); // Wait 5ms for EEPROM to write p += maxw; // Increment array address a += maxw; // Increment address n -= maxw; // Decrement byte count } } /* rdprom - read bytes from the EEPROM over the IIC bus */ void rdprom(unsigned char *p, int n, word a) { EPROM_ADDR=a; IIC_trans_start(EPROM_IIC_ADDR,EPROM_ADDR_ADDRL); IIC_trans_ack(EPROM_ADDR_ADDRH); IIC_trans_ack(0xFF); IIC_stop(); IIC_rec_start(EPROM_IIC_ADDR); n--; // Loop n-1 times before last read while (n > 0) { // Up to last byte IIC_rec_wait(); *p++ = MMDRR; // Get data n--; // Decrement total count } IIC_rec_disableAck(); IIC_rec_wait(); *p++ = MMDRR; // Get data IIC_stop(); }