Question

So, I have to do this challenge, which is to implement a camera surveillance system for a 8051 microcontroller.

These are the specifications:

  1. Each camera is related to a movement sensor, and each time it detects a movement, the recording of this camera will be among the ones that will be registered and saved. If the sensor doesn't capture any movement for more than 5 seconds, this camera will not be recorded anymore;

  2. If there's no camera on, the video recorder must be on "pause";

  3. If more than one camera is on, a multiplexer (mux) have to be used to select the camera signals in a way so each camera is recorded during 3 seconds. This way, all the active cameras must be recorded during 3 seconds. If just one camera is active, it's signal must be the only one in the mux.

This I have already accomplished in the code below. And what we have to do now is to optimize the size of the code without the compiler optimizations. The code is 198 bytes by now, but I'm trying to get below 180 bytes.

Is it possible? I already tried to do the calculations of the #define, but the compiler already optimize that for me.

#include <REG51F.h>

#define TIMEOUT 50
#define TIMEOUT_REC 30

#define FrClk 12000000
#define FreqTimer0_emHz 10
#define VALOR_TH0 ((65536 - (FrClk /(12 * FreqTimer0_emHz ))) >> 8)
#define VALOR_TL0 ((65536 - (FrClk /(12 * FreqTimer0_emHz ))) & 0xFF)


data bit PAUSE_INT;
data bit PAUSE_TMP;
sbit PAUSE = P0^0;
sbit SENSOR1 = P0^1;
sbit SENSOR2 = P0^2;
sbit SENSOR3 = P0^3;
sbit SENSOR4 = P0^4;
sbit MUX0 = P0^5;
sbit MUX1 = P0^6;

data unsigned char CAM[4];
data unsigned char REC;
data unsigned char index;
data unsigned char count;

void timer0_int (void) interrupt 1 using 2 {
    for (index = 0; index < 4; index++)
        if(CAM[index])
        CAM[index]--;

    if (!PAUSE_INT && REC)
        REC--;
    else
    { 
         REC = TIMEOUT_REC;
         index = (index + 1) & 0x03;
         for (count = 0; !CAM[index] && count < 4; index = (index + 1) & 0x03, count++);

         MUX0 = index & 0x1;
         MUX1 = index & 0x2;


         PAUSE_INT = 0;
    }   
}

int main(void)
{

    PAUSE_TMP = 1;
    PAUSE_INT = 0;

    index = 0;

    //timer0_init
    EA = 1;
    TR0 = 0;              
    TMOD = (TMOD & 0xF0) | 0x01;  
    TH0 = VALOR_TH0;  
    TL0 = VALOR_TL0;
    ET0 = 1;     
    TR0 = 1;     

    while(1) {

        if (SENSOR1)
        {
                CAM[0] = TIMEOUT;
        }

        if (SENSOR2)
        {
                CAM[1] = TIMEOUT;
        }

        if (SENSOR3)
        {
                CAM[2] = TIMEOUT;
        }

        if (SENSOR4)
        {
                CAM[3] = TIMEOUT;
        }

        if (CAM[0] || CAM[1] || CAM[2] || CAM[3])
        {
                if (PAUSE_TMP)
                        PAUSE_INT = 1;
                PAUSE_TMP = 0;
        }
        else {
                PAUSE_TMP = 1;
        }

            PAUSE = PAUSE_TMP;
    }


}
Was it helpful?

Solution

You're probably going to have to look at the generated assembly code for this in order to wring the last few bytes out of it. It's probably possible to shave a few here and there by reusing a variable or combining operations. The resulting code won't be pretty - or maintainable - but it just might get you below your cutoff.

OTHER TIPS

I think a switch case instead of if(sensor1,2,3,4) could help some.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top