标题:
STC8H单片机 uart接收和发送问题
[打印本页]
作者:
wanxiang
时间:
2023-10-26 14:31
标题:
STC8H单片机 uart接收和发送问题
我用stc8h官网的例程可以实现收发,然后我设置管脚转换,由于我实际使用的串口是P36,P37
但是我设置了串口的UART1_SW_P36_P37,发现单片机只有发送,没有接收,COM1.RX_Cnt里面的数据一直是0。
原管脚配置代码是
/******************* 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_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固定使用
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
}
复制代码
我修改了P36和P37的代码如下:
void GPIO_config(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //结构定义
GPIO_InitStructure.Pin = 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_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固定使用
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_P36_P37);
}
复制代码
上面配置我只修改了gpio里的GPIO_InitStructure.Pin = GPIO_Pin_6 | GPIO_Pin_7; 和 uart里面的 UART1_SW(UART1_SW_P36_P37);
10-通过串口1发送命令读写EEPROM测试程序.zip
2023-10-26 14:28 上传
点击文件名下载附件
389.34 KB, 下载次数: 5
官网下载的源码例程
作者:
56379075
时间:
2023-10-26 16:26
可能是UART_Configuration(UART1, &COMx_InitStructure); //初始化串口1 UART1,UART2,UART3,UART4。这句有问题,P36,P37属于串口2,你这个初始化哪个
作者:
wulin
时间:
2023-10-26 17:53
通过设置寄存器P_SW1,串口1切换
// P_SW1 = 0x00; //RXD/P3.0, TXD/P3.1
P_SW1 = 0x40; //RXD_2/P3.6, TXD_2/P3.7
// P_SW1 = 0x80; //RXD_3/P1.6, TXD_3/P1.7
// P_SW1 = 0xc0; //RXD_4/P4.3, TXD_4/P4.4
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1