lb0857 发表于 2022-1-16 08:54:32

stc15f系列,双串口不同波特率的设置 请教


/*---------------------------------------------------------------------*/
/* --- STC MCU Limited ------------------------------------------------*/
/* --- STC 1T Series MCU Demo Programme -------------------------------*/
/*---------------------------------------------------------------------*/

#include    "reg51.h"
#include    "intrins.h"

#define MAIN_Fosc       11059200L   //定义主时钟


typedef   unsigned char   u8;
typedef   unsigned int    u16;
typedef   unsigned long   u32;

sfr AUXR = 0x8E;
sfr S2CON = 0x9A;   //
sfr S2BUF = 0x9B;   //
sfr TH2= 0xD6;
sfr TL2= 0xD7;
sfr IE2   = 0xAF;   //STC12C5A60S2系列
sfr INT_CLKO = 0x8F;
sfr P_SW1 = 0xA2;
sfr P_SW2 = 0xBA;

sfr P1M1 = 0x91;    //PxM1.n,PxM0.n   =00--->Standard,    01--->push-pull
sfr P1M0 = 0x92;    //                  =10--->pure input,11--->open drain


#define   Baudrate1   115200UL
#define   Baudrate2   115200UL   //



/*************功能说明    **************

双串口全双工中断方式收发通讯程序。

通过PC向MCU发送数据, MCU收到后通过串口把收到的数据原样返回.

******************************************/


#define UART1_BUF_LENGTH    32
#define UART2_BUF_LENGTH    32



u8TX1_Cnt;    //发送计数
u8RX1_Cnt;    //接收计数
u8TX2_Cnt;    //发送计数
u8RX2_Cnt;    //接收计数
bit B_TX1_Busy; //发送忙标志
bit B_TX2_Busy; //发送忙标志

u8idata RX1_Buffer; //接收缓冲
u8idata RX2_Buffer; //接收缓冲


void    UART1_config(u8 brt);   // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率.
void    UART2_config(u8 brt);   // 选择波特率, 2: 使用Timer2做波特率, 其它值: 无效.
void    PrintString1(u8 *puts);
void    PrintString2(u8 *puts);



//========================================================================
// 函数: void main(void)
// 描述: 主函数。
// 参数: none.
// 返回: none.
// 版本: VER1.0
// 日期: 2014-11-28
// 备注:
//========================================================================
void main(void)
{
    P0M1 = 0;   P0M0 = 0;   //设置为准双向口
    P1M1 = 0;   P1M0 = 0;   //设置为准双向口
    P2M1 = 0;   P2M0 = 0;   //设置为准双向口
    P3M1 = 0;   P3M0 = 0;   //设置为准双向口
    P4M1 = 0;   P4M0 = 0;   //设置为准双向口
    P5M1 = 0;   P5M0 = 0;   //设置为准双向口
    P6M1 = 0;   P6M0 = 0;   //设置为准双向口
    P7M1 = 0;   P7M0 = 0;   //设置为准双向口

    UART1_config(1);    // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率.
    UART2_config(2);    // 选择波特率, 2: 使用Timer2做波特率, 其它值: 无效.
    EA = 1;             //允许全局中断

   
    PrintString1("STC15F2K60S2 UART1 Test Prgramme!\r\n");//SUART1发送一个字符串
    PrintString2("STC15F2K60S2 UART2 Test Prgramme!\r\n");//SUART2发送一个字符串

    while (1)
    {
      if((TX1_Cnt != RX1_Cnt) && (!B_TX1_Busy))   //收到数据, 发送空闲
      {
            SBUF = RX1_Buffer;
            B_TX1_Busy = 1;
            if(++TX1_Cnt >= UART1_BUF_LENGTH)   TX1_Cnt = 0;
      }

      if((TX2_Cnt != RX2_Cnt) && (!B_TX2_Busy))   //收到数据, 发送空闲
      {
            S2BUF = RX2_Buffer;
            B_TX2_Busy = 1;
            if(++TX2_Cnt >= UART2_BUF_LENGTH)   TX2_Cnt = 0;
      }
    }
}


//========================================================================
// 函数: void PrintString1(u8 *puts)
// 描述: 串口1发送字符串函数。
// 参数: puts:字符串指针.
// 返回: none.
// 版本: VER1.0
// 日期: 2014-11-28
// 备注:
//========================================================================
void PrintString1(u8 *puts)
{
    for (; *puts != 0;puts++)   //遇到停止符0结束
    {
      SBUF = *puts;
      B_TX1_Busy = 1;
      while(B_TX1_Busy);
    }
}

