Program Description - smartlab-bits/smartlite GitHub Wiki
controller8051.c
Program is written for a 8051 family microcontroller. The exact details of microcontroller used can be found on here: [hyperlinked text].
This program is designed to handle the output of 2 PIR sensors using 1 microcontroller. The program polls for both the PIR sensors to detect the motion across the room. It is also connected to 2 lights which are controlled by the program based on the output of the PIR sensors.
The functionality of the program is as follows:
There is a light linked with each PIR sensor. When motion is detected for the first time by the PIR sensor for the first time, the corresponding light is turned ON for a certain period of time - T55. During this time-T55, the PIR sensor is not polled to detect motion. For the next T5 seconds, the PIR sensor is polled, but the light continues to be ON. If motion is detected during this time, the light will be automatically kept ON for the next T55 time span and T5 time follows it. If motion is not detected in T5 time, the light is turned OFF and the PIR sensor is continuously polled for detecting motion. This process continues indefinitely.
The exact working of the code is described here:
โreg51.hโ header file provides library functions to control and access the functions of the 8051 microcontroller. The sbit command is used to map the pins on microcontroller to an identifier. Here, PIR1 and PIR2 map to pins X1^Y1 and X2^Y2 on the microcontroller to which the PIR sensors are connected. Also, LIGHT1 and LIGHT2 are mapped to X3^Y3 and X4^Y4 respectively. PIR==0 means that the motion is detected. Setting LIGHT=1 turns the light ON and LIGHT=0 turns the light OFF.
An infinite while loop is used to poll the PIR sensors. Since, threads cannot be started on a microcontroller both the PIR sensors are monitored on the same thread. The PIR sensor is designed to give its output signal for 2 seconds so that the output is not missed in the program. See the specifications of the PIR sensor [ hyperlinked text ].
The variables status1 and status2 keep track of the PIR sensor state. The states are: 1. Polling 2. T55 state 3. T5 state. These states are already explained in the previous section[functionality]. The value of status variables indicate the state of the PIR sensor. The mapping is: _0 for polling, 1 for T55 and 2 for T5. _
The variables timer1 and timer2 are used to keep track of time. In this program, time is measured by counting number of iterations of the loop rather than using the inbuilt timer function of the microcontroller because threads cannot be started to count the number of overflows in the counter. So, slight variations in time measurements can be expected due to variations in processing power of the microcontroller and also due to the changes in number of statements executed per iteration with change in the states of PIR sensor.
Rest of the program is self-explanatory. Each iteration polls for both the sensors and updates the state of each PIR sensor.