这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 综合技术 » 基础知识 » IO 求助!用IO口模拟串口!

共2条 1/1 1 跳转至

IO 求助!用IO口模拟串口!

院士
2006-09-17 18:14:16     打赏
IO 求助!用IO口模拟串口!



关键词: 求助     模拟     串口    

院士
2006-12-22 22:43:00     打赏
2楼
问 哪位用ARM做过用IO口模拟过串口中!
怎么做啊, 1: 我原来做过一个原来用51做过一个,不过只有发送没有接收
不过加上接收也不难
需要一个外部中断
你要的话可以传给你一个
eaivip@hotmail.com 2: 使用定时器匹配输出!挺好用!定时器的管脚组TXD,以前我只做了个发送。
原理:
波特率决定定时器的定时长度。
软件中,判断下一个位是1 or 0,然后设置匹配时的输出即可! 3: 两个不够用?不如用硬件来实现好。 4: 我也在LPC2114上用过用的话给你传一个
  我的是zhangjj_0225@163.com 5: 用LPC3132做了个16串口的通信程序
//定时器 26us(9600bps)或416us(1200bps)
void Timer1(void)
{
    static unsigned char ucTimer1Loop=0;

    Rev485Byte16Comm(ucTimer1Loop);
    if(ucTimer1Loop==0)
        Send485Byte16Comm();

    ucTimer1Loop++;
    if(ucTimer1Loop>=3)
        ucTimer1Loop=0;

}


//接收一个字节
void Rev485Byte(unsigned char ucTimeNum,unsigned char ucStart485Pos,unsigned char uc485Num)
{
    unsigned char i;
    //当有数据发送时,关闭接收功能
    uc485Num+=ucStart485Pos;
    if((str485SendByte.ucSendByteFlag[ucStart485Pos]==EMPTY_IRQ_FALG)
        &&(str485send[ucStart485Pos].uc485Send_Tail_P==str485send[ucStart485Pos].uc485Send_Head_P))
    {
        for(i=ucStart485Pos;i<uc485Num;i++)
        {
            if(str485RevByte.ucTime[i]==NO_REV_FLAG&&str485RevByte.ucPinFlag[i]==0)
            {
                str485RevByte.ucTime[i]=ucTimeNum+1;
                if(str485RevByte.ucTime[i]>=3)
                    str485RevByte.ucTime[i]=0;
            }
            else if(str485RevByte.ucTime[i]==ucTimeNum)
            {

                str485RevByte.ucBitPos[i]++;

                if(str485RevByte.ucBitPos[i]<2)
                {
                    if(str485RevByte.ucPinFlag[i]!=0)
                    {
                        str485RevByte.ucTime[i]=NO_REV_FLAG;
                        str485RevByte.ucBitPos[i]=0;
                    }
                }
                else if(str485RevByte.ucBitPos[i]<10)
                {
                    str485RevByte.ucRevByte[i]>>=1;
                    
                    if(str485RevByte.ucPinFlag[i])
                        str485RevByte.ucRevByte[i]|=0x80;
                    
                }
                else if(str485RevByte.ucBitPos[i]>=11)
                {
                    if(str485RevByte.ucPinFlag[i])
                    {
                        str485rev[i].uc485RevBuff[str485rev][i].uc485Rev_Head_P]=str485RevByte.ucRevByte[i];
                        str485rev[i].uc485Rev_Head_P+=1;

                        if(str485rev[i].uc485Rev_Head_P>=Maxn485RevLen)
                            str485rev[i].uc485Rev_Head_P=0;
                    }
                    //    str485RevByte.ucRevByteFlag[i]=RET_SUCCESS;
                    
                    str485RevByte.ucTime[i]=NO_REV_FLAG;
                    str485RevByte.ucBitPos[i]=0;
                }
                
            }
        }
    }    
}

//发送一个字节
void Send485Byte(unsigned char ucStart485Pos,unsigned char uc485Num)
{            
    unsigned char i,ucTemp;
    uc485Num+=ucStart485Pos;
    for(i=ucStart485Pos;i<uc485Num;i++)
    {
        //将要发送的字节送入发送寄存器
        if((str485SendByte.ucSendByteFlag[i]==EMPTY_IRQ_FALG)&&(str485send[i].uc485Send_Tail_P<str485send[i].uc485Send_Head_P))
        {
            ucTemp=str485send[i].uc485SendBuff[str485send][i].uc485Send_Tail_P];
            str485SendByte.usSendByte[i]=(unsigned short)ucTemp;
            if(EvenOdd(ucTemp))
                str485SendByte.usSendByte[i]|=0x0100;
            str485SendByte.ucSendByteFlag[i]=HAVE_IRQ_FALG;
            str485send[i].uc485Send_Tail_P++;
        }

        //发一10位数据
        str485SendByte.ucPinFlag[i]=1;
        if(str485SendByte.ucSendByteFlag[i]==HAVE_IRQ_FALG)
        {
            if(str485SendByte.ucBitPos[i]==NO_SEND_FLAG)
            {
                str485SendByte.ucBitPos[i]=0;
                str485SendByte.ucPinFlag[i]=0;
            }
            else if(str485SendByte.ucBitPos[i]<9)
            {
                if((str485SendByte.usSendByte[i]&0x0001)==0x0000)
                    str485SendByte.ucPinFlag[i]=0;
                str485SendByte.usSendByte[i]>>=1;
                str485SendByte.ucBitPos[i]++;
            }
            else
            {
                str485SendByte.ucPinFlag[i]=1;
                str485SendByte.ucBitPos[i]=NO_SEND_FLAG;
                str485SendByte.ucSendByteFlag[i]=EMPTY_IRQ_FALG;
            }
        }
    }    
    
}

//奇偶校验
unsigned char EvenOdd(unsigned char ucTemp)
{
    unsigned char ucRet;
    ucRet=0;
    if(table_odd_even_bit[ucTemp&0x0F])
        ucRet=1;

    if(table_odd_even_bit[ucTemp>>4])
        ucRet++;
    ucRet&=0x01;
    return ucRet;
}

///////////////////////////////////
//从1,2路接收一个字节
void Rev485Byte16Comm(unsigned char ucTimeNum)
{     
    unsigned int  uiPIN0Value,uiPIN1Value;

    uiPIN0Value = IO0PIN;
    uiPIN1Value = IO1PIN;

    if(uiPIN0Value&PIN_RX1)
        str485RevByte.ucPinFlag[0]=1;
    else
        str485RevByte.ucPinFlag[0]=0;

    if(    uiPIN1Value &PIN_RX2)
        str485RevByte.ucPinFlag[1]=1;
    else
        str485RevByte.ucPinFlag[1]=0;


//从3,4路接收一个字节

    if(uiPIN0Value&PIN0_RX3)
        str485RevByte.ucPinFlag[2]=1;
    else
        str485RevByte.ucPinFlag[2]=0;

    if(    uiPIN1Value &PIN1_RX4)
        str485RevByte.ucPinFlag[3]=1;
    else
        str485RevByte.ucPinFlag[3]=0;


//从5,6路接收一个字节

    if(uiPIN0Value&PIN0_RX5)
        str485RevByte.ucPinFlag[4]=1;
    else
        str485RevByte.ucPinFlag[4]=0;

    if(    uiPIN1Value &PIN1_RX6)
        str485RevByte.ucPinFlag[5]=1;
    else
        str485RevByte.ucPinFlag[5]=0;

//从7,8路接收一个字节
    if(uiPIN1Value&PIN1_RX7)
        str485RevByte.ucPinFlag[6]=1;
    else
        str485RevByte.ucPinFlag[6]=0;

    if(    uiPIN0Value &PIN0_RX8)
        str485RevByte.ucPinFlag[7]=1;
    else
        str485RevByte.ucPinFlag[7]=0;


//从9,10路接收一个字节

    if(uiPIN1Value&PIN1_RX9)
        str485RevByte.ucPinFlag[8]=1;
    else
        str485RevByte.ucPinFlag[8]=0;

    if(    uiPIN0Value &PIN0_RX10)
        str485RevByte.ucPinFlag[9]=1;
    else
        str485RevByte.ucPinFlag[9]=0;


//从11,12路接收一个字节

    if(uiPIN1Value&PIN1_RX11)
        str485RevByte.ucPinFlag[10]=1;
    else
        str485RevByte.ucPinFlag[10]=0;

    if(    uiPIN0Value &PIN0_RX12)
        str485RevByte.ucPinFlag[11]=1;
    else
        str485RevByte.ucPinFlag[11]=0;

//从13,14路接收一个字节

    if(uiPIN1Value&PIN1_RX13)
        str485RevByte.ucPinFlag[12]=1;
    else
        str485RevByte.ucPinFlag[12]=0;

    if(    uiPIN1Value &PIN1_RX14)
        str485RevByte.ucPinFlag[13]=1;
    else
        str485RevByte.ucPinFlag[13]=0;

//从15,16路接收一个字节

    if(uiPIN0Value&PIN0_RX15)
        str485RevByte.ucPinFlag[14]=1;
    else
        str485RevByte.ucPinFlag[14]=0;

    if(    uiPIN1Value &PIN1_RX16)
        str485RevByte.ucPinFlag[15]=1;
    else
        str485RevByte.ucPinFlag[15]=0;

    Rev485Byte(ucTimeNum,0,16);
}
//向485发送一个字节
void Send485Byte16Comm(void)
{
        Send485Byte(0,16);

        if(str485SendByte.ucPinFlag[0]==0)
            IO0CLR  = PIN_TX1;                    
        else
            IO0SET =PIN_TX1;

        if(str485SendByte.ucPinFlag[1]==0)
            IO1CLR  = PIN_TX2;                    
        else
            IO1SET =PIN_TX2;

//向3,4路485发送一个字节

        if(str485SendByte.ucPinFlag[2]==0)
            IO0CLR  = PIN0_TX3;                    
        else
            IO0SET =PIN0_TX3;

        if(str485SendByte.ucPinFlag[3]==0)
            IO0CLR  = PIN0_TX4;                    
        else
            IO0SET =PIN0_TX4;


//向5,6路485发送一个字节

        if(str485SendByte.ucPinFlag[4]==0)
            IO0CLR  = PIN0_TX5;                    
        else
            IO0SET =PIN0_TX5;

        if(str485SendByte.ucPinFlag[5]==0)
            IO0CLR  = PIN0_TX6;                    
        else
            IO0SET =PIN0_TX6;


//向7,7路485发送一个字节

        if(str485SendByte.ucPinFlag[6]==0)
            IO0CLR  = PIN0_TX7;                    
        else
            IO0SET =PIN0_TX7;

        if(str485SendByte.ucPinFlag[7]==0)
            IO0CLR  = PIN0_TX8;                    
        else
            IO0SET =PIN0_TX8;

//从9,10路接收一个字节

        if(str485SendByte.ucPinFlag[8]==0)
            IO0CLR  = PIN0_TX9;                    
        else
            IO0SET =PIN0_TX9;

        if(str485SendByte.ucPinFlag[9]==0)
            IO0CLR  = PIN0_TX10;                    
        else
            IO0SET =PIN0_TX10;

//向11,12路485发送一个字节
        if(str485SendByte.ucPinFlag[10]==0)
            IO0CLR  = PIN0_TX11;                    
        else
            IO0SET =PIN0_TX11;

        if(str485SendByte.ucPinFlag[11]==0)
            IO0CLR  = PIN0_TX12;                    
        else
            IO0SET =PIN0_TX12;
//向13,14路485发送一个字节
        if(str485SendByte.ucPinFlag[12]==0)
            IO0CLR  = PIN0_TX13;                    
        else
            IO0SET =PIN0_TX13;

        if(str485SendByte.ucPinFlag[13]==0)
            IO0CLR  = PIN0_TX14;                    
        else
            IO0SET =PIN0_TX14;

//向15,16路485发送一个字节

        if(str485SendByte.ucPinFlag[14]==0)
            IO0CLR  = PIN0_TX15;                    
        else
            IO0SET =PIN0_TX15;

        if(str485SendByte.ucPinFlag[15]==0)
            IO0CLR  = PIN0_TX16;                    
        else
            IO0SET =PIN0_TX16;
}


6: 好代码~~学习 7: 看看我的串口驱动,硬件的软件的扩展的,统一接口

共2条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]