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

📄 insystem program.c

📁 华邦51单片机的ISP下载程序,使用keilc51编译
💻 C
字号:
#include <STDIO.H>
//#include "W78e365.H"
#include "ABSACC.H"
unsigned long Fosc        = 22118400;           // HZ
unsigned long BaudRate    = 9600;

/*********************************************************/
unsigned int CodeTotalNumber = 8-1;
//                                W    B    0    5    D    0    0    0
unsigned char UserCode[]     = {0x57,0x42,0x30,0x35,0x44,0x30,0x30,0x30};

/*********************************************************/
//              ISP Mode APROM File
/*********************************************************/

sfr P3                  = 0xb0;
sfr P1                  = 0x90;
sbit P1_0               = P1^0;
sbit P1_1               = P1^1;
sbit P1_2               = P1^2;
sbit P1_3               = P1^3;
sbit P1_4               = P1^4;
sbit P1_5               = P1^5;
sbit P1_6               = P1^6;
sbit P1_7               = P1^7;

sfr a                   = 0xe0;
sfr b                   = 0xf0;
sfr dpl                 = 0x82;
sfr dph                 = 0x83;
sfr sbuf                = 0x99;

sfr pcon                = 0x87;
//sbit idl                = pcon^0;
//sbit pd                 = pcon^1;
//sbit gf0                = pcon^2;
//sbit gf1                = pcon^3;
//sbit smode              = pcon^7;

sfr scon                = 0x98;
sbit ti                 = scon^1;
sbit ri                 = scon^0;

sfr ie                  = 0xa8;
sbit ex0                = ie^0;
sbit et0                = ie^1;
sbit ex1                = ie^2;
sbit et1                = ie^3;
sbit es                 = ie^4;
sbit et2                = ie^5;
sbit ea                 = ie^7;

sfr ip                  = 0xb8;
sbit px0                = ip^0;
sbit pt0                = ip^1;
sbit px1                = ip^2;
sbit pt1                = ip^3;
sbit ps                 = ip^4;
sbit pt2                = ip^5;

sfr psw                 = 0xd0;
sbit p                  = psw^0;
sbit ov                 = psw^2;
sbit rs0                = psw^3;
sbit rs1                = psw^4;
sbit f0                 = psw^5;
sbit ac                 = psw^6;
sbit c                  = psw^7;

sfr tcon                = 0x88;
sbit it0                = tcon^0;
sbit ie0                = tcon^1;
sbit it1                = tcon^2;
sbit ie1                = tcon^3;
sbit tr0                = tcon^4;
sbit tf0                = tcon^5;
sbit tr1                = tcon^6;
sbit tf1                = tcon^7;

sfr t2con               = 0xc8;
sbit cprl2              = t2con^0;
sbit ct2                = t2con^1;
sbit tr2                = t2con^2;
sbit exen2              = t2con^3;
sbit tclk               = t2con^4;
sbit rclk               = t2con^5;
sbit exf2               = t2con^6;
sbit tf2                = t2con^7;

sfr tmod                = 0x89;
sfr th0                 = 0x8c;
sfr tl0                 = 0x8a;
sfr th1                 = 0x8d;
sfr tl1                 = 0x8b;
sfr th2                 = 0xcd;
sfr tl2                 = 0xcc;
sfr rcap2h              = 0xcb;
sfr rcap2l              = 0xca;

sfr CHPCON              = 0xbf;
sfr CHPENR              = 0xf6;
sfr SFRAL               = 0xc4;
sfr SFRAH               = 0xc5;
sfr SFRFD               = 0xc6;
sfr SFRCN               = 0xc7;

sfr turbo_TA            = 0xc7;
sfr turbo_SFRAL         = 0xac;
sfr turbo_SFRAH         = 0xad;
sfr turbo_SFRFD         = 0xae;
sfr turbo_SFRCN         = 0xaf;
sfr turbo_CHPCON        = 0x9f;
sfr turbo_CKCON         = 0x8e;         // clock control
sfr turbo_T2MOD         = 0xc9;         // clock control

bit jmpLDROM           = 0x20;
bit TimerOut           = 0x21;


void InitialPowerON(void);
void Smode0BaudRate(void);
void Smode1BaudRate(void);
void Timer2BaudRate(void);
void ChangeAPtoLD(void);
void RecDataTimer(void);



/************************************************/
void Timer0ISR (void) interrupt 1 using 1
{
        tr0=0;
        tf0=0;
}



/************************************************/
void SerialInt(void) interrupt 4 using 3
{
        unsigned int i,j,code_a;

        if(ri==0)
        { ti=0; }
        else
        {
                jmpLDROM=1;

                i=0x00;
                while(1)
                {
                        j=sbuf;
                        code_a=UserCode[i];
                        if(j!=code_a)
                        {
                                jmpLDROM=0;
                                goto ERROR;
                        }
                        while(i==CodeTotalNumber)
                        goto OK;
                        RecDataTimer();
                        if(TimerOut==1)
                        { jmpLDROM=0;
                          goto ERROR; }

                        i++;
                }
        }
        OK: ;
        ERROR: ;
        ri=0;
        tr0=0;
}



/************************************************/
void main(void)
{
        InitialPowerON();
        jmpLDROM=0;

        while(1)
        {
        if(jmpLDROM==1)
                {
                        es=0;
                        ti=0;
                        sbuf=0x00;             //Command OK Code
                        while(ti==0)
                        { ; }
                        ti=0;
                        es=1;
                        ChangeAPtoLD();
                }
        }
}





/************************************************/
void InitialPowerON(void)
{
        turbo_CKCON=0x01;

        ie=0x00;
        et0=1;          // timer0 interrupt enable
        es=1;           // RS-232 interrupt enable
        ea=1;           // all interrupt enable

        Timer2BaudRate();
}

/************************************************/
void Smode0BaudRate(void)
{
        unsigned long i;

        scon=0x50;      // mode=mode1 ,ren=enable
        t2con=0x00;
        tmod=tmod|0x20;      // Timer1=auto-raload
        pcon=pcon&0x7f;

        th1=256-(Fosc/(BaudRate*384));

        i=(((Fosc*10)/(BaudRate*384))-((Fosc/(BaudRate*394))*10));
        if(i>6)
        {
                th1=th1-1;
        }

        tl1=th1;
        tr1=1;
}



/************************************************/
void Smode1BaudRate(void)
{
        unsigned long i;

        scon=0x50;      // mode=mode1 ,ren=enable
        t2con=0x00;
        tmod=tmod|0x20;      // Timer1=auto-raload
        pcon=pcon|0x80;

        th1=256-(Fosc/(BaudRate*192));

        i=(((Fosc*10)/(BaudRate*192))-((Fosc/(BaudRate*192))*10));
        if(i>6)
        {
                th1=th1-1;
        }

        tl1=th1;
        tr1=1;
}



/************************************************/
void Timer2BaudRate(void)
{
        unsigned long i;

        scon=0x50;      // mode=mode1 ,ren=enable
        t2con=0x30;     // Timer2=BaudRate CLK

        rcap2h=(65536-(Fosc/(BaudRate*32)))/256;
        rcap2l=65536-(Fosc/(BaudRate*32));

        i=(((Fosc*10)/(BaudRate*32))-((Fosc/(BaudRate*32))*10));
        if(i>7)
        {
                rcap2l=rcap2l-1;

                if(rcap2l==0xff)
                {
                        rcap2h=rcap2h-1;
                }
        }

        th2=rcap2h;
        tl2=rcap2l;
        tr2=1;
}



/************************************************/
void RecDataTimer(void)
{
        unsigned int delay;
        ri=0;
        TimerOut=0;

        tmod=0x11;
        tr0=1;
        for(delay=50; delay>0; delay--)
        {
                th0=(65536-(1000*(Fosc/12000000)))/256; // delay x 1ms
                tl0=65536-(1000*(Fosc/12000000));       // delay x 1ms
                for(tf0=0; tf0==0; )
                {
                        while(ri==1)
                        { goto OK ; }
                }
        }
        TimerOut=1;
        OK: ;
}


/************************************************/
void ChangeAPtoLD(void)
{
        CHPENR=0x87;         // write enable
        CHPENR=0x59;         // write enable
        CHPCON=0x03;         // bank=4k,flash mode
        tl0=0xfe;
        th0=0xff;
        tr0=1;
        pcon=pcon|0x01;           // enter idle mode
        ;
        ;
        ;
                turbo_TA=0xaa;          // write enable
                turbo_TA=0x55;          // write enable
                turbo_CHPCON=0x03;      // bank=4k,flash mode
                tl0=0xfe;
                th0=0xff;
                tr0=1;
                pcon=pcon|0x01;              // enter idle mode
        ;
        ;
        ;
}



⌨️ 快捷键说明

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