//========================================================================
// 函数: void PrintString2(u8 *puts)
// 描述: 串口2发送字符串函数。
// 参数: puts:字符串指针.
// 返回: none.
// 版本: VER1.0
// 日期: 2014-11-28
// 备注:
//========================================================================
void PrintString2(u8 *puts)
{
    for (; *puts != 0;puts++)   //遇到停止符0结束
    {
      S2BUF = *puts;
      B_TX2_Busy = 1;
      while(B_TX2_Busy);
    }
}

//========================================================================
// 函数: SetTimer2Baudraye(u16 dat)
// 描述: 设置Timer2做波特率发生器。
// 参数: dat: Timer2的重装值.
// 返回: none.
// 版本: VER1.0
// 日期: 2014-11-28
// 备注:
//========================================================================
void    SetTimer2Baudraye(u16 dat)
{
    AUXR &= ~(1<<4);    //Timer stop
    AUXR &= ~(1<<3);    //Timer2 set As Timer
    AUXR |=(1<<2);    //Timer2 set as 1T mode
    TH2 = dat / 256;
    TL2 = dat % 256;
    IE2&= ~(1<<2);    //禁止中断
    AUXR |=(1<<4);    //Timer run enable
}

//========================================================================
// 函数: void   UART1_config(u8 brt)
// 描述: UART1初始化函数。
// 参数: brt: 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率.
// 返回: none.
// 版本: VER1.0
// 日期: 2014-11-28
// 备注:
//========================================================================
void    UART1_config(u8 brt)
{
    /*********** 波特率使用定时器2 *****************/
    if(brt == 2)
    {
      AUXR |= 0x01;       //S1 BRT Use Timer2;
      SetTimer2Baudraye(65536UL - (MAIN_Fosc / 4) / Baudrate1);
    }

    /*********** 波特率使用定时器1 *****************/
    else
    {
      TR1 = 0;
      AUXR &= ~0x01;      //S1 BRT Use Timer1;
      AUXR |=(1<<6);    //Timer1 set as 1T mode
      TMOD &= ~(1<<6);    //Timer1 set As Timer
      TMOD &= ~0x30;      //Timer1_16bitAutoReload;
      TH1 = (u8)((65536UL - (MAIN_Fosc / 4) / Baudrate1) / 256);
      TL1 = (u8)((65536UL - (MAIN_Fosc / 4) / Baudrate1) % 256);
      ET1 = 0;    //禁止中断
      INT_CLKO &= ~0x02;//不输出时钟
      TR1= 1;
    }
    /*************************************************/

    SCON = (SCON & 0x3f) | 0x40;    //UART1模式, 0x00: 同步移位输出, 0x40: 8位数据,可变波特率, 0x80: 9位数据,固定波特率, 0xc0: 9位数据,可变波特率
//PS= 1;    //高优先级中断
    ES= 1;    //允许中断
    REN = 1;    //允许接收
    P_SW1 &= 0x3f;
    P_SW1 |= 0x80;      //UART1 switch to, 0x00: P3.0 P3.1, 0x40: P3.6 P3.7, 0x80: P1.6 P1.7 (必须使用内部时钟)
//PCON2 |=(1<<4);   //内部短路RXD与TXD, 做中继, ENABLE,DISABLE

    B_TX1_Busy = 0;
    TX1_Cnt = 0;
    RX1_Cnt = 0;
}


//========================================================================
// 函数: void   UART2_config(u8 brt)
// 描述: UART2初始化函数。
// 参数: brt: 选择波特率, 2: 使用Timer2做波特率, 其它值: 无效.
// 返回: none.
// 版本: VER1.0
// 日期: 2014-11-28
// 备注:
//========================================================================
void    UART2_config(u8 brt)    // 选择波特率, 2: 使用Timer2做波特率, 其它值: 无效.
{
    if(brt == 2)
    {
      SetTimer2Baudraye(65536UL - (MAIN_Fosc / 4) / Baudrate2);

      S2CON &= ~(1<<7);   // 8位数据, 1位起始位, 1位停止位, 无校验
      IE2   |= 1;         //允许中断
      S2CON |= (1<<4);    //允许接收
      P_SW2 &= ~0x01;
      P_SW2 |= 1;         //UART2 switch to: 0: P1.0 P1.1,1: P4.6 P4.7

      B_TX2_Busy = 0;
      TX2_Cnt = 0;
      RX2_Cnt = 0;
    }
}


