#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<n;i++)
|
// {
|
// if(j=0;j<i;j++)
|
// {
|
// S3BUF = rece[0];
|
//
|
// }
|
// }
|
//}
|
|
uchar GetInput()
|
{
|
|
return ~P0;
|
/*
|
Coils input;
|
Coils input1;
|
input.bits.b0=~IN0;
|
input.bits.b1=~IN1;
|
input.bits.b2=~IN2;
|
input.bits.b3=~IN3;
|
input.bits.b4=~IN4;
|
input.bits.b5=~IN5;
|
input.bits.b6=~IN6;
|
input.bits.b7=~IN7;
|
|
intput.Byte = P0;
|
|
return input.Byte;
|
*/
|
}
|
|
void PutOutput(uchar out)
|
{
|
P2 = out;
|
return ;
|
/*
|
Coils output;
|
output.Byte=out;
|
|
|
OUT0 = output.bits.b0;
|
OUT1 = output.bits.b1;
|
OUT2 = output.bits.b2;
|
OUT3 = output.bits.b3;
|
OUT4 = output.bits.b4;
|
OUT5 = output.bits.b5;
|
OUT6 = output.bits.b6;
|
OUT7 = output.bits.b7;
|
*/
|
}
|
|
void Uart3SendPacket(char * str, uchar len)
|
{
|
uchar i;
|
for (i=0;i<len;i++)
|
{
|
while (uart3busy);
|
S3BUF = str[i];
|
uart3busy=1;
|
}
|
}
|
/*
|
void SLPSendPacket(char * str, uchar len)
|
{
|
Uart3SendPacket(str,len);
|
}
|
*/
|
void Uart3RecvPacket()
|
{
|
if (uart3recvtimeout <2){
|
uart3recvtimeout++;
|
}else
|
{ /// recieved packet;
|
RUN=~RUN; //ÉÁµÆ
|
SLPparsePacket(&SLP1,uart3recvbuf,uart3recvlen);
|
uart3recvlen =0;
|
}
|
}
|
|
void Uart1RecvPacket()
|
{
|
if (uart1recvtimeout <2){
|
uart1recvtimeout++;
|
}else
|
{ /// recieved packet;
|
RUN=~RUN; //ÉÁµÆ
|
ModbusparsePacket(uart1recvbuf,uart1recvlen);
|
uart1recvlen =0;
|
}
|
}
|
|
void OtherProcess()
|
{
|
/*
|
if(inputdata.bits.b4==1)
|
{
|
OUT0 = ~ OUT0;
|
OUT1 = ~ OUT1;
|
OUT2 = ~ OUT2;
|
OUT3 = ~ OUT3;
|
OUT4 = ~ OUT4;
|
OUT5 = ~ OUT5;
|
OUT6 = ~ OUT6;
|
OUT7 = ~ OUT7;
|
//Delay_ms(500);
|
}
|
*/
|
}
|
|
uchar GetJumper()
|
{
|
Coils keys;
|
keys.bits.b0=~KEY1;
|
keys.bits.b1=~KEY2;
|
keys.bits.b2=~KEY3;
|
keys.bits.b3=~KEY4;
|
keys.bits.b4=~KEY5;
|
keys.bits.b5=~KEY6;
|
keys.bits.b6=0;
|
keys.bits.b7=0;
|
|
return keys.Byte;
|
}
|
int main(void)
|
{
|
unsigned char daa;
|
daa = DPH;
|
|
// Init
|
IO_Init();
|
// Timer0_Isr();
|
Uart3Init();
|
// Timer0_Init();
|
// Timer1_Init();
|
|
ES = 1;
|
EA = 1;
|
|
// Get config jumpper
|
|
inputdata.Byte = GetJumper();
|
bSLPMaster = inputdata.bits.b5 ; //master?
|
nSLPStation = inputdata.Byte & 0x0f;
|
SLPInit(&SLP1,Uart3SendPacket);
|
SLP1.bSLPMaster = bSLPMaster;
|
SLP1.nStation = nSLPStation;
|
|
// bSLPMaster=1;
|
|
while(1)
|
{
|
// toggle LED
|
// RUN =~ RUN;
|
// Delay_ms(1000);
|
|
// Get Input Port;
|
inputdata.Byte = GetInput();
|
SLP1.SLPinputB = inputdata.Byte;
|
RUN =~ RUN;
|
// Delay_ms(1);
|
// receive packet
|
Uart3RecvPacket();
|
// Process Packet
|
SLPProcess(&SLP1);
|
|
if (SLP1.SLPErrSign) {
|
ERR = 0;
|
}else {
|
ERR =1;
|
|
}
|
|
// Set Ouput Port
|
PutOutput(SLP1.SLPoutputB);
|
|
|
// receive packet
|
Uart1RecvPacket();
|
// Process Packet
|
ModbusProcess();
|
|
|
// Other process
|
OtherProcess();
|
|
|
Delay_us(100);
|
|
nCount++;
|
}
|
|
//return ;
|
}
|
|
/*
|
ÖжÏϵͳ£¬º¯ÊýÃûÓëÇ°ÃæÃüÃûÎ޹أ¬Ö»ÓëinterruptºóÃæ½ÓµÄÐòºÅÓйØ
|
void INT0_Isr() interrupt 0 // ÍⲿÖжÏ0
|
void TM0_Isr() interrupt 1 // ¶¨Ê±Æ÷0
|
void INT1_Isr() interrupt 2 // ÍⲿÖжÏ1
|
void TM1_Isr() interrupt 3 // ¶¨Ê±Æ÷1
|
void UART1_Isr() interrupt 4 // ´®¿Ú1
|
void ADC_Isr() interrupt 5 // Ä£Êýת»»
|
void LVD_Isr() interrupt 6 // µÍѹ¼ì²â
|
void PCA_Isr() interrupt 7 // ²¶»ñ
|
void UART2_Isr() interrupt 8 // ´®¿Ú2
|
void SPI_Isr() interrupt 9 // ´®ÐÐÍâÉè½Ó¿Ú
|
void INT2_Isr() interrupt 10 // ÍⲿÖжÏ2
|
void INT3_Isr() interrupt 11 // ÍⲿÖжÏ3
|
void TM2_Isr() interrupt 12 // ¶¨Ê±Æ÷2
|
void INT4_Isr() interrupt 16 // ÍⲿÖжÏ4
|
void UART3_Isr() interrupt 17 // ´®¿Ú3
|
void UART4_Isr() interrupt 18 // ´®¿Ú4
|
void TM3_Isr() interrupt 19 // ¶¨Ê±Æ÷3
|
void TM4_Isr() interrupt 20 // ¶¨Ê±Æ÷4
|
void CMP_Isr() interrupt 21 // ±È½ÏÆ÷
|
void PWM0_Isr() interrupt 22 // PWM0
|
void PWM0FD_Isr() interrupt 23 // PWM0Òì³£¼ì²â
|
void I2C_Isr() interrupt 24 // I2C×ÜÏß
|
|
|
void PWM1_Isr() interrupt 28 // PWM1
|
void PWM2_Isr() interrupt 29 // PWM2
|
void PWM3_Isr() interrupt 30 // PWM3
|
void PWM4_Isr() interrupt 31 // PWM4
|
|
// 32ÒÔÉϵÄÖжϺţ¬¼´ÖжÏÏòÁ¿´óÓÚ0100HµÄÖжϣ¬keil±àÒë»á³ö´í£¬ÐèÒªÌØÊâ´¦Àí²Å¿ÉʹÓÃ
|
void PWM5_Isr() interrupt 32 // PWM5
|
void PWM2FD_Isr() interrupt 33 // PWM2Òì³£¼ì²â
|
void PWM4FD_Isr() interrupt 34 // PWM4Òì³£¼ì²â
|
void PTKSU_Isr() interrupt 35 // ´¥Ãþ°´¼üÖжÏ
|
*/
|