Hi,
I don't know if I should continue on this topic or create a new one. Please advise.
I tried what you have said but is still not working. However, I changed this chip with Atmega328p just to test the "Programming enable instruction" and some other stuffs and Boooom!! it works !!.
So, since I don't have for the time being the "At89s51" I will continue working on Atmega328p and how to program it from scratch using Arduino. I know that there are great ISP programmers for AVR in the market, but I try to build it and code it myself to learn as much as possible.
I'm now struggling to write to the "Program memory page" in the Atmega, and what I've understood till now is that I should load the program memory page at first with low and high data byte, and after send write instruction to the chip to save data in the memory page. (Correct me if i'm wrong).
This is the code that I'm using:
#define ENABLE_PROGRAMMING 0xAC530000L
#define CHIP_ERASE 0xAC800000L
#define READ_BYTE_LSB 0x20000000L
#define READ_BYTE_MSB 0x28000000L
#define WRITE_BYTE 0x4C000000L
#define LOAD_BYTE_MSB 0x48000041L
#define LOAD_BYTE_LSB 0x40000041L
#define SIGNATURE0 0x30000000L
#define SIGNATURE1 0x30000100L
#define SIGNATURE2 0x30000200L
#define READ_LOCK_BITS 0x58000000L
#define WRITE_LOCK_BITS 0xACE00000L
#define LOCKB_NOPROTECT 0x000000FFL
#define LOCKB_NOPROGRAM 0x000000FEL
#define LOCKB_ALLPROTECT 0x000000FCL
int RST_P = 9;
int MOSI_P = 10;
int MISO_P = 11;
int CLK_P = 12;
void setup()
{
Serial.begin(9600);
pinMode(8, OUTPUT);
pinMode(MOSI_P, OUTPUT);
pinMode(MISO_P, INPUT);
pinMode(RST_P, OUTPUT);
pinMode(CLK_P, OUTPUT);
digitalWrite(RST_P, HIGH);
digitalWrite(MOSI_P, HIGH);
digitalWrite(CLK_P, LOW);
}
void sendCmd(long cmd, boolean output)
{
boolean counter_bool = false;
int counter = 0;
for (int i=31; i>=0; i--)
{
if (cmd&bit(i))
{
digitalWrite(MOSI_P, HIGH);
}
else
{
digitalWrite(MOSI_P, LOW);
}
digitalWrite(CLK_P, HIGH);
delay(1);
if (output/*&&counter_bool*/)
Serial.print(digitalRead(MISO_P));
digitalWrite(CLK_P, LOW);
}
Serial.println("");
}
void programmingEnable(boolean x)
{
int c = 0;
if (x)
{
digitalWrite(MOSI_P, LOW);
digitalWrite(CLK_P, LOW);
digitalWrite(RST_P, LOW);
//A LED indicator
for (int i=0; i<4; i++) {
delay(40);
digitalWrite(8, c=(~c));
}
//digitalWrite(CLK_P, HIGH);
}
else
{
digitalWrite(RST_P, HIGH);
digitalWrite(MOSI_P, HIGH);
digitalWrite(CLK_P, HIGH);
}
delay(1);
}
void programChip(char *program)
{
long address = 0x00000000;
int counter = 0;
int len = strlen(program);
while (counter != len)
{
for (;;)
counter ++;
}
}
void loop()
{
programmingEnable(true);
sendCmd(ENABLE_PROGRAMMING, false);
sendCmd(LOAD_BYTE_LSB, false);
sendCmd(LOAD_BYTE_MSB, false);
sendCmd(WRITE_BYTE, false);
sendCmd(WRITE_BYTE, false);
sendCmd(READ_BYTE_LSB, true);
while(1);
}
What am I supposed to do to write correctly to the program memory ?
Thanks