//========================================================================
// 函数: void UART1_int (void) interrupt UART1_VECTOR
// 描述: UART1中断函数。
// 参数: nine.
// 返回: none.
// 版本: VER1.0
// 日期: 2014-11-28
// 备注:
//========================================================================
void UART1_int (void) interrupt 4
{
    if(RI)
    {
      RI = 0;
      RX1_Buffer = SBUF;
      if(++RX1_Cnt >= UART1_BUF_LENGTH)   RX1_Cnt = 0;
    }

    if(TI)
    {
      TI = 0;
      B_TX1_Busy = 0;
    }
}

//========================================================================
// 函数: void UART2_int (void) interrupt UART2_VECTOR
// 描述: UART2中断函数。
// 参数: nine.
// 返回: none.
// 版本: VER1.0
// 日期: 2014-11-28
// 备注:
//========================================================================
void UART2_int (void) interrupt 8
{
    if((S2CON & 1) != 0)
    {
      S2CON &= ~1;    //Clear Rx flag
      RX2_Buffer = S2BUF;
      if(++RX2_Cnt >= UART2_BUF_LENGTH)   RX2_Cnt = 0;
    }

    if((S2CON & 2) != 0)
    {
      S2CON &= ~2;    //Clear Tx flag
      B_TX2_Busy = 0;
    }

}


/*************************************************************************************/

上面是STC-isp自带例程程序.
现在需要
UART2      Baudrate2   9600UL
UART1   Baudrate1   115200UL
该如何在初始化中设置呢.

仅仅更改这里无效:
#define   Baudrate1   115200UL
#define   Baudrate2   9600UL   //

lb0857 发表于 2022-1-16 08:54:57

上面是STC-isp自带例程程序.
现在需要
UART2      Baudrate2   9600UL
UART1   Baudrate1   115200UL
该如何在初始化中设置呢.

仅仅更改这里无效:
#define   Baudrate1   115200UL
#define   Baudrate2   9600UL   //

qd118118 发表于 2022-1-16 09:52:00

使用2个定时器试试

lb0857 发表于 2022-1-16 10:47:15

qd118118 发表于 2022-1-16 09:52
使用2个定时器试试

好像还是不行
手册还在认真学习中

小李非刀 发表于 2022-1-17 20:33:51

这是STC官方例程,没问题的哦。

lb0857 发表于 2022-1-17 21:19:57

小李非刀 发表于 2022-1-17 20:33
这是STC官方例程,没问题的哦。

上面是STC-isp自带例程程序.
现在需要
UART2   波特率设置为9600
UART1   波特率设置为   115200
请假, 该如何在初始化中设置呢.

小李非刀 发表于 2022-1-18 23:39:36

STC官方例程:
/*************        功能说明        **************
请先别修改程序, 直接下载"UART1-UART2.hex"测试, 下载时选择主频22.1184MHZ.

2串口全双工中断方式收发通讯程序。

MCU上电后先发送一串字符串.

上位机通过串口向MCU发送数据, MCU收到后通过串口把收到的数据原样返回.

默认参数:
所有串口均设置均为 1位起始位, 8位数据位, 1位停止位, 无校验.
每个串口可以使用不同的波特率.
串口1(P3.0 P3.1): 115200bps.
串口2(P1.0 P1.1):57600bps.

******************************************/

#define MAIN_Fosc        22118400L        //定义主时钟

#include        "..\..\STC15Fxxxx.H"



/*************        本地常量声明        **************/
#define        RX1_Length        128                /* 接收缓冲长度 */
#define        RX2_Length        128                /* 接收缓冲长度 */


/*************        本地变量声明        **************/
u8        xdata        RX1_Buffer;        //接收缓冲
u8        xdata        RX2_Buffer;        //接收缓冲

u8        TX1_read,RX1_write;        //读写索引(指针).
u8        TX2_read,RX2_write;        //读写索引(指针).

bit        B_TX1_Busy,B_TX2_Busy;        // 发送忙标志


/*************        本地函数声明        **************/
void        UART1_config(u32 brt, u8 timer, u8 io);        // brt: 通信波特率,timer=2: 波特率使用定时器2, 其它值: 使用Timer1做波特率. io=0: 串口1切换到P3.0 P3.1,=1: 切换到P3.6 P3.7,=2: 切换到P1.6 P1.7,=3: 切换到P4.3 P4.4.
void        UART2_config(u32 brt, u8 timer, u8 io);        // brt: 通信波特率,timer=2: 波特率使用定时器2, 其它值: 使用Timer2做波特率. io=0: 串口2切换到P1.0 P1.1,=1: 切换到P4.6 P4.7.
void         PrintString1(u8 *puts);
void         PrintString2(u8 *puts);




