STC15w4k58s4 芯片的4串口同时收发例程
单片机源程序如下:
- #include "intrins.H"
- #include "uart.H"
- #define uchar unsigned char
- #define uint unsigned int
- uchar uart1buf[6]={0xa5,0x5a,0x4a,0xf8,0xff,0xff};
- uchar uart2buf[6]={0xa5,0x5a,0x4a,0xf8,0xff,0xff};
- uchar uart3buf[6]={0xa5,0x5a,0x4a,0xf8,0xff,0xff};
- uchar uart4buf[6]={0xa5,0x5a,0x4a,0xf8,0xff,0xff};
- uchar uart2_i,uart3_i,uart4_i;
- bit flag2=0,flag3=0,flag4=0; //flag of reveived new packs 收到新数据包标志
- void delay(uint k)
- {
- uint i,j;
- for(i=0;i<k;i++)
- {
- for(j=0;j<121;j++)
- {;}}
- }
- void main()
- {
- uchar i;
- uint l1,l2,l3;
- Uart1Init();
- Uart2Init();
- Uart3Init();
- Uart4Init();
- while(1)
- {
- if(flag3==1||flag2==1||flag4==1)
- {
- flag4=0;
- flag3=0;
- flag2=0;
- for(i=0;i<6;i++)
- uart1buf[i]=uart3buf[i];
- l1=256*uart2buf[2]+uart2buf[3];
- l1=l1/10*7;
- l2=256*uart3buf[2]+uart3buf[3];
- l2=l2/10*7;
- l3=256*uart4buf[2]+uart4buf[3];
- if(l1<=l2&&l1<=l3)
- {
- uart1buf[2]=l1/256;
- uart1buf[3]=l1%256;
- }
- if(l2<=l1&&l2<=l3)
- {
- uart1buf[2]=l2/256;
- uart1buf[3]=l2%256;
- }
- if(l3<=l1&&l3<=l2)
- {
- uart1buf[2]=uart4buf[2];
- uart1buf[3]=uart4buf[3];
- }
- if(uart1buf[2]>=0xea)
- {
- uart1buf[2]=0xea;
- uart1buf[3]=0x00;
- }
- for(i=0;i<6;i++)
- UART1_SendData(uart1buf[i]);
- }
- // delay(500);
- }
- }
- void Uart2() interrupt 8 using 1
- {
- if (S2CON & S2RI)
- {
- S2CON &= ~S2RI; //清除S2RI位
- uart2buf[uart2_i]=S2BUF;
- uart2_i++;
- if(uart2_i==6)
- {
- uart2_i=0;
- flag2=1;
- if(uart2buf[2]==0&&uart2buf[3]==0)
- {
- uart2buf[2]==0x2a;
- uart2buf[3]==0xf8;
- }
- }
- }
- if (S2CON & S2TI)
- {
- S2CON &= ~S2TI; //清除S2TI位
- }
- }
- void Uart3() interrupt 17 using 1
- {
- if (S3CON & S3RI)
- {
- S3CON &= ~S3RI; //清除S2RI位
- uart3buf[uart3_i]=S3BUF;
- uart3_i++;
- if(uart3_i==6)
- {
- uart3_i=0;
- flag3=1;
- if(uart3buf[2]==0&&uart3buf[3]==0)
- {
- uart3buf[2]==0x2a;
- uart3buf[3]==0xf8;
- }
- }
- }
- if (S3CON & S3TI)
- {
- S3CON &= ~S3TI; //清除S3TI位
- }
- }
- void Uart4() interrupt 18 using 1
- {
- if (S4CON & S4RI)
- {
- S4CON &= ~S4RI; //清除S2RI位
- uart4buf[uart4_i]=S4BUF;
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
2017.06.23 四串口.rar
(77.06 KB, 下载次数: 298)
|