#include "STC15F2K60S2.H" #include "intrins.h" #include "SLP.h" #include "modbus.h" //#include "main.h" //#include "STC15Wxx.h" //#include "delay.h" //#include "uart.h" //#include "tim.h" //#include "gpio.h" //#include "pwm.h" //#include "modbus.h" //#include "unibus.h" #define S3RI 0x01 //S3CON.0 #define S3TI 0x02 //S3CON.1 #define S3RB8 0x04 //S3CON.2 #define S3TB8 0x08 //S3CON.3 //#define uchar unsigned char //#define uint unsigned int //typedef unsigned char uchar; typedef unsigned int uint; sbit IN0=P0^0; sbit IN1=P0^1; sbit IN2=P0^2; sbit IN3=P0^3; sbit IN4=P0^4; sbit IN5=P0^5; sbit IN6=P0^6; sbit IN7=P0^7; sbit TXD3_2=P5^1; sbit RXD3_2=P5^0; sbit OUT0=P2^0; sbit OUT1=P2^1; sbit OUT2=P2^2; sbit OUT3=P2^3; sbit OUT4=P2^4; sbit OUT5=P2^5; sbit OUT6=P2^6; sbit OUT7=P2^7; sbit ERR=P4^5; sbit RUN=P4^6; sbit KEY1=P3^6; sbit KEY2=P3^7; sbit KEY3=P4^1; sbit KEY4=P4^2; sbit KEY5=P4^3; sbit KEY6=P4^4; bit uart1busy; bit uart3busy; //#pragma anon_unions char uart1recvbuf[8]; uchar uart1recvlen=0; uchar uart1recvtimeout =0; char uart3recvbuf[8]; uchar uart3recvlen=0; uchar uart3recvtimeout =0; int nCount=0; stSLPDef SLP1; unsigned char bSLPMaster; unsigned char nSLPStation; void PutOutput(uchar a); typedef union tagcoils { uchar Byte; struct stByte{ uchar b0:1; uchar b1:1; uchar b2:1; uchar b3:1; uchar b4:1; uchar b5:1; uchar b6:1; uchar b7:1; }bits; }Coils; Coils inputdata; /* void Delay1ms() //@22.1184MHz { uchar data i, j; _nop_(); _nop_(); i = 22; j = 128; do { while (--j); } while (--i); } void Delay_ms(uint n) { while(n--) { Delay1ms(); } } */ void Delay_us(uint n) { uchar data j; while(n--){ j=5; while (--j); } } void Timer0_Isr(void) interrupt 1 {} /* void Timer0_Init(void) //125΢Ãë@22.1184MHz { AUXR |= 0x80; //¶¨Ê±Æ÷ʱÖÓ1Tģʽ TMOD &= 0xF0; //ÉèÖö¨Ê±Æ÷ģʽ TL0 = 0x33; //ÉèÖö¨Ê±³õʼֵ TH0 = 0xF5; //ÉèÖö¨Ê±³õʼֵ TF0 = 0; //Çå³ýTF0±êÖ¾ TR0 = 1; //¶¨Ê±Æ÷0¿ªÊ¼¼Æʱ ET0 = 1; //ʹÄܶ¨Ê±Æ÷0ÖÐ¶Ï } */ void Timer1_Isr(void) interrupt 3 {} /* void Timer1_Init(void) //1ºÁÃë@22.1184MHz { AUXR |= 0x40; //¶¨Ê±Æ÷ʱÖÓ1Tģʽ TMOD &= 0x0F; //ÉèÖö¨Ê±Æ÷ģʽ TL1 = 0x9A; //ÉèÖö¨Ê±³õʼֵ TH1 = 0xA9; //ÉèÖö¨Ê±³õʼֵ TF1 = 0; //Çå³ýTF1±êÖ¾ TR1 = 1; //¶¨Ê±Æ÷1¿ªÊ¼¼Æʱ ET1 = 1; //ʹÄܶ¨Ê±Æ÷1ÖÐ¶Ï } */ //void UartInit(void) //115200bps@22.1184MHz //{ // SCON = 0x50; //8λÊý¾Ý,¿É±ä²¨ÌØÂÊ // AUXR |= 0x40; //¶¨Ê±Æ÷ʱÖÓ1Tģʽ // AUXR &= 0xFE; //´®¿Ú1Ñ¡Ôñ¶¨Ê±Æ÷1Ϊ²¨ÌØÂÊ·¢ÉúÆ÷ // TMOD &= 0x0F; //ÉèÖö¨Ê±Æ÷ģʽ // TL1 = 0xD0; //ÉèÖö¨Ê±³õʼֵ // TH1 = 0xFF; //ÉèÖö¨Ê±³õʼֵ // ET1 = 0; //½ûÖ¹¶¨Ê±Æ÷ÖÐ¶Ï // TR1 = 1; //¶¨Ê±Æ÷1¿ªÊ¼¼Æʱ //} //void UART1_Isr() interrupt 4 //{} //void Uart3Init(void) //115200bps@22.1184MHz //{ // S3CON = 0x10; //8λÊý¾Ý,¿É±ä²¨ÌØÂÊ // S3CON |= 0x40; //´®¿Ú3Ñ¡Ôñ¶¨Ê±Æ÷3Ϊ²¨ÌØÂÊ·¢ÉúÆ÷ // T4T3M |= 0x02; //¶¨Ê±Æ÷ʱÖÓ1Tģʽ // T3L = 0xD0; //ÉèÖö¨Ê±³õʼֵ // T3H = 0xFF; //ÉèÖö¨Ê±³õʼֵ // T4T3M |= 0x08; //¶¨Ê±Æ÷3¿ªÊ¼¼Æʱ //} void Uart3Init(void) //57600bps@22.1184MHz { S3CON = 0x10; //8λÊý¾Ý,¿É±ä²¨ÌØÂÊ S3CON |= 0x40; //´®¿Ú3Ñ¡Ôñ¶¨Ê±Æ÷3Ϊ²¨ÌØÂÊ·¢ÉúÆ÷ T4T3M |= 0x02; //¶¨Ê±Æ÷ʱÖÓ1Tģʽ T3L = 0xA0; //ÉèÖö¨Ê±³õʼֵ T3H = 0xFF; //ÉèÖö¨Ê±³õʼֵ T4T3M |= 0x08; //¶¨Ê±Æ÷3¿ªÊ¼¼Æʱ P_SW2 |= 0x02; IE2 |= 0x08; } void UART3_Isr() interrupt 17 { if (S3CON & S3RI) { S3CON &= ~S3RI; //Çå³ýS3RIλ uart3recvbuf[uart3recvlen] = S3BUF; if (uart3recvlen < sizeof(uart3recvbuf)-1 ) {uart3recvlen++;} uart3recvtimeout=0; // PutOutput(S3BUF); // P0 = S3BUF; //P0ÏÔʾ´®¿ÚÊý¾Ý // P2 = (S3CON & S3RB8); //P2.2ÏÔʾУÑéλ } if (S3CON & S3TI) { S3CON &= ~S3TI; //Çå³ýS3TIλ uart3busy = 0; //Çåæ±êÖ¾ } } void IO_Init(void) { P0M0 = 0x00; P0M1 = 0x00; P1M0 = 0x00; P1M1 = 0x00; P2M0 = 0xff; P2M1 = 0x00; P3M0 = 0x00; P3M1 = 0x00; P4M0 = 0x00; P4M1 = 0x00; P5M0 = 0x00; P5M1 = 0x00; P6M0 = 0x00; P6M1 = 0x00; P7M0 = 0x00; P7M1 = 0x00; } //void senddata(uint n) //{ // uchar i ,j; // if(i=0,i