C51 COMPILER V6.02  MODBUS                                                                 04/04/2008 14:22:29 PAGE 1   


C51 COMPILER V6.02, COMPILATION OF MODULE MODBUS
OBJECT MODULE PLACED IN .\MODBUS.OBJ
COMPILER INVOKED BY: E:\Brno\zaloha\disk_C\ATMEL\C51\Keil6\C51\BIN\C51.EXE .\MODBUS.c OPTIMIZE(9,SPEED) NOINTPROMOTE DEB
                    -UG OBJECTEXTEND

stmt level    source

   1          #include <AT89C51CC03.h>
   2          #include <string.h>
   3          
   4          #include "MODBUS.h"
   5          #include "main.h"
   6          #include "LCD_functions.h"
   7          #include "buttons.h"
   8          
   9          // ==============================
  10          // ====== global variables ======
  11          // ==============================
  12          xdata unsigned char COMbuffRX[256];
  13          xdata unsigned int COMbuffRxPos;
  14          xdata unsigned char COMbuffTX[256];
  15          xdata unsigned char COMbuffTxLen,COMbuffTxPos;
  16          data char MODBUS_timeout;
  17          xdata unsigned int MODBUScomEvents;
  18          
  19          
  20          //=====================================================================================================
  21          // initialize MODBUS
  22          //=====================================================================================================
  23          void MODBUSinit(void)
  24          {
  25   1      	// UART init @ 115200bd
  26   1      	SCON=0xDC;
  27   1      	T2CON=0x30;
  28   1      	TL2=RCAP2L=(65524)&0xFF;
  29   1      	TH2=RCAP2H=(65524>>8)&0xFF;
  30   1      	TR2=1;
  31   1      	ES=1;
  32   1      
  33   1      	COMbuffTxLen=COMbuffTxPos=0;
  34   1      	COMbuffRxPos=0;
  35   1      
  36   1      	// switch RS485 to RX
  37   1      	RTXS=0;
  38   1      
  39   1      	// timer 0
  40   1      	sfrwrd(TL1,TH1,65536-XTAL*50/(6*MODBUS_BAUD));
  41   1      	TMOD|=0x10;
  42   1      	TR1=1;
  43   1      	ET1=1;
  44   1      
  45   1      	MODBUS_timeout=0;
  46   1      
  47   1      	// INT priorities
  48   1      	IPL0|=0x10;
  49   1      	IPH0|=0x10;
  50   1      
  51   1      	// comunication events
  52   1      	MODBUScomEvents=0;
  53   1      }
  54          
C51 COMPILER V6.02  MODBUS                                                                 04/04/2008 14:22:29 PAGE 2   

  55          //=====================================================================================================
  56          // INT timer 1 - MODBUS timeout
  57          //=====================================================================================================
  58          void INT_timer1(void) interrupt 3 using 1
  59          {
  60   1      	ET1=0;
  61   1      	sfrwrd(TL1,TH1,65536-XTAL*50/(6*MODBUS_BAUD));	
  62   1      	TF1=0;
  63   1      	ET1=1;
  64   1      
  65   1      	//if(COMbuffRxPos>0) 
  66   1      	{
  67   2      		MODBUS_timeout=1;
  68   2      		LYEL=1;
  69   2      	}
  70   1      }
  71          
  72          //=====================================================================================================
  73          // INT UART RX/TX
  74          //=====================================================================================================
  75          void INT_UART(void) interrupt 4 using 2
  76          {
  77   1      	if(TI==1)
  78   1      	{
  79   2      		// TX interruption
  80   2      		
  81   2      		// if some data in buffer, send next byte
  82   2      		if(COMbuffTxPos<COMbuffTxLen)
  83   2      		{
  84   3      			// switch RS485 to TX
  85   3      			RTXS=1;
  86   3      			SBUF=COMbuffTX[COMbuffTxPos++];
  87   3      		}
  88   2      		else
  89   2      		{
  90   3      			// switch RS485 back to RX
  91   3      			RTXS=0;	
  92   3      			COMbuffTxPos=0;			
  93   3      		}
  94   2      
  95   2      		TI=0;
  96   2      	}
  97   1      
  98   1      	if(RI==1)
  99   1      	{
 100   2      		// RX interruption
 101   2      		
 102   2      		// read recieved byte
 103   2      		if(COMbuffRxPos<256)
 104   2      		{
 105   3      			COMbuffRX[COMbuffRxPos++]=SBUF;	
 106   3      			
 107   3      			// reset timeout
 108   3      			sfrwrd(TL1,TH1,65536-XTAL*50/(6*MODBUS_BAUD));
 109   3      			TF1=0;
 110   3      			MODBUS_timeout=0;
 111   3      			LYEL=0;
 112   3      		}
 113   2      
 114   2      		RI=0;
 115   2      	}
 116   1      }