//========================================================================
// 函数: void main(void)
// 描述: 主函数
// 参数: none.
// 返回: none.
// 版本: VER1.0
// 日期: 2018-4-2
// 备注:
//========================================================================
void main(void)
{
        u8        i;

        EAXRAM();

        UART1_config(115200UL, 1, 0);        // brt: 通信波特率,timer=2: 波特率使用定时器2, 其它值: 使用Timer1做波特率. io=0: 串口1切换到P3.0 P3.1,=1: 切换到P3.6 P3.7,=2: 切换到P1.6 P1.7,=3: 切换到P4.3 P4.4.
        UART2_config( 57600UL, 2, 0);        // brt: 通信波特率,timer=2: 波特率使用定时器2, 其它值: 使用Timer2做波特率. io=0: 串口2切换到P1.0 P1.1,=1: 切换到P4.6 P4.7.

        EA = 1;

        for(i=0; i<RX1_Length; i++)                RX1_Buffer = 0;
        B_TX1_Busy= 0;
        TX1_read    = 0;
        RX1_write   = 0;

        for(i=0; i<RX2_Length; i++)                RX2_Buffer = 0;
        B_TX2_Busy= 0;
        TX2_read    = 0;
        RX2_write   = 0;
       
        PrintString1("STC15xxxx UART1 Test Prgramme!\r\n");
        PrintString2("STC15xxxx UART2 Test Prgramme!\r\n");


        while (1)
        {

                if((TX1_read != RX1_write) && !B_TX1_Busy)        //收到过数据, 并且发送空闲
                {
                        B_TX1_Busy = 1;                //标志发送忙
                        SBUF = RX1_Buffer;        //发一个字节
                        if(++TX1_read >= RX1_Length)        TX1_read = 0;        //避免溢出处理
                }

                if((TX2_read != RX2_write) && !B_TX2_Busy)        //收到过数据, 并且发送空闲
                {
                        B_TX2_Busy = 1;                //标志发送忙
                        S2BUF = RX2_Buffer;        //发一个字节
                        if(++TX2_read >= RX2_Length)        TX2_read = 0;        //避免溢出处理
                }
        }
}


//========================================================================
// 函数: SetTimer2Baudraye(u16 dat)
// 描述: 设置Timer2做波特率发生器。
// 参数: dat: Timer2的重装值.
// 返回: none.
// 版本: VER1.0
// 日期: 2018-4-2
// 备注:
//========================================================================
void        SetTimer2Baudrate(u16 dat)        // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率.
{
        AUXR &= ~(1<<4);        //Timer stop
        AUXR &= ~(1<<3);        //Timer2 set As Timer
        AUXR |=(1<<2);        //Timer2 set as 1T mode
        TH2 = (u8)(dat >> 8);
        TL2 = (u8)dat;
        IE2&= ~(1<<2);        //禁止中断
        AUXR |=(1<<4);        //Timer run enable
}


//========================================================================
// 函数: void        UART1_config(u32 brt, u8 timer, u8 io)
// 描述: UART1初始化函数。
// 参数:   brt: 通信波特率.
//       timer: 波特率使用的定时器, timer=2: 波特率使用定时器2, 其它值: 使用Timer1做波特率.
//          io: 串口1切换到的IO,io=0: 串口1切换到P3.0 P3.1,=1: 切换到P3.6 P3.7,=2: 切换到P1.6 P1.7,=3: 切换到P4.3 P4.4.
// 返回: none.
// 版本: VER1.0
// 日期: 2018-4-2
// 备注:
//========================================================================
void        UART1_config(u32 brt, u8 timer, u8 io)        // brt: 通信波特率,timer=2: 波特率使用定时器2, 其它值: 使用Timer1做波特率. io=0: 串口1切换到P3.0 P3.1,=1: 切换到P3.6 P3.7,=2: 切换到P1.6 P1.7.
{
        brt = 65536UL - (MAIN_Fosc / 4) / brt;
        if(timer == 2)        //波特率使用定时器2
        {
                AUXR |= 0x01;                //S1 BRT Use Timer2;
                SetTimer2Baudrate((u16)brt);
        }

        else                //波特率使用定时器1
        {
                TR1 = 0;
                AUXR &= ~0x01;                //S1 BRT Use Timer1;
                AUXR |=(1<<6);        //Timer1 set as 1T mode
                TMOD &= ~(1<<6);        //Timer1 set As Timer
                TMOD &= ~0x30;                //Timer1_16bitAutoReload;
                TH1 = (u8)(brt >> 8);
                TL1 = (u8)brt;
                ET1 = 0;                        // 禁止Timer1中断
                INT_CLKO &= ~0x02;        // Timer1不输出高速时钟
                TR1= 1;                        // 运行Timer1
        }

        P_SW1 &= ~0xc0;
               if(io == 1)        {        P_SW1 |= 0x40;        P3n_standard(0xc0);        }        //UART1 使用P36 P37口
        else if(io == 2)        {        P_SW1 |= 0x80;        P1n_standard(0xc0);        }        //UART1 使用P16 P17口
        else                                {                                        P3n_standard(0x03);        }        //UART1 使用P30 P31口

        SCON = (SCON & 0x3f) | (1<<6);        // 8位数据, 1位起始位, 1位停止位, 无校验
//        PS= 1;        //高优先级中断
        ES= 1;        //允许中断
        REN = 1;        //允许接收
}


//========================================================================
// 函数: void        UART2_config(u32 brt, u8 timer, u8 io)
// 描述: UART2初始化函数。
// 参数:   brt: 通信波特率.
//       timer: 波特率使用的定时器, timer=2: 波特率使用定时器2, 其它值: 使用Timer2做波特率.
//          io: 串口2切换到的IO,io=0: 串口2切换到P1.0 P1.1,=1: 切换到P4.6 P4.7.
// 返回: none.
// 版本: VER1.0
// 日期: 2018-4-2
// 备注:
//========================================================================
void        UART2_config(u32 brt, u8 timer, u8 io)        // brt: 通信波特率,timer=2: 波特率使用定时器2, 其它值: 使用Timer2做波特率. io=0: 串口2切换到P1.0 P1.1,=1: 切换到P4.6 P4.7.
{
        brt = 65536UL - (MAIN_Fosc / 4) / brt;
        if(timer == 2)        SetTimer2Baudrate((u16)brt);        //波特率使用定时器2
        else                        SetTimer2Baudrate((u16)brt);        //波特率使用定时器2                两个条件都使用Timer2, 是为了跟另外串口函数兼容

        S2CON &= ~(1<<7);        // 8位数据, 1位起始位, 1位停止位, 无校验
        IE2   |= 1;                        //允许中断
        S2CON |= (1<<4);        //允许接收
        if(io == 1)        {        P_SW2 |= 1;                P4n_standard(0xc0);}        //切换到 P4.6 P4.7
        else                {        P_SW2 &= ~1;        P1n_standard(0x03);}        //切换到 P1.0 P1.1
}


//========================================================================
// 函数: void PrintString1(u8 *puts)
// 描述: 串口1字符串打印函数
// 参数: puts: 字符串指针.
// 返回: none.
// 版本: VER1.0
// 日期: 2018-4-2
// 备注:
//========================================================================
void PrintString1(u8 *puts)
{
    for (; *puts != 0;        puts++)
        {
                B_TX1_Busy = 1;                //标志发送忙
                SBUF = *puts;                //发一个字节
                while(B_TX1_Busy);        //等待发送完成
        }
}

//========================================================================
// 函数: void PrintString2(u8 *puts)
// 描述: 串口2字符串打印函数
// 参数: puts: 字符串指针.
// 返回: none.
// 版本: VER1.0
// 日期: 2018-4-2
// 备注:
//========================================================================
void PrintString2(u8 *puts)
{
    for (; *puts != 0;        puts++)
        {
                B_TX2_Busy = 1;                //标志发送忙
                S2BUF = *puts;                //发一个字节
                while(B_TX2_Busy);        //等待发送完成
        }
}


//========================================================================
// 函数: void UART1_int (void) interrupt UART1_VECTOR
// 描述: 串口1中断函数
// 参数: none.
// 返回: none.
// 版本: VER1.0
// 日期: 2018-4-2
// 备注:
//========================================================================
void UART1_int (void) interrupt UART1_VECTOR
{
        if(RI)
        {
                RI = 0;
                RX1_Buffer = SBUF;
                if(++RX1_write >= RX1_Length)        RX1_write = 0;
        }

        if(TI)
        {
                TI = 0;
                B_TX1_Busy = 0;
        }
}

//========================================================================
// 函数: void UART2_int (void) interrupt UART2_VECTOR
// 描述: 串口2中断函数
// 参数: none.
// 返回: none.
// 版本: VER1.0
// 日期: 2018-4-2
// 备注:
//========================================================================
void UART2_int (void) interrupt UART2_VECTOR
{
        if(RI2)
        {
                CLR_RI2();
                RX2_Buffer = S2BUF;
                if(++RX2_write >= RX2_Length)        RX2_write = 0;
        }

        if(TI2)
        {
                CLR_TI2();
                B_TX2_Busy = 0;
        }

}

lb0857 发表于 2022-1-19 08:47:35

谢谢楼上 {:handshake:}{:handshake:}
页: [1]
查看完整版本: stc15f系列,双串口不同波特率的设置 请教