夕口山关 发表于 2023-10-19 17:45:25

STC8H3K32S2串口3卡死在while(COM3.B_TX_busy);

代码如下
#include "config.h"
#include "STC8G_H_GPIO.h"
#include "STC8G_H_UART.h"
#include "STC8G_H_NVIC.h"
#include "STC8G_H_Delay.h"
#include "STC8G_H_Switch.h"

#define LED P32

void GPIO_config(void)
{
        GPIO_InitTypeDef        GPIO_InitStructure;                //结构定义
        GPIO_InitStructure.Pin= GPIO_Pin_0 | GPIO_Pin_1;                //指定要初始化的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_P0,&GPIO_InitStructure);        //初始化
       
        GPIO_InitStructure.Pin= GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;                //指定要初始化的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_P3,&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_BRT_Use   = BRT_Timer1;                        //选择波特率发生器, BRT_Timer1, BRT_Timer2 (注意: 串口2固定使用BRT_Timer2)
        COMx_InitStructure.UART_BaudRate= 115200ul;                        //波特率, 一般 110 ~ 115200
        COMx_InitStructure.UART_RxEnable= ENABLE;                                //接收允许,   ENABLE或DISABLE
        COMx_InitStructure.BaudRateDouble = DISABLE;                        //波特率加倍, ENABLE或DISABLE
        UART_Configuration(UART1, &COMx_InitStructure);                //初始化串口1 UART1,UART2,UART3,UART4
        NVIC_UART1_Init(ENABLE,Priority_1);                //中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
        UART1_SW(UART1_SW_P30_P31);                //UART1_SW_P30_P31,UART1_SW_P36_P37,UART1_SW_P16_P17,UART1_SW_P43_P44
       
        COMx_InitStructure.UART_Mode      = UART_8bit_BRTx;                //模式,   UART_ShiftRight,UART_8bit_BRTx,UART_9bit,UART_9bit_BRTx
        COMx_InitStructure.UART_BRT_Use   = BRT_Timer3;                        //选择波特率发生器,   BRT_Timer2, BRT_Timer3 (注意: 串口2固定使用BRT_Timer2)
        COMx_InitStructure.UART_BaudRate= 9600ul;                        //波特率,   110 ~ 115200
        COMx_InitStructure.UART_RxEnable= ENABLE;                                //接收允许,   ENABLE或DISABLE
        UART_Configuration(UART3, &COMx_InitStructure);                //初始化串口 UART1,UART2,UART3,UART4
        NVIC_UART3_Init(ENABLE,Priority_1);                //中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3

        UART3_SW(UART3_SW_P00_P01);                //UART3_SW_P00_P01,UART3_SW_P50_P51
}

int main(){
       
        EAXSFR();                /* 扩展寄存器访问使能 */
       
        GPIO_config();
        UART_config();
        EA = 1;
       
        while(1)
        {
                LED=0;
                delay_ms(500);
                LED=1;
                delay_ms(500);
                PrintString1("111\t");
                PrintString3("333\t");
               
        }
       
}

夕口山关 发表于 2023-10-19 17:46:35

注释掉PrintString3可以打印串口1 不注释就会卡死在while(COM3.B_TX_busy);

夕口山关 发表于 2023-10-19 17:52:04

以下是完整代码 求解 串口1可以用 串口3用不了

神农鼎 发表于 2023-10-19 18:36:13

用仿真功能仿,找错误就快多了


【新提醒】仿真STC8系列MCU,用【一箭双雕之USB转双串口/全自动烧录器】 - 仿真/ISP下载/做自己的ISP/编译器/头文件 - 国芯论坛-STC全球32位8051爱好者互助交流社区 - STC全球32位8051爱好者互助交流社区 (stcaimcu.com)

夕口山关 发表于 2023-10-19 19:04:52

找到问题了 这个芯片只有2个串口没有串口3
页: [1]
查看完整版本: STC8H3K32S2串口3卡死在while(COM3.B_TX_busy);