Activity 4. Simple alarm system

The purpose of this activity is to create a simple alarm system. The system sensors are simulated by 4 switches connected to RB4 ~ RB7. The alarm works as follows: a switch in RB0 arms or disarms the system. If the system is armed and one of the 4 switches changes state, then the microcontroller activates an LED (or a buzzer) for 6 seconds. The sensor / switch that gave the alarm is displayed in PORTD.

(40 minutes)


Activity 4

Step 1. The circuit is drawn at the Proteus Design Suite.



Step 2. The program in C language is written.

Write in CCS C Compiler the program in C language

 #include <main.h> // the file main.h with the

                             // initial settings is included

                             // This file must be placed in the same

                             // folder with the project

                             // Also the 18F4550.h file must exist

                             // in the same folder with the project

#byte PORTD =0xF83 // We attribute to the memory position 0xF83

                                 // the name PORTD

                                 // This means that we define a 8 bit

                                 // variable whose value will be stored

                                 // to the memory position F83h

                                 // The memory position F83h is the PORTD

                                 // data register

#byte PORTB=0xF81  // We attribute to the memory position 0xF81

                                 // the name PORTD

                                 // This means that we define a 8 bit

                                 // variable whose value will be stored

                                 // to the memory position F81h

                                 // The memory position F81h is the PORTD

                                 // data register

#byte PORTC=0xF82  // We attribute to the memory position 0xF82

                                 // the name PORTC

                                 // This means that we define a 8 bit

                                 // variable whose value will be stored

                                 // to the memory position F82h

                                 // The memory position F82h is the PORTC

                                 // data register

 

//Declaration of functions, global variables

void init (void);       //initialization routine

void rb (void);         //interrupt service routine statement (from RB4, RB5, RB6, RB7)

 

int8 lastPORTB;         //Global variable to hold the last value of PORTΒ

           

void main(){

     init();            //call the initialization routine

     while (TRUE) {;}   //the main program does nothing

}

 

//interrupt service routine (change on RB4~RB7)

#INT_RB   

void rb (void){     

   int8 changes;       //Define an 8bit variable

   changes = lastPORTB ^ PORTB;     //The changed bit becomes 1 and appears in the corresponding position in the change variable

   lastPORTB=PORTB;                 //The new PORTB value is transferred to the lastPORTB variable

 

   if(input(PIN_B0)==1){   

      output_high(PIN_C0);    //alarm is activated

      PORTD=changes;          //The changed bit of PORTB is displayed on PORTD | a LED is on

      delay_ms(6000);         //wait for 6 seconds

      output_low(PIN_C0);     //alarm is de-activated

      PORTD=0x00;             //LED is off

   }

}

                                                                 

//initialization routine    

void init (void){

   set_tris_b(0xff);        // PORTB is defined as input

   set_tris_d(0x00);        // PORTD is defined as output

   set_tris_c(0x00);        // PORTC is defined as output

 

   enable_interrupts(GLOBAL);    //Enable global interrupts

   enable_interrupts(INT_RB);    //Enable change interrupt by RB4, RB5, RB6, RB7

 

   PORTD=0x00;                    //The PORTD data register is given the value 0

   PORTC=0x00;                    //The PORTC data register is given the value 0

   lastPORTB=PORTB;               //The new PORTB value is transferred to the lastPORTB variable

}



Step 3. Compile the program in order to create the hex.file (program in machine code). Load the program (hex.file) to the microcontroller.



Step4. Run the simulation and check the correct operation of the circuit.