| ??? 04/12/10 06:37 Read: times |
#174987 - Managed to fix lockups, but it doesnt read/write Responding to: ???'s previous message |
ok, I think I fixed the lockups I was having. I completely re-written the read and write subroutines.
void EEPROM_Write(unsigned int *address, unsigned char eeprom_data) // call function, with two input values, the address in which to write and the data to write to it
{
EECON=0x02; //Enable EEPROM
*address=eeprom_data; //Latch the data
EECON=0x52; //Programming -- I tried both
EECON=0xA2; //Sequence -- x4 and x2
Delay(5); //Delay to give the EEBUSY time to rise
while(((EECON&0x01)==0x01)); //Wait till EEBUSY is off
EECON=0x00; //Disable EEPROM
}
char EEPROM_Read(unsigned int *address) // //call the function and specify the needed address
{
unsigned char eeprom_data='5'; //I set the EEPROM_Data to the char 5 to test if it even gets changed
EECON=0x02; //Access EPROM
AUXR=0x2C; //Extend the Read cycles to accomidate for slow memory
eeprom_data=*address; //write to eeprom_data the value stored at the needed memory
EECON=0x00; //Allow usage of EPROM
AUXR=0x0C; //Restore normal R/W Cycles
return(eeprom_data);
}
however... those subroutines still dont write data into the EEPROM, and when reading it always returns a black block char on the LCD (I think its value is FF). heres how I call the two functions: b=EEPROM_Read(&user_name[a]); EEPROM_Write(&user_name[a],namestring[a]); and heres the full code for reference #include <at89c5131.h> //user the 895131 library
#define userlimit 45 //define userlimit - 45 MAX under onboard 1K EPROM
void new();
void lcd_clear();
void lcd_config();
void encrypt();
void Delay(int wait);
void lcd_display(char strng[],bit line,char slot);
void lcd_line(bit row , char slot);
char scan_keypad();
char EEPROM_Read(unsigned int *address);
void EEPROM_Write(unsigned int *address, unsigned char eeprom_data);
void lcd_blink(bit status);
unsigned char xdata lcd_command _at_ 0x4000; //define a data port at 4000 and call it lcd_command - for programming the LCD
unsigned char xdata lcd_data _at_ 0x4001; //define a data port at 4001 and call it lcd_data - for writing on the LCD
unsigned char xdata keypad _at_ 0x6000; //define a data port at 6000 and call it keypad - for reading the keypad
unsigned char xdata encrypted_password[5*userlimit] _at_ 0x0000; //encrypted password.5*userlimit.
unsigned char xdata user_name[16*userlimit] _at_ 0x00E1; //stores user name.16*userlimit.
unsigned char xdata user_level[userlimit] _at_ 0x03C0; //store user level.1*userlimit.
unsigned char a,b='5',encrypted[5],key[5],password[5],namestring[]={'Y','a','n','i','v',' ','K','f','i','r',' ',' ',' ',' ',' ',' '}; //global
void main() //main program
{
lcd_clear();
lcd_display("Starting",0,0);
for(a=0;a<16;a++)
EEPROM_Write(&user_name[a],namestring[a]);
EEPROM_Write(&user_level[0],1);
Delay(2000);
lcd_config(); //configure the LCD
lcd_clear();
while(1)
{
new();
}
}
void new()
{
for(a=0;a<5;a++)
key[a]=0;
lcd_clear();
lcd_display("Enter a password",0,0);
lcd_blink(1);
for(a=0;a<5;a++)
{
password[a]=scan_keypad();
lcd_line(1,a);
Delay(400);
lcd_data='*';
}
lcd_clear();
lcd_display("Swipe Your card ",0,0);
while(P1_3==1);
for(a=0;a<5;a++)
{
for(b=0;b<8;b++)
{
while(P3_1==1);
while(P3_1==0);
if(P3_0==0)
{
key[a]=key[a]|0x80;
key[a]=key[a]>>1;
}
}
}
while(P1_3==0);
encrypt();
lcd_clear();
lcd_display("Saving...",0,0);
for(a=0;a<5;a++)
EEPROM_Write(&encrypted_password[a],encrypted[a]);
lcd_display("User made:",0,0);
lcd_blink(0);
for(a=0;a<16;a++)
{
b=EEPROM_Read(&user_name[a]);
lcd_command=0xc0+a;
Delay(200);
lcd_data=b;
}
}
void encrypt()
{
char temp;
lcd_clear();
lcd_display("Encrypting",0,0);
for(a=0;a<5;a++)
password[a]=(password[a]*(a+3))+(90+a); // multiply by 3+slot and add 90+slot
for(a=0;a<4;a++)
{
if(a<2)
{
temp=password[4-a]; //Mirror
password[4-a]=password[0+a]; //Image
password[0+a]=temp; //Array
}
encrypted[a]=(password[a]^(!key[a])); // Xor operation between password and keycard data
password[a]=0; //purge former password from memory by nullfying it.
key[a]=0; //purge former keycard from memory by nullfying it.
}
}
void lcd_clear()
{
lcd_command=0x01; // clear screen
lcd_command=0x02; // reset position
Delay(10);
}
void lcd_config()
{
Delay(10);
lcd_command=0x38;// 8 Bit Dual line 5*7 dots shape.
Delay(10);
lcd_command=0x0e;//Display on, Cursor on without blinking
Delay(10);
lcd_command=0x01;//Clear screen
Delay(10);
lcd_command=0x02;//Cursor to the beginning of the line
Delay(10);
lcd_command=0x06;// Increment cursor, no display shift.
}
void Delay(int wait)
{
long i;
for(i=0;i<wait;i++)
{
TMOD=0x01;
TL0=0x5F;
TH0=0xF9;
TR0=1;
while(!TF0);
TF0=0;
}
}
void lcd_display(char strng[],bit line,char slot)
{
char i=0;// define i as a char for aing the array
Delay(1);
while(strng[i]) // while the string containts data, do the loop
{
if(line==1)
lcd_command=0xc0+slot+i;
else
lcd_command=0x80+slot+i;
Delay(1);
lcd_data=strng[i]; // display character from array on the lcd
i=i+1; // increment the i variable by one to a the next array slot
}
}
void lcd_line(bit row , char slot) //define default value of row to 1 and of slot to 0
{
if (row==0)// if the required line is 1
lcd_command=0x80+slot;// display on the first line
else// else, display on the
lcd_command=0xc0+slot;// second line
Delay(10);
}
char scan_keypad()
{
unsigned char ascii;
while(P1_0==0);
ascii=keypad;
if((ascii>=0x00)&(ascii<0x04))
ascii++;
if((ascii>0x06)&(ascii<0x0C))
ascii--;
if(ascii==0x0C)
ascii='*'-'0';
if(ascii==0x0D)
ascii=0;
if(ascii==0x0E)
ascii='#'-'0';
Delay(10);
return(ascii);
}
void EEPROM_Write(unsigned int *address, unsigned char eeprom_data)
{
EECON=0x02; //Enable EEPROM
*address=eeprom_data; //Latch the data
EECON=0x52; //Programming
EECON=0xA2; //Sequence
Delay(5); //Delay to give the EEBUSY time to rise
while(((EECON&0x01)==0x01)); //Wait till EEBUSY is off
EECON=0x00;
}
char EEPROM_Read(unsigned int *address)
{
unsigned char eeprom_data='5';
EECON=0x02; //Access EPROM
AUXR=0x2C; //Extend the Read cycles to accomidate for slow memory
eeprom_data=*address;
EECON=0x00; //Allow usage of EPROM
AUXR=0x0C; //Restore normal R/W Cycles
return(eeprom_data);
}
void lcd_blink(bit status)
{
if(status==0)
lcd_command=0x0c; // turn blinking off
else
lcd_command=0x0f; // turn blinking on
Delay(10);
}
|
| Topic | Author | Date |
| EEPROM Read/Write Issues | 01/01/70 00:00 | |
| if it was an external EEPROM | 01/01/70 00:00 | |
| if it was an external EEPROM | 01/01/70 00:00 | |
| Special sequence. | 01/01/70 00:00 | |
| Thanks, but can you please simplyfy it? | 01/01/70 00:00 | |
| Hope this explains | 01/01/70 00:00 | |
| Managed to fix lockups, but it doesnt read/write | 01/01/70 00:00 | |
| You have to choose your API | 01/01/70 00:00 | |
| Already did all that | 01/01/70 00:00 | |
| char vs int | 01/01/70 00:00 | |
| I gave you a function | 01/01/70 00:00 | |
| Still no luck | 01/01/70 00:00 | |
| unsigned int is not the size of the unsigned int* pointer | 01/01/70 00:00 | |
| Why not use the function I gave you. | 01/01/70 00:00 | |
| I did use you function | 01/01/70 00:00 | |
| My apologies. | 01/01/70 00:00 | |
| Sorry to keep dragging you back here... | 01/01/70 00:00 | |
| FLIP is a pain | 01/01/70 00:00 | |
| We have progress! | 01/01/70 00:00 | |
| Terribly sorry to bump but I really need help | 01/01/70 00:00 | |
| Found the solution! | 01/01/70 00:00 | |
| Study your C textbooks | 01/01/70 00:00 | |
it cant distinguise the sign bit | 01/01/70 00:00 |



