PROGRAMMA SU SCHEDA MASTER Microprocessore usato PIC16F690 */ #include //Fusibili di configurazione (disabilitato BORDIS e PWRDIS) __CONFIG(INTIO & UNPROTECT & WDTDIS & BORDIS & PWRTEN & MCLRDIS); //Variabili a livello globale unsigned char tempo = 0; int Routine_Ritardo(int func); unsigned char valore = 0; #define Ok RC2 //Pulsante OK #define Set RC1 //Pulsante SET #define RX RB5 //Porta ricevente #define TX RB7 //Porta trasmittente #define Retro RA0 //Retroilluminazione display #define EnableDati RC0 //--------------------------------------------------------------------------- void interrupt intervallo (void) //Interrupt di TIMER0 settato a 1 ms { if (T0IF) //Se overflow { if (tempo != 0) { tempo--; //Decremento T0IF = 0; //Reset } } } //----------------------------------------------------------------------------- //Funzione di ritardo (ogni oscillazione è uguale a 1 ms) int Routine_Ritardo(func) { tempo = func; while (tempo != 0) { asm("nop"); } return 0; } //------------------------------------------------------------------------------------ void Trasmissione(void) { while (TXIF != 0) { EnableDati = 1; //Modalità trasmissione abilitata Routine_Ritardo(1); TXREG = 0b00000001; } } //---------------------------------------------------------------------------- void main(void) { // INIZIALIZZAZIONE HARDWARE ANSEL = 0x00; ANSELH= 0X00; //Esclusione porte analogiche, tutto digitale i/o // PORT A TRISA = 0b11111000; //Port A // PORT B TRISB = 0b01111111; //Port B // PORT C TRISC = 0b00001110; //Port C // Oscillatore impostato a 8MHz OPTION = 0b10000010; //Prescaler 1:8 e disabilitazione Pull-up INTCON = 0b11100000;//0b10100000; GIE, PEIE = 1 11100000 OSCCON = 0b01110000; //Settaggio oscillatore interno a 8 MHz TMR0 = 6; //Caricamento TIMER0 a 1 ms TXSTA = 0b00100100; //Byte trasmissione seriale RCSTA = 0b10011000; //Byte ricezione seriale SPBRG = 47; //Settaggio per velocità di comunicazione (baud rate) //---------------------------------------------------------------------------- while (1) //Retro = 1 spento; Retro = 0 acceso { if (Ok == 1) { Routine_Ritardo(40); Retro = 1; } else if (Ok == 0) { Routine_Ritardo(40); Retro = 0; Trasmissione(); } } } PROGRAMMA SU SCHEDA SLAVE #include //Fusibili di configurazione (disabilitato BORDIS e PWRDIS) __CONFIG(INTIO & UNPROTECT & WDTDIS & BORDIS & PWRTEN & MCLRDIS); //Variabili a livello globale int tempo = 0; //Routine per ritardo int Routine_Ritardo(int func); //Ritardo per pause e antirimbalzo unsigned char ValoreRicevuto = 0; //Comunicazione //Input #define Rx RB5 //Pin di ricezione porta seriale 485 //Output #define Sirena RC6 //Sirene d'allarme #define Tx RB7 //Pin di trasmissione porta seriale 485 #define EnableDati RC0 //Pin di gestione trasmissione o ricezione porta seriale 485 //------------------------------------------------------------------------- void interrupt intervallo (void) //Interrupt di TIMER0 settato a 1 ms { if (T0IF) //Se overflow { if (RCIF != 0) ValoreRicevuto = RCREG; if (tempo != 0) { tempo--; //Decremento } T0IF = 0; //Reset } } //----------------------------------------------------------------------------- //Funzione di ritardo (ogni oscillazione è uguale a 1 ms) int Routine_Ritardo(func) { tempo = func; while (tempo != 0) { asm("nop"); } return 0; } //----------------------------------------------------------------------------------------------------- void main(void) { // INIZIALIZZAZIONE HARDWARE ANSEL = 0x00; ANSELH= 0X00; //Esclusione porte analogiche, tutto digitale i/o // PORT A TRISA = 0b11111101; //Port A // PORT B TRISB = 0b01111111; //Port B // PORT C TRISC = 0b10111110; //Port C // Oscillatore impostato a 8MHz OPTION = 0b10000010; //Prescaler 1:8 e disabilitazione Pull-up INTCON = 0b11100000; PIE1 = 0b00100000; / OSCCON = 0b01110000; //Settaggio oscillatore interno a 8 MHz TMR0 = 6; //Caricamento TIMER0 a 1 ms RCSTA = 0b10010000; SPBRG = 47; //Settaggio per velocità di comunicazione (baud rate) EnableDati = 0; PORTA = 0; PORTB = 0; PORTC = 0; //---------------------------------------------------------------------------------------- while (1) { //Ricezione(); if (ValoreRicevuto == 0b00000000) { Sirena = 0; } else if (ValoreRicevuto == 0b00000001) { Sirena = 1; } } }