⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch11_at89cxx 刻录程序彻底公开.c

📁 8051单片机刻录程序彻底公开 用于单片机刻录器
💻 C
📖 第 1 页 / 共 4 页
字号:
         if (v!=data)
         { err+=1;
           display_error(n,v,data);
         }
         if (err>=8) break;

        p20_write_byte(0xff);
         avr_oe_low();
         avr_bs_hi();
         data=avr_read_byte();          /* read high byte */
         avr_oe_hi();
         v=read_XDATA(BUFFER+n+1);
         if (v!=data)
         { err+=1;
           display_error(n+1,v,data);
         }
         if (err>=8) break;
         if ((m & 0x007f)==0) display_char('V');
        }
        line_feed();

        p20_write_byte(0xff);
        p20_rst_hi();
        return(err);
}

int display_error(int addr,int right,int wrong)
{
        display_str(" Error at address ",0);
        display_addr((unsigned int)addr);
        display_hex(right); display_str("(SRAM) ",0);
        display_hex(wrong); display_str("(READ)",1);
}

int avr_flash_reading()
{
 int m,n,data;
 int sum;

        avr_8255_mode();
        p20_rst_12v();
        avr_command(AVR_FLASH_READ);

        for (m=0,sum=0,n=0;n<pgm_length;m+=1,n+=2)
        {
         avr_load_addrall(m);
        p20_write_byte(0xff);
         avr_oe_low();  avr_bs_low();
         data=avr_read_byte();          /* read low byte */
         write_XDATA(BUFFER+n,data);
         sum=sum+data;

        p20_write_byte(0xff);
         avr_bs_hi();
         data=avr_read_byte();          /* read high byte */
         avr_oe_hi();
         write_XDATA(BUFFER+n+1,data);
         sum=sum+data;
         if ((m & 0x007f)==0) display_char('R');
        }
        line_feed();
        p20_write_byte(0xff);
        p20_rst_hi();
        return(sum);
}

int avr_erase()
{
/*      avr_8255_mode();        */
        p20_rst_12v();
        avr_command(AVR_ERASE); /* erase FLASH & EEPROM */
        avr_write_mode();
        delay_10ms();
       delay_10ms();
        avr_wr_low();           /* erase lock bit */
        avr_wr_hi();
        p20_rst_hi();
        delay_10ms();
        delay_10ms();
        avr_idle();
}

int avr_flash_programming()
{
 int m,n;
 int dl,dh,data;

        avr_8255_mode();
        p20_rst_12v();
        avr_erase();
        display_str("Erase the whole Flash and EEPROM first",1);
        p20_rst_12v();
        avr_command(AVR_PGM_FLASH);
        data=0;
        for (m=0,n=0;n<pgm_length;m++,n+=2)
        {
          avr_load_addr(m);     /* load addr_low then addr_high */
          dl=read_XDATA(BUFFER+n);
          dh=read_XDATA(BUFFER+n+1);
/*        avr_load_data(m,m);   */
          avr_load_data(dl,dh);   /*   load data 2 byte */
          if ((m & 0x007f)==0) display_char('P');
        }
        line_feed();
        p20_rst_hi();
        avr_idle();
}

int avr_load_data(int data1,int data2)
{
        avr_write_mode();

        avr_xa1_low();
        avr_xa0_hi();
        p20_write_byte(data1); /* data_low output */
        avr_xtal1_pulse();

        avr_bs_low();
        avr_wr_low();
        avr_wr_hi();
        delay_2ms();

        avr_xa1_low();
        avr_xa0_hi();
        p20_write_byte(data2); /* data_high output */
        avr_xtal1_pulse();

        avr_bs_hi();
        avr_wr_low();
        avr_wr_hi();
        delay_2ms();
}

int avr_command(int command)
{
        avr_write_mode();
        p20_xtal1_low();
        avr_xa1_hi();
        avr_xa0_low();
        avr_bs_low();
        p20_write_byte(command);
        avr_xtal1_pulse();
}


int avr_load_lowaddr(int addr)
{
        avr_write_mode();
        avr_xa1_low();
        avr_xa0_low();
        avr_bs_low();
        p20_write_byte(addr & 0xff); /* low address */
        avr_xtal1_pulse();
}

