51串口双机通信

个人用

#include <reg52.h>

sbit we=P2^7;
sbit du=P2^6;

unsigned char code table[] = {
0x3f , 0x06 , 0x5b , 0x4f,
0x66 , 0x6d , 0x7d , 0x07,
0x7f , 0x6f , 0x77 , 0x7c,
0x39 , 0x5e , 0x79 , 0x71,
0x00};

void show();
void delayms(unsigned time);
void matrixscan(unsigned char *row,unsigned char *col);

unsigned char dis_num[6]={0};
unsigned char rec_num=0;

void main()
{
    unsigned char row,col;

    TMOD=0x20;
    TH1=TL1=0xfd;
    TR1=1;
    
    SM0=0;
    SM1=1;
    REN=1;
    
    EA=1;
    ES=1;
    while(1)
    {
        //显示接收
        show();
        //发送按键
        matrixscan(&row,&col);
        if(row!=255)
        {
            SBUF=row;
            while(!TI);
            TI=0;
            SBUF=col;
            while(!TI);
            TI=0;
        }
    }
}

void serial() interrupt 4
{

    if(RI)
    {
        RI=0;
        if(rec_num==6)
            rec_num=0;
        dis_num[rec_num++]=SBUF;
    }
}    

void matrixscan(unsigned char *row,unsigned char *col)
{
    unsigned char i,j;
    unsigned char temp;
    
    *row=*col=255;
    for(i=0;i<4;i++)
    {
        P3=~(1<<i);        //第i+1行给低电平
        if((P3|0x0f)!=0xff)    //判断此时是否有列为低电平
        {                    //有则得到行列位置i+1,j+1
            delayms(10);
            if((temp=P3|0x0f)!=0xff)
            {
                while((P3|0x0f)!=0xff);
                for(j=0;j<4 && ( temp& 0x10<<j );j++)
                    ;
                *row=i+1;
                     *col=j+1;
                break;
            }
        }
    }
    P3=0xff;    //还原电平
}


void show()
{
    unsigned char i;
    
     for(i=0;i<6;i++)
     {
         P0=0xff;
         we=1;
         we=0;      

         P0=table[dis_num[(i+rec_num)%6]];
         du=1;
         du=0;

         P0=~(0x01<<i);
         we=1;
         we=0;

         delayms(1);
     }

}

void delayms(unsigned time)
{
    unsigned i,j;

    for(i=time;i>0;i--)
        for(j=110;j>0;j--)
        ;
}
自己
#include <reg52.h>
#include <stdio.h>

unsigned char keyscan();
void delayms(unsigned time);
void show();

unsigned char code table[] = {
0x3f , 0x06 , 0x5b , 0x4f,
0x66 , 0x6d , 0x7d , 0x07,
0x7f , 0x6f , 0x77 , 0x7c,
0x39 , 0x5e , 0x79 , 0x71,
0x00};

unsigned char dis_num[4]={0};
unsigned char rec_num=0;


void main()
{
    unsigned char num;

    SM0=0;
    SM1=1;

    TMOD=0x20;
    TH1=TL1=0xfd;
    TR1=1;

    EA=1;
    ES=1;
    REN=1;
    
    while(1)
    {

        //显示接收
        show();

        if(num=keyscan())
        {
            //改变led
            P2=~( 0x01<<(num-1) );    
            //发送
            SBUF=num;
            while(!TI);
            TI=0;
        }
    }
    
}

unsigned char keyscan()
{
    unsigned char temp,i;
    if((P3|0x03)!=0xff)
    {
        delayms(10);
        if((temp=P3|0x03)!=0xff)
        {                        
            while((P3|0x03)!=0xff);
        
            for(i=0;i<6 && (temp& 0x80>>i);i++)
                ;
            return i+1;
        }
    }
    return 0;
}

void delayms(unsigned time)
{
    unsigned i,j;

    for(i=time;i>0;i--)
        for(j=110;j>0;j--)
        ;
}

void show()
{
    unsigned char i;
    
     for(i=0;i<4;i++)
     {
         P1=0xff; 
         P0=~table[dis_num[(rec_num+i)%4]];
         P1=~(0x01<<i);

         delayms(1);
     }
}



void serial() interrupt 4
{
    if(RI)
    {
        RI=0;
        if(rec_num==4)
                rec_num=0;
        dis_num[rec_num++]=SBUF;
    }
}
实验室

 

posted @ 2014-03-06 09:33  ZackCoder  阅读(600)  评论(0编辑  收藏  举报