C51 COMPILER V6.02  MODBUS                                                                 04/04/2008 14:22:29 PAGE 3   

 117          
 118          //=====================================================================================================
 119          // MODBUS
 120          //=====================================================================================================
 121          void MODBUS(void)
 122          {
 123   1      	xdata unsigned int rdaddr,wraddr;
 124   1      	xdata unsigned char rdlen,wrlen;
 125   1      	unsigned char xdata * xdata dptr;
 126   1      
 127   1      	ES=0;
 128   1      	if(MODBUS_timeout==1)
 129   1      	{
 130   2      		if(COMbuffRxPos>3 && COMbuffRX[0]==MODBUS_ADDR)
 131   2      		{
 132   3      			if(CRC16(COMbuffRX,COMbuffRxPos-2)==*(unsigned int*)(&COMbuffRX[COMbuffRxPos-2]))
 133   3      			{
 134   4      				switch(COMbuffRX[1])
 135   4      				{
 136   5      					case 0x0B:
 137   5      						// ------------------------------------
 138   5      						// ------ Get Comm Event Counter ------
 139   5      						// ------------------------------------
 140   5      						dptr=COMbuffTX;
 141   5      						*dptr++=MODBUS_ADDR;
 142   5      						*dptr++=0x0B;
 143   5      						*dptr++=0x00;
 144   5      						*dptr++=0x00;
 145   5      						*((unsigned int xdata*)dptr)++=MODBUScomEvents;
 146   5      
 147   5      						// add CRC16
 148   5      						*((unsigned int xdata *)dptr)++=CRC16(COMbuffTX,dptr-COMbuffTX);
 149   5      						COMbuffTxLen=dptr-COMbuffTX;
 150   5      						TI=1;
 151   5      						MODBUScomEvents++;
 152   5      						break;
 153   5      					case 0x2B:
 154   5      						// ----------------------------------------
 155   5      						// ------ Read Device Identification ------
 156   5      						// ----------------------------------------
 157   5      						if(COMbuffRX[2]==0x0E)
 158   5      						{
 159   6      							dptr=COMbuffTX;
 160   6      							*dptr++=MODBUS_ADDR;
 161   6      							*dptr++=0x2B;
 162   6      							*dptr++=0x0E;
 163   6      							*dptr++=COMbuffRX[3];  // ID code? Waht is it for???
 164   6      							*dptr++='?';  // What the hell this byte should do???
 165   6      							*dptr++=0x00;  // also don't know what is it for
 166   6      							*dptr++=0x00;  // next object ID ... wtf??? Reques says nothing about multiple IDs
 167   6      							*dptr++=1;  // number of objects? I think 1! Reques says nothing about multiple IDs
 168   6      							*dptr++=COMbuffRX[4];  // object ID
 169   6      
 170   6      							// device identification
 171   6      							switch(COMbuffRX[4])
 172   6      							{
 173   7      								case 0x00:
 174   7      									*dptr++=16;
 175   7      									memcpy((void*)dptr,"Stanislav Maslan",16);
 176   7      									dptr+=16;
 177   7      									break;
 178   7      								case 0x01:
C51 COMPILER V6.02  MODBUS                                                                 04/04/2008 14:22:29 PAGE 4   

 179   7      									*dptr++=13;
 180   7      									memcpy((void*)dptr,"SHITTYLCDTERM",13);
 181   7      									dptr+=13;
 182   7      									break;
 183   7      								case 0x02:
 184   7      									*dptr++=5;
 185   7      									memcpy((void*)dptr,"v1.00",5);
 186   7      									dptr+=5;
 187   7      									break;
 188   7      								case 0x03:
 189   7      									*dptr++=30;
 190   7      									memcpy((void*)dptr,"http://elektronika.kvalitne.cz",30);
 191   7      									dptr+=30;
 192   7      									break;
 193   7      								case 0x04:
 194   7      									*dptr++=23;
 195   7      									memcpy((void*)dptr,"Industrial LCD terminal",23);
 196   7      									dptr+=23;
 197   7      									break;
 198   7      								default:
 199   7      									// error - unsknown function
 200   7      									COMbuffTX[0]=MODBUS_ADDR;
 201   7      									COMbuffTX[1]=COMbuffRX[1]|0x80;
 202   7      									COMbuffTX[2]=0x02;	// error code
 203   7      									dptr=&COMbuffTX[3];
 204   7      									MODBUScomEvents--;
 205   7      									break;
 206   7      							}
 207   6      							// add CRC16
 208   6      							*((unsigned int xdata *)dptr)++=CRC16(COMbuffTX,dptr-COMbuffTX);
 209   6      							COMbuffTxLen=dptr-COMbuffTX;
 210   6      							TI=1;
 211   6      							MODBUScomEvents++;
 212   6      						}
 213   5      						else 
 214   5      							goto unknownf;
 215   5      						break;
 216   5      					case 0x46:
 217   5      						// --------------------------------------------------
 218   5      						// ------ custom function: read, write special ------
 219   5      						// --------------------------------------------------
 220   5      
 221   5      						// address and data len for reading
 222   5      						rdaddr=*(unsigned int xdata*)(&COMbuffRX[2]);
 223   5      						rdlen=COMbuffRX[4];
 224   5      						// address and data len for writting
 225   5      						wraddr=*(unsigned int xdata*)(&COMbuffRX[5]);
 226   5      						wrlen=COMbuffRX[7];
 227   5      
 228   5      						// ----- data to write (actualy it's not direct writting into data memory, just some defined write o
             -perations) ------
 229   5      						
 230   5      						// toggle LED or turn of all LEDs
 231   5      						if(wraddr==MODBUS_ADDR_LEDTOGGLE && wrlen==1)
 232   5      						{
 233   6      							if(COMbuffRX[8]&0x01) LRED^=1;
 234   6      							if(COMbuffRX[8]&0x02) LYEL^=1;
 235   6      							if(COMbuffRX[8]&0x04) LGRN^=1;
 236   6      							if(COMbuffRX[8]&0x08) LBLU^=1;
 237   6      							if(COMbuffRX[8]&0x80) LRED=LGRN=LBLU=LYEL=1;
 238   6      						}
 239   5      
C51 COMPILER V6.02  MODBUS                                                                 04/04/2008 14:22:29 PAGE 5   

 240   5      						// on off backlight
 241   5      						if(wraddr==MODBUS_ADDR_BACKLIGHT && wrlen==1)
 242   5      							LLED=(COMbuffRX[8]==0)?1:0;
 243   5      						
 244   5      						// emulate button up and down event
 245   5      						if(wraddr==MODBUS_ADDR_BTNEVENT && wrlen==2)
 246   5      						{	
 247   6      							ebtndown|=COMbuffRX[8];	
 248   6      							ebtnup|=COMbuffRX[9];
 249   6      						}
 250   5      
 251   5      						// send standard answer
 252   5      						dptr=COMbuffTX;
 253   5      						*dptr++=MODBUS_ADDR;
 254   5      						*dptr++=COMbuffRX[1];
 255   5      
 256   5      						
 257   5      						// data to read (actualy it's not direct reading from data memory, just some defined read operations
             -)
 258   5      
 259   5      						// read all LEDs state
 260   5      						if(rdaddr==MODBUS_ADDR_LEDS && rdlen==1)
 261   5      						{
 262   6      							*dptr++=1;
 263   6      							*dptr++=(LRED==0?0x01:0) + (LYEL==0?0x02:0) + (LGRN==0?0x04:0) + (LBLU==0?0x08:0) + (LLED==0?0x10:0
             -);
 264   6      						}
 265   5      
 266   5      						// add CRC16
 267   5      						*((unsigned int xdata *)dptr)++=CRC16(COMbuffTX,dptr-COMbuffTX);
 268   5      						COMbuffTxLen=dptr-COMbuffTX;
 269   5      						TI=1;
 270   5      						MODBUScomEvents++;
 271   5      
 272   5      						break;
 273   5      					case 0x47:
 274   5      						// -------------------------------------------------------
 275   5      						// ------ custom function: read, write MCU memories ------
 276   5      						// -------------------------------------------------------
 277   5      						switch(COMbuffRX[2])
 278   5      						{
 279   6      							case mrmXDATA:
 280   6      								// ------ read XRAM data ------
 281   6      								rdaddr=*((unsigned int xdata*)&COMbuffRX[3]);
 282   6      								rdlen=COMbuffRX[5];
 283   6      								if(rdlen>247) rdlen=247;
 284   6      								dptr=COMbuffTX;
 285   6      								*dptr++=MODBUS_ADDR;
 286   6      								*dptr++=0x47;
 287   6      								*dptr++=mrmXDATA;
 288   6      								*((unsigned int xdata*)dptr)++=rdaddr;
 289   6      								*dptr++=rdlen;
 290   6      								memcpy((void*)dptr,(void*)rdaddr,rdlen);
 291   6      								dptr+=rdlen;
 292   6      								break;
 293   6      							case mrmCODE:
 294   6      								// ------ read CODE data :-D ------
 295   6      								rdaddr=*((unsigned int xdata*)&COMbuffRX[3]);
 296   6      								rdlen=COMbuffRX[5];
 297   6      								if(rdlen>247) rdlen=247;
 298   6      								dptr=COMbuffTX;
 299   6      								*dptr++=MODBUS_ADDR;
C51 COMPILER V6.02  MODBUS                                                                 04/04/2008 14:22:29 PAGE 6   

 300   6      								*dptr++=0x47;
 301   6      								*dptr++=mrmCODE;
 302   6      								*((unsigned int xdata*)dptr)++=rdaddr;
 303   6      								*dptr++=rdlen;
 304   6      								memcpy((void*)dptr,(unsigned char code*)rdaddr,rdlen);
 305   6      								dptr+=rdlen;
 306   6      								break;
 307   6      							default:
 308   6      								// error - unknown mem type
 309   6      								COMbuffTX[0]=MODBUS_ADDR;
 310   6      								COMbuffTX[1]=COMbuffRX[1]|0x80;
 311   6      								COMbuffTX[2]=0x02;	// error code
 312   6      								dptr=&COMbuffTX[3];
 313   6      								MODBUScomEvents--;
 314   6      								break;
 315   6      						}
 316   5      
 317   5      						// add CRC16
 318   5      						*((unsigned int xdata *)dptr)++=CRC16(COMbuffTX,dptr-COMbuffTX);
 319   5      						COMbuffTxLen=dptr-COMbuffTX;
 320   5      						TI=1;
 321   5      						MODBUScomEvents++;
 322   5      						break;
 323   5      					default:
 324   5      					unknownf:
 325   5      						// error - unsknown function
 326   5      						COMbuffTX[0]=MODBUS_ADDR;
 327   5      						COMbuffTX[1]=COMbuffRX[1]|0x80;
 328   5      						COMbuffTX[2]=0x01;	// error code
 329   5      						*(unsigned int xdata*)(&COMbuffTX[3])=CRC16(COMbuffTX,3);
 330   5      						COMbuffTxLen=5;
 331   5      						TI=1;
 332   5      						break;
 333   5      				}
 334   4      			}
 335   3      			COMbuffRxPos=0;
 336   3      		}
 337   2      		else
 338   2      			COMbuffRxPos=0;
 339   2      	}
 340   1      	ES=1;
 341   1      }
 342          


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =   1404    ----
   CONSTANT SIZE    =     92    ----
   XDATA SIZE       =    518       8
   PDATA SIZE       =   ----    ----
   DATA SIZE        =      1    ----
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)
