MCU 用STC15W408AS,接 MAX485,测试485通讯,有 RX_Buffer缓存。
在 接收端 设置接收数组。只能接收到第一次的数据,后续再次发生送,好像是不接收?
具体原因不详。测试ing,还请大神指导
// 代码
- /******************************************
- ***
- *PROJICT CODE: STC15
- *CREATE DATE : 2017/10/30
- *CREATED BY : Chneg
- *FUNCTION :
- *MODIFY DATE :
- *DOCUMENT :
- *OTHERS :
- ******************************************
- ***/
- #include "main.h"
- /************* 本地函数声明 **************/
- /************* 外部函数和变量声明 *****************/
- /************* 串口1初始化函数 *****************/
- 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_Timer2; //使用波特率, BRT_Timer1, BRT_Timer2 (注意: 串口2固定使用BRT_Timer2)
- COMx_InitStructure.UART_BaudRate = 9600ul; //波特率, 一般 110 ~ 115200
- COMx_InitStructure.UART_RxEnable = ENABLE; //接收允许, ENABLE或DISABLE
- COMx_InitStructure.BaudRateDouble = DISABLE; //波特率加倍, ENABLE或DISABLE
- COMx_InitStructure.UART_Interrupt = ENABLE; //中断允许, ENABLE或DISABLE
- COMx_InitStructure.UART_Polity = PolityLow; //中断优先级, PolityLow,PolityHigh
- COMx_InitStructure.UART_P_SW = UART1_SW_P30_P31; //切换端口, UART1_SW_P30_P31,UART1_SW_P36_P37,UART1_SW_P16_P17(必须使用内部时钟)
- COMx_InitStructure.UART_RXD_TXD_Short = DISABLE; //内部短路RXD与TXD, 做中继, ENABLE,DISABLE
- USART_Configuration(USART1, &COMx_InitStructure); //初始化串口1 USART1,USART2
- PrintString1("STC15 UART1 Test Prgramme!\r\n"); //SUART1发送一个字符串
-
- /**
- *RS 485 使能端口
- sbit RS485_DIR = P2^0; //RS485方向选择引脚
- RS485_DIR = 0; //RS485设置为接收方向
- */
- }
- /*************main.c*********************************/
- void main()
- {
- u8 i = 0;
- UART_config();
- EA = 1;
-
- while(1)
- {
- RS485_DE = 1;
- RS485_RE = 1;
- // PrintString1("\r\n");
- PrintString1("Is New Over");
- for(i = 0;i < 8;i++)
- {
- TX1_write2buff(0x40 + i);
- delay_ms(1000);
-
- RS485_DE = 0;
- RS485_RE = 0;
- if(RX1_Buffer[1] == 0xAA)
- {
- RS485_DE = 1;
- RS485_RE = 1;
-
- PrintString1("进入中断");
- delay_ms(500);
-
- // RX1_Buffer[1] = 0;
- }
- RS485_DE = 1;
- RS485_RE = 1;
- }
-
-
- } // <-- end while
- } // <-- end main
复制代码- /******************************************
- ***
- *PROJICT CODE: 串口程序
- *CREATE DATE : 2017/10/20
- *CREATED BY : CHneg
- *FUNCTION : 初始化 数据发送 数据 接收
- *MODIFY DATE : /
- *DOCUMENT : STC DataSheet
- *OTHERS : /
- ******************************************
- ***/
- #include "main.h"
- COMx_Define COM1;
- u8 idata TX1_Buffer[COM_TX1_Lenth]; //发送缓冲
- u8 idata RX1_Buffer[COM_RX1_Lenth]; //接收缓冲
- u8 ExtsAddr = 0x00;
- u8 DestAddr = 0x01;
- u8 RTUOverFlag=0,RTUStartFlag=0,UART_R_RTU[20],RTUFunc,
- RTUPst,RTUDataLen,UARTRTUDataCheckedFlag=0;
- u8 USART_Configuration(u8 UARTx, COMx_InitDefine *COMx)
- {
- u8 i;
- u32 j;
-
- if(UARTx == USART1)
- {
- COM1.id = 1;
- COM1.TX_read = 0;
- COM1.TX_write = 0;
- COM1.B_TX_busy = 0;
- COM1.RX_Cnt = 0;
- COM1.RX_TimeOut = 0;
- COM1.B_RX_OK = 0;
- for(i=0; i<COM_TX1_Lenth; i++) TX1_Buffer[i] = 0;
- for(i=0; i<COM_RX1_Lenth; i++) RX1_Buffer[i] = 0;
- if(COMx->UART_Mode > UART_9bit_BRTx) return 1; //模式错误
- if(COMx->UART_Polity == PolityHigh) PS = 1; //高优先级中断
- else PS = 0; //低优先级中断
- SCON = (SCON & 0x3f) | COMx->UART_Mode;
- if((COMx->UART_Mode == UART_9bit_BRTx) ||(COMx->UART_Mode == UART_8bit_BRTx)) //可变波特率
- {
- j = (MAIN_Fosc / 4) / COMx->UART_BaudRate; //按1T计算
- if(j >= 65536UL) return 2; //错误
- j = 65536UL - j;
- if(COMx->UART_BRT_Use == BRT_Timer1)
- {
- TR1 = 0;
- AUXR &= ~0x01; //S1 BRT Use Timer1;
- TMOD &= ~(1<<6); //Timer1 set As Timer
- TMOD &= ~0x30; //Timer1_16bitAutoReload;
- AUXR |= (1<<6); //Timer1 set as 1T mode
- TH1 = (u8)(j>>8);
- TL1 = (u8)j;
- ET1 = 0; //禁止中断
- TMOD &= ~0x40; //定时
- INT_CLKO &= ~0x02; //不输出时钟
- TR1 = 1;
- }
- else if(COMx->UART_BRT_Use == BRT_Timer2)
- {
- AUXR &= ~(1<<4); //Timer stop
- AUXR |= 0x01; //S1 BRT Use Timer2;
- AUXR &= ~(1<<3); //Timer2 set As Timer
- AUXR |= (1<<2); //Timer2 set as 1T mode
- TH2 = (u8)(j>>8);
- TL2 = (u8)j;
- IE2 &= ~(1<<2); //禁止中断
- AUXR &= ~(1<<3); //定时
- AUXR |= (1<<4); //Timer run enable
- }
- else return 2; //错误
- }
- else if(COMx->UART_Mode == UART_ShiftRight)
- {
- if(COMx->BaudRateDouble == ENABLE) AUXR |= (1<<5); //固定波特率SysClk/2
- else AUXR &= ~(1<<5); //固定波特率SysClk/12
- }
- else if(COMx->UART_Mode == UART_9bit) //固定波特率SysClk*2^SMOD/64
- {
- if(COMx->BaudRateDouble == ENABLE) PCON |= (1<<7); //固定波特率SysClk/32
- else PCON &= ~(1<<7); //固定波特率SysClk/64
- }
- if(COMx->UART_Interrupt == ENABLE) ES = 1; //允许中断
- else ES = 0; //禁止中断
- if(COMx->UART_RxEnable == ENABLE) REN = 1; //允许接收
- else REN = 0; //禁止接收
- P_SW1 = (P_SW1 & 0x3f) | (COMx->UART_P_SW & 0xc0); //切换IO
- if(COMx->UART_RXD_TXD_Short == ENABLE) PCON2 |= (1<<4); //内部短路RXD与TXD, 做中继, ENABLE,DISABLE
- else PCON2 &= ~(1<<4);
- return 0;
- }
- return 3; //其它错误
- }
- /*************** 装载串口发送缓冲 *******************************/
- void TX1_write2buff(u8 dat) //写入发送缓冲,指针+1
- {
- TX1_Buffer[COM1.TX_write] = dat; //装发送缓冲
- if(++COM1.TX_write >= COM_TX1_Lenth) COM1.TX_write = 0;
- if(COM1.B_TX_busy == 0) //空闲
- {
- COM1.B_TX_busy = 1; //标志忙
- TI = 1; //触发发送中断
- }
- }
- void PrintString1(u8 *puts) // 发送 字符串
- {
- for (; *puts != 0; puts++)
- TX1_write2buff(*puts); //遇到停止符0结束
- }
- void MovCharSBuffer(unsigned char c) // 数据帧定义及中断函数
- {
- SBUF=c;
- while(TI!=1);
- TI=0;
- }
- /**************发送数据帧函数***********************/
- void Send_RTU(unsigned char DestAddr,unsigned char S_RTUFunc,unsigned char S_RTUDataLen,unsigned char *S_RTUData)
- {
- unsigned char i,S_RTUCheck;
- ES=0;
- EA=0;
- REN=0;
- //RTUDataLen=0x00;
- delay_ms(1);
- S_RTUCheck=0;
- MovCharSBuffer(FrameHead); // 0x40 @ /1 帧头
- MovCharSBuffer(DestAddr); // 0x01 /2
- S_RTUCheck=S_RTUCheck+DestAddr; // 0+ 0x01
- MovCharSBuffer(S_RTUFunc); // 0x11 /3
- S_RTUCheck=S_RTUCheck+S_RTUFunc; // 0x01 +0x11
- MovCharSBuffer(S_RTUDataLen); // 0x07 /4
- S_RTUCheck=S_RTUCheck+S_RTUDataLen; // 0x12 +0x07 = 0x19
-
- if(S_RTUDataLen!=0)
- {
- for(i=0;i<S_RTUDataLen;i++)
- {
- MovCharSBuffer(*S_RTUData); // /N* 数据
- S_RTUCheck=S_RTUCheck+*S_RTUData;
- S_RTUData++;
- }
- }
- MovCharSBuffer(S_RTUCheck); // /7
- MovCharSBuffer(0x23); // /8
- ES=1;
- EA=1;
- REN=1;
- }
- /********************* UART1中断函数************************/
- void UART1_int (void) interrupt UART1_VECTOR
- {
- if(RI)
- {
- RI = 0;
- if(COM1.B_RX_OK == 0)
- {
- if(COM1.RX_Cnt >= COM_RX1_Lenth)
- COM1.RX_Cnt = 0;
- RX1_Buffer[COM1.RX_Cnt++] = SBUF;
-
- COM1.RX_TimeOut = TimeOutSet1;
-
- }
- }
- if(TI)
- {
- TI = 0;
- if(COM1.TX_read != COM1.TX_write)
- {
- SBUF = TX1_Buffer[COM1.TX_read];
- if(++COM1.TX_read >= COM_TX1_Lenth)
- COM1.TX_read = 0;
- }
- else COM1.B_TX_busy = 0;
- }
- }
复制代码
|