STC32G12K128串口初始化后首字节发送乱码
本帖最后由 wj66 于 2023-7-6 10:19 编辑STC32G12K128串口程序初始化后首字节发送乱码,用的是官方的示例,串口3发送的。
//========================================================================
// 主时钟定义
//========================================================================
//#define MAIN_Fosc 22118400L //定义主时钟
//#define MAIN_Fosc 12000000L //定义主时钟
#define MAIN_Fosc 11059200L //定义主时钟
//#define MAIN_Fosc 5529600L //定义主时钟
//#define MAIN_Fosc 24000000L //定义主时钟
/******************* IO配置函数 *******************/
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; //指定要初始化的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_P5,&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_Timer3; //选择波特率发生器, BRT_Timer3, BRT_Timer2 (注意: 串口2固定使用BRT_Timer2)
COMx_InitStructure.UART_BaudRate= 9600ul; //波特率, 一般 110 ~ 115200
COMx_InitStructure.UART_RxEnable= ENABLE; //接收允许, ENABLE或DISABLE
UART_Configuration(UART3, &COMx_InitStructure); //初始化串口1 UART1,UART2,UART3,UART4
NVIC_UART3_Init(ENABLE,Priority_1); //中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
UART3_SW(UART3_SW_P50_P51); //UART3_SW_P00_P01,UART3_SW_P50_P51
}
/**********************************************/
void main(void)
{
u8 i;
WTST = 0; //设置程序指令延时参数,赋值为0可将CPU执行指令的速度设置为最快
EAXSFR(); //扩展SFR(XFR)访问使能
CKCON = 0; //提高访问XRAM速度
GPIO_config();
UART_config();
EA = 1;
PrintString3("STC32G UART3 Test Programme!\r\n"); //UART3发送一个字符串
while (1)
{
delay_ms(10000);
// if(COM3.RX_TimeOut > 0) //超时计数
// {
// if(--COM3.RX_TimeOut == 0)
// {
// if(COM3.RX_Cnt > 0)
// {
// for(i=0; i<COM3.RX_Cnt; i++) TX3_write2buff(RX3_Buffer); //收到的数据原样返回
// }
// COM3.RX_Cnt = 0;
// }
// }
PrintString3("STC32G UART3 Test Programme!\r\n"); //UART3发送一个字符串
}
}
串口收到的字节如下:
*胀2G UART3 Test Programme!
STC32G UART3 Test Programme!
00 2A D5 CD 32 47 20 55 41 52 54 33 20 54 65 73 74 20 50 72 6F 67 72 61 6D 6D 65 21 0D 0A
53 54 43 33 32 47 20 55 41 52 54 33 20 54 65 73 74 20 50 72 6F 67 72 61 6D 6D 65 21 0D 0A
前面收到的4个字节都是乱码,后面就一直正常了。。。
这是上电时电源抖动造成的
你可以在代码中的这里家延时等待一下
页:
[1]