void read_command(unsigned char ADDRESS)
{
unsigned char n,i,k,nbit,m,d=0;
CS=1;
//Send 1 twice
for(i=1; i<=2; i++)
{
DIN=1;
send_clock();
}
DIN=0;
send_clock();
n=ADDRESS;
for(k=1;k<=8;k++)
{
nbit= n & 0x80;
if (nbit != 0)
DIN=1;
else
DIN=0;
send_clock();
nbit=nbit <<1;
}
for(m=0;m<=8;m++)
{
d=d | DOUT;
d<<1;
send_clock();
}
CS=0;
}
for(k=1;k<=8;k++)
{
nbit= n & 0x80;
if (nbit != 0)
DIN=1;
else
DIN=0;
send_clock();
nbit=nbit <<1;
}
silvio said:Hi Maverick Max
you wrote
Code:for(k=1;k<=8;k++) { nbit= n & 0x80; if (nbit != 0) DIN=1; else DIN=0; send_clock(); nbit=nbit <<1; }
Instead nbit=nbit <<1; don't you think it must be n=n <<1;
Which port of AT89C51 is tied to DOUT of AT93C46 ?
Just because you performed a bitwise operator d=d | DOUT; between an unsigned char and a bit DOUT
Bear in mind that Output data changes are synchronized with the rising edges of serial clock SK. It should be noted that a dummy bit (logic “0”) precedes the 8- or 16-bit data output string (I know you did that because the "m" starts from 0 instead of 1 in the second for loop).
Maverickmax said:In regards to your last question, are you saying that the DOUT generate logic 0 or 1 when Serial Clock goes high at the same time? Are you insinuateing that the last part of my codes may be wrong?
void send_clock()
{
PULSE_CLOCK=1;
PULSE_CLOCK=0;
}
// header files
#include "main.h"
#include "port.h"
#include "delay_loop.h"
//global variables
//Function Prototypes
void write_command(unsigned char DATA);
void read_command(unsigned char ADDRESS);
void send_clock();
void process(unsigned char byte);
void EW_enable();
void EW_disable();
//void Erase_command();
void send_clock()
{
PULSE_CLOCK=1;
PULSE_CLOCK=0;
}
void process(unsigned char byte)
{
unsigned char k,nbit;
for(k=1;k<=8;k++)
{
nbit= byte & 0x80;
if (nbit != 0)
DIN=1;
else
DIN=0;
send_clock();
byte=byte <<1;
}
}
void read_command(unsigned char ADDRESS)
{
unsigned char i,m,d=0,loop=8;
CS=1;
//Send two 1s (Dummies data)
for(i=1; i<=2; i++)
{
DIN=1;
send_clock();
}
process(ADDRESS);
for(m=0;m<=8;m++)
{
d=d | DOUT;
send_clock();
if(loop !=0)
{
d=d<<1;
}
loop--;
}
CS=0;
CS=1;
CS=0;
P1=d; //Send result to P1 (leds)
}
void write_command(unsigned char DATA)
{
unsigned char const location=0x00;
CS=1;
// Transmit 1,1,0
DIN=1;
send_clock();
DIN=0;
send_clock();
DIN=1;
send_clock();
process(location); //Select address
process(DATA); //Write data
CS=0;
CS=1;
CS=0;
}
void EW_enable()
{
CS=1;
DIN=1;
send_clock();
DIN=0;
send_clock();
process(0x7F);
CS=0;
CS=1;
}
void EW_disable()
{
CS=1;
DIN=1;
send_clock();
DIN=0;
send_clock();
process(0x30);
CS=0;
CS=1;
}
/*
void Erase_command()
{
CS=1;
DIN=1;
send_clock();
DIN=1;
send_clock();
DIN=1;
send_clock();
process(0x00); //Erase Data at address 0x00
CS=0;
CS=1;
}
*/
void main(void)
{
int s=1;
while(1)
{
if(s==1)
{
P1=0;
CS=0;
DIN=0;
DOUT=0;
PULSE_CLOCK=0;
EW_enable();
write_command(0xFF);
Delay_Loop(200);
read_command(0x00); //Read data at address 0x00
EW_disable();
}
s--;
}
}
void Erase_command()
{
CS=1;
DIN=1;
send_clock();
DIN=1;
send_clock();
process(0x80); //Erase Data at address 0x00
CS=0;
CS=1;
}
*/
void write_command(unsigned char DATA)
{
unsigned char const location=0x80; // the 2nd OP CODE bit is now the MSBit of address 0000000 (7 bits of "0")
CS=1;
// Transmit 1,0,1
DIN=1;
send_clock();
DIN=0;
send_clock();
// DIN=1;
// send_clock();
process(location); //Select address
process(DATA); //Write data
CS=0; //let's say that minimum tCS is attempt (1 us AT89C51 machine cycle)
CS=1; //you can wait now until DOUT goes high as a result of succesful writing
CS=0;
}
// header files
#include "main.h"
#include "port.h"
#include "delay_loop.h"
//global variables
unsigned char const ADDRESS=0xF0; // 1111000X
unsigned char const DATA=0x55; // 10101010
//Port configurations
sbit CS = P3^0;
sbit SK = P3^1;
sbit DIN = P3^2;
sbit DOUT = P3^3;
sbit ERROR = P3^4;
//Function Prototypes
void outdata(unsigned char byte, int const loop);
void indata();
void status();
void EW_enable();
void write_command();
void read_command();
void Erase_command();
void EW_disable();
// Transmit data to AT93C46
void outdata(unsigned char byte, int const loop)
{
unsigned char l,nbit;
for(l=1;l<=loop;l++)
{
SK=0; // Drop SK
nbit= byte & 0x80;
if (nbit != 0)
DIN=1;
else
DIN=0;
byte=byte <<1;
SK=1; // Raise SK
}
SK=0; // Drop SK
}
void indata()
{
unsigned char m,d=0,cycle=8;
// Dummy bit (0)
SK=0;
SK=1;
if(DOUT == 0)
{
for(m=1;m<=9;m++)
{
SK=0; // Drop SK
d=d | DOUT;
if(cycle !=0)
{
d=d<<1;
}
cycle--;
SK=1; // Raise SK
}
SK=0; // Drop SK
P1=d; // Display 8-bits of DATA
}
else
{
ERROR=1;
}
}
// read status after either writing or erasing process
void status()
{
DOUT=1; // Float pin
CS=1; // Raise CS
Delay_Loop(100); // 50uS delay
if(DIN !=1)
{
ERROR=1; // Display Error
}
CS=0;
}
// Enable Write/Erase Programming mode
void EW_enable()
{
CS=1; // Raise CS
outdata(0x80,3); // Transmit start bit (1) and op-code (00)
outdata(0xC0,7); // Transmit 7 bits of address - 11XXXXX (X - don't care)
CS=0; // Drop CS
}
void write_command()
{
CS=1; // Raise CS
outdata(0xA0,3); // Transmit start bit (1) and op-code (01)
outdata(ADDRESS,7); // Write 7-bits of address (0xF0- 1111000X)
outdata(DATA,8); // Write DATA - (0x55 - 10101010)
CS=0;
status();
}
void read_command()
{
CS=1;
outdata(0xC0,3); // Transmit dummy bit (1) and op-code (10)
outdata(ADDRESS,7); // Transmit 7-bits of address (0xF0 - 1111000X)
indata(); // Read dummy bit (0) and 8 bit of DATA
CS=0; // Drop CS
}
void Erase_command()
{
outdata(0xE0,3); // Transmit start bit (1) and dummy bit (11)
outdata(ADDRESS,7); // Send 7 bits of ADDRESS (0xF0 - 1111000X)
CS=0;
status();
}
void EW_disable()
{
CS=1; // Raise CS
outdata(0x80,3); // Transmit start bit (1) and op-code (00)
outdata(0x00,7); // Transmit 7 bits of address - 00XXXXX (X - don't care)
CS=0; // Drop CS
}
void main(void)
{
int s=1;
//Output
P1=0;
CS=0;
DIN=0;
SK=0;
ERROR=0;
//Input
DOUT=1;
while(1)
{
if(s==1)
{
EW_enable();
write_command();
Delay_Loop(1000);
read_command();
Erase_command();
EW_disable();
}
// --s;
}
}
if(DIN !=1)
{
ERROR=1; // Display Error
}
void indata()
{
unsigned char m,d=0;
SK=0; // Drop SK
for(m=1;m<=9;m++) // 9 bits including "0" dummy bit
{
SK=1; // Raise SK, data will be available after max. 250 ns (tPDO)
d=d<<1; // make room for the current read bit
d=d | DOUT;
SK=0; // Drop SK
}
P1=d; // Display 8-bits of DATA
}
silvio said:Hi Maverickmax,
The best solution is to use a TimeOut code (do not wait more than 10 ms inside indata() function if Ready flag didn't came out)
// read status after either writing or erasing process
void status()
{
unsigned long time_out 160U; // Generate approximately 10mS delay
DOUT=1; // Float pin
CS=1; // Raise CS
while((--time_out !=0) && (DOUT!=0)); // Wait for 10mS or Ready (DOUT==1)
if(DOUT !=1)
{
ERROR=1; // Nofity the current status - Error
}
else
{
ERROR=0; // Nofity the current status - Ok
}
CS=0; // Drop CS
}
Maverickmax said:Am I right that when erase process completes, all of bits in DOUT indicates "1" states?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?