Activity 3. Simple home alarm system

The purpose of the activity is for the PIC18F4550 to function as a simple alarm system. The microcontroller uses a 16x2 liquid crystal display as output device. Instead of magnetic reed switches, dip switches are used.

(75 minutes)


Activity 3

=> Alarm system function:

      • in the protection area there are 5 switches/sensors and they are grouped in 2 zones
      • Zone A has 2 switches/sensors
      • Zone B has 3 switches/sensors
      • each zone can be activated independently of the other using 2 switches
      • two LEDs indicate the activation of each zone
      • the alarm activates a buzzer
      • the buzzer is ON until the alarm zone is deactivated

 => Microcontroller’s inputs:

             Table 1. Pins’ description

PIN

Description

RB0

Activation of Zone A

RB1

Activation of Zone Β

RB3

Switch/sensor 1 – Zone A

RB4

Switch/sensor 2 – Zone A

RB5

Switch/sensor 3 – Zone B

RB6

Switch/sensor 4 – Zone B

RB7

Switch/sensor 5 – Zone B

 

=> All inputs are activated with “0”

=> This alarm system could be used to protect the area in which the user is inside



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



Step 2. The program in C language is written.

Write in CCS 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

               

#include <flex_lcd.h> // The h file of the lcd driver

                      // should be in the same folder where we will save our program.

                      // The #define LCD_DB4 PIN_B4 etc statements in flex_lcd.c

                      // should be checked and possibly modified.

                      // These statements determine the pins of the microcontroller

                      // that are connected to LCD 16x2.

                           

#byte PORTB =0xF81  

                     // We attribute to the memory position 0xF81 the name PORTB.

                     // 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 PORTD=0xF83    // F83h is the position or PORTD data register

                     // at the data memory of the microcontroller

                     // SFR Special Function Register

 

 

#byte PORTA=0xF80    // F80h is the position or PORTA data register

                     // at the data memory of the microcontroller

                     // SFR Special Function Register         

 

 

boolean zone1; //flag raised when Zone 1 is activated

boolean zone2; //flag raised when Zone 2 is activated

boolean alarm; //flag raised when the alarm goes off

 

// ********* main program ************************

void main() {

   set_tris_d(0x00);    //PORTD is defined as output to drive the LCD

   set_tris_b(0xFF);    //PORTB is defined as input for sensors and control switches

   set_tris_a(0x00);    //PORTA is defined as output to set on/off the buzzer and LEDs

  

   output_low(PIN_A0);  //buzzer activations

   output_low(PIN_A1);  //LED1 (zone1) is off

   output_low(PIN_A2);  //LED2 (zone2) is off

   lcd_init();          //Initialization routine

   lcd_putc("\f");      //Clear display

   printf(lcd_putc," Alarm is off"); //display the message

  

   while(TRUE) {

     

      //check for zoneA activation

      if(input(PIN_B0)==0){

         zone1=1; //zoneA is activated

      }

      else{

         zone1=0; //zoneA is de-activated

         alarm=0; //alarm turned off

      }

      //check for zoneB activation

      if(input(PIN_B1)==0){

         zone2=1; //zoneB is activated

      }

      else{

         zone2=0; //zoneB is de-activated

         alarm=0; //alarm turned off

      }

     

      //print a message on the LCD

      if(zone1==0 && zone2==0){

         lcd_putc("\f"); 

         printf(lcd_putc," Alarm is off");

         delay_ms(150);

      }

      else{

         lcd_putc("\f"); 

         printf(lcd_putc," Alarm is on");

         delay_ms(150);

      }

     

      if(alarm==0){

         if(zone1==1){

            output_high(PIN_A1); //LED for zoneA is ON

            //check the sensors

            if(input(PIN_B3)==0){

               alarm=1;

               lcd_putc("\f");  

               printf(lcd_putc," Activated by\nSensor1");

            }

            else if(input(PIN_B4)==0){

               alarm=1;

               lcd_putc("\f");  

               printf(lcd_putc," Activated by\nSensor2");

            }

           

            if(alarm==1){

               output_high(PIN_A0); //buzzer is on

               while(input(PIN_B0)==0){;} //wait to turn off the ZoneA

            }

         }

         else{

            output_low(PIN_A1); //LED for zoneA is OFF

            output_low(PIN_A0); //buzzer is OFF

         }

     

         if(zone2==1){

            output_high(PIN_A2);//LED for zoneB is ON

            //check the sensors

            if(input(PIN_B5)==0){

               alarm=1;

               lcd_putc("\f");  

               printf(lcd_putc," Activated by\nSensor3");

            }

            else if(input(PIN_B6)==0){

               alarm=1;

               lcd_putc("\f");  

               printf(lcd_putc," Activated by\nSensor4");

            }

            else if(input(PIN_B7)==0){

               alarm=1;

               lcd_putc("\f");  

               printf(lcd_putc," Activated by\nSensor5");

            }

           

            if(alarm==1){

               output_high(PIN_A0); //buzzer is on

               while(input(PIN_B1)==0){;} //wait to turn off the ZoneB

            }

         }

         else{

            output_low(PIN_A2); //LED for zoneB is OFF

            output_low(PIN_A0); //buzzer is OFF

         }

      } 

   }

}



Step 3. Use the CCS C Compiler to translate the programm from C language to the microcontroller machine code. Load to the microcontroller the hex file (machine code) that was created from the CCS Compiler.

.


Step 4. Run the simulation and check the correct operation of the circuit.