⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 plc.i

📁 此源码为商用的电力抄表系统的主机端源程序。通过电力载波模块
💻 I
字号:
#line 1 "plc.c" /0   #line 1 "C:\KEIL\C51\INC\PHILIPS\REG922.H" /0       sfr P0   = 0x80; sfr P1   = 0x90; sfr P3   = 0xB0; sfr PSW  = 0xD0; sfr ACC  = 0xE0; sfr B    = 0xF0; sfr SP   = 0x81; sfr DPL  = 0x82; sfr DPH  = 0x83; sfr PCON = 0x87; sfr TCON = 0x88; sfr TMOD = 0x89; sfr TL0  = 0x8A; sfr TL1  = 0x8B; sfr TH0  = 0x8C; sfr TH1  = 0x8D; sfr IEN0 = 0xA8; sfr IP0  = 0xB8; sfr SCON = 0x98; sfr SBUF = 0x99;   sfr AUXR1  = 0xA2; sfr SADDR  = 0xA9; sfr SADEN  = 0xB9; sfr BRGR0  = 0xBE; sfr BRGR1  = 0xBF; sfr BRGCON = 0xBD; sfr CMP1   = 0xAC; sfr CMP2   = 0xAD; sfr DIVM   = 0x95; sfr FMADRH = 0xE7; sfr FMADRL = 0xE6; sfr FMCON  = 0xE4; sfr FMDATA = 0xE5; sfr I2ADR  = 0xDB; sfr I2CON  = 0xD8; sfr I2DAT  = 0xDA; sfr I2SCLH = 0xDD; sfr I2SCLL = 0xDC; sfr I2STAT = 0xD9; sfr IP1    = 0xF8; sfr IP1H   = 0xF7; sfr KBCON  = 0x94; sfr KBMASK = 0x86; sfr KBPATN = 0x93; sfr P0M1   = 0x84; sfr P0M2   = 0x85; sfr P1M1   = 0x91; sfr P1M2   = 0x92; sfr P3M1   = 0xB1; sfr P3M2   = 0xB2; sfr PCONA  = 0xB5; sfr PT0AD  = 0xF6; sfr RSTSRC = 0xDF; sfr RTCCON = 0xD1; sfr RTCH   = 0xD2; sfr RTCL   = 0xD3; sfr SSTAT  = 0xBA; sfr TAMOD  = 0x8F; sfr TRIM   = 0x96; sfr WDCON  = 0xA7; sfr WDL    = 0xC1; sfr WFEED1 = 0xC2; sfr WFEED2 = 0xC3; sfr IP0H   = 0xB7; sfr IEN1   = 0xE8;    sbit CY   = PSW^7; sbit AC   = PSW^6; sbit F0   = PSW^5; sbit RS1  = PSW^4; sbit RS0  = PSW^3; sbit OV   = PSW^2; sbit F1   = PSW^1; sbit P    = PSW^0;   sbit TF1  = TCON^7; sbit TR1  = TCON^6; sbit TF0  = TCON^5; sbit TR0  = TCON^4; sbit IE1  = TCON^3; sbit IT1  = TCON^2; sbit IE0  = TCON^1; sbit IT0  = TCON^0;   sbit EA   = IEN0^7; sbit EWDRT = IEN0^6; sbit EBO  = IEN0^5; sbit ES   = IEN0^4;   sbit ESR  = IEN0^4;  sbit ET1  = IEN0^3; sbit EX1  = IEN0^2; sbit ET0  = IEN0^1; sbit EX0  = IEN0^0;   sbit EC   = IEN1^2; sbit EKBI = IEN1^1; sbit EI2C = IEN1^0;   sbit PST  = IP0^6; sbit PC   = IP0^2; sbit PKBI = IP0^1; sbit Pi2c = IP0^0;   sbit PWDRT = IP0^6; sbit PBO  = IP0^5; sbit PS   = IP0^4;   sbit PSR  = IP0^4; sbit PT1  = IP0^3; sbit PX1  = IP0^2; sbit PT0  = IP0^1; sbit PX0  = IP0^0;    sbit SM0  = SCON^7;   sbit FE   = SCON^7; sbit SM1  = SCON^6; sbit SM2  = SCON^5; sbit REN  = SCON^4; sbit TB8  = SCON^3; sbit RB8  = SCON^2; sbit TI   = SCON^1; sbit RI   = SCON^0;   sbit I2EN  = I2CON^6; sbit STA   = I2CON^5; sbit STO   = I2CON^4; sbit SI    = I2CON^3; sbit AA    = I2CON^2; sbit CRSEL = I2CON^0;   sbit KB7 = P0^7;   sbit T1  = P0^7; sbit KB6 = P0^6;    sbit KB5 = P0^5; sbit KB4 = P0^4; sbit KB3 = P0^3; sbit KB2 = P0^2; sbit KB1 = P0^1; sbit KB0 = P0^0;      sbit RST     = P1^5; sbit INT1    = P1^4; sbit INT0    = P1^3;   sbit SDA     = P1^3; sbit T0      = P1^2;   sbit SCL     = P1^2; sbit RxD     = P1^1; sbit TxD     = P1^0;   sbit XTAL1= P3^1; sbit XTAL2= P3^0;   #line 7 "plc.c" /0    #line 1 "sart.h" /0    #line 1 "C:\KEIL\C51\INC\PHILIPS\REG922.H" /0   #line 11 "C:\KEIL\C51\INC\PHILIPS\REG922.H" /1                                                                                                                                                                             #line 183 "C:\KEIL\C51\INC\PHILIPS\REG922.H" /0 #line 8 "sart.h" /0       sbit      RX_RDY        = 0x96;							  sbit      TX_BUSY       = 0x87;							  sbit      RX_BUSY       = 0x94;							  sbit      D_RDY         = 0x83;							  sbit      D_SYNC        = 0x80;							  sbit      SCLK      	= 0x81;							  sbit      SDATA   	    = 0x82;							  sbit      Rxok          = 0x97;							  sbit      RST_PLC       = 0x86;							  sbit      TX_EN         = 0x92;							  sbit      SYNC_50Hz     = 0x93;							  sbit      K2            = P3^1;	 sbit      K1            = P3^0;						   void uart_init(); int uart_recved(); int uart_processed(); unsigned char uart_getchar();   int recv_amr_frame(void);  void uart_sendchar(char c); void uart_sendblock(char buf[], int len);    void sync_sendchar(char c); int sync_recved(); int sync_data_ready(); int sync_getchar(); int sync_getblock(char *buf, int len); #line 9 "plc.c" /0   #line 1 "plc.h" /0                                                  void send_data(unsigned char c);    void send_block(unsigned char block[], unsigned int len);   void write_reg(unsigned char op, unsigned char val);      int read_reg(unsigned char op); int read_buffer(unsigned char op, unsigned char buf[], unsigned int len);  void send_command(unsigned char cmd);  int set_rate(unsigned char val); int get_rate(); int set_mode(unsigned char val); int get_mode();   int set_fec(unsigned int on);   int set_autosync(unsigned int on);  int set_op(unsigned int normal_op);  int set_timing(unsigned char t1, unsigned char t2, unsigned char t3);  int get_timing(unsigned char t[3]);  int set_thres_sync(unsigned char val); int get_thres_sync();  int set_thres_det(unsigned char val); int get_thres_det();  int set_carrier(unsigned char f1, unsigned char f2, unsigned char f3, unsigned char f4); int get_carrier(unsigned char f[4]);  int set_scaling(unsigned char val); int get_scaling();  int set_agc(unsigned char agc[8]); int get_agc(unsigned char agc[8]);  int read_crc();  int read_rssi(unsigned char rssi[2]);  int write_txbuffer(unsigned char buffer[]);  int read_txbuffer(unsigned char buffer[]);  int read_rxbuffer(unsigned char buffer[]);  void send();      int plc_init(unsigned int smod); int plc_config(unsigned char buf[], unsigned int len); int plc_send(unsigned char frame[]); int plc_resend(); int plc_recved(); int plc_getframe(unsigned char frame[]); int plc_reset(); void plc_power_on(void);  void ist_count();  void timeout_t2(void); void thres_adjust();  void timeout_t4(void);  void w_flsh(unsigned char dat,unsigned char dat1); void Remote_Replies(unsigned char buf[]);  void Rx_Command_Reply(unsigned char comm);     #line 10 "plc.c" /0   #line 1 "timer.h" /0    void timer0_init(char interval, int count);   void setup_t0(unsigned int t, void (*proc)()); void disable_t0(void);  void setup_t1(unsigned int t, void (*proc)()); void disable_t1(void);  void setup_t2(unsigned int t, void (*proc)()); void disable_t2(void);  void setup_t3(unsigned int t, void (*proc)()); void disable_t3(void);  void setup_t4(unsigned int t, void (*proc)()); void disable_t4(void);  void wdt_init(); void clr_wdt();#line 11 "plc.c" /0   #line 1 "wr_flash.h" /0   #line 1 "C:\KEIL\C51\INC\PHILIPS\REG922.H" /0   #line 11 "C:\KEIL\C51\INC\PHILIPS\REG922.H" /1                                                                                                                                                                             #line 183 "C:\KEIL\C51\INC\PHILIPS\REG922.H" /0 #line 2 "wr_flash.h" /0             extern  bit  FLASH_WriteNByte(unsigned int addr, unsigned char  *dat_buf, unsigned char no);    extern  void  FLASH_ReadNByte(unsigned int addr, unsigned char *dat_buf, unsigned char no);         bit  FLASH_WriteByte(unsigned int addr, unsigned char dat);   unsigned int  FLASH_ReadByte(unsigned int addr);#line 12 "plc.c" /0               idata unsigned char cur_smod = 2;	  idata unsigned char frame_sz = 18;     unsigned char code plc_setting[] = {0x30,0x00,0x32,0x10,0x33,0x01,0x34,0x02,0x35,0x12,0x36,0x08,0x37,0x3C,0x38,0x60,0x39,0x64,0x3a,0x68,0x3b,0x6c,0x3c,0x04,0x3e,0x00,0x3f,0x00};   idata unsigned char thres_ea; idata unsigned char wet;  bit packet_recved; bit packet_recving; bit timeout_40s; bit timeout_1s; bit timeout_1m; extern bit reply;  bit rx_adj; bit rx_stop; bit delay_tx; idata unsigned char thres_set; void timeout_t2(void);      void send_data(unsigned char c) { sync_sendchar(c); }     void send_block(unsigned char block[], unsigned int len) {  unsigned char i;  for (i = 0; i < len; i++)   send_data(block[i]);  }     void write_reg(unsigned char op, unsigned char val) {  unsigned char buf[2];  buf[0] = op; buf[1] = val; send_block(buf, 2);  }     int read_reg(unsigned char op) {  unsigned char i;    send_data(op);        i = 0; while (i++ < 100) { if (sync_data_ready()) break; }  if (!sync_data_ready()) {    return -1;    }  i = sync_getchar();    return  i;    }    int read_buffer(unsigned char op, unsigned char buf[], unsigned int len) {  unsigned int i;     send_data(op);     if (cur_smod == 1) {    return -1; }   i = 0; while (i++ < 100) { if (sync_data_ready()) break; }  if (!sync_data_ready()) {   return -1;    }  i = sync_getblock(buf, len);   return i;    }        int set_thres_ea(unsigned char val) {  write_reg(0x32, val);      if (read_reg(0xF2) != (unsigned int)val) return -1; }     int set_thres_eb(unsigned char val) {  write_reg(0x36, val);    if (read_reg(0xF6) != (unsigned int)val) return -1; }    int read_crc() { return (read_reg(0xF1) & 0x01);  }      int write_txbuffer(unsigned char buffer[]) { send_data(0x7F); send_block(buffer, frame_sz);  return 0; }               int plc_config(unsigned char buf[], unsigned int len) {  send_block(buf, len);  return 1; }        int plc_recved() { return sync_recved(); }    int plc_getframe(unsigned char *frame) {  unsigned int res;  res = read_buffer(0xC0, frame, frame_sz);  if (res < 0) return -1;  return frame_sz;  }    void thres_adjust(void) { timeout_1s = 0; rx_adj = 0; rx_stop = 1; wet = 0; set_thres_eb(1);  disable_t2();   while(rx_stop){    clr_wdt(); if(timeout_40s){      thres_ea -= 8 + 16;     if (thres_ea < 16) {thres_ea = 16; plc_power_on();}   timeout_40s = 0; set_thres_ea(thres_ea);	 rx_adj = 0; }  timeout_1s = 0; setup_t0(600, ist_count);   while(timeout_1s == 0){clr_wdt();}   if(wet > 4) { thres_ea +=  16; if (thres_ea > 0x80) thres_ea = 0x80;    rx_adj = 1;      set_thres_ea(thres_ea); } if (wet == 4) { thres_ea +=  8; if (thres_ea > 0x80){ thres_ea = 0x80; plc_power_on();}   rx_adj = 1;      set_thres_ea(thres_ea); rx_stop = 0;  } else rx_stop = 0;  wet = 0; }   disable_t0();    clr_wdt();    thres_ea += 16; if (thres_ea > 0x80) thres_ea = 0x80; set_thres_ea(thres_ea);   set_thres_eb(thres_ea>>1); wet = 0; setup_t2(4000, timeout_t2); }     void timeout_t3(void) { disable_t3();     if (RX_BUSY){wet ++;}   else {  packet_recved = 1; packet_recving = 1; } }    void RX_BUSY_ISR (void) interrupt 2		     {  setup_t3(10, timeout_t3);      packet_recving = 0;  }    void ist_count()  {   timeout_1s = 1;  }    void timeout_t2(void) { timeout_40s = 1;  }    void timeout_t4(void) { delay_tx = 0; disable_t4(); }     void plc_power_on(void) {  unsigned int i; clr_wdt();   RST_PLC = 0; for (i = 0; i < 3000; i++) {;} clr_wdt(); RST_PLC = 1; for (i = 0; i < 100; i++) {;}    plc_config(plc_setting, sizeof(plc_setting)); clr_wdt();  clr_wdt();  set_thres_eb(0); thres_set = 0; thres_ea = 0x05;  while (thres_set < 4) { wet = 0;  timeout_1s = 0;   setup_t0(600, ist_count);    while (timeout_1s == 0) { clr_wdt();} disable_t0();  if	(wet > 4) { thres_ea += 16; if (thres_ea > 0x80)  thres_ea = 0x80;  set_thres_ea(thres_ea); } else { thres_set++; } }  thres_ea += 16; if (thres_ea > 0x80) thres_ea = 0x80;  set_thres_ea(thres_ea); set_thres_eb(thres_ea>>1); clr_wdt();  packet_recved = 0; packet_recving = 0; wet = 0; }    void w_flsh(unsigned char dat,unsigned char dat1) {  unsigned char buf[2];  write_reg(0x33, dat); write_reg(0x3c, dat1); buf[0] = dat; buf[1] = dat1; EA = 0;  FLASH_WriteNByte(0x1c00, buf, 2);   EA = 1; }      void Rx_Command_Reply(unsigned char comm) {  unsigned char Command; Command = comm & 0x3f; switch(Command){    case 0x01:{reply = 1;}break;  case 0x02:{reply = 0;}break; } }     void Remote_Replies(unsigned char buf[])  { buf[0] = 0xa1;   write_txbuffer(buf);	  sync_sendchar(0x7e); }    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -