TP3 RBT 2GII Ex2 ENETCOM
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 // Table des variables, des bits et des Registres d’E/S 00002 //_______________________________________________________________________________ 00003 // Ports et variables d’E/S | Bits et Reg. d’E/S | Ecriture Modbus | Adresse 00004 // ------------------------------------------------------------------------------ 00005 // Led2 : PB_8 | bit 0 de BitsOut[0] | oui (fonction 5) | 0x1000 00006 // BP1 : PA_8 | bit 0 de BitsIn[0] | non | 0x2000 00007 // BP2 : PB_12 | bit 1 de BitsIn[0] | non | 0x2001 00008 // Rap_Cyc * 100 (pour Moteur) | RegOut[0] | oui (fonction 6) | 0x3000 00009 // Pot.read() * 100 (pour Led1)| RegOut[1] | non | 0x3001 00010 // Pot.read_u16 () (pour Pot) | RegIn[0] | non | 0x4000 00011 //------------------------------------------------------------------------------- 00012 00013 // Exemple de trames de requete 00014 // allumer Led2 : 07 05 1000 FF00 00015 // Lecture Etat des 2 boutons poussoir : 07 02 2000 0002 00016 // Rapport Cyclique =50% pour moteur (50=0x32): 07 06 3000 0032 00017 // Rapport Cyclique =100% pour moteur (100=0x64): 07 06 3000 0064 00018 // Lecture de la valeur numerique de l'entree analogique PA_0 (Pot) : 07 04 4000 0001 00019 00020 #include "mbed.h" 00021 #include "mb.h" 00022 00023 #define BITS_OUT_START 0x1001 //Bits de sorties (fonctions 1 et 5) 00024 #define BITS_OUT_NBITS 16 00025 #define BITS_OUT_BYTES BITS_OUT_NBITS/8 //16/8 = 2 octets 00026 00027 #define BITS_IN_START 0x2001 //Bits d'entées (fonction 2) 00028 #define BITS_IN_NBITS 16 00029 #define BITS_IN_BYTES BITS_IN_NBITS/8 //16/8 = 2 octets 00030 00031 #define REG_OUT_START 0x3001 //Registres de sorties (fonctions 3 et 6) 00032 #define REG_OUT_NREGS 4 00033 00034 #define REG_IN_START 0x4001 //Registres d'entées (fonction 4) 00035 #define REG_IN_NREGS 4 00036 00037 #define SLAVE_ID 0x07 00038 00039 static USHORT BitsOutStart = BITS_OUT_START; // (fonctions 1 et 5) 00040 static UCHAR BitsOut[BITS_OUT_BYTES] = {0xA5,0x5A}; 00041 static USHORT BitsInStart = BITS_IN_START; // (fonction 2) 00042 static UCHAR BitsIn[BITS_IN_BYTES] = {0x3C,0xC3}; 00043 static USHORT RegOutStart = REG_OUT_START; // (fonctions 3 et 6) 00044 static USHORT RegOut[REG_OUT_NREGS] = {0x0123,0x4567,0x89AB,0xCDEF}; 00045 static USHORT RegInStart = REG_IN_START; // (fonction 4) 00046 static USHORT RegIn[REG_IN_NREGS] = {0x1122,0x3344,0x5566,0x7788}; 00047 00048 int main() { 00049 eMBInit( MB_RTU, SLAVE_ID, 0, 9600, MB_PAR_NONE ); 00050 eMBEnable(); 00051 00052 PwmOut moteur(PB_11); 00053 PwmOut Led1(..........); 00054 AnalogIn Pot(PA_0); 00055 DigitalIn BP1(PA_8,PullUp); 00056 DigitalIn BP2(..................); 00057 DigitalOut Led2(PB_8); 00058 moteur.period_ms(.......); 00059 RegOut[0]=20; //RegOut[0] = rapport cyclique du moteur * 100 = 20% 00060 while(1) { 00061 //**** 1 ****// 00062 eMBPoll(); 00063 //**** 2 ****// 00064 if(BitsOut[0] & 0x01) Led2=1; else Led2=0; 00065 moteur = RegOut[0]/100.0; 00066 //**** 3 ****// 00067 if(!BP1 && RegOut[0] <100) { 00068 RegOut[0] = RegOut[0] + 10; 00069 moteur = RegOut[0]/.........; 00070 wait(0.5); 00071 } 00072 if(!BP2 && RegOut[0] > ....){ 00073 RegOut[0] = RegOut[0] - ....; 00074 moteur = RegOut[0]/.........; 00075 wait(......); 00076 } 00077 Led1 = Pot.read(); 00078 //**** 4 ****// 00079 RegOut[1] = Pot.read() * .......; 00080 //**** 5 ****// 00081 if(Led2) BitsOut[0] |=.....; 00082 else BitsOut[0] &=.....; 00083 if(BP1) BitsIn[0] |=.....; 00084 else BitsIn[0] &=.....; 00085 if(BP2) BitsIn[0] |=.....; 00086 else BitsIn[0] &=.....; 00087 RegIn[0] = Pot.read_u16(); 00088 } 00089 } 00090 00091 00092 00093 00094 00095 00096 00097 00098 00099 00100 00101 00102 00103 00104 00105 00106 00107 00108 00109 00110 00111 00112 00113 00114 00115 00116 00117 00118 00119 00120 00121 00122 00123 00124 // Procedure résevée pour les bits de sorties (Foction 1 et 5) 00125 // pucRegBuffer => coils buffer usAddress => coils address 00126 // usNCoils => coils number eMode => read or write 00127 eMBErrorCode 00128 eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode ) 00129 { 00130 eMBErrorCode eStatus = MB_ENOERR; 00131 int iIntRegIndex; 00132 int iIntBufNum; 00133 int iIntBitNum; 00134 int iExtRegIndex=0; 00135 int iExtBufNum; 00136 int iExtBitNum; 00137 UCHAR ucTemp; 00138 if( ( usAddress >= BITS_OUT_START ) 00139 && ( usAddress + usNCoils <= BITS_OUT_START + BITS_OUT_NBITS ) ) 00140 { 00141 iIntRegIndex = ( int )( usAddress - BitsOutStart ); 00142 00143 while( usNCoils > 0 ) 00144 { 00145 iIntBufNum=iIntRegIndex/8; 00146 iIntBitNum=iIntRegIndex%8; 00147 iExtBufNum=iExtRegIndex/8; 00148 iExtBitNum=iExtRegIndex%8; 00149 00150 switch ( eMode ) 00151 { 00152 case MB_REG_READ: 00153 // Read coils 00154 if(iExtBitNum==0){ 00155 pucRegBuffer[iExtBufNum]=0; 00156 } 00157 ucTemp=(BitsOut[iIntBufNum]>>iIntBitNum) & 1; 00158 pucRegBuffer[iExtBufNum]|=ucTemp<<iExtBitNum; 00159 break; 00160 00161 case MB_REG_WRITE: 00162 // Write coils 00163 ucTemp=BitsOut[iIntBufNum]&(~(1<<iIntBitNum)); 00164 ucTemp|=((pucRegBuffer[iExtBufNum]>>iExtBitNum) & 1)<<iIntBitNum; 00165 BitsOut[iIntBufNum]=ucTemp; 00166 break; 00167 } 00168 iIntRegIndex++; 00169 iExtRegIndex++; 00170 usNCoils--; 00171 00172 } 00173 } 00174 else 00175 { 00176 eStatus = MB_ENOREG; 00177 } 00178 return eStatus; 00179 } 00180 00181 // Procedure résevée pour les bits d'entrées (Foction 2) 00182 // pucRegBuffer => discrete buffer usAddress => discrete address 00183 // usNDiscrete => discrete number 00184 eMBErrorCode 00185 eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete ) 00186 { 00187 eMBErrorCode eStatus = MB_ENOERR; 00188 int iIntRegIndex; 00189 int iIntBufNum; 00190 int iIntBitNum; 00191 int iExtRegIndex=0; 00192 int iExtBufNum; 00193 int iExtBitNum; 00194 UCHAR ucTemp; 00195 if( ( usAddress >= BITS_IN_START ) 00196 && ( usAddress + usNDiscrete <= BITS_IN_START + BITS_IN_NBITS ) ) 00197 { 00198 iIntRegIndex = ( int )( usAddress - BitsInStart ); 00199 00200 while( usNDiscrete > 0 ) 00201 { 00202 iIntBufNum=iIntRegIndex/8; 00203 iIntBitNum=iIntRegIndex%8; 00204 iExtBufNum=iExtRegIndex/8; 00205 iExtBitNum=iExtRegIndex%8; 00206 00207 // Read discrete inputs 00208 if(iExtBitNum==0){ 00209 pucRegBuffer[iExtBufNum]=0; 00210 } 00211 ucTemp=(BitsIn[iIntBufNum]>>iIntBitNum) & 1; 00212 pucRegBuffer[iExtBufNum]|=ucTemp<<iExtBitNum; 00213 00214 iIntRegIndex++; 00215 iExtRegIndex++; 00216 usNDiscrete--; 00217 } 00218 } 00219 else 00220 { 00221 eStatus = MB_ENOREG; 00222 } 00223 return eStatus; 00224 } 00225 00226 // Procedure résevée pour les registres de sorties (Foction 3 et 6) 00227 // pucRegBuffer => holding register buffer usAddress holding => register address 00228 // usNRegs holding register number eMode => read or write 00229 eMBErrorCode 00230 eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode ) 00231 { 00232 eMBErrorCode eStatus = MB_ENOERR; 00233 int iRegIndex; 00234 00235 if( ( usAddress >= REG_OUT_START ) && 00236 ( usAddress + usNRegs <= REG_OUT_START + REG_OUT_NREGS ) ) 00237 { 00238 iRegIndex = ( int )( usAddress - RegOutStart ); 00239 switch ( eMode ) 00240 { 00241 //*** Pass current register values to the protocol stack. *** 00242 case MB_REG_READ: 00243 while( usNRegs > 0 ) 00244 { 00245 *pucRegBuffer++ = ( UCHAR ) ( RegOut[iRegIndex] >> 8 ); 00246 *pucRegBuffer++ = ( UCHAR ) ( RegOut[iRegIndex] & 0xFF ); 00247 iRegIndex++; 00248 usNRegs--; 00249 } 00250 break; 00251 00252 //***Update current register values with new values from the protocol stack.*** 00253 case MB_REG_WRITE: 00254 while( usNRegs > 0 ) 00255 { 00256 RegOut[iRegIndex] = *pucRegBuffer++ << 8; 00257 RegOut[iRegIndex] |= *pucRegBuffer++; 00258 iRegIndex++; 00259 usNRegs--; 00260 } 00261 } 00262 } 00263 else 00264 { 00265 eStatus = MB_ENOREG; 00266 } 00267 return eStatus; 00268 } 00269 // Procedure résevée pour les registres de sorties (Foction 4) 00270 // pucRegBuffer => input register buffer usAddress => input register address 00271 // usNRegs input register number 00272 eMBErrorCode 00273 eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs ) 00274 { 00275 eMBErrorCode eStatus = MB_ENOERR; 00276 int iRegIndex; 00277 00278 if( ( usAddress >= REG_IN_START ) 00279 && ( usAddress + usNRegs <= REG_IN_START + REG_IN_NREGS ) ) 00280 { 00281 iRegIndex = ( int )( usAddress - RegInStart ); 00282 while( usNRegs > 0 ) 00283 { 00284 *pucRegBuffer++ = ( unsigned char )( RegIn[iRegIndex] >> 8 ); 00285 *pucRegBuffer++ = ( unsigned char )( RegIn[iRegIndex] & 0xFF ); 00286 iRegIndex++; 00287 usNRegs--; 00288 } 00289 } 00290 else 00291 { 00292 eStatus = MB_ENOREG; 00293 } 00294 return eStatus; 00295 }
Generated on Fri Jul 22 2022 05:29:42 by
1.7.2