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 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
| /****************************************************************************
*****************************************************************************
PROGRAMME DE LA CARTE "CONTROLE" DU SYSTEME maison intelligente
dialogue webserver
=+=+=+= Lycée Louis Armand - Villefranche =+=+=+=
Version 9-4-2009 par A. DELORE
*****************************************************************************
*****************************************************************************/
#include <16F877A.h>
#device icd=true // Autorisation debugger
#FUSES NOWDT // No Watch Dog Timer
#FUSES HS // High speed Osc (> 4mhz)
#FUSES NOPUT // No Power Up Timer
#FUSES NOPROTECT // Code not protected from reading
#FUSES NODEBUG // No Debug mode for ICD
#FUSES BROWNOUT // Reset when brownout detected
#FUSES NOLVP // No low voltage prgming, B3(PIC16) used for I/O
#FUSES NOCPD // No EE protection
#FUSES NOWRT // Program memory not write protected
#use delay(clock=20000000) // Fréquence du quartz 20MHz
/* Définition des paramètres pour la liaison série webserver */
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
/* définition de la liaison I2c*/
#use i2c(Master,Fast=100000,sda=PIN_C4,scl=PIN_C3) //Fast=xxxxx permet de diminuer la vitesse de l'horloge
//pour assurerla communication en fonction de la longueur
// Adresse des esclaves
#define adALARM 0xF0
#define adVOLET 0xF2
#define adTEMP 0xF4
// Définition des E/S TOR sur carte CONTROLE
#define resetSP1 PIN_D1 // Reset du module SitePlayer
#define comm PIN_D3 // LED communication
#define led_chauffage pin_d4
#define led_volet pin_d5
#define led_alarme pin_d6
#define bouton_chauffage pin_b2
#define bouton_volet pin_b1
#define bouton_alarme pin_b0
// Déclaration des fonctions utilisées (utile pour le compilateur)
void initialisation();
void trait_bouton();
void trait_alarme();
void marche_carte();
void ecri_mot_sp1();
/*****************************************************************************/
void I2C_Envoie_Octet(int _adESCLAVE, int _adInterne, int _Donnee);
int I2C_Lit_Octet(int adESCLAVE, int adInterne);
void I2C_Lit_Octet_temp(int adESCLAVE, int adInterne);
// Déclarations du type des variables globales
// int1 : 1 bit
int1 timeout_error;
int1 bouton_appuye_chauffage,bouton_appuye_volet,bouton_appuye_alarme;
// int : 8 bits (-128 à 127)
int nboctet, i, etape, timeout;
int retard = 50;
int lit_char();
int data;
int chauffage,volet,alarme; //memo etat carte
int posvolet,etat_al;
// char : 8 bits (0 à 255)
char sp1_tx[16];
char sp1_rx[16];
char etat_temp[4]={"***"};
char etat_alarme[4]={"***"};
char etat_volet[4]={"***"};
char pos_volet[7]={"******"};
char al_porte[7]={"******"};
char al_garage[7]={"******"};
char al_exter[7]={"******"};
char temp_salon[2], temp_cuisine[2] , temp_chambre1[2], temp_chambre2[2];
// int16 = 16 bits
// float = virgule flottante
/* ============= PROGRAMME PRINCIPAL ==================*/
void main()
{
initialisation();
do
{
trait_bouton(); //acquisition boutons tactiles
marche_carte(); //texte pour SP1 On/OFF
I2C_Envoie_Octet(adVOLET, 0x80, volet); //emission marche carte volet
if(volet)
{
posvolet=I2C_Lit_Octet(adVOLET,0x80); //volet ouvert,ferme?
if (posvolet)
strcpy(pos_volet,"FERME ");
else
strcpy(pos_volet,"OUVERT");
}
else
strcpy(pos_volet,"******");
I2C_Envoie_Octet(adALARM, 0x80, alarme); //emission marche alarme
if(alarme)
{
etat_al=I2C_Lit_Octet(adALARM,0x80); //etat des alarmes
trait_alarme();
}
else
{
strcpy(al_porte,"******");
strcpy(al_garage,"******");
strcpy(al_exter,"******");
}
I2C_Envoie_Octet(adTEMP, 0x80, chauffage); //emission marche carte temperature
if(chauffage)
I2C_Lit_Octet_temp(adTEMP,0x80); //lecture température 8 octets
else
{
temp_salon[0]=0;
temp_salon[1]=0;
temp_cuisine[0]=0;
temp_cuisine[1]=0;
temp_chambre1[0]=0;
temp_chambre1[1]=0;
temp_chambre2[0]=0;
temp_chambre2[1]=0;
}
ecri_mot_sp1(); // Ecriture des mots dans SP1
//delay_ms(retard);
if(etape==1) //cligno led
{
output_high(comm);
etape=0;
}
else
{
output_low(comm);
etape=1;
}
}
while(1); // Le programme reboucle indéfiniment.
}
/* ============== FIN DU PROGRAMME PRINCIPAL ========== */
/* ====== FONCTIONS APPELEES PAR LE PROGRAMME PRINCIPAL ======= */
/* Configuration du microcontroleur */
void initialisation()
{
/* Initialisation des variables */
chauffage=0;
volet=0;
alarme=0;
etat_al=0;
bouton_appuye_chauffage=0;
bouton_appuye_volet=0;
bouton_appuye_alarme=0;
etape=0;
/* Reset du Webserveur SP1 */
output_high(resetSP1);
delay_ms(100);
output_low(resetSP1);
/* Attente démarrage du Web serveur SP1 (mini 150 ms) */
for (i = 0 ; i < 5 ; i++)
{
output_low(comm); // Clignotement rapide de la LED communication
delay_ms(100);
output_high(comm);
delay_ms(100);
}
/* Init communication avec Web Serveur SP1 */
for (i = 0 ; i < 20 ; i++)
{
putc(0); // Init communication SP1 (20 NOP)
}
delay_ms(50);
//init temperature
temp_salon[0]=0;
temp_salon[1]=0;
temp_cuisine[0]=0;
temp_cuisine[1]=0;
temp_chambre1[0]=0;
temp_chambre1[1]=0;
temp_chambre2[0]=0;
temp_chambre2[1]=0;
}
/********** Ecriture mots dans Web Serveur SP1 ************/
void ecri_mot_sp1()
{
int j;
sp1_tx[0] = 0x87; // Code écriture 8 octets
sp1_tx[1] = 0x0; // à l'adresse 0000
sp1_tx[2] = temp_salon[0];
sp1_tx[3] = temp_salon[1];
sp1_tx[4] = temp_cuisine[0];
sp1_tx[5] = temp_cuisine[1];
sp1_tx[6] = temp_chambre1[0];
sp1_tx[7] =temp_chambre1[1];
sp1_tx[8] = temp_chambre2[0];
sp1_tx[9] =temp_chambre2[1];
for (nboctet = 0 ; nboctet < 10 ; nboctet++)
{
putc(sp1_tx[nboctet]); // écriture sur liaison série
}
delay_ms(50);
sp1_tx[0] = 0x8B; //12 octets
sp1_tx[1] = 0x0F; //adresse 0F
for (j = 0 ; j< 4 ; j++)
sp1_tx[2+j] = etat_temp[j];
for (j = 0 ; j< 4 ; j++)
sp1_tx[6+j] = etat_alarme[j];
for (j = 0 ; j< 4 ; j++)
sp1_tx[10+j] = etat_volet[j];
for (nboctet = 0 ; nboctet < 14 ; nboctet++)
{
putc(sp1_tx[nboctet]); // écriture sur liaison série
}
delay_ms(50);
sp1_tx[0] = 0x8C; //13 octets
sp1_tx[1] = 0x1f; //adresse 1F
for (j = 0 ; j< 7 ; j++)
sp1_tx[2+j] = pos_volet[j];
for (j = 0 ; j< 7 ; j++)
sp1_tx[9+j] = al_porte[j];
for (nboctet = 0 ; nboctet < 15 ; nboctet++)
{
putc(sp1_tx[nboctet]); // écriture sur liaison série
}
delay_ms(50);
sp1_tx[0] = 0x8c; //13 octets
sp1_tx[1] = 0x2F; //adresse 2F
for (j = 0 ; j< 7 ; j++)
sp1_tx[2+j] = al_garage[j];
for (j = 0 ; j< 7 ; j++)
sp1_tx[9+j] = al_exter[j];
for (nboctet = 0 ; nboctet < 15 ; nboctet++)
{
putc(sp1_tx[nboctet]); // écriture sur liaison série
}
delay_ms(50);
}
/*traitement des boutons tactiles*/
void trait_bouton()
{
//***** detection appui sur bouton chauffage*******
if (input(bouton_chauffage) && (bouton_appuye_chauffage==0))
{
bouton_appuye_chauffage=1;
if (chauffage==0)
{
chauffage=1;
output_high (led_chauffage);
}
else
{
chauffage=0;
output_low (led_chauffage);
}
}
if (!input(bouton_chauffage))
{
bouton_appuye_chauffage=0;
}
//******** detection appui sur bouton volet
if (input(bouton_volet) && (bouton_appuye_volet==0))
{
bouton_appuye_volet=1;
if (volet==0)
{
volet=1;
output_high (led_volet);
}
else
{
volet=0;
output_low (led_volet);
}
}
if (!input(bouton_volet))
{
bouton_appuye_volet=0;
}
//*************** detection appui sur bouton alarme
if (input(bouton_alarme) && (bouton_appuye_alarme==0))
{
bouton_appuye_alarme=1;
if (alarme==0)
{
alarme=1;
output_high (led_alarme);
}
else
{
alarme=0;
output_low (led_alarme);
}
}
if (!input(bouton_alarme))
{
bouton_appuye_alarme=0;
}
}
//**********texte ON/OFFpour affichage SP1***************
void marche_carte()
{
If(chauffage)
strcpy(etat_temp,"ON ");
else
strcpy(etat_temp,"OFF");
If(alarme)
strcpy(etat_alarme,"ON ");
else
strcpy(etat_alarme,"OFF");
If(volet)
strcpy(etat_volet,"ON ");
else
strcpy(etat_volet,"OFF");
}
//*******************traitement des alarmes********************
void trait_alarme()
{
if(etat_al & 0b00000001)
strcpy(al_porte,"INTRUS");
else
strcpy(al_porte,"NEANT ");
if(etat_al & 0b00000010)
strcpy(al_garage,"INTRUS");
else
strcpy(al_garage,"NEANT ");
if(etat_al & 0b00000100)
strcpy(al_exter,"INTRUS");
else
strcpy(al_exter,"NEANT ");
}
/****************emission octets I2C*******************************************/
void I2C_Envoie_Octet(int _adESCLAVE, int _adInterne, int _Donnee)
{
i2c_start();
i2c_write(_adESCLAVE);
i2c_write(_Donnee);
i2c_stop();
}
/*****************************************************************************/
/*****************************************************************************/
int I2C_Lit_Octet(int adESCLAVE, int adInterne)
{
int retour;
i2c_start();
i2c_write(adESCLAVE + 1); // On ajoute 1 pour mettre à 1 le bit R/W
retour = i2c_read(0);
i2c_stop();
return retour;
}
/*******************lecture temperature*/
void I2C_Lit_Octet_temp(int adESCLAVE, int adInterne)
{
i2c_start();
i2c_write(adESCLAVE + 1); // On ajoute 1 pour mettre à 1 le bit R/W
temp_salon[0]= i2c_read(1);
temp_salon[1]= i2c_read(1);
temp_cuisine[0]= i2c_read(1);
temp_cuisine[1]= i2c_read(1);
temp_chambre1[0]= i2c_read(1);
temp_chambre1[1]= i2c_read(1);
temp_chambre2[0] = i2c_read(1);
temp_chambre2[1]= i2c_read(0);
i2c_stop();
} |
Partager