📄 at89c2051 刻录程序.c
字号:
/* AT89C2051 programmer for FLAG51 */
/* program name:AT2051.C */
/* writtern by Lin Shen Mou */
/* 1996/02/28 */
#include "c8051io.h"
#include "c8051sr.h"
extern struct special_function_bits SFB;
#define AT_PIN 40
#define rate 0xfd
#define t_of_delay 8
#define AUTOLOCK 1
#define AT2051 0
#define AT1051 1
#define AT89C51 2
#define AT89C52 3
#define IN_BUF 0xa000 /* a000-bfff */
#define OUT_BUF 0xc000 /* c000-dfff */
#define BUFFER 0xa000 /* a000-bfff 8192 bytes */
#define PPIPA1 0x6000
#define PPIPB1 0x6001
#define PPIPC1 0x6002
#define PPICTL1 0x6003
#define PPIPA2 0x7000
#define PPIPB2 0x7001
#define PPIPC2 0x7002
#define PPICTL2 0x7003
#define COUNT 65536-10000 /* 10 mS */
#define TmsH (COUNT)/256
#define TmsL (COUNT)%256
#define CMD_LINE 23
const char command[25][80]={
{ "********* AT89 Series of FLASH Microcontroller programmer *********"},
{ "? or / - Commands list "},
{ "B lank - Blank check of AT89C5X/X051 "},
{ "E rase - Erase internal flash memory "},
{ "P rogram - Programming from RAM BUFFER "},
{ "A uto - Erase,Blank check,Programming then Lock bit1 & bit2 "},
{ "V erify - Verify AT89CXX & RAM BUFFER "},
{ "s ignature- Read signature of the chip "},
{ "L file - Download binary file from PC to RAM BUFFER "},
{ "S file - Upload RAM BUFFER to PC "},
{ "1 - Lock bit1 of AT89CXX (Futher programming is disabled) "},
{ "2 - Lock bit1+2 of AT89CXX (Verify is disabled) "},
{ "3 - Lock bit1+2+3 of AT89C51/AT89C52 only "},
{ "R ead - Read then save at RAM BUFFER "},
{ "D isplay - Display RAM BUFFER (A000H-BFFFH) "},
{ "C hecksum - Calculate RAM BUFFER checksum "},
{ "X - Clear memory RAM BUFFER "},
{ "T ype - Select ATMEL flash type(AT2051/AT1051/AT89C51/AT89C52) "},
{ "^ - Start programming loop test "},
{ "O - back to DOS shell "},
{ "Q uit - Exit to DOS "},
{ "<SHIFT>+1,2,3,4 - Select AT89C1051/AT89C2051/AT89C51/AT89C52 "},
{ "*******************************************************************"}
};
const char dummy[100] = { "XXXXX" };
const char hex_table[20] = { "0123456789ABCDEF" };
const char title0[80] =
{ "ATMEL's AT89 Microcontroller programmer V5.0 all rigths reserved" };
const char title1[80] =
{ "program written by Lin S.M. '96/09/05" };
const char title2[80] =
{ "Any other questions about ATMEL FLASH Microcontroller,please mail to:" };
const char title3[80] =
{ "Mr Lin S.M. P.O.BOX 1859,KAOHSIUNG 800,TAIWAN ROC" };
const char title4[80] =
{ " or E-mail:chipware@ksts.seed.net.tw" };
const char title5[80] =
{ "Please read [README] file in disk before you program AT89CXX" };
const char msg_at89c51[10] ={ "AT89C51" };
const char msg_at89c52[10] ={ "AT89C52" };
const char msg_at89c2051[10]={ "AT89C2051" };
const char msg_at89c1051[10]={ "AT89C1051" };
const char msg_erase[40] ={ "Erase flash memory" };
const char msg_blank[40] ={ "Blank check..." };
const char msg_no_blank[40] ={ "Blank check fail !!" };
const char msg_verify[40] ={ "Verify..." };
const char msg_verify_ok[40]={ "Verify OK on code data" };
const char msg_no_match[40] ={ "Verify fail !!" };
const char msg_pgm[40] ={ "Programming ..." };
const char msg_pass[40] ={ "FLASH MEMORY programmed successfully" };
const char msg_fail[40] ={ "Fail in programming" };
int display_char(char);
int display_char_no_wait(char);
int display_addr(unsigned int);
int test_count=0;
int flash_type;
int pgm_length;
int checksum=0;
int vpp=12;
int main()
{
int result;
int s_in;
int q;
int sum,prompt;
init_8255(0x92,0x82);
flash_type=read_CODE(0x7fff) & 0x07;
if (flash_type<0 || flash_type>3) flash_type=0;
select_pgm_length();
for (q=1;q<=0x80;q*=2)
{ P1=q; delay(10); }
P1=0;
enable_int0();
power_on_message();
line_feed();
display_str(title0,1);
display_str(title1,1);
line_feed();
delay(200);
display_str(title2,1);
display_str(title3,1);
display_str(title4,1);
display_str(title5,1);
line_feed();
display_str("System ROM(0000H-7FFFH) checksum ",0);
clear_buffer(8192);
check_rom();
display_str("OK!",1);
display_str("System RAM(8000H-FFFFH) R/W function test -",1);
check_mem();
display_prompt();
beep_start();
while (1) {
if (flash_type==AT2051 || flash_type==AT1051)
{
check_read_and_verify_sw();
check_pgm_sw();
}
if (SFB.RI==1) {
s_in=SBUF; /* read a command */
SFB.RI=0; /* reset RI flag */
prompt=1;
switch(s_in) {
case '^':
line_feed();
life_test();
break;
case 'L':
case 'l':
download_file();
beep_ok();
break;
case 's':
line_feed();
show_signature(0);
break;
/* case '#':
delay(10);
display_char(flash_type);
prompt=0;
break;
*/
case 'S':
for (q=0;q<200;q++) delay_10ms();
upload_file();
break;
case 'A':
case 'a':
line_feed();
beep_start();
display_str(msg_erase,1);
chip_erase();
result=blank_check(32);
if (result==0) {
result=programming();
}
if (result==0) display_str(msg_pass,1);
else display_str(msg_fail,1);
if (result==0) beep_ok();
else beep_err();
if (result!=0)
{
show_fail_in_programming();
show_mail_box();
}
if (result==0) {
display_str("Lock bit1",1);
lock_bit1();
display_str("Lock bit2",1);
lock_bit2();
}
break;
case 'P':
case 'p':
line_feed();
beep_start();
display_str(msg_erase,1);
result=chip_erase();
if (result==0) result=programming();
if (result==0) display_str(msg_pass,1);
else display_str(msg_fail,1);
if (result==0) beep_ok();
else beep_err();
if (result!=0)
{
show_fail_in_programming();
show_mail_box();
}
break;
case 'V':
line_feed();
result=verify(1);
if (result==0) display_str(msg_verify_ok,1);
else display_str(msg_no_match,1);
if (result==0) beep_ok();
else beep_err();
break;
case 'v':
line_feed();
result=verify(0); /* break if error happen */
if (result==0) display_str(msg_verify_ok,1);
else display_str(msg_no_match,1);
if (result==0) beep_ok();
else beep_err();
break;
case 'r':
case 'R':
line_feed();
display_str("Read then save into RAM BUFFER",1);
checksum=chip_read();
display_str("Checksum = ",0);
display_addr((unsigned int)checksum);
line_feed();
/* p20_show_signature(1); */
beep_ok();
break;
case 'B':
case 'b':
line_feed();
display_str("Blank checking...",1);
result=blank_check(pgm_length);
if (result==0) display_str("This chip is BLANK",1);
else display_str("This chip is NOT BLANK!",1);
if (result==0) beep_ok();
else beep_err();
show_signature(1);
break;
case 'E':
case 'e':
line_feed();
display_str(msg_erase,1);
result=chip_erase();
if (result==0) display_str("Erase completed",1);
else display_str("Erase fail!",1);
if (result==0) beep_ok();
else beep_err();
break;
case 'D':
case 'd':
line_feed();
display_str("Display RAM BUFFER",1);
checksum=display_mem_block(BUFFER,BUFFER+pgm_length);
display_str("RAM BUFFER checksum = ",0);
display_addr((unsigned int)checksum);
line_feed();
break;
case 'C':
case 'c':
line_feed();
display_str("Calculate checksum = ",0);
checksum=calculate_checksum();
display_addr_woh((unsigned int)checksum);
line_feed();
break;
case 'X':
case 'x':
line_feed();
display_str("Clear RAM BUFFER to 00H");
clear_buffer(pgm_length);
beep_ok();
break;
case '?':
case '/':
line_feed();
command_list();
break;
case '1':
line_feed();
display_str("Lock bit1",1);
lock_bit1();
beep_ok();
break;
case '2':
line_feed();
display_str("Lock bit1 + bit2",1);
lock_bit1();
lock_bit2();
beep_ok();
break;
case '3':
line_feed();
display_str("Lock bit1 + bit2 + bit3",1);
lock_bit1();
lock_bit2();
lock_bit3();
beep_ok();
break;
case 't':
case 'T':
line_feed();
if (AT_PIN==20)
{
switch (flash_type)
{
case AT2051: flash_type=AT1051;
display_str("Change type to AT89C1051 (1K)",1);
break;
case AT1051: flash_type=AT2051;
display_str("Change type to AT89C2051 (2K)",1);
break;
}
}
else
{
switch (flash_type)
{
case AT2051: flash_type=AT1051;
display_str("Change type to AT89C1051 (1K)",1);
break;
case AT1051: flash_type=AT89C51;
display_str("Change type to AT89C51 (4K)",1);
break;
case AT89C51:flash_type=AT89C52;
display_str("Change type to AT89C52 (8K)",1);
break;
case AT89C52:flash_type=AT2051;
display_str("Change type to AT89C2051 (2K)",1);
break;
}
}
program_setting();
break;
case '!': line_feed(); flash_type=AT1051;
display_str("Change type to AT89C1051 (1K)",1);
program_setting();
break;
case '@': line_feed(); flash_type=AT2051;
display_str("Change type to AT89C2051 (2K)",1);
program_setting();
break;
case '#': line_feed(); flash_type=AT89C51;
display_str("Change type to AT89C51 (4K)",1);
program_setting();
break;
case '$': line_feed(); flash_type=AT89C52;
display_str("Change type to AT89C52 (8K)",1);
program_setting();
break;
case 0x0d:
case 0x0a:
case 0x00:
line_feed();
break;
default:
line_feed();
display_str("Type '?' or '/' for help !",1);
beep_err();
break;
}; /* end of switch */
if (prompt==1) display_prompt();
}
} /* end of while (1) */
return(0);
} /* end of main program */
int program_setting()
{
beep_start();
select_pgm_length();
display_str("Program count : ",0);
display_addr(pgm_length);
line_feed();
display_str("Calculate checksum = ",0);
checksum=calculate_checksum();
display_addr_woh((unsigned int)checksum);
line_feed();
line_feed();
}
int select_pgm_length()
{
switch (flash_type)
{
case AT2051:
pgm_length=2048;
break;
case AT1051:
pgm_length=1024;
break;
case AT89C51:
pgm_length=4096;
break;
case AT89C52:
pgm_length=8192;
break;
}
}
int lock_bit1()
{
if (flash_type==AT2051 || flash_type==AT1051) p20_lock_bit1();
if (flash_type==AT89C51 || flash_type==AT89C52) p40_lock_bit1();
}
int lock_bit2()
{
if (flash_type==AT2051 || flash_type==AT1051) p20_lock_bit2();
if (flash_type==AT89C51 || flash_type==AT89C52) p40_lock_bit2();
}
int lock_bit3()
{
p40_lock_bit3();
}
int show_signature(int c)
{
if (flash_type==AT2051 || flash_type==AT1051)
{ p20_show_signature(c); }
if (flash_type==AT89C51 || flash_type==AT89C52)
{ p40_show_signature(c); }
}
int p20_show_signature(int mode)
{
int result;
result=p20_read_signature(0);
display_str("Chip signatures are :",0);
display_str(" (000H)=",0); display_hex(result);
result=p20_read_signature(1);
display_str("H,(001H)=",0); display_hex(result);
display_char('H');
result=p20_read_signature(2);
display_str(",(002H)=",0); display_hex(result);
display_char('H');
line_feed();
if (mode==0) {
display_str("If (000H)=1EH indicates manufatured by ATMEL",1);
display_str("If (001H)=21H/11H indicates ATMEL 89C2051/1051",1);
display_str("If (000H)=FFH indicates 12V programming",1);
display_str("If lock_bit2 is set to 1,signature reading is then disabled",1);
}
}
int p40_show_signature(int mode)
{
int result;
result=read_signature();
display_str("Chip signature are :",0);
display_str(" (30H)=",0); display_hex(result%256);
display_str("H,(31H)=",0); display_hex(result/256);
display_char('H');
result=read_extra_signature();
display_str(",(32H)=",0); display_hex(result%256);
display_char('H');
line_feed();
if (mode==0) {
display_str("If (31H)=1EH indicate manufatured by ATMEL",1);
display_str("If lock_bit2 or 3 is set to 1,signature reading is then disabled",1);
}
}
int blank_check(int c)
{
int result;
if (flash_type==AT2051 || flash_type==AT1051)
result=p20_blank_check(c);
if (flash_type==AT89C51 || flash_type==AT89C52)
result=p40_blank_check(c);
return(result);
}
int p20_blank_check(int count) /* test AT89C2051/1051 & BUFFER */
{
int m,data;
int err=0;
p20_read_code_mode();
reset_addr();
for (m=0;(m<count && err==0);m++)
{
data=p20_read_byte() & 0xff;
if (data!=0xff) err=1;
next_addr();
if (m%1024==0 && count>1000) display_char_no_wait('B'); /* 1 page programmed */
}
delay(10);
if (count>1000) line_feed();
return(err);
}
int p40_blank_check(int count) /* test AT89C51/52 & BUFFER */
{
int m,data;
int err=0;
read_code_mode();
for (m=0;(m<count && err==0);m++)
{
addr_out(m);
data=read_byte() & 0xff;
if (data!=0xff) err=1;
if (m%1024==0 && count>1000) display_char_no_wait('B'); /* 1 page programmed */
}
delay(10);
if (count>1000) line_feed();
return(err);
}
int chip_read()
{
int x;
if (flash_type==AT2051 || flash_type==AT1051)
x=p20_chip_read();
if (flash_type==AT89C51 || flash_type==AT89C52)
x=p40_chip_read();
return(x);
}
int p20_chip_read()
{
int m;
int data,sum;
sum=0;
p20_read_code_mode();
display_str("read count = ",0);
display_addr(pgm_length);
line_feed();
reset_addr();
for (m=0;m<pgm_length;m++)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -