1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
| void main()
{
unsigned char SpeedStep;
unsigned int KeyBd, KeyBdOld;
unsigned int AccessoryOld;
unsigned int Accessory;
unsigned char KeyBdCnt;
unsigned int AtdData;
unsigned int AtdDataNew;
unsigned int AtdSpeedCnt;
unsigned char FSM_KeyBoard;
unsigned char FSM_Contacts;
unsigned char SCIdata;
unsigned int Cur_Speed, New_Speed;
long long cal;
unsigned char FiltreCnt, FiltreCntMax;
InitRegisters(); // initialise les registres du micro
while (n_Ustable==0) __RESET_WATCHDOG();
InitVariables(); // initialise les valriables globales
EnableInterrupts; // démasque les interrutions
InitLcd(); // initialise le LCD: reset hard et init la librairie graphique
if ((SRS_POR==1) || (SRS_ILOP==1)) // cas d'une mise sous tension ou après un téléchargement de logiciel: les paramètres sont restaurés à partir de la flash
VersionLcd(); // affichage de la séquence logo->nom->version
InitViewPortLcd(); // initialise les différents segments d'information de l'écran.
RestoreMotorStatusFromFlash(); // restaure les paramètres de la pompe
EnableCommand(); // fait table rase des IT survenues pendant l'init.
// init des variables locales (variables connues uniquement de "main" et pas des autres fonctions)
SpeedStep = 1;
KeyBd = 0xffff;
KeyBdOld = 0xffff;
KeyBdCnt = 0;
AtdSpeedCnt = 0;
AtdData = 0xFF;
FSM_KeyBoard = 0;
FSM_Contacts = 0;
FiltreCnt = 0;
FiltreCntMax = 5;
AccessoryOld = AccessoryRx;
Accessory = AccessoryRx;
for(;;) // Boucle infinie
{
__RESET_WATCHDOG(); // kicks the dog
Gestion_Tension();
if (LcdUpdateFlag != 0)
UpdateLcd();
// exécute la gestion du mode RS-232 dès qu'au moins un octet est reçu ou que le cycle complet de gestion RS-232 n'est pas fini
if (BusyFlag)
{
Gestion_RS232();
}
else
{
switch(MOTORSTA & MaskMode)
{
/*******************************************************************************************
COMMANDE AVEC LE CLAVIER et PEDALE, POIGNEE
*******************************************************************************************/
case KEYBOARD:
switch(FSM_KeyBoard)
{
case 0: // temps d'exécution : 2µs //lecture du clavier toutes les 10ms
if (Flag10ms == 1)
{
Flag10ms = 0;
KeyBd = rdKB();
if (KeyBdOld == KeyBd) {
KeyBdCnt++;
if (KeyBdCnt == FilterSeuil2) // FilterSeuil2 = 45
{
KeyBdCnt --; //appuie long sur + ou - : défilement rapide de la vitesse affichée à l'écran
SpeedStep = 7;
}
else
SpeedStep = 1;
FSM_KeyBoard = 1; //Simple appuie sur + ou - : défilement pas à pas de la vitesse affichée.
}
else {
KeyBdCnt = 0;
KeyBdOld = KeyBd;
FSM_KeyBoard = 2;
}
if (AccessoryCnt > 0) AccessoryCnt ++; // compteur filtre anti-rebond pour les accessoires (pédale / poignée): cf case 2
}
else FSM_KeyBoard = 2;
break;
case 1: // temps d'exécution : 50µs // Interprétation du mot lu sur le clavier
if ((KeyBdCnt == FilterBound) || (KeyBdCnt > FilterSeuil1))
{
switch(KeyBd)
{
case 0xfeff: // START
LastKeyPressed = 'G';
StartMotor(); // motor ON
break;
case 0xfbff: // STOP
LastKeyPressed = 'H';
if ( (MOTORSTA & MaskPrime)==MaskPrime )
PrimeMotor();
StopMotor(); // motor OFF
break;
case 0xffaf: // Touche + et -
LastKeyPressed = '?';
flag = 1; // revalide gestion PFDs par interruption
break;
case 0xfebf: // Touche - et start
LastKeyPressed = '?';
flag = 0; // invalide gestion PFDs par interruption
PFD1 = 1; // PFDs forcé à 1
PFD2 = 1;
break;
case 0xffbe: // Touche - et CW
LastKeyPressed = '?';
flag = 0; // invalide gestion PFDs par interruption
PFD1 = 0; // PFDs forcé à 0
PFD2 = 0;
break;
case 0xffbf: // -
LastKeyPressed = '-';
SpeedDecMotor(SpeedStep); // decrement speed
break;
case 0xffef: // +
LastKeyPressed = '+';
SpeedIncMotor(SpeedStep); // increment speed
break;
case 0xfffb: // sens 1
LastKeyPressed = '<';
AntiClkwsRotMotor(); // Anticlockwise rotation
break;
case 0xfffe: // sens 2
LastKeyPressed = '>';
ClkwsRotMotor(); // Clockwise rotation
break;
case 0xdfff: // Prime
LastKeyPressed = '&';
if (KeyBdCnt == FilterBound)
PrimeMotor();
break;
default:
break;
}
}
FSM_KeyBoard = 2;
break;
case 2: // gestion de la pédale
if (AccessoryOn == 1)
{
AccessoryOn = 0;
AccessoryCnt = 1;
if ((MOTORSTA & MaskStart)== 0)
{ // START
StartMotor(); // motor ON
}
else
{ // STOP
StopMotor(); // motor OFF
if ( (MOTORSTA & MaskPrime)==MaskPrime )
PrimeMotor();
}
}
else
{
if (AccessoryCnt > ACCESSORY_CNT_MAX) {
if ((SCI2S1 & 0x0f)!=0) SCIdata = SCI2D;
SCIdata = SCI2D;
SCI2C3 = 0x0f;
AccessoryCnt = 0;
}
}
FSM_KeyBoard = 0; // = 1 : clavier non actif
break;
default:
Flag10ms=0;
FSM_KeyBoard = 0;
break;
}
break;
/*
Accessory = AccessoryRx; // Filtre anti-rebond
if(AccessoryOld == Accessory)
{
AccessoryCnt ++;
if (AccessoryCnt >= FilterBound) // Filterbound = 5
{
if (AccessoryRx == 0)
// == 0 quand on appuie sur le pédale (contact établi)
{
if ((MOTORSTA & MaskPrime)==MaskPrime) // condition pour savoir si l'on se trouve en mode PRIME
{
SpeedIndex = SpeedIndexSave;
// TPM1C2SC_CH2IE = 1; //IT Timer1 enabled si égal à 1
MOTORSTA &= ~MaskPrime;
}
else
{
StartMotor();
AccessoryOld = Accessory; // Si le mode PRIME n'est pas activé, on démarre le moteur
}
}
else
{
StopMotor(); // Contact non établi (Rx == 1), on stoppe le moteur
}
}
}
else
{
AccessoryCnt = 0;
AccessoryOld = Accessory;
FSM_KeyBoard = 2;
}
FSM_KeyBoard = 0;
break;
default:
Flag10ms=0;
FSM_KeyBoard = 0;
break;
}
break;
*/
/*******************************************************************************************
//
// COMMMANDE AVEC LES CONTACTS ELECTRIQUES
//
*******************************************************************************************/
case CONTACTS:
switch(FSM_Contacts)
{
case 0: // temps d'exécution : 12µs //Lecture des contacts électriques toutes les 10ms
if (Flag10ms == 1)
{
Flag10ms = 0;
// rotation test
if (ContactsSens == 1) ClkwsRotMotor();
else AntiClkwsRotMotor();
// start/stop test
if (ContactsStop == 1) { // Stop case
// restore context if mode isn't the Keyboard mode
SpeedIndex = SpeedIndexSave;
MOTORSTA &= ~MaskMode;
if ((MOTORSTA_Save & MaskSens)==CLOCKWISE)
ClkwsRotMotor();
else
AntiClkwsRotMotor();
LcdUpdateFlag |= (LcdMaskMode + LcdMaskSpeed + LcdMaskSens);
AtdSpeedCnt = 0;
AtdData = 0xFF;
FSM_KeyBoard = 0;
FSM_Contacts =0; //
StopMotor();
}
else // Start case
{
StartMotor();
FSM_KeyBoard = 0; // =1 anterieur
}
}
FSM_Contacts ++;
FSM_KeyBoard = 0; //
break;
case 1: //teste la fin de la conversion, la compare à
//la valeur courante et évalue la stabilité de
//la valeur courante par rapport à la dernière
//valeur mesurée.
if (ATD1SC_CCF==1)
{
AtdDataNew = (unsigned int)ATD1RH + ATD1RH/ATDcorrection;
if (AtdDataNew != AtdData)
AtdSpeedCnt ++;
else
AtdSpeedCnt = 0;
if (AtdSpeedCnt > ATD_COUNTER_MAX)
{
AtdData = AtdDataNew;
AtdSpeedCnt = 0;
FSM_Contacts ++;
}
else
FSM_Contacts = 0;
}
else
FSM_Contacts = 0;
break;
case 2: //Calcul de la nouvelle vitesse en fonction de
//la tension mesurée. (entre 0 et 2.5V)
if (AtdData >= 0x7E)
SpeedIndex = SpeedIndexSave;
else
{
if (SpeedIndexSave > 999)
Cur_Speed = 10 * (SpeedIndexSave - 899);
else
Cur_Speed = SpeedIndexSave + 1;
cal = (long)Cur_Speed * (long)AtdData;
cal = cal / (long)0x7E;
New_Speed = (unsigned int)cal;
if (New_Speed <= 1000)
SpeedIndex = New_Speed;
else
SpeedIndex = New_Speed/10 + 899;
}
TPM1C2SC_CH2IE = 1;
FSM_Contacts = 0;
LcdUpdateFlag |= LcdMaskSpeed;
break;
default:
Flag10ms = 0;
FSM_Contacts = 0;
break;
}
break;
}
}
}
}
// END -------------------------------------------------------------- |
Partager