89S51/52燒錄器 – 透過RS232及89C2051燒錄

發了一大堆燒錄器,都是不用經過另一顆單晶片做轉換的。

而我這次要發的這一篇則是多了一顆小小的2051單晶片來做溝通的橋梁,最重要的是,我獨家取得此款燒錄器的原始碼

好,接下來我就先大略的介紹一下這種燒錄器的特點。

  • 利用單晶片,使整體體積變得更小
  • 可以使用USB to Serial轉接頭
  • 成本低廉
  • 攜帶方便(?) 總覺得,我每次整理出來的要點都很少很少……(我這是精簡!你懂不懂啊!) OK,接下來就是最重要的部分,電路圖與燒錄程式啦!

  • 電路圖

  • 燒錄程式下載
  • ISP232.HEX〈AT89C2051的程式〉

ISP232.HEX原始碼(C語言)

From:馬蓋先技能檢定研習室

#include
char bdata flag;
sbit rx_flag=flag^0;
sbit tx_flag=flag^1;
sbit write_flag=flag^2;
sbit write_enable=flag^3;
/*sbit reset=P1^0;
sbit clock=P1^1;
sbit din=P1^2;
sbit dout=P1^3;*/
sbit reset=P1^4;
sbit clock=P1^7;
sbit din=P1^6;
sbit dout=P1^5;
char data rx_buf[2];
char data write_buf;
char data check;
char data hex_asc[]="0123456789ABCDEF";
unsigned char data data_buf;
void delay_1ms(unsigned int);
void set_fun();
void init_rs232();
void program_enable();          //
void erase_chip();              //
void write_page(char);          //
void read_page(char);           //
void write_byte(char);          //
void tx_byte(char);
void read_chip(char);
void write(char);               //
void lock_bit();
main()
{
    delay_1ms(0);
    init_rs232();
    tx_flag=1;
    P1=0xff;
    //clock=0;
    //dout=0;
    //reset=1;
    clock=1;
    dout=1;
    reset=0;
    while(1)
    {
        set_fun();
    }
}
void delay_1ms(unsigned int k)
{
    unsigned int i,j;
    for (i=0;i<=k;i++)
            for (j=0;j<=110;j++);
}
void set_fun()
{
  if(rx_flag==1)
  {
    rx_flag=0;
    switch (rx_buf[0])
      {
        case 1 : read_chip(rx_buf[1]);
                 break;
        case 2 : erase_chip();
                 break;
        case 3 : write(rx_buf[1]);
                 break;
        case 4 : lock_bit();
                 break;
        default : break;
      }
  }
}
void read_chip(char page)
{
    char i;
    program_enable();
    if(check==0x69)
    {
        while(tx_flag!=1);
        tx_flag=0;
        SBUF=0x01;
        delay_1ms(300);
        for(i=0;i<=page;i++)
        {
            /*while(tx_flag!=1);
            tx_flag=0;
            SBUF='A';
            while(tx_flag!=1);
            tx_flag=0;
            SBUF='

;
            while(tx_flag!=1);
            tx_flag=0;
            SBUF=hex_asc[i/16];
            while(tx_flag!=1);
            tx_flag=0;
            SBUF=hex_asc[i%16];
            while(tx_flag!=1);
            tx_flag=0;
            SBUF='0';
            while(tx_flag!=1);
            tx_flag=0;
            SBUF='0';
            while(tx_flag!=1);
            tx_flag=0;
            SBUF=0x0d;
            while(tx_flag!=1);
            tx_flag=0;
            SBUF=0x0a;*/
            read_page(i);
        }
        reset=0;
        clock=1;
        //be=1;
        while(tx_flag!=1);
        delay_1ms(3);
        tx_flag=0;
        SBUF=0x03;
    }
    else
    {
        while(tx_flag!=1);
        tx_flag=0;
        SBUF=0x02;
        reset=0;
        clock=1;
        //be=1;
    }
}
void program_enable()
{
    char i,temp;
    clock=0;
    reset=1;
    //be=0;
    reset=1;
    delay_1ms(50);
    write_byte(0xAC);
    write_byte(0x53);
    write_byte(0x00);
    for(i=0;i<8;i++)
    {
        if(din==1)
            temp = temp * 2 + 1;
        else
            temp = temp * 2;
        clock=1;
        clock=0;
    }
    check=temp;
    //tx_byte(temp);
    /*while(tx_flag!=1);
    tx_flag=0;
    SBUF=0x0d;
    while(tx_flag!=1);
    tx_flag=0;
    SBUF=0x0a;*/
}
void erase_chip()
{
    program_enable();
    if(check==0x69)
    {
        delay_1ms(500);
        write_byte(0xAC);
        write_byte(0x80);
        write_byte(0x00);
        write_byte(0x00);
        delay_1ms(500);
        reset=0;
        clock=1;
        //be=1;
        while(tx_flag!=1);
        tx_flag=0;
        SBUF=0x01;
    }
    else
    {
        while(tx_flag!=1);
        tx_flag=0;
        SBUF=0x02;
        reset=0;
        clock=1;
        //be=1;
    }
}
void write(char write_len)
{
    unsigned char i;
    write_flag=1;
    erase_chip();
    delay_1ms(300);
    program_enable();
    if(check==0x69)
    {
        delay_1ms(300);
        while(tx_flag!=1);
        tx_flag=0;
        SBUF=0x30;
        for(i=0;i<=write_len;i++)
            write_page(i);
        reset=0;
        clock=1;
        //be=1;
        delay_1ms(100);
        //read_chip(1);
        write_flag=0;
    }
    else
    {
        while(tx_flag!=1);
        tx_flag=0;
        SBUF=0x02;
        reset=0;
        clock=1;
        //be=1;
    }
}
void write_page(char page)
{
    unsigned int i;
    write_byte(0x50);
    write_byte(page);
    for(i=0;i<256;i++)
    {
        while(write_enable!=1);
        write_enable=0;
        if(page==rx_buf[1] & i==255);
        else
        {
            while(tx_flag!=1);
            tx_flag=0;
            SBUF=0x30;
        }
        write_byte(write_buf);
        delay_1ms(0);
    }
}
void read_page(char page)
{
    char i,temp;
    //int count;
    unsigned int j;
    write_byte(0x30);
    write_byte(page);
    for(j=0;j<256;j++)
    {
        for(i=0;i<8;i++)
        {
            if(din==1)
            temp = temp * 2 + 1;
        else
            temp = temp * 2;
        clock=1;
        clock=0;
        }
        delay_1ms(1);
        tx_byte(temp);
        /*count++;
        if(count%16==0)
        {
            while(tx_flag!=1);
            tx_flag=0;
            SBUF=0x0d;
            while(tx_flag!=1);
            tx_flag=0;
            SBUF=0x0a;
        }*/
        temp=0;
    }
}
void lock_bit()
{
    program_enable();
    if(check==0x69)
    {
        delay_1ms(500);
        write_byte(0xAC);
        write_byte(0xe0);
        write_byte(0x00);
        write_byte(0x00);
        delay_1ms(500);
        reset=0;
        clock=1;
        delay_1ms(500);
        program_enable();
        write_byte(0xAC);
        write_byte(0xe1);
        write_byte(0x00);
        write_byte(0x00);
        delay_1ms(500);
        reset=0;
        clock=1;
        delay_1ms(500);
        program_enable();
        write_byte(0xAC);
        write_byte(0xe2);
        write_byte(0x00);
        write_byte(0x00);
        delay_1ms(500);
        reset=0;
        clock=1;
        delay_1ms(500);
        program_enable();
        write_byte(0xAC);
        write_byte(0xe3);
        write_byte(0x00);
        write_byte(0x00);
        delay_1ms(500);
        reset=0;
        clock=1;
        //be=1;
        while(tx_flag!=1);
        tx_flag=0;
        SBUF=0x01;
    }
    else
    {
        while(tx_flag!=1);
        tx_flag=0;
        SBUF=0x02;
        reset=0;
        clock=1;
        //be=1;
    }
}
void write_byte(char temp)
{
    char i;
    for(i=0;i<8;i++)
    {
        if(temp&128)
            dout=1;
        else
            dout=0;
        clock=1;
        clock=0;
        temp=temp*2;
    }
    delay_1ms(0);
}
void SCON_int(void) interrupt 4
{
    static char count;
    if(RI==1)
    {
        RI=0;
        if(write_flag==1)
        {
            write_buf=SBUF;
            write_enable=1;
        }
        else
        {
            rx_buf[count]=SBUF;
            //P0=rx_buf[count];
            count++;
            if(count==2)
            {
                rx_flag=1;
                count=0;
            }
        }
    }
    else
    {
        TI=0;
        tx_flag=1;
    }
}
void tx_byte(char temp)
{
    while(tx_flag!=1);
    tx_flag=0;
    SBUF=hex_asc[(temp>>4)&0x0f];
    while(tx_flag!=1);
    tx_flag=0;
    SBUF=hex_asc[temp&0x0f];
        //while(tx_flag!=1);
        //tx_flag=0;
        //SBUF=0x20;
}
void init_rs232()
{
    TMOD=0x20;      //timer1 set mode 2
    PCON=PCON | 0x80;   //SMOD=1
    TH1=0xfd;       //19200/s
    TR1=1;          //start timer1
    SCON=0x50;      //set mode 1
    IE=0x90;            //enable int
}
獨夜:

看評論 (14)