问答

汇集网友智慧,解决技术难题

21ic问答首页 - STC8G 串口2无法发送数据

串口 stc8g IO UART pi GPIO

STC8G 串口2无法发送数据

sdlo772021-09-21
使用官方测试实例,串口2可以i接收数据,发送数据端口检测不到。程序如下:
/******************* IO配置函数 *******************/
void        GPIO_config(void)
{
        GPIO_InitTypeDef        GPIO_InitStructure;                //结构定义

        GPIO_InitStructure.Pin  = GPIO_Pin_1;//GPIO_Pin_6 | GPIO_Pin_7;                //指定要初始化的IO, GPIO_Pin_0 ~ GPIO_Pin_7
        GPIO_InitStructure.Mode = GPIO_PullUp;        //指定IO的输入或输出方式,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
        GPIO_Inilize(GPIO_P1,&GPIO_InitStructure);        //初始化
}

/***************  串口初始化函数 *****************/
void        UART_config(void)
{
        COMx_InitDefine                COMx_InitStructure;                                        //结构定义
        COMx_InitStructure.UART_Mode      = UART_8bit_BRTx;                //模式,   UART_ShiftRight,UART_8bit_BRTx,UART_9bit,UART_9bit_BRTx
        COMx_InitStructure.UART_BaudRate  = 115200ul;//57600ul;                        //波特率,     110 ~ 115200
        COMx_InitStructure.UART_RxEnable  = ENABLE;                                //接收允许,   ENABLE或DISABLE
        COMx_InitStructure.UART_Interrupt = ENABLE;                                //中断允许,   ENABLE或DISABLE
        COMx_InitStructure.UART_Priority    = Priority_0;                        //指定中断优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
        COMx_InitStructure.UART_P_SW = UART2_SW_P10_P11;//UART2_SW_P46_P47;        //切换端口,   UART2_SW_P10_P11,UART2_SW_P46_P47
        UART_Configuration(UART2, &COMx_InitStructure);                //初始化串口2 UART1,UART2,UART3,UART4

        PrintString2("STC8 UART2 Test Programme!\r\n");        //UART2发送一个字符串
}

/**********************************************/
void main(void)
{
        u8        i;

        GPIO_config();
        UART_config();
        EA = 1;

        while (1)
        {
                delay_ms(1);
                if(COM2.RX_TimeOut > 0)                //超时计数
                {
                        if(--COM2.RX_TimeOut == 0)
                        {
                                if(COM2.RX_Cnt > 0)
                                {
                                        for(i=0; i<COM2.RX_Cnt; i++)        TX2_write2buff(RX2_Buffer[i]);        //收到的数据原样返回
                                }
                                COM2.RX_Cnt = 0;
                        }
                }
        }
}


回答 +关注 10
14266人浏览 11人回答问题 分享 举报
11 个回答
12下一页

您需要登录后才可以回复 登录 | 注册