int avr_load_addr(int addr)
{
      avr_write_mode();
        avr_xa1_low();
        avr_xa0_low();
        avr_bs_low();
        p20_write_byte(addr & 0xff); /* low address */
        avr_xtal1_pulse();

       if ((addr & 0xff)==0)
       {
        avr_xa1_low();
        avr_xa0_low();
        avr_bs_hi();
        p20_write_byte(addr >>8);
        avr_xtal1_pulse();
       }

/*      avr_xa1_low();
        avr_xa0_low();
        avr_bs_hi();
        p20_write_byte(addr/256);
        avr_xtal1_pulse();      */
}

int avr_load_addrall(int addr)
{
/*     avr_write_mode();        */
        avr_xa1_low();
        avr_xa0_low();
        avr_bs_low();
        p20_write_byte(addr & 0xff); /* low address */
        avr_xtal1_pulse();

        avr_xa1_low();
        avr_xa0_low();
        avr_bs_hi();
        p20_write_byte(addr/256);
        avr_xtal1_pulse();
}

int avr_idle()
{
        avr_xa1_hi();
       avr_xa0_hi();
        p20_rst_hi();
        p20_xtal1_low();
}

int avr_xa1_low() { p20_p37_low(); }
int avr_xa1_hi()  { p20_p37_hi();  }
int avr_xa0_low() { p20_p35_low(); }
int avr_xa0_hi()  { p20_p35_hi();  }
int avr_bs_low()  { p20_p34_low(); }
int avr_bs_hi()   { p20_p34_hi();  }
int avr_wr_low()  { p20_p33_low(); }
int avr_wr_hi()   { p20_p33_hi();  }
int avr_oe_low()  { prog_low(); }
int avr_oe_hi()   { prog_hi();  }


int avr_xtal1_pulse()
{
        p20_xtal1_low();
        p20_xtal1_hi();
        p20_xtal1_low();
}


/* AT90S1200 programming code above */

int select_SPF(int code)
{
        init_8255(0x80,0x82); /* 1st-PA,PB,PC output */
                              /* 2nd-PA out,PB & PC input */
        rst_hi();
        psen_low();
        prog_hi();
        vpp_5v();

        p26_low();
        p27_hi();
        p36_low();
        p37_hi();

        if (code==0)
      
     { write_byte(0x00); display_str("Enable AT89S8252 Serial Programmable Fuse",1); }
     if (code==1)
     { write_byte(0x01); display_str("Disable AT89S8252 Serial Programmable Fuse",1); }

        vpp_12v();
        prog_hi();
        prog_low();
        delay_10ms();     /* delay 10 mS */
        prog_hi();
        vpp_5v();
}

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 AT90S1200:
                      pgm_length=1024;
                      eeprom_length=64;
                      break;
         case AT4051:
         case AT89C51:
                      pgm_length=4096;
                      break;
         case AT89C52:
                      pgm_length=8192;
                      break;
         case AT89S8252:
                      pgm_length=8192;
                      eeprom_length=2048;
                      break;
        }
}

int lock_bit1()
{
        if (flash_type==AT2051 || flash_type==AT4051 || flash_type==AT1051)
           p20_lock_bit1();
        if (flash_type==AT89C51 || flash_type==AT89C52)
           p40_lock_bit1();
        if (flash_type==AT89S8252) at89s8252_lock_bit(1);
}

int lock_bit2()
{
        if (flash_type==AT2051 || flash_type==AT4051 || flash_type==AT1051)
            p20_lock_bit2();
        if (flash_type==AT89C51 || flash_type==AT89C52) p40_lock_bit2();
        if (flash_type==AT89S8252) at89s8252_lock_bit(2);
}

int lock_bit3()
{
        if (flash_type==AT89C51 || flash_type==AT89C52) p40_lock_bit3();
        if (flash_type==AT89S8252) at89s8252_lock_bit(3);
}

int show_signature(int c)
{
        if (flash_type==AT2051 || flash_type==AT4051 || flash_type==AT1051 )
        { p20_show_signature(c); }
        if (flash_type==AT89C51 || flash_type==AT89C52 || flash_type==AT89S8252)
        { p40_show_signature(); }
        if (flash_type==AT90S1200)
        { avr_signature_reading(); }
}

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)=41H/21H/11H indicates ATMEL 89C4051/2051/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 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);
 } */

 if (flash_type==AT89S8252)
 {
  result=read_at89s8252_SPF();
  if (result==0) display_str("AT89S8252 Serial Programmable Fuse <ENABLE>",1);
  else           display_str("AT89S8252 Serial Programmable Fuse <DISABLE>",1);
  read_at89s8252_lockbit();
 }
}

int read_at89s8252_SPF()
{
 int result;

  read_signature_mode();
  p26_hi();
  p27_hi();
  p36_low();
  p37_hi();
  vpp_12v();
  result=read_byte() & 0x01;
  vpp_5v();
  return(result);
}

int read_at89s8252_lockbit()
{
 int result;

  read_signature_mode();
  p26_hi();
  p27_hi();
  p36_low();
  p37_low();
  vpp_12v();
  result=read_byte() & 0x07;
  vpp_5v();
  if (result & 0x04) display_str("AT89S8252 Lock bit-1 Unprogrammed",1);
  else               display_str("AT89S8252 Lock bit-1 programmed",1);
  if (result & 0x02) display_str("          Lock bit-2 Unprogrammed",1);
  else               display_str("          Lock bit-2 programmed",1);
  if (result & 0x01) display_str("          Lock bit-3 Unprogrammed",1);
  else               display_str("          Lock bit-3 programmed",1);
}

int blank_check(int c)
{
 int result;
        if (flash_type==AT2051 || flash_type==AT4051 || flash_type==AT1051)
          result=p20_blank_check(c);
        if (flash_type==AT89C51 || flash_type==AT89C52 || flash_type==AT89S8252)
          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_mode();
        p20_reset_addr();
        for (m=0;(m<count && err==0);m++)
        {
          data=p20_read_byte() & 0xff;
          if (data!=0xff) err=1;
          p20_next_addr();
         if (m%1024==0 && count>1000) display_char('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();
        if (flash_type==AT89S8252) vpp_12v();
        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('B');  /* 1 page programmed */
        }
        vpp_5v();
        delay(10);
        if (count>1000) line_feed();
        return(err);
}

int chip_read()
{
 int x;
        if (flash_type==AT2051 || flash_type==AT4051 || flash_type==AT1051)
         x=p20_chip_read();
        if (flash_type==AT89C51 || flash_type==AT89C52 || flash_type==AT89S8252)
         x=p40_chip_read();
        return(x);
}

int p20_chip_read()
{
 int m;
 int data,sum;

        sum=0;
        p20_read_mode();
        display_str("read count = ",0);
        display_addr(pgm_length);
        line_feed();
        p20_reset_addr();
        for (m=0;m<pgm_length;m++)
        {
         data=p20_read_byte();
         write_XDATA(BUFFER+m,data);
         sum+=data;
         p20_next_addr();
         if (m%1024==0) display_char('R');  /* 1 page programmed */
        }
        line_feed();
      return(sum);
}

int p40_chip_read()
{
 int m;
 int data,sum;

        sum=0;
        read_code_mode();
        if (flash_type==AT89S8252) vpp_12v();

        for (m=0;m<pgm_length;m++)
        {
         addr_out(m);
         data=read_byte();
         write_XDATA(BUFFER+m,data);
         sum+=data;
         if (m %1024==0) display_char('R');  /* 1 page programmed */
        }
        vpp_5v();
        line_feed();
        return(sum);
}

int p20_read_mode()
{
        init_8255(0x92,0x82);
        p20_rst_hi();
        p20_prog_hi();
        p20_p33_low();
        p20_p34_low();
        p20_p35_hi();
        p20_p37_hi();
}

int p20_read_signature(int addr)
{
 int c1;

        init_8255(0x92,0x82);
        p20_rst_hi();
        p20_prog_hi();
        p20_p33_low();
        p20_p34_low();
        p20_p35_low();
        p20_p37_low();

        p20_reset_addr();
        delay_10ms();     /* delay 10 mS */

        switch(addr)
        {
         case 0:
                c1=p20_read_byte();         /* c1 must be 1EH */
                break;
         case 1:
                p20_next_addr();
                c1=p20_read_byte();         /* c2 must be 21H */
                break;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -