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

📄 core_vga.c

📁 嵌入式开发 嵌入式开发 嵌入式开发
💻 C
📖 第 1 页 / 共 5 页
字号:
    // 设置鼠标处理事务
    regs.x.ax=0x14;// 功能号20. 交换中断程序
    regs.x.cx=0x07;
        // 2007-4-5 9:37 Old is 1; 为1表示仅位置发生改变而触发中断服务
        // 增加了对 左按钮按下和松开的事件监控.
    regs.x.dx=FP_OFF(&MouseMaskProc);
    sregs.es=FP_SEG(&MouseMaskProc);
    int86x(0x33, &regs, &regs, &sregs);
    OldMask=regs.x.cx;
    OldProcSeg=sregs.es;
    OldProcOff=regs.x.dx;
}

/***
 Func: Release Mouse TSR part for this Application.
 Paramters:
    NULL
 Returns:
    NULL
***/
void ReleaseMouse(void)
{
    union REGS regs;
    struct SREGS sregs;
    regs.x.ax=0x14;
    regs.x.cx=OldMask;
    regs.x.dx=OldProcOff;
    sregs.es=OldProcSeg;
    int86x(0x33, &regs, &regs, &sregs);
    farfree(MouseImageBuffer);
}

/*
void ___CANFUNCs___(void)
*/

void interrupt (*old_isr0)(void);
void interrupt can_isr0(void);
/***
 Func: Get VECT of special IRQ
 Paramters:
    irq_nr : IRQ number
    Bvect : the variable for save VECT of (irq_nr); {be return value}
 Returns:
    NULL
***/
void GetVectIRQ(int irq_nr, unsigned char *Bvect)
{
    unsigned char VECT_TABLE[] = {
        0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
        0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77
    };
    *Bvect=VECT_TABLE[irq_nr];
}

/***
 Func: for time delay by program-command
 Paramters:
    tms : the delay max counter.
 Returns:
    NULL
***/
void wait(const int tms)
{
    int ttms=0;
    while( (ttms++)<tms );
}

/***
 Func: write special register of A/D&CAN
 Parameters:
    addr: address of register's number.
    v : value of you wanna set this register.
 Returns:
    NULL
***/
void can0w(unsigned char addr, unsigned char v)
{
    outportb(0x300, addr); wait(10);
    outportb(0x301, v);
}

/***
 Func: read special register of A/D&CAN
 Parameters:
    addr: address of register's number.
 Returns:
    the register's value.
***/
unsigned char can0r(unsigned char addr)
{
    unsigned char Bret;
    outportb(0x300, addr); wait(10);
    Bret=inportb(0x301);
    return Bret;
}

/***
 Func: CAN Initialization.
 Parameters:
    irq_nr : IRQ of hardware.
 Returns:
    1=successful; 0=failed.
***/
int CAN_Init(int irq_nr)
{
    unsigned char B,vect;
    B=can0r(0);
    if( B&0x01 )
    {
        can0w(0, 0x60);
        if( can0r(0)&0x01 )
        {   return 0;}
    }
    can0w(0, 0x01);
    if( !(can0r(0)&0x01) )
    {   return 0;}
    if( !(irq_nr==10 || irq_nr==11 || irq_nr==12 || irq_nr==14 || irq_nr==15) )
    {   return 0;}
    GetVectIRQ(irq_nr, &vect);
    outportb(0x21, 0xff);
    disable();
    old_isr0=getvect(vect);
    setvect(vect, can_isr0);
    outportb(0x20, 0x20);
    enable();
    outportb(0x21, 0x00);
    outportb(0xA1, 0x01);
    MsgCount=0;
    rMsgCount=0;
    memset(BuffCAN, 0, (int)sizeof(CAN_MSG)*200);
    return 1;
}

/***
 Func: CAN Release.
 Parameters:
    irq_nr : IRQ of hardware.
 Returns:
    NULL
***/
void CAN_Exit(int irq_nr)
{
    unsigned char B,Bvect;
    B=inportb(0x21);
    outportb(0x21, 0xff);
    disable();
    GetVectIRQ(irq_nr, &Bvect);
    setvect(Bvect, old_isr0);
    enable();
    outportb(0x21, B);
}

/***
 Func: CAN StartRunning.
 Parameters:
    NULL
 Returns:
    NULL
***/
void CAN_Run(void)
{
    unsigned char B;
    B=can0r(0);
    if( B&0x01 )
    {   can0w(0, (B&0xfe));}
}

/***
 Func: Config CAN
 Parameters:
    NULL
 Returns:
    1=successful; 0=failed.
***/
int CAN_Config(void)
{
    unsigned char B;
    can0w(0, 1); // 复位
    can0w(0, 0x3);//
    can0w(6, 0xC3);
    can0w(7, 0xC9);
    B=can0r(6);
    if( B!=0xC3 )
    {   return 0;}
    B=can0r(7);
    if( B!=0xC9 )
    {   return 0;}
    can0w(4, 0x00);
    can0w(5, 0xff);
    can0w(8, 0xfa);
    return 1;
}

/***
 Func: CAN - TSR
 Parameters:
    NULL
 Returns:
    NULL
***/
void interrupt can_isr0(void)
{
    int t,j;
    unsigned char B0,B1;
    disable();
    
    B0=can0r(3);
    if( B0&0x01 )
    {   // rif
        do {
            BuffCAN[MsgCount].Buffer[0]=can0r(20);
            BuffCAN[MsgCount].Buffer[1]=can0r(21);
            j=BuffCAN[MsgCount].Buffer[1]&0x0F;
            for(t=0; t<j; t++)
            {   BuffCAN[MsgCount].Buffer[t+2]=can0r(22+t);}
            can0w(1, 4);
            BuffCAN[MsgCount].Bflag=1;
            if( ++MsgCount==200 ) {   MsgCount=0;}
            B1=0;//can0r(2);//2007-5-9 12:01:42 为解决飞点B1=0.
        } while(B1&0x01);
    }
    outportb(0x20, 0x20);
    outportb(0xA0, 0x20);
    enable();
}

/***
 Func: CAN message receive. (Query mode)
 Paramters:
    msg_ptr : (type see MSG_STRUCT define).
 Returns:
    1=has message received; 0=no message received.
***/
int CAN_RecvMsg(MSG_STRUCT *msg_ptr)
{
    int t;
    
    if( BuffCAN[rMsgCount].Bflag )
    {
        msg_ptr->id = BuffCAN[rMsgCount].Buffer[0];
        msg_ptr->id<<=3;
        msg_ptr->id|=(BuffCAN[rMsgCount].Buffer[1]>>5);
        msg_ptr->rtr=(BuffCAN[rMsgCount].Buffer[1]>>4)&0x01;
        msg_ptr->dlen=(BuffCAN[rMsgCount].Buffer[1]&0x0F);
        for(t=0; t<(msg_ptr->dlen); t++)
        {   msg_ptr->data[t] = BuffCAN[rMsgCount].Buffer[t+2];}
        BuffCAN[rMsgCount].Bflag=0;
        memset(BuffCAN[rMsgCount].Buffer, 0, 10);
        if( ++rMsgCount==200 ) {   rMsgCount=0;}
        return 1;
    }
    return 0;
}

/***
 Func: CAN message send. (synchronize-mode)
 Paramters:
    msg : (type see MSG_STRUCT define).
 Returns:
    NULL
***/
void CAN_SendMsg(MSG_STRUCT msg)
{
    int t;
    unsigned char B;
    while( !(can0r(2)&0x04) ); // TBS=0 锁定 有信息等待发送或正在发送
    B=msg.id>>3;
    can0w(10, B);
    B=msg.id&0x07;
    B<<=5;
    if( msg.rtr )
    {   B|=0x10;}
    B+=msg.dlen;
    can0w(11, B);
    for(t=0; t<msg.dlen; t++)
    {   can0w(12+t, msg.data[t]);}
    can0w(1, 1); // TR=1; 置发送请求
    while(1)
    {
        B=can0r(2);
        if( B&0x40 )
        {   // ES=1; 出错;至少出现一个错误计数器满或者超过sja1000的报警限制
            delay(30);
            can0w(0, 1); // RR=1 复位请求 中止发送/接收信息 进入复位模式
            delay(20);
            can0w(0x00, 0xfe); //  1111 1110
                /*
                CR.7 ; 保留1
                CR.6 ; 保留2
                CR.5 ; 保留3
                CR.4 ; OIE 溢出中断使能
                CR.3 ; EIE 错误中断使能
                CR.2 ; TIE 发送中断使能
                CR.1 ; RIE 接收中断使能
                CR.0 ; sja1000收到一个下降沿后,进入工作模式
                */
            B=can0r(2);
            break;
        }
        if( B&0x0C ) // TCS&TBS 发送完毕 和 发送缓冲区状态释放
        {   break;}
    }
}

/*
void ___INTERFACE___(void)
*/

/***
 Func: Get Length of String buffer.
 Paramters:
    buff : string buffer.
 Returns:
    length of string buffer. (engligh is 1/1; chinese is 1/2)
***/
int GetBuffLen(BYTE *buff)
{
    int rLen=0;
    BYTE Q,W;
    while( *buff )
    {
        Q=*buff++;
        W=*buff;
        if( (Q>=0xA1) && (W>=0xA1) )
        {   buff++; rLen++;}
        else
        {
            if( (Q>=0x20) && (Q<0x7b) )
            {   rLen++;}
        }
        if( !W ) { break;}
    }
    return (rLen);
}

/***
 Func: show english font on screen. (0816)
 Parameters:
    x,y : start point coordinates
    ptr : the content of you wanna display.
    r,g,b : color of font.
 Returns:
    NULL
***/
void ShowASCMem(int x, int y, char *ptr, int r, int g, int b)
{
    int xfrom=x,yfrom=y,t,k;
    unsigned char B,buffer[15];
    while( *ptr )
    {
        B=*ptr++;
        if( B>0x20 && B<0x7B )
        {
            memcpy(buffer, LibraryASC+(B-0x21)*15L, 15);
            for(t=0; t<15; t++)
            {
                for(k=0; k<8; k++)
                {
                    if( buffer[t]&(0x80>>k) )
                    {   PutPixel(xfrom+k, yfrom+t, r, g, b);}
                }
            }
            xfrom+=8;
        }
        else if( B==0x20 ) {   xfrom+=8;}
    }
}

/***
放大显示内存字库中的半角字体
(主要应用于英文字母和数字显示)
size=(8*hX)*(15*vX).
***/
void ShowASCMemX(int x, int y, int hX, int vX, char *ptr, int r, int g, int b)
{
    int hLoop,vLoop;
    int xNew,yNew,yLoop,dLoop;
    unsigned char tChar,buffer[15];
    xNew=x;
    yNew=y;
    while(*ptr)
    {
        tChar=*ptr++;
        if(tChar>0x20 && tChar<0x7b)
        {
            memcpy(buffer, LibraryASC+(tChar-0x21)*15, 15);
            for(yLoop=0; yLoop<15; yLoop++)
            {
                for(dLoop=0; dLoop<8; dLoop++)
                {
                    if(buffer[yLoop]&(0x80>>dLoop))
                    {
                        for(hLoop=0; hLoop<hX; hLoop++)
                        {
                            for(vLoop=0; vLoop<vX; vLoop++)
                            {   PutPixel(xNew+hLoop+dLoop*hX, yNew+vLoop+yLoop*vX, r, g, b);}
                        }
                    }
                }
            }
            xNew+=8*hX;
        }
        else if(tChar==0x20)
        {   xNew+=8*hX;}
        else if(tChar=='\r' || tChar=='\n')
        {   xNew=x; yNew+=16*vX;}
        else if(tChar=='\t')
        {   xNew+=16*hX;}
    }
}

/***
显示字库文件中的全角字体
(主要应用于中文和全角表示的英文字母和数字显示)
size=16*16.
***/
void ShowGB2312(int x, int y, char *ptr, int r, int g, int b)
{
    long fOffset=0L;
    int xfrom=x,yfrom=y,t,k,m=0;
    unsigned char Bw,Bq,buffer[32]={0};
    while( *ptr )
    {
        Bq=*ptr++;
        Bw=*ptr;
        if( Bq>=0xa1 && Bw>=0xa1 )
        {   // 全角字
            ptr++; Bq-=0xa1; Bw-=0xa1;
            fOffset = Bq*94L+Bw;
            fOffset<<=5; // fOffset*=32;
            fseek(fpGB16, fOffset, SEEK_SET);
            fread(buffer, 32L, 1, fpGB16);
            for(t=0,m=0; t<16; t++)
            {
                for(k=0; k<8; k++)
                {
                    if( buffer[m]&(0x80>>k) )
                    {   PutPixel(xfrom+k, yfrom+t, r, g, b);}
                }
                m++;
                for(k=0; k<8; k++)
                {
                    if( buffer[m]&(0x80>>k) )
                    {   PutPixel(xfrom+8+k, yfrom+t, r, g, b);}
                }
                m++;
            }
            xfrom+=16;
        }
        else

⌨️ 快捷键说明

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