STC15标准程序,有需要的可以参考。
单片机源程序如下:
- //本程序的功能是串口1\串口2\串口3和串口4独立接受数据,并将接收到的数据再发送出去
- #include<STC15W4K32S4.H>
- #include "stdio.h"
- #include "intrins.h"
- #include "stdlib.h"
- #include "absacc.h"
- #define uchar unsigned char
- #define uint unsigned int
- #define ulong unsigned long
- uchar idata SendBuf1[6], SendBuf2[6], SendBuf3[6], SendBuf4[6];
- sbit OUT1 = P1^7;
- sbit OUT2 = P3^2;
- sbit OUT3 = P2^7;
- sbit OUT4 = P0^7;
- sbit OUT5 = P3^3;
- void serial_port1_initial();
- void send_UART1();
- void UART1_Interrupt_Receive(void);
- void serial_port2_initial();
- void send_UART2();
- void serial_port3_initial();
- void send_UART3();
- void serial_port4_initial();
- void send_UART4();
- void UART2_Interrupt_Receive(void);
- void delayus(uint i);
- void delayms(uint i);
- void main(void)
- {
- OUT1=0;
- OUT2=0;
- OUT3=0;
- OUT4=0;
- OUT5=0;
- serial_port1_initial();
- serial_port2_initial();
- serial_port3_initial();
- serial_port4_initial();
- P0M0 =0x00;
- P0M1 =0x00;
- P1M0 =0x00;
- P1M1 =0x00;
- P2M0 =0x00;
- P2M1 =0x00;
- P3M0 =0x00;
- P3M1 =0x00;
- P4M0 =0x00;
- P4M1 =0x00;
- P5M0 =0x00;
- P5M1 =0x00;
- delayms(1000);
- while(1)
- {
- /*
- send_UART1();
- send_UART2();
- send_UART3();
- send_UART4();
- OUT1=0;
- OUT2=0;
- OUT3=0;
- OUT4=0;
- delayms(10);
- OUT1=1;
- OUT2=1;
- OUT3=1;
- OUT4=1;
- OUT1=~OUT1;
- OUT2=~OUT2;
- OUT3=~OUT3;
- OUT4=~OUT4;
- */
- delayus(1000);
- OUT1=~OUT1;
- OUT2=~OUT2;
- OUT3=~OUT3;
- OUT4=~OUT4;
- OUT5=~OUT5;
- }
- }
- void serial_port1_initial() //用定时器1作为波特率发生器
- {
- TMOD &= 0x0F; //0x0f:设定定时器1为16位自动重装方式 0x2f: 设定定时器1为8位自动重装方式
- SCON = 0x50; //8位数据,可变波特率
- AUXR = 0x40; //定时器1时钟为Fosc,即1T S1ST2=0,串口1选择定时器1为波特率发生器
- AUXR1= 0x40; //0x00=串口1在P3.0,P3.1,0x40=串口1在P3.6,P3.7,
- TH1 = 0xFE; //设定定时初值
- TL1 = 0xE0; //设定定时初值 9600/11.0592Mhz
- ET1 = 0; //禁止定时器1中断
- TR1 = 1; //启动定时器1
- ES = 1;
- EA = 1;
- }
- void send_UART1()
- {
- unsigned int i=0;
- TI = 0;
- for (i=0;i<6;i++)
- {
- SBUF = SendBuf1[i];
- while (TI==0);
- TI=0;
- }
-
- }
- void UART1_Interrupt_Receive(void) interrupt 4
- {
- unsigned char k =0;
- if(RI == 1)
- {
- RI = 0;
- SendBuf1[0] = SendBuf1[1];
- SendBuf1[1] = SendBuf1[2];
- SendBuf1[2] = SendBuf1[3];
- SendBuf1[3] = SendBuf1[4];
- SendBuf1[4] = SendBuf1[5];
- SendBuf1[5] = SBUF;
-
- }
- }
- void serial_port2_initial()
- {
-
- S2CON = 0x50;//01010000 8位可变波特率,无奇偶校验,允许接收
- T2H = 0xFE; //设定定时初值
- T2L = 0xE0; //设定定时初值
- AUXR |= 0x14; ////定时器2时钟为Fosc,即1T ,启动定时器2,
- IE2 = 0x01; //ES2 = 1;
- P_SW2=0x00; //串口2、3、4的位置
- EA = 1;
- }
- void serial_port3_initial()
- {
-
- S3CON = 0x50;//01010000 8位可变波特率,无奇偶校验,允许接收 ,选择定时器4作为波特率发生器 0x10=选择定时器2作为波特率发生器
- T3H = 0xFE; //设定定时初值
- T3L = 0xE0; //设定定时初值
- T4T3M |= 0x0A; ////定时器3时钟为Fosc,即1T ,启动定时器3,
- IE2 |= 0x08; //ES3 = 1;
- P_SW2=0x00; //串口2、3、4的位置
- EA = 1;
- }
- void serial_port4_initial()
- {
-
- S4CON = 0x50;//01010000 8位可变波特率,无奇偶校验,允许接收 ,选择定时器4作为波特率发生器 0x10=选择定时器2作为波特率发生器
- T4H = 0xFE; //设定定时初值
- T4L = 0xE0; //设定定时初值
- T4T3M |= 0xA0; ////定时器3时钟为Fosc,即1T ,启动定时器3,
- IE2 |= 0x10; //ES4 = 1;
- P_SW2=0x00; //串口2、3、4的位置
- EA = 1;
- }
- void send_UART2()
- {
- uchar temp = 0, i=0;
- S2CON = S2CON & 0xfd; //S2TI = 0;
- for (i=0;i<6;i++)
- {
- S2BUF = SendBuf2[i];
- do
- {
- temp = S2CON;
- temp = temp & 0x02; //temp=S2TI
- }while(temp == 0);
- S2CON = S2CON & 0xfd; //S2TI = 0;
- }
- }
- void send_UART3()
- {
- uchar temp = 0, i=0;
- S3CON = S3CON & 0xfd; //S2TI = 0;
- for (i=0;i<6;i++)
- {
- S3BUF = SendBuf3[i];
- do
- {
- temp = S3CON;
- temp = temp & 0x02; //temp=S2TI
- }while(temp == 0);
- S3CON = S3CON & 0xfd; //S2TI = 0;
- }
- }
- void send_UART4()
- {
- uchar temp = 0, i=0;
- S4CON = S4CON & 0xfd; //S2TI = 0;
- for (i=0;i<6;i++)
- {
- S4BUF = SendBuf4[i];
- do
- {
- temp = S4CON;
- temp = temp & 0x02; //temp=S2TI
- }while(temp == 0);
- S4CON = S4CON & 0xfd; //S2TI = 0;
- }
- }
- void UART2_Interrupt_Receive(void) interrupt 8
- {
- unsigned char k = 0;
- k = S2CON;
- k = k & 0x01;
- if(k == 1)
- {
- S2CON = S2CON & 0xfe;
- SendBuf2[0] = SendBuf2[1];
- SendBuf2[1] = SendBuf2[2];
- SendBuf2[2] = SendBuf2[3];
- SendBuf2[3] = SendBuf2[4];
- SendBuf2[4] = SendBuf2[5];
- SendBuf2[5] = S2BUF;
-
- }
- }
- void UART3_Interrupt_Receive(void) interrupt 17
- {
- unsigned char k = 0;
- k = S3CON;
- k = k & 0x01;
- if(k == 1)
- {
- S3CON = S3CON & 0xfe;
- SendBuf3[0] = SendBuf3[1];
- SendBuf3[1] = SendBuf3[2];
- SendBuf3[2] = SendBuf3[3];
- SendBuf3[3] = SendBuf3[4];
- SendBuf3[4] = SendBuf3[5];
- SendBuf3[5] = S3BUF;
-
- }
- }
- void UART4_Interrupt_Receive(void) interrupt 18
- {
- unsigned char k = 0;
- k = S4CON;
- k = k & 0x01;
- if(k == 1)
- {
- S4CON = S4CON & 0xfe;
- SendBuf4[0] = SendBuf4[1];
- SendBuf4[1] = SendBuf4[2];
- SendBuf4[2] = SendBuf4[3];
- SendBuf4[3] = SendBuf4[4];
- SendBuf4[4] = SendBuf4[5];
- SendBuf4[5] = S4BUF;
-
- }
- }
- void delayus(uint i)
- {
- unsigned int j=0;
- unsigned int g = 0;
- for(j=0;j<i;j++)
- {
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- }
- }
- void delayms(uint i)
- {
- unsigned int j=0;
- unsigned int g = 0;
- for(j=0;j<i;j++)
- {
- for(g=0;g<829;g++)
- {
- _nop_();
- _nop_();
- _nop_();
- }
- }
- }
复制代码
所有资料51hei提供下载:
STC15标准程序.rar
(30.84 KB, 下载次数: 148)
|