我用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);
|