14
133
1149
金牌会员
使用道具 举报 送花
<div class="blockcode"><blockquote>#include <STC8H.h> #include <stdio.h> #include "intrins.h" //======================================================================== // 数据类型定义 //======================================================================== typedef unsigned char u8; // 8 bits typedef unsigned int u16; // 16 bits typedef unsigned long u32; // 32 bits typedef signed char int8; // 8 bits typedef signed int int16; // 16 bits typedef signed long int32; // 32 bits typedef unsigned char uint8; // 8 bits typedef unsigned int uint16; // 16 bits typedef unsigned long uint32; // 32 bits #define MAIN_Fosc 22118400L //定义主时钟 // 11059200UL #define UART3_BRT (65536 - MAIN_Fosc / 115200 / 4) #define UART4_BRT (65536 - MAIN_Fosc/ 115200 / 4) #define COM3_BUF_LENGTH 64 #define COM4_BUF_LENGTH 128 bit B_COM3_Tx_Busy; u16 COM3_Rx_Cnt; u16 COM3_Tx_Cnt; u8 xdata COM3_Rx_Buffer[COM3_BUF_LENGTH]; bit B_COM4_Tx_Busy; u16 COM4_Rx_Cnt; u16 COM4_Tx_Cnt; u8 xdata COM4_Rx_Buffer[COM4_BUF_LENGTH]; void UART3_Isr() interrupt 17 { if (S3CON & 0x02) { S3CON &= ~0x02; B_COM3_Tx_Busy = 0; } if (S3CON & 0x01) { S3CON &= ~0x01; COM3_Rx_Buffer[COM3_Rx_Cnt] = S3BUF; if(++COM3_Rx_Cnt >= COM3_BUF_LENGTH - 1) COM3_Rx_Cnt = 0; //防溢出 COM3_Rx_Buffer[COM3_Rx_Cnt] = 0; } } void Uart3_Init() { S3CON = 0x50; T3L = UART3_BRT; T3H = UART3_BRT >> 8; T4T3M |= 0x0A; IE2 |= 0x08; COM3_Rx_Cnt = 0; COM3_Tx_Cnt= 0; B_COM3_Tx_Busy = 0; } // -------- 串口3 发送字符串 ------------------------------------------- void COM3_PrintString(unsigned char *puts) //发送一个字符串 { for (; *puts != 0; puts++) //遇到停止符0结束 { S3BUF = *puts; B_COM3_Tx_Busy = 1; while(B_COM3_Tx_Busy); } } void Uart3_SendChar(char dat) { while (B_COM3_Tx_Busy); B_COM3_Tx_Busy = 1; S3BUF = dat; } void Uart3_SendStr(char *p) { while (*p) { Uart3_SendChar(*p++); } } void Uart4_Isr() interrupt 18 { if (S4CON & 0x02) // ((S4CON & 0x02) != 0) { S4CON &= ~0x02; B_COM4_Tx_Busy = 0; } if ((S4CON & 0x01) != 0) // (S4CON & 0x01) { S4CON &= ~0x01; COM4_Rx_Buffer[COM4_Rx_Cnt] = S4BUF; if(++COM4_Rx_Cnt >= COM4_BUF_LENGTH - 1) COM4_Rx_Cnt = 0; //防溢出 COM4_Rx_Buffer[COM4_Rx_Cnt] = 0; } } void Uart4_Init() { S4CON = 0x50; T4L = UART4_BRT; T4H = UART4_BRT >> 8; T4T3M |= 0xA0; IE2 |= 0x10; COM4_Rx_Cnt = 0; COM4_Tx_Cnt= 0; B_COM4_Tx_Busy = 0; } void COM4_PrintString(unsigned char *puts) //发送一个字符串 { for (; *puts != 0; puts++) //遇到停止符0结束 { S4BUF = *puts; B_COM4_Tx_Busy = 1; while(B_COM4_Tx_Busy); } } void Uart4_SendChar(char dat) { while (B_COM4_Tx_Busy); B_COM4_Tx_Busy = 1; S4BUF = dat; } void Uart4_SendStr(char *p) { while (*p) { Uart4_SendChar(*p++); } } void GPIO_Init(void) // 管脚状态初始化 { // I/O口工作模式PnM1和PnM0组合,00-准双向口,01-推挽输出,10-高阻输入,11-开漏输出 P0M0 = 0x00; P0M1 = 0x00; P1M0 = 0x00; P1M1 = 0x00; P2M0 = 0x00; P2M1 = 0x00; P3M0 = 0x00; P3M1 = 0x00; P4M0 = 0x00; P4M1 = 0x00; P5M0 = 0x00; P5M1 = 0x00; } void Pin_Switch(void) // 功能管脚切换 { P_SW2 &= 0xFD; // B1=0 设置串口3的功能脚为P00和P01 P_SW2 &= ~0x04; //UART4: RxD4(P0.2), TxD4(P0.3) } void main() { u16 i,j,n; u8 temp[20]; u8 tt[] = "temp data \r\n"; GPIO_Init(); // 管脚状态初始化 Pin_Switch(); // 功能管脚切换 Uart3_Init(); Uart4_Init(); EA = 1; i=0; j=0; while (1) { if(i>1000) { i=0; j++; if(j>1000) { j=0; P52 = ~P52; Uart3_SendStr("Uart 3 Test !\r\n"); // COM3_PrintString("COM3_PrintString ------ \r\n"); Uart4_SendStr("Uart 4 Test !\r\n"); // Uart4_SendStr(tt); // UART1发送一个字符串 sprintf(temp,"COM4_Rx_Cnt = %d \n",COM4_Rx_Cnt); Uart4_SendStr(temp); // UART1发送一个字符串 } } i++; if(COM3_Rx_Cnt > 2) { // Uart3_SendStr(COM3_Rx_Buffer); COM3_PrintString(COM3_Rx_Buffer); COM3_Rx_Cnt = 0; } if(COM4_Rx_Cnt > 2) { for(n = 0; n < COM4_Rx_Cnt; n++) { Uart4_SendChar(COM4_Rx_Buffer[n]); } // Uart4_SendStr(COM4_Rx_Buffer); // Uart4_SendStr("Uart 4 Get data ---- !\r\n"); //// COM4_PrintString(COM4_Rx_Buffer); COM4_Rx_Cnt = 0; } } } 复制代码
717
1万
管理员
神*** 发表于 2024-2-29 10:56 深圳国芯人工智能有限公司-工具软件 (stcai.com)
神*** 发表于 2024-2-29 10:48 用我们 官方经典串口程序
本版积分规则 发表回复 回帖后跳转到最后一页
|手机版|小黑屋|深圳国芯人工智能有限公司 ( 粤ICP备2022108929号-2 )
GMT+8, 2025-5-2 13:07 , Processed in 0.141928 second(s), 101 queries .
Powered by Discuz! X3.5
© 2001-2025 Discuz